blob: 6f88e03a3bf6e76df2746a4c1a0e914d493a2a18 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020032static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
33static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
34static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
35static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010036static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020037static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010038static int eval8(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
39static int eval9(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
40static int eval9_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000041
Bram Moolenaar48e697e2016-01-23 22:17:30 +010042static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020043static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020044
Bram Moolenaare21c1582019-03-02 11:57:09 +010045/*
46 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010047 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010048 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020049 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010050num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010051{
52 varnumber_T result;
53
Bram Moolenaar99880f92021-01-20 21:23:14 +010054 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010055 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010056 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010057 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010058 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010059 if (failed != NULL)
60 *failed = TRUE;
61 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010062 if (n1 == 0)
63 result = VARNUM_MIN; // similar to NaN
64 else if (n1 < 0)
65 result = -VARNUM_MAX;
66 else
67 result = VARNUM_MAX;
68 }
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +010069 else if (n1 == VARNUM_MIN && n2 == -1)
70 {
71 // specific case: trying to do VARNUM_MIN / -1 results in a positive
72 // number that doesn't fit in varnumber_T and causes an FPE
73 result = VARNUM_MAX;
74 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010075 else
76 result = n1 / n2;
77
78 return result;
79}
80
81/*
82 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010083 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010084 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020085 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010086num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010087{
Bram Moolenaar99880f92021-01-20 21:23:14 +010088 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010089 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010090 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010091 if (failed != NULL)
92 *failed = TRUE;
93 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010094 return (n2 == 0) ? 0 : (n1 % n2);
95}
96
Bram Moolenaar33570922005-01-25 22:26:29 +000097/*
98 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +000099 */
100 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100101eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000102{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200103 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200104 func_init();
Bram Moolenaara7043832005-01-21 11:56:39 +0000105}
106
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000107#if defined(EXITFREE) || defined(PROTO)
108 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100109eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000110{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200111 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100112 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200113 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000114
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200115 // autoloaded script names
116 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000117
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200118 // unreferenced lists and dicts
119 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200120
121 // functions not garbage collected
122 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000123}
124#endif
125
Bram Moolenaare6b53242020-07-01 17:28:33 +0200126 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200127fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
128{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100129 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200130 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000131
132 if (eap == NULL)
133 return;
134
135 evalarg->eval_cstack = eap->cstack;
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100136 if (sourcing_a_script(eap) || eap->ea_getline == get_list_line)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200137 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100138 evalarg->eval_getline = eap->ea_getline;
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000139 evalarg->eval_cookie = eap->cookie;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200140 }
141}
142
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143/*
144 * Top level evaluation function, returning a boolean.
145 * Sets "error" to TRUE if there was an error.
146 * Return TRUE or FALSE.
147 */
148 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100149eval_to_bool(
150 char_u *arg,
151 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200152 exarg_T *eap,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100153 int skip, // only parse, don't execute
154 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155{
Bram Moolenaar33570922005-01-25 22:26:29 +0000156 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200157 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200158 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100159 int r;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200160
Bram Moolenaar37c83712020-06-30 21:18:36 +0200161 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162
163 if (skip)
164 ++emsg_skip;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100165 if (use_simple_function)
166 r = eval0_simple_funccal(arg, &tv, eap, &evalarg);
167 else
168 r = eval0(arg, &tv, eap, &evalarg);
169 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 else
172 {
173 *error = FALSE;
174 if (!skip)
175 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200176 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200177 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200178 else
179 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000180 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 }
182 }
183 if (skip)
184 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200185 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200187 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188}
189
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100190/*
191 * Call eval1() and give an error message if not done at a lower level.
192 */
193 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200194eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100195{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100196 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100197 int ret;
198 int did_emsg_before = did_emsg;
199 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200200 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100201
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200202 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
203
204 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100205 if (ret == FAIL)
206 {
207 // Report the invalid expression unless the expression evaluation has
208 // been cancelled due to an aborting error, an interrupt, or an
209 // exception, or we already gave a more specific error.
210 // Also check called_emsg for when using assert_fails().
211 if (!aborting() && did_emsg == did_emsg_before
212 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200213 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100214 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200215 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100216 return ret;
217}
218
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200220 * Return whether a typval is a valid expression to pass to eval_expr_typval()
221 * or eval_expr_to_bool(). An empty string returns FALSE;
222 */
223 int
224eval_expr_valid_arg(typval_T *tv)
225{
226 return tv->v_type != VAR_UNKNOWN
227 && (tv->v_type != VAR_STRING
228 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
229}
230
231/*
Bram Moolenaar82418262022-09-28 16:16:15 +0100232 * When calling eval_expr_typval() many times we only need one funccall_T.
233 * Returns NULL when no funccall_T is to be used.
234 * When returning non-NULL remove_funccal() must be called later.
235 */
236 funccall_T *
237eval_expr_get_funccal(typval_T *expr, typval_T *rettv)
238{
239 if (expr->v_type != VAR_PARTIAL)
240 return NULL;
241
242 partial_T *partial = expr->vval.v_partial;
243 if (partial == NULL)
244 return NULL;
245 if (partial->pt_func == NULL
246 || partial->pt_func->uf_def_status == UF_NOT_COMPILED)
247 return NULL;
248
249 return create_funccal(partial->pt_func, rettv);
250}
251
252/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253 * Evaluate an expression, which can be a function, partial or string.
254 * Pass arguments "argv[argc]".
zeertzjqad0c4422023-08-17 22:15:47 +0200255 * If "want_func" is TRUE treat a string as a function name, not an expression.
Bram Moolenaar82418262022-09-28 16:16:15 +0100256 * "fc_arg" is from eval_expr_get_funccal() or NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100257 * Return the result in "rettv" and OK or FAIL.
258 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200259 int
Bram Moolenaar82418262022-09-28 16:16:15 +0100260eval_expr_typval(
261 typval_T *expr,
zeertzjqad0c4422023-08-17 22:15:47 +0200262 int want_func,
Bram Moolenaar82418262022-09-28 16:16:15 +0100263 typval_T *argv,
264 int argc,
265 funccall_T *fc_arg,
266 typval_T *rettv)
Bram Moolenaar48570482017-10-30 21:48:41 +0100267{
268 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100269 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200270 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100271
zeertzjqad0c4422023-08-17 22:15:47 +0200272 if (expr->v_type == VAR_PARTIAL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100273 {
274 partial_T *partial = expr->vval.v_partial;
275
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200276 if (partial == NULL)
277 return FAIL;
278
Bram Moolenaar822ba242020-05-24 23:00:18 +0200279 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200280 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100281 {
Bram Moolenaar82418262022-09-28 16:16:15 +0100282 funccall_T *fc = fc_arg != NULL ? fc_arg
283 : create_funccal(partial->pt_func, rettv);
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100284 int r;
285
286 if (fc == NULL)
287 return FAIL;
288
Bram Moolenaar69082912022-09-22 21:35:19 +0100289 // Shortcut to call a compiled function with minimal overhead.
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100290 r = call_def_function(partial->pt_func, argc, argv,
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000291 DEF_USE_PT_ARGV, partial, NULL, fc, rettv);
Bram Moolenaar82418262022-09-28 16:16:15 +0100292 if (fc_arg == NULL)
293 remove_funccal();
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100294 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100295 return FAIL;
296 }
297 else
298 {
299 s = partial_name(partial);
300 if (s == NULL || *s == NUL)
301 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200302 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000303 funcexe.fe_evaluate = TRUE;
304 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
306 return FAIL;
307 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100308 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200309 else if (expr->v_type == VAR_INSTR)
310 {
311 return exe_typval_instr(expr, rettv);
312 }
zeertzjqad0c4422023-08-17 22:15:47 +0200313 else if (expr->v_type == VAR_FUNC || want_func)
314 {
315 s = expr->v_type == VAR_FUNC
316 ? expr->vval.v_string
317 : tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
318 if (s == NULL || *s == NUL)
319 return FAIL;
320 CLEAR_FIELD(funcexe);
321 funcexe.fe_evaluate = TRUE;
322 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
323 return FAIL;
324 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100325 else
326 {
Bram Moolenaarfc4c4482022-01-26 21:17:04 +0000327 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
Bram Moolenaar48570482017-10-30 21:48:41 +0100328 if (s == NULL)
329 return FAIL;
330 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200331 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100332 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200333 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100334 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200335 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200336 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100337 return FAIL;
338 }
339 }
340 return OK;
341}
342
343/*
344 * Like eval_to_bool() but using a typval_T instead of a string.
345 * Works for string, funcref and partial.
346 */
347 int
348eval_expr_to_bool(typval_T *expr, int *error)
349{
350 typval_T rettv;
351 int res;
352
zeertzjqad0c4422023-08-17 22:15:47 +0200353 if (eval_expr_typval(expr, FALSE, NULL, 0, NULL, &rettv) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100354 {
355 *error = TRUE;
356 return FALSE;
357 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200358 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100359 clear_tv(&rettv);
360 return res;
361}
362
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363/*
364 * Top level evaluation function, returning a string. If "skip" is TRUE,
365 * only parsing to "nextcmd" is done, without reporting errors. Return
366 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
367 */
368 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100369eval_to_string_skip(
370 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200371 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100372 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373{
Bram Moolenaar33570922005-01-25 22:26:29 +0000374 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200376 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377
Bram Moolenaar37c83712020-06-30 21:18:36 +0200378 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 if (skip)
380 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200381 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 retval = NULL;
383 else
384 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100385 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000386 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 }
388 if (skip)
389 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200390 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391
392 return retval;
393}
394
395/*
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100396 * Initialize "evalarg" for use.
397 */
398 void
399init_evalarg(evalarg_T *evalarg)
400{
401 CLEAR_POINTER(evalarg);
402 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
403}
404
405/*
406 * If "evalarg->eval_tofree" is not NULL free it later.
407 * Caller is expected to overwrite "evalarg->eval_tofree" next.
408 */
409 static void
410free_eval_tofree_later(evalarg_T *evalarg)
411{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000412 if (evalarg->eval_tofree == NULL)
413 return;
414
415 if (ga_grow(&evalarg->eval_tofree_ga, 1) == OK)
416 ((char_u **)evalarg->eval_tofree_ga.ga_data)
417 [evalarg->eval_tofree_ga.ga_len++]
418 = evalarg->eval_tofree;
419 else
420 vim_free(evalarg->eval_tofree);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100421}
422
423/*
424 * After using "evalarg" filled from "eap": free the memory.
425 */
426 void
427clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
428{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000429 if (evalarg == NULL)
430 return;
431
432 garray_T *etga = &evalarg->eval_tofree_ga;
433
434 if (evalarg->eval_tofree != NULL || evalarg->eval_using_cmdline)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100435 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000436 if (eap != NULL)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100437 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000438 // We may need to keep the original command line, e.g. for
439 // ":let" it has the variable names. But we may also need
440 // the new one, "nextcmd" points into it. Keep both.
441 vim_free(eap->cmdline_tofree);
442 eap->cmdline_tofree = *eap->cmdlinep;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100443
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000444 if (evalarg->eval_using_cmdline && etga->ga_len > 0)
445 {
446 // "nextcmd" points into the last line in eval_tofree_ga,
447 // need to keep it around.
448 --etga->ga_len;
449 *eap->cmdlinep = ((char_u **)etga->ga_data)[etga->ga_len];
450 vim_free(evalarg->eval_tofree);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100451 }
452 else
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000453 *eap->cmdlinep = evalarg->eval_tofree;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100454 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000455 else
456 vim_free(evalarg->eval_tofree);
457 evalarg->eval_tofree = NULL;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100458 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000459
460 ga_clear_strings(etga);
461 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100462}
463
464/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000465 * Skip over an expression at "*pp".
466 * Return FAIL for an error, OK otherwise.
467 */
468 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200469skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000470{
Bram Moolenaar33570922005-01-25 22:26:29 +0000471 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000472
473 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200474 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000475}
476
477/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200478 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200479 * If in Vim9 script and line breaks are encountered, the lines are
480 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200481 * "arg" is advanced to just after the expression.
482 * "start" is set to the start of the expression, "end" to just after the end.
483 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200484 * Return FAIL for an error, OK otherwise.
485 */
486 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200487skip_expr_concatenate(
488 char_u **arg,
489 char_u **start,
490 char_u **end,
491 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200492{
493 typval_T rettv;
494 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200495 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200496 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200497 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200498 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200499 int evaluate = evalarg == NULL
500 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200501
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200502 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200503 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200504 {
505 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200506 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200507 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200508 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200509 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200510 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200511 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200512
513 // Don't evaluate the expression.
514 if (evalarg != NULL)
515 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200516 *arg = skipwhite(*arg);
517 res = eval1(arg, &rettv, evalarg);
518 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200519 if (evalarg != NULL)
520 evalarg->eval_flags = save_flags;
521
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200522 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200523 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200524 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200525 if (evalarg->eval_ga.ga_len == 1)
526 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200527 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200528 ga_clear(gap);
529 gap->ga_itemsize = 0;
530 }
531 else
532 {
533 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200534 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200535
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200536 // Line breaks encountered, concatenate all the lines.
537 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200538 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200539
540 // free the lines only when using getsourceline()
541 if (evalarg->eval_cookie != NULL)
542 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200543 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200544 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200545 // Do not free the last line, "arg" points into it, free it
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100546 // later. Also free "eval_tofree" later if needed.
547 free_eval_tofree_later(evalarg);
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200548 evalarg->eval_tofree =
549 ((char_u **)gap->ga_data)[gap->ga_len - 1];
550 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200551 ga_clear_strings(gap);
552 }
553 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200554 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200555 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200556
557 // free lines that were explicitly marked for freeing
558 ga_clear_strings(freegap);
559 }
560
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200561 gap->ga_itemsize = 0;
562 if (p == NULL)
563 return FAIL;
564 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200565 vim_free(evalarg->eval_tofree_lambda);
566 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200567 // Compute "end" relative to the end.
568 *end = *start + STRLEN(*start) - endoff;
569 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200570 }
571
572 return res;
573}
574
575/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100576 * Convert "tv" to a string.
zeertzjq19dfa272023-06-15 10:56:41 +0100577 * When "convert" is TRUE convert a List into a sequence of lines.
Bram Moolenaar883cf972021-01-15 18:04:43 +0100578 * Returns an allocated string (NULL when out of memory).
579 */
580 char_u *
581typval2string(typval_T *tv, int convert)
582{
583 garray_T ga;
584 char_u *retval;
Bram Moolenaar883cf972021-01-15 18:04:43 +0100585
586 if (convert && tv->v_type == VAR_LIST)
587 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000588 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100589 if (tv->vval.v_list != NULL)
590 {
591 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
592 if (tv->vval.v_list->lv_len > 0)
593 ga_append(&ga, NL);
594 }
595 ga_append(&ga, NUL);
596 retval = (char_u *)ga.ga_data;
597 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100598 else
599 retval = vim_strsave(tv_get_string(tv));
600 return retval;
601}
602
603/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200604 * Top level evaluation function, returning a string. Does not handle line
605 * breaks.
zeertzjq19dfa272023-06-15 10:56:41 +0100606 * When "convert" is TRUE convert a List into a sequence of lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 * Return pointer to allocated memory, or NULL for failure.
608 */
609 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100610eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100611 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100612 int convert,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100613 exarg_T *eap,
614 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615{
Bram Moolenaar33570922005-01-25 22:26:29 +0000616 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100618 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100619 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100621 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100622 if (use_simple_function)
623 r = eval0_simple_funccal(arg, &tv, NULL, &evalarg);
624 else
625 r = eval0(arg, &tv, NULL, &evalarg);
626 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 retval = NULL;
628 else
629 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100630 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000631 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100633 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634
635 return retval;
636}
637
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100638 char_u *
639eval_to_string(
640 char_u *arg,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100641 int convert,
642 int use_simple_function)
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100643{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100644 return eval_to_string_eap(arg, convert, NULL, use_simple_function);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100645}
646
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000648 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100649 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200650 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 */
652 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100653eval_to_string_safe(
654 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000655 int use_sandbox,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100656 int keep_script_version,
657 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658{
659 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200660 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200661 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200662 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663
Bram Moolenaar9530b582022-01-22 13:39:08 +0000664 if (!keep_script_version)
665 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200666 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000667 if (use_sandbox)
668 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100669 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200670 may_garbage_collect = FALSE;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100671 retval = eval_to_string(arg, FALSE, use_simple_function);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000672 if (use_sandbox)
673 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100674 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200675 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200676 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200677 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 return retval;
679}
680
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681/*
682 * Top level evaluation function, returning a number.
683 * Evaluates "expr" silently.
684 * Returns -1 for an error.
685 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200686 varnumber_T
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100687eval_to_number(char_u *expr, int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688{
Bram Moolenaar33570922005-01-25 22:26:29 +0000689 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200690 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000691 char_u *p = skipwhite(expr);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100692 int r = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693
694 ++emsg_off;
695
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100696 if (use_simple_function)
697 r = may_call_simple_func(expr, &rettv);
698 if (r == NOTDONE)
699 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
700 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 retval = -1;
702 else
703 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100704 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000705 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 }
707 --emsg_off;
708
709 return retval;
710}
711
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000712/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000713 * Top level evaluation function.
714 * Returns an allocated typval_T with the result.
715 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000716 */
717 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200718eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000719{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100720 return eval_expr_ext(arg, eap, FALSE);
721}
722
723 typval_T *
724eval_expr_ext(char_u *arg, exarg_T *eap, int use_simple_function)
725{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000726 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200727 evalarg_T evalarg;
728
729 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000730
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200731 tv = ALLOC_ONE(typval_T);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100732 if (tv != NULL)
733 {
734 int r = NOTDONE;
735
736 if (use_simple_function)
737 r = eval0_simple_funccal(arg, tv, eap, &evalarg);
738 if (r == NOTDONE)
739 r = eval0(arg, tv, eap, &evalarg);
740
741 if (r == FAIL)
742 VIM_CLEAR(tv);
743 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000744
Bram Moolenaar37c83712020-06-30 21:18:36 +0200745 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000746 return tv;
747}
748
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000750 * "*arg" points to what can be a function name in the form of "import.Name" or
751 * "Funcref". Return the name of the function. Set "tofree" to something that
752 * was allocated.
753 * If "verbose" is FALSE no errors are given.
754 * Return NULL for any failure.
755 */
756 static char_u *
757deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000758 char_u **arg,
759 char_u **tofree,
760 evalarg_T *evalarg,
761 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000762{
763 typval_T ref;
764 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100765 int save_flags = 0;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000766
767 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100768 if (evalarg != NULL)
769 {
770 // need to evaluate this to get an import, like in "a.Func"
771 save_flags = evalarg->eval_flags;
772 evalarg->eval_flags |= EVAL_EVALUATE;
773 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100774 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100775 {
776 dictitem_T *v;
777
778 // If <SID>VarName was used it would not be found, try another way.
779 v = find_var_also_in_script(name, NULL, FALSE);
780 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100781 {
782 name = NULL;
783 goto theend;
784 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100785 copy_tv(&v->di_tv, &ref);
786 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000787 if (*skipwhite(*arg) != NUL)
788 {
789 if (verbose)
790 semsg(_(e_trailing_characters_str), *arg);
791 name = NULL;
792 }
793 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
794 {
795 name = ref.vval.v_string;
796 ref.vval.v_string = NULL;
797 *tofree = name;
798 }
799 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
800 {
801 if (ref.vval.v_partial->pt_argc > 0
802 || ref.vval.v_partial->pt_dict != NULL)
803 {
804 if (verbose)
805 emsg(_(e_cannot_use_partial_here));
806 name = NULL;
807 }
808 else
809 {
810 name = vim_strsave(partial_name(ref.vval.v_partial));
811 *tofree = name;
812 }
813 }
814 else
815 {
816 if (verbose)
817 semsg(_(e_not_callable_type_str), name);
818 name = NULL;
819 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100820
821theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000822 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100823 if (evalarg != NULL)
824 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000825 return name;
826}
827
828/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100829 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200830 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
831 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000832 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200834 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100835call_vim_function(
836 char_u *func,
837 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200838 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200839 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000841 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200842 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000843 char_u *arg;
844 char_u *name;
845 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000846 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100848 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200849 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000850 funcexe.fe_firstline = curwin->w_cursor.lnum;
851 funcexe.fe_lastline = curwin->w_cursor.lnum;
852 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000853
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000854 // The name might be "import.Func" or "Funcref". We don't know, we need to
855 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000856 // autoload script has errors. Guess that when there is a dot in the name
857 // showing errors is the right choice.
858 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000859 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000860 if (ignore_errors)
861 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000862 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000863 if (ignore_errors)
864 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000865 if (name == NULL)
866 name = func;
867
868 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
869
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000870 if (ret == FAIL)
871 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000872 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000873
874 return ret;
875}
876
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100877/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100878 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000879 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
880 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000881 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000882 */
883 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100884call_func_retstr(
885 char_u *func,
886 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200887 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000888{
889 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000890 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000891
Bram Moolenaarded27a12018-08-01 19:06:03 +0200892 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000893 return NULL;
894
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100895 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000896 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 return retval;
898}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000899
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000900/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100901 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000902 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000903 * Returns NULL when there is something wrong.
Bram Moolenaar55e93662022-09-10 13:52:26 +0100904 * Gives an error when the returned value is not a list.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000905 */
906 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100907call_func_retlist(
908 char_u *func,
909 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200910 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000911{
912 typval_T rettv;
913
Bram Moolenaarded27a12018-08-01 19:06:03 +0200914 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000915 return NULL;
916
917 if (rettv.v_type != VAR_LIST)
918 {
Bram Moolenaar55e93662022-09-10 13:52:26 +0100919 semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
920 vartype_name(rettv.v_type));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000921 clear_tv(&rettv);
922 return NULL;
923 }
924
925 return rettv.vval.v_list;
926}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927
Bram Moolenaare70dd112022-01-21 16:31:11 +0000928#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100930 * Evaluate "arg", which is 'foldexpr'.
931 * Note: caller must set "curwin" to match "arg".
932 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
933 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 */
935 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000936eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000938 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000939 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200940 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000942 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000943 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100944 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100946 arg = skipwhite(wp->w_p_fde);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000947 current_sctx = wp->w_p_script_ctx[WV_FDE];
948
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000950 if (use_sandbox)
951 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100952 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 *cp = NUL;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100954
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100955 // Evaluate the expression. If the expression is "FuncName()" call the
956 // function directly.
957 if (eval0_simple_funccal(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 retval = 0;
959 else
960 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100961 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000962 if (tv.v_type == VAR_NUMBER)
963 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000964 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 retval = 0;
966 else
967 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100968 // If the result is a string, check if there is a non-digit before
969 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000970 s = tv.vval.v_string;
zeertzjqa991ce92023-10-06 19:16:36 +0200971 if (*s != NUL && !VIM_ISDIGIT(*s) && *s != '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 *cp = *s++;
973 retval = atol((char *)s);
974 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000975 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 }
977 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000978 if (use_sandbox)
979 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100980 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200981 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000982 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200984 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985}
986#endif
987
Ernie Rael64885642023-10-04 20:16:22 +0200988#ifdef LOG_LOCKVAR
989typedef struct flag_string_S
990{
991 int flag;
992 char *str;
993} flag_string_T;
994
995 static char *
996flags_tostring(int flags, flag_string_T *_fstring, char *buf, size_t n)
997{
998 char *p = buf;
999 *p = NUL;
1000 for (flag_string_T *fstring = _fstring; fstring->flag; ++fstring)
1001 {
1002 if ((fstring->flag & flags) != 0)
1003 {
1004 size_t len = STRLEN(fstring->str);
1005 if (n > p - buf + len + 7)
1006 {
1007 STRCAT(p, fstring->str);
1008 p += len;
1009 STRCAT(p, " ");
1010 ++p;
1011 }
1012 else
1013 {
1014 STRCAT(buf, "...");
1015 break;
1016 }
1017 }
1018 }
1019 return buf;
1020}
1021
1022flag_string_T glv_flag_strings[] = {
1023 { GLV_QUIET, "QUIET" },
1024 { GLV_NO_AUTOLOAD, "NO_AUTOLOAD" },
1025 { GLV_READ_ONLY, "READ_ONLY" },
1026 { GLV_NO_DECL, "NO_DECL" },
1027 { GLV_COMPILING, "COMPILING" },
1028 { GLV_ASSIGN_WITH_OP, "ASSIGN_WITH_OP" },
1029 { GLV_PREFER_FUNC, "PREFER_FUNC" },
1030 { 0, NULL }
1031};
1032#endif
1033
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034/*
Ernie Raelee865f32023-09-29 19:53:55 +02001035 * Fill in "lp" using "root". This is used in a special case when
1036 * "get_lval()" parses a bare word when "lval_root" is not NULL.
1037 *
1038 * This is typically called with "lval_root" as "root". For a class, find
1039 * the name from lp in the class from root, fill in lval_T if found. For a
1040 * complex type, list/dict use it as the result; just put the root into
1041 * ll_tv.
1042 *
1043 * "lval_root" is a hack used during run-time/instr-execution to provide the
1044 * starting point for "get_lval()" to traverse a chain of indexes. In some
1045 * cases get_lval sees a bare name and uses this function to populate the
1046 * lval_T.
1047 *
1048 * For setting up "lval_root" (currently only used with lockvar)
1049 * compile_lock_unlock - pushes object on stack (which becomes lval_root)
1050 * execute_instructions: ISN_LOCKUNLOCK - sets lval_root from stack.
1051 */
1052 static void
Ernie Rael4c8da022023-10-11 21:35:11 +02001053fill_lval_from_lval_root(lval_T *lp, lval_root_T *lr)
Ernie Raelee865f32023-09-29 19:53:55 +02001054{
1055#ifdef LOG_LOCKVAR
Ernie Rael4c8da022023-10-11 21:35:11 +02001056 ch_log(NULL, "LKVAR: fill_lval_from_lval_root(): name %s, tv %p",
1057 lp->ll_name, (void*)lr->lr_tv);
Ernie Raelee865f32023-09-29 19:53:55 +02001058#endif
Ernie Rael4c8da022023-10-11 21:35:11 +02001059 if (lr->lr_tv == NULL)
1060 return;
Ernie Rael64885642023-10-04 20:16:22 +02001061 if (!lr->lr_is_arg && lr->lr_tv->v_type == VAR_CLASS)
Ernie Raelee865f32023-09-29 19:53:55 +02001062 {
Ernie Rael64885642023-10-04 20:16:22 +02001063 if (lr->lr_tv->vval.v_class != NULL)
Ernie Raelee865f32023-09-29 19:53:55 +02001064 {
1065 // Special special case. Look for a bare class variable reference.
Ernie Rael64885642023-10-04 20:16:22 +02001066 class_T *cl = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001067 int m_idx;
1068 ocmember_T *m = class_member_lookup(cl, lp->ll_name,
1069 lp->ll_name_end - lp->ll_name, &m_idx);
1070 if (m != NULL)
1071 {
1072 // Assuming "inside class" since bare reference.
Ernie Rael64885642023-10-04 20:16:22 +02001073 lp->ll_class = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001074 lp->ll_oi = m_idx;
1075 lp->ll_valtype = m->ocm_type;
1076 lp->ll_tv = &lp->ll_class->class_members_tv[m_idx];
1077#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001078 ch_log(NULL, "LKVAR: ... class member %s.%s",
1079 lp->ll_class->class_name, lp->ll_name);
Ernie Raelee865f32023-09-29 19:53:55 +02001080#endif
1081 return;
1082 }
1083 }
1084 }
1085
1086#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001087 ch_log(NULL, "LKVAR: ... type: %s", vartype_name(lr->lr_tv->v_type));
Ernie Raelee865f32023-09-29 19:53:55 +02001088#endif
Ernie Rael64885642023-10-04 20:16:22 +02001089 lp->ll_tv = lr->lr_tv;
Ernie Raelee865f32023-09-29 19:53:55 +02001090 lp->ll_is_root = TRUE;
1091}
1092
1093/*
Ernie Rael64885642023-10-04 20:16:22 +02001094 * Check if the class has permission to access the member.
1095 * Returns OK or FAIL.
1096 */
1097 static int
1098get_lval_check_access(
1099 class_T *cl_exec, // executing class, NULL if :def or script level
1100 class_T *cl, // class which contains the member
1101 ocmember_T *om, // member being accessed
1102 char_u *p, // char after member name
1103 int flags) // GLV flags to check if writing to lval
1104{
1105#ifdef LOG_LOCKVAR
1106 ch_log(NULL, "LKVAR: get_lval_check_access(), cl_exec %p, cl %p, %c",
1107 (void*)cl_exec, (void*)cl, *p);
1108#endif
1109 if (cl_exec == NULL || cl_exec != cl)
1110 {
Ernie Raele6c9aa52023-10-06 19:55:52 +02001111 char *msg = NULL;
Ernie Rael64885642023-10-04 20:16:22 +02001112 switch (om->ocm_access)
1113 {
1114 case VIM_ACCESS_PRIVATE:
Ernie Rael03042a22023-11-11 08:53:32 +01001115 msg = e_cannot_access_protected_variable_str;
Ernie Raele6c9aa52023-10-06 19:55:52 +02001116 break;
Ernie Rael64885642023-10-04 20:16:22 +02001117 case VIM_ACCESS_READ:
1118 // If [idx] or .key following, read only OK.
1119 if (*p == '[' || *p == '.')
1120 break;
1121 if ((flags & GLV_READ_ONLY) == 0)
Ernie Raele6c9aa52023-10-06 19:55:52 +02001122 msg = e_variable_is_not_writable_str;
Ernie Rael64885642023-10-04 20:16:22 +02001123 break;
1124 case VIM_ACCESS_ALL:
1125 break;
1126 }
Ernie Raele6c9aa52023-10-06 19:55:52 +02001127 if (msg != NULL)
1128 {
1129 emsg_var_cl_define(msg, om->ocm_name, 0, cl);
1130 return FAIL;
1131 }
1132
Ernie Rael64885642023-10-04 20:16:22 +02001133 }
1134 return OK;
1135}
1136
1137/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001138 * Get an lval: variable, Dict item or List item that can be assigned a value
1139 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1140 * "name.key", "name.key[expr]" etc.
1141 * Indexing only works if "name" is an existing List or Dictionary.
1142 * "name" points to the start of the name.
1143 * If "rettv" is not NULL it points to the value to be assigned.
1144 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1145 * wrong; must end in space or cmd separator.
1146 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001147 * flags:
1148 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001149 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001150 * GLV_NO_AUTOLOAD: do not use script autoloading
1151 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001152 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001153 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001154 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001155 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001156 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001157get_lval(
1158 char_u *name,
1159 typval_T *rettv,
1160 lval_T *lp,
1161 int unlet,
1162 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001163 int flags, // GLV_ values
1164 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001165{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001166 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001167 char_u *expr_start, *expr_end;
1168 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001169 dictitem_T *v;
1170 typval_T var1;
1171 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001172 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001173 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001174 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01001175 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001176 int quiet = flags & GLV_QUIET;
Ernie Rael86eda072023-09-05 07:33:48 +02001177 int writing = 0;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001178 int vim9script = in_vim9script();
Ernie Rael64885642023-10-04 20:16:22 +02001179 class_T *cl_exec = NULL; // class that is executing, or NULL.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001180
Ernie Raelee865f32023-09-29 19:53:55 +02001181#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001182 if (lval_root == NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001183 ch_log(NULL, "LKVAR: get_lval(): name: %s, lval_root (nil)", name);
Ernie Rael64885642023-10-04 20:16:22 +02001184 else
Ernie Rael4c8da022023-10-11 21:35:11 +02001185 ch_log(NULL, "LKVAR: get_lval(): name: %s, lr_tv %p lr_is_arg %d",
1186 name, (void*)lval_root->lr_tv, lval_root->lr_is_arg);
Ernie Rael64885642023-10-04 20:16:22 +02001187 char buf[80];
Ernie Rael4c8da022023-10-11 21:35:11 +02001188 ch_log(NULL, "LKVAR: ...: GLV flags: %s",
Ernie Rael64885642023-10-04 20:16:22 +02001189 flags_tostring(flags, glv_flag_strings, buf, sizeof(buf)));
Ernie Raelee865f32023-09-29 19:53:55 +02001190#endif
1191
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001192 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02001193 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001194
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001195 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001196 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001197 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001198 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02001199 lp->ll_name_end = find_name_end(name, NULL, NULL,
1200 FNE_INCL_BR | fne_flags);
1201 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001202 }
1203
Bram Moolenaara749a422022-02-12 19:52:25 +00001204 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001205 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00001206 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
1207 {
1208 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
1209 return NULL;
1210 }
1211
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001212 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001213 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001214 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001215 if (expr_start != NULL)
1216 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001217 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01001218 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001219 && *p != '[' && *p != '.')
1220 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001221 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001222 return NULL;
1223 }
1224
1225 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1226 if (lp->ll_exp_name == NULL)
1227 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001228 // Report an invalid expression in braces, unless the
1229 // expression evaluation has been cancelled due to an
1230 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001231 if (!aborting() && !quiet)
1232 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001233 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001234 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001235 return NULL;
1236 }
1237 }
1238 lp->ll_name = lp->ll_exp_name;
1239 }
1240 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001241 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001242 lp->ll_name = name;
1243
Bram Moolenaar4525a572022-02-13 11:57:33 +00001244 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001245 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001246 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001247 // However, "g:[key]" is indexing a dictionary.
1248 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001249 {
1250 --p;
1251 lp->ll_name_end = p;
1252 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001253 if (*skipwhite(p) == ':')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001254 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001255 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001256
Bram Moolenaar9510d222022-09-11 15:14:05 +01001257 if (is_scoped_variable(name))
1258 {
1259 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1260 return NULL;
1261 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001262 if (VIM_ISWHITE(*p))
1263 {
1264 semsg(_(e_no_white_space_allowed_before_colon_str), p);
1265 return NULL;
1266 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001267 if (tp == p + 1 && !quiet)
1268 {
1269 semsg(_(e_white_space_required_after_str_str), ":", p);
1270 return NULL;
1271 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001272 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001273 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001274 semsg(_(e_using_type_not_in_script_context_str), p);
1275 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001276 }
Yegappan Lakshmananb5a07192023-10-05 20:14:43 +02001277 if (vim9script && (flags & GLV_NO_DECL) &&
1278 !(flags & GLV_FOR_LOOP))
1279 {
1280 // Using a type and not in a "var" declaration.
1281 semsg(_(e_trailing_characters_str), p);
1282 return NULL;
1283 }
1284
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001285
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001286 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001287 lp->ll_type = parse_type(&tp,
1288 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1289 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001290 if (lp->ll_type == NULL && !quiet)
1291 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001292 lp->ll_name_end = tp;
1293 }
Ernie Raelee865f32023-09-29 19:53:55 +02001294 // TODO: check inside class?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001295 }
1296 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001297 if (lp->ll_name == NULL)
1298 return p;
1299
Bram Moolenaar62aec932022-01-29 21:45:34 +00001300 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001301 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001302 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001303
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001304 if (import != NULL)
1305 {
1306 ufunc_T *ufunc;
1307 type_T *type;
1308
Bram Moolenaar753885b2022-08-24 16:30:36 +01001309 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001310 lp->ll_sid = import->imp_sid;
1311 lp->ll_name = skipwhite(p + 1);
1312 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1313 lp->ll_name_end = p;
1314
1315 // check the item is exported
1316 cc = *p;
1317 *p = NUL;
1318 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001319 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001320 {
1321 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001322 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001323 }
1324 *p = cc;
1325 }
1326 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001327
Ernie Rael0f058d12023-10-14 11:25:04 +02001328 // Without [idx] or .key we are done.
1329 if (*p != '[' && *p != '.')
Ernie Raelee865f32023-09-29 19:53:55 +02001330 {
1331 if (lval_root != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001332 fill_lval_from_lval_root(lp, lval_root);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001333 return p;
Ernie Raelee865f32023-09-29 19:53:55 +02001334 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001335
Ernie Rael0f058d12023-10-14 11:25:04 +02001336 if (vim9script && lval_root != NULL)
1337 cl_exec = lval_root->lr_cl_exec;
Ernie Rael4c8da022023-10-11 21:35:11 +02001338 if (vim9script && lval_root != NULL && lval_root->lr_tv != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001339 {
1340 // using local variable
Ernie Rael64885642023-10-04 20:16:22 +02001341 lp->ll_tv = lval_root->lr_tv;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001342 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001343 }
1344 else
1345 {
1346 cc = *p;
1347 *p = NUL;
1348 // When we would write to the variable pass &ht and prevent autoload.
1349 writing = !(flags & GLV_READ_ONLY);
1350 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001351 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001352 if (v == NULL && !quiet)
1353 semsg(_(e_undefined_variable_str), lp->ll_name);
1354 *p = cc;
1355 if (v == NULL)
1356 return NULL;
1357 lp->ll_tv = &v->di_tv;
1358 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001359
Bram Moolenaar4525a572022-02-13 11:57:33 +00001360 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001361 {
1362 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001363 semsg(_(e_variable_already_declared_str), lp->ll_name);
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001364 return NULL;
1365 }
1366
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001367 /*
1368 * Loop until no more [idx] or .key is following.
1369 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001370 var1.v_type = VAR_UNKNOWN;
1371 var2.v_type = VAR_UNKNOWN;
Ernie Rael0f058d12023-10-14 11:25:04 +02001372 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001373 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001374 vartype_T v_type = lp->ll_tv->v_type;
1375
1376 if (*p == '.' && v_type != VAR_DICT
1377 && v_type != VAR_OBJECT
1378 && v_type != VAR_CLASS)
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001379 {
1380 if (!quiet)
Ernie Raelf8da3242023-10-11 21:22:12 +02001381 semsg(_(e_dot_not_allowed_after_str_str),
1382 vartype_name(v_type), name);
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001383 return NULL;
1384 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001385 if (v_type != VAR_LIST
1386 && v_type != VAR_DICT
1387 && v_type != VAR_BLOB
1388 && v_type != VAR_OBJECT
1389 && v_type != VAR_CLASS)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001390 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001391 if (!quiet)
Ernie Raelf8da3242023-10-11 21:22:12 +02001392 semsg(_(e_index_not_allowed_after_str_str),
1393 vartype_name(v_type), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001394 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001395 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001396
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001397 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001398 int r = OK;
Bram Moolenaard505d172022-12-18 21:42:55 +00001399 if (v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001400 r = rettv_list_alloc(lp->ll_tv);
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +02001401 else if (v_type == VAR_BLOB && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001402 r = rettv_blob_alloc(lp->ll_tv);
1403 if (r == FAIL)
1404 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001405
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001406 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001407 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001408 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001409 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001410 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001411 }
Ernie Rael4c8da022023-10-11 21:35:11 +02001412#ifdef LOG_LOCKVAR
1413 ch_log(NULL, "LKVAR: get_lval() loop: p: %s, type: %s", p,
1414 vartype_name(v_type));
1415#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001416
Bram Moolenaar4525a572022-02-13 11:57:33 +00001417 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001418 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001419 && lp->ll_tv == &v->di_tv
1420 && ht != NULL && ht == get_script_local_ht())
1421 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001422 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001423
1424 // Vim9 script local variable: get the type
1425 if (sv != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001426 {
Bram Moolenaar10c65862020-10-08 21:16:42 +02001427 lp->ll_valtype = sv->sv_type;
Ernie Rael4c8da022023-10-11 21:35:11 +02001428#ifdef LOG_LOCKVAR
1429 ch_log(NULL, "LKVAR: ... loop: vim9 assign type: %s",
1430 vartype_name(lp->ll_valtype->tt_type));
1431#endif
1432 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001433 }
1434
Bram Moolenaar8c711452005-01-14 21:53:12 +00001435 len = -1;
Ernie Rael0f058d12023-10-14 11:25:04 +02001436 if (*p == '.')
Bram Moolenaar8c711452005-01-14 21:53:12 +00001437 {
1438 key = p + 1;
1439 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1440 ;
1441 if (len == 0)
1442 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001443 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001444 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001445 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001446 }
1447 p = key + len;
1448 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001449 else
1450 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001451 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001452 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001453 if (*p == ':')
1454 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001455 else
1456 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001457 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001458 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001459 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001460 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001461 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001462 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001463 clear_tv(&var1);
1464 return NULL;
1465 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001466 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001467 }
1468
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001469 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001470 if (*p == ':')
1471 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001472 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001473 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001474 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001475 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001476 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001477 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001478 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001479 if (rettv != NULL
1480 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001481 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001482 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001483 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001484 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001485 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001486 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001487 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001488 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001489 }
1490 p = skipwhite(p + 1);
1491 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001492 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001493 else
1494 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001495 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001496 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001497 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001498 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001499 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001500 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001501 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001502 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001503 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001504 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001505 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001506 clear_tv(&var2);
1507 return NULL;
1508 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001509 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001510 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001511 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001512 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001513 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001514
Bram Moolenaar8c711452005-01-14 21:53:12 +00001515 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001516 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001517 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001518 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001519 clear_tv(&var1);
1520 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001521 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001522 }
1523
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001524 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001525 ++p;
1526 }
Ernie Rael4c8da022023-10-11 21:35:11 +02001527#ifdef LOG_LOCKVAR
Ernie Rael0f058d12023-10-14 11:25:04 +02001528 if (len == -1)
Ernie Rael4c8da022023-10-11 21:35:11 +02001529 ch_log(NULL, "LKVAR: ... loop: p: %s, '[' key: %s", p,
1530 empty1 ? ":" : (char*)tv_get_string(&var1));
1531 else
1532 ch_log(NULL, "LKVAR: ... loop: p: %s, '.' key: %s", p, key);
Ernie Rael4c8da022023-10-11 21:35:11 +02001533#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00001534
Bram Moolenaard505d172022-12-18 21:42:55 +00001535 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001536 {
1537 if (len == -1)
1538 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001539 // "[key]": get key from "var1"
1540 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001541 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001542 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001543 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001544 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001545 }
1546 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001547 lp->ll_list = NULL;
Ernie Raelee865f32023-09-29 19:53:55 +02001548 lp->ll_object = NULL;
1549 lp->ll_class = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001550
1551 // a NULL dict is equivalent with an empty dict
1552 if (lp->ll_tv->vval.v_dict == NULL)
1553 {
1554 lp->ll_tv->vval.v_dict = dict_alloc();
1555 if (lp->ll_tv->vval.v_dict == NULL)
1556 {
1557 clear_tv(&var1);
1558 return NULL;
1559 }
1560 ++lp->ll_tv->vval.v_dict->dv_refcount;
1561 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001562 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001563
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001564 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001565
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001566 // When assigning to a scope dictionary check that a function and
1567 // variable name is valid (only variable name unless it is l: or
1568 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001569 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001570 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001571 int prevval;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001572
1573 if (len != -1)
1574 {
1575 prevval = key[len];
1576 key[len] = NUL;
1577 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001578 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001579 prevval = 0; // avoid compiler warning
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001580 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
zeertzjq91c75d12022-11-05 20:21:58 +00001581 && (rettv->v_type == VAR_FUNC
1582 || rettv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001583 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001584 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001585 if (len != -1)
1586 key[len] = prevval;
1587 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001588 {
1589 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001590 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001591 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001592 }
1593
Bram Moolenaar10c65862020-10-08 21:16:42 +02001594 if (lp->ll_valtype != NULL)
1595 // use the type of the member
1596 lp->ll_valtype = lp->ll_valtype->tt_member;
1597
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001598 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001599 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001600 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001601 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001602 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001603 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001604 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001605 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001606 return NULL;
1607 }
1608
Bram Moolenaar31b81602019-02-10 22:14:27 +01001609 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001610 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001611 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001612 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001613 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001614 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001615 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001616 }
1617 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001618 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001619 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001620 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001621 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001622 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001623 p = NULL;
1624 break;
1625 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001626 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001627 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001628 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1629 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001630 {
1631 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001632 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001633 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001634
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001635 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001636 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001637 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001638 else if (v_type == VAR_BLOB)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001639 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001640 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1641
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001642 /*
1643 * Get the number and item for the only or first index of the List.
1644 */
1645 if (empty1)
1646 lp->ll_n1 = 0;
1647 else
1648 // is number or string
1649 lp->ll_n1 = (long)tv_get_number(&var1);
1650 clear_tv(&var1);
1651
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001652 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001653 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001654 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001655 return NULL;
1656 }
1657 if (lp->ll_range && !lp->ll_empty2)
1658 {
1659 lp->ll_n2 = (long)tv_get_number(&var2);
1660 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001661 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1662 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001663 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001664 }
1665 lp->ll_blob = lp->ll_tv->vval.v_blob;
1666 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001667 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001668 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001669 else if (v_type == VAR_LIST)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001670 {
1671 /*
1672 * Get the number and item for the only or first index of the List.
1673 */
1674 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001675 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001676 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001677 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001678 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001679 clear_tv(&var1);
1680
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001681 lp->ll_dict = NULL;
Ernie Raelee865f32023-09-29 19:53:55 +02001682 lp->ll_object = NULL;
1683 lp->ll_class = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001684 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001685 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1686 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001687 if (lp->ll_li == NULL)
1688 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001689 clear_tv(&var2);
1690 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001691 }
1692
Bram Moolenaar10c65862020-10-08 21:16:42 +02001693 if (lp->ll_valtype != NULL)
1694 // use the type of the member
1695 lp->ll_valtype = lp->ll_valtype->tt_member;
1696
Bram Moolenaar8c711452005-01-14 21:53:12 +00001697 /*
1698 * May need to find the item or absolute index for the second
1699 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001700 * When no index given: "lp->ll_empty2" is TRUE.
1701 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001702 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001703 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001704 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001705 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001706 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001707 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001708 if (check_range_index_two(lp->ll_list,
1709 &lp->ll_n1, lp->ll_li,
1710 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001711 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001712 }
1713
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001714 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001715 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001716 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001717 {
Ernie Raelee865f32023-09-29 19:53:55 +02001718 lp->ll_dict = NULL;
1719 lp->ll_list = NULL;
1720
1721 class_T *cl;
Ernie Rael4c8da022023-10-11 21:35:11 +02001722 if (v_type == VAR_OBJECT)
Ernie Raelee865f32023-09-29 19:53:55 +02001723 {
Ernie Rael4c8da022023-10-11 21:35:11 +02001724 if (lp->ll_tv->vval.v_object == NULL)
1725 {
1726 if (!quiet)
1727 emsg(_(e_using_null_object));
1728 return NULL;
1729 }
Ernie Raelee865f32023-09-29 19:53:55 +02001730 cl = lp->ll_tv->vval.v_object->obj_class;
1731 lp->ll_object = lp->ll_tv->vval.v_object;
1732 }
1733 else
1734 {
1735 cl = lp->ll_tv->vval.v_class;
1736 lp->ll_object = NULL;
1737 }
1738 lp->ll_class = cl;
1739
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001740 // TODO: what if class is NULL?
1741 if (cl != NULL)
1742 {
1743 lp->ll_valtype = NULL;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001744
1745 if (flags & GLV_PREFER_FUNC)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001746 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001747 // First look for a function with this name.
1748 // round 1: class functions (skipped for an object)
1749 // round 2: object methods
1750 for (int round = v_type == VAR_OBJECT ? 2 : 1;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001751 round <= 2; ++round)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001752 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001753 int m_idx;
1754 ufunc_T *fp;
1755
1756 fp = method_lookup(cl,
1757 round == 1 ? VAR_CLASS : VAR_OBJECT,
1758 key, p - key, &m_idx);
Ernie Raelee865f32023-09-29 19:53:55 +02001759 lp->ll_oi = m_idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001760 if (fp != NULL)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001761 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001762 lp->ll_ufunc = fp;
1763 lp->ll_valtype = fp->uf_func_type;
1764 break;
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001765 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001766 }
1767 }
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001768
1769 if (lp->ll_valtype == NULL)
1770 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001771 int m_idx;
Ernie Raelee865f32023-09-29 19:53:55 +02001772 ocmember_T *om
1773 = member_lookup(cl, v_type, key, p - key, &m_idx);
1774 lp->ll_oi = m_idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001775 if (om != NULL)
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001776 {
Ernie Rael64885642023-10-04 20:16:22 +02001777 if (get_lval_check_access(cl_exec, cl, om,
1778 p, flags) == FAIL)
1779 return NULL;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001780
Yegappan Lakshmanane5437c52023-12-16 14:11:19 +01001781 // When lhs is used to modify the variable, check it is
1782 // not a read-only variable.
1783 if ((flags & GLV_READ_ONLY) == 0
1784 && (*p != '.' && *p != '[')
1785 && oc_var_check_ro(cl, om))
1786 return NULL;
1787
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001788 lp->ll_valtype = om->ocm_type;
1789
1790 if (v_type == VAR_OBJECT)
1791 lp->ll_tv = ((typval_T *)(
1792 lp->ll_tv->vval.v_object + 1)) + m_idx;
1793 else
1794 lp->ll_tv = &cl->class_members_tv[m_idx];
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001795 }
1796 }
1797
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001798 if (lp->ll_valtype == NULL)
1799 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001800 member_not_found_msg(cl, v_type, key, p - key);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001801 return NULL;
1802 }
1803 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001804 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001805 }
1806
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001807 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001809 return p;
1810}
1811
1812/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001813 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001814 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001815 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001816clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001817{
1818 vim_free(lp->ll_exp_name);
1819 vim_free(lp->ll_newkey);
1820}
1821
1822/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001823 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001824 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001825 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1826 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001827 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001828 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001829set_var_lval(
1830 lval_T *lp,
1831 char_u *endp,
1832 typval_T *rettv,
1833 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001834 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1835 char_u *op,
1836 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001837{
1838 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001839 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001840
1841 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001842 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001843 cc = *endp;
1844 *endp = NUL;
h-east2bd6a092023-05-19 19:01:17 +01001845 if (in_vim9script() && check_reserved_name(lp->ll_name, FALSE) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001846 return;
1847
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001848 if (lp->ll_blob != NULL)
1849 {
1850 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001851
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001852 if (op != NULL && *op != '=')
1853 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001854 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001855 return;
1856 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001857 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001858 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001859
1860 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1861 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001862 if (lp->ll_empty2)
1863 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1864
Bram Moolenaar68452172021-04-12 21:21:02 +02001865 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1866 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001867 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001868 }
1869 else
1870 {
1871 val = (int)tv_get_number_chk(rettv, &error);
1872 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001873 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001874 }
1875 }
1876 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001877 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001878 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001879
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001880 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1881 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001882 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001883 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001884 *endp = cc;
1885 return;
1886 }
1887
Bram Moolenaarff697e62019-02-12 22:28:33 +01001888 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001889 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001890 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001891 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001892 {
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001893 if (di != NULL && di->di_tv.v_type == VAR_TYPEALIAS)
1894 {
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02001895 semsg(_(e_cannot_modify_typealias),
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001896 di->di_tv.vval.v_typealias->ta_name);
1897 clear_tv(&tv);
1898 return;
1899 }
1900
Bram Moolenaar79518e22017-02-17 16:31:35 +01001901 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001902 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1903 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001904 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001905 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001906 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001907 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001908 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001909 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001910 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001911 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001912 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1913 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001914 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001915 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001916 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001917 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001918 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001919 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001920 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001921 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001922 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001923 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001924 else if (lp->ll_range)
1925 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001926 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1927 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001928 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001929 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001930 return;
1931 }
1932
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001933 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1934 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001935 }
1936 else
1937 {
1938 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001939 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001940 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001941 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1942 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001943 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001944 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001945 return;
1946 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001947
1948 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001949 && check_typval_arg_type(lp->ll_valtype, rettv,
1950 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001951 return;
1952
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001953 if (lp->ll_newkey != NULL)
1954 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001955 if (op != NULL && *op != '=')
1956 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001957 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001958 return;
1959 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001960 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1961 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001962 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001963
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001964 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001965 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001966 if (di == NULL)
1967 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001968 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1969 {
1970 vim_free(di);
1971 return;
1972 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001973 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001974 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001975 else if (op != NULL && *op != '=')
1976 {
1977 tv_op(lp->ll_tv, rettv, op);
1978 return;
1979 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001980 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001981 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001982
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001983 /*
1984 * Assign the value to the variable or list item.
1985 */
1986 if (copy)
1987 copy_tv(rettv, lp->ll_tv);
1988 else
1989 {
1990 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001991 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001992 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001993 }
1994 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001995}
1996
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001997/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001998 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1999 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002000 * Returns OK or FAIL.
2001 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002002 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002003tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002004{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002005 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002006 char_u numbuf[NUMBUFLEN];
2007 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002008 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002009
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02002010 // Can't do anything with a Funcref or Dict on the right.
2011 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002012 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02002013 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
2014 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002015 {
2016 switch (tv1->v_type)
2017 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002018 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002019 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002020 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002021 case VAR_DICT:
2022 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002023 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002024 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002025 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002026 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002027 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002028 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002029 case VAR_CLASS:
2030 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002031 case VAR_TYPEALIAS:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002032 break;
2033
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002034 case VAR_BLOB:
2035 if (*op != '+' || tv2->v_type != VAR_BLOB)
2036 break;
2037 // BLOB += BLOB
2038 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2039 {
2040 blob_T *b1 = tv1->vval.v_blob;
2041 blob_T *b2 = tv2->vval.v_blob;
2042 int i, len = blob_len(b2);
2043 for (i = 0; i < len; i++)
2044 ga_append(&b1->bv_ga, blob_get(b2, i));
2045 }
2046 return OK;
2047
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002048 case VAR_LIST:
2049 if (*op != '+' || tv2->v_type != VAR_LIST)
2050 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002051 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02002052 if (tv2->vval.v_list != NULL)
2053 {
2054 if (tv1->vval.v_list == NULL)
2055 {
2056 tv1->vval.v_list = tv2->vval.v_list;
2057 ++tv1->vval.v_list->lv_refcount;
2058 }
2059 else
2060 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2061 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002062 return OK;
2063
2064 case VAR_NUMBER:
2065 case VAR_STRING:
2066 if (tv2->v_type == VAR_LIST)
2067 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002068 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002069 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002070 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002071 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002072 if (tv2->v_type == VAR_FLOAT)
2073 {
2074 float_T f = n;
2075
Bram Moolenaarff697e62019-02-12 22:28:33 +01002076 if (*op == '%')
2077 break;
2078 switch (*op)
2079 {
2080 case '+': f += tv2->vval.v_float; break;
2081 case '-': f -= tv2->vval.v_float; break;
2082 case '*': f *= tv2->vval.v_float; break;
2083 case '/': f /= tv2->vval.v_float; break;
2084 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002085 clear_tv(tv1);
2086 tv1->v_type = VAR_FLOAT;
2087 tv1->vval.v_float = f;
2088 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002089 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002090 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002091 switch (*op)
2092 {
2093 case '+': n += tv_get_number(tv2); break;
2094 case '-': n -= tv_get_number(tv2); break;
2095 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002096 case '/': n = num_divide(n, tv_get_number(tv2),
2097 &failed); break;
2098 case '%': n = num_modulus(n, tv_get_number(tv2),
2099 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002100 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002101 clear_tv(tv1);
2102 tv1->v_type = VAR_NUMBER;
2103 tv1->vval.v_number = n;
2104 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002105 }
2106 else
2107 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002108 if (tv2->v_type == VAR_FLOAT)
2109 break;
2110
Bram Moolenaarff697e62019-02-12 22:28:33 +01002111 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002112 s = tv_get_string(tv1);
2113 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002114 clear_tv(tv1);
2115 tv1->v_type = VAR_STRING;
2116 tv1->vval.v_string = s;
2117 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002118 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002119
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002120 case VAR_FLOAT:
2121 {
2122 float_T f;
2123
Bram Moolenaarff697e62019-02-12 22:28:33 +01002124 if (*op == '%' || *op == '.'
2125 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002126 && tv2->v_type != VAR_NUMBER
2127 && tv2->v_type != VAR_STRING))
2128 break;
2129 if (tv2->v_type == VAR_FLOAT)
2130 f = tv2->vval.v_float;
2131 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002132 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002133 switch (*op)
2134 {
2135 case '+': tv1->vval.v_float += f; break;
2136 case '-': tv1->vval.v_float -= f; break;
2137 case '*': tv1->vval.v_float *= f; break;
2138 case '/': tv1->vval.v_float /= f; break;
2139 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002140 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002141 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002142 }
2143 }
2144
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002145 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002146 return FAIL;
2147}
2148
2149/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002150 * Evaluate the expression used in a ":for var in expr" command.
2151 * "arg" points to "var".
2152 * Set "*errp" to TRUE for an error, FALSE otherwise;
2153 * Return a pointer that holds the info. Null when there is an error.
2154 */
2155 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002156eval_for_line(
2157 char_u *arg,
2158 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002159 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002160 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002161{
Bram Moolenaar33570922005-01-25 22:26:29 +00002162 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02002163 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002164 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002165 typval_T tv;
2166 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002167 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002168
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002169 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002170
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002171 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002172 if (fi == NULL)
2173 return NULL;
2174
Bram Moolenaar404557e2021-07-05 21:41:48 +02002175 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
2176 &fi->fi_semicolon, FALSE);
2177 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002178 return fi;
2179
Bram Moolenaar404557e2021-07-05 21:41:48 +02002180 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002181 if (expr[0] != 'i' || expr[1] != 'n'
2182 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002183 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02002184 if (in_vim9script() && *expr == ':' && expr != var_list_end)
2185 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
2186 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002187 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002188 return fi;
2189 }
2190
2191 if (skip)
2192 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002193 expr = skipwhite_and_linebreak(expr + 2, evalarg);
2194 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002195 {
2196 *errp = FALSE;
2197 if (!skip)
2198 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002199 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002200 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002201 l = tv.vval.v_list;
2202 if (l == NULL)
2203 {
2204 // a null list is like an empty list: do nothing
2205 clear_tv(&tv);
2206 }
2207 else
2208 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002209 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002210 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002211
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002212 // No need to increment the refcount, it's already set for
2213 // the list being used in "tv".
2214 fi->fi_list = l;
2215 list_add_watch(l, &fi->fi_lw);
2216 fi->fi_lw.lw_item = l->lv_first;
2217 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002218 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002219 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002220 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002221 fi->fi_bi = 0;
2222 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002223 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002224 typval_T btv;
2225
2226 // Make a copy, so that the iteration still works when the
2227 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002228 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002229 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002230 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002231 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002232 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002233 else if (tv.v_type == VAR_STRING)
2234 {
2235 fi->fi_byte_idx = 0;
2236 fi->fi_string = tv.vval.v_string;
2237 tv.vval.v_string = NULL;
2238 if (fi->fi_string == NULL)
2239 fi->fi_string = vim_strsave((char_u *)"");
2240 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002241 else
2242 {
Sean Dewar80d73952021-08-04 19:25:54 +02002243 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002244 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002245 }
2246 }
2247 }
2248 if (skip)
2249 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002250 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002251
2252 return fi;
2253}
2254
2255/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002256 * Used when looping over a :for line, skip the "in expr" part.
2257 */
2258 void
2259skip_for_lines(void *fi_void, evalarg_T *evalarg)
2260{
2261 forinfo_T *fi = (forinfo_T *)fi_void;
2262 int i;
2263
2264 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002265 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002266}
2267
2268/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002269 * Use the first item in a ":for" list. Advance to the next.
2270 * Assign the values to the variable (list). "arg" points to the first one.
2271 * Return TRUE when a valid item was found, FALSE when at end of list or
2272 * something wrong.
2273 */
2274 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002275next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002276{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002277 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002278 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002279 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002280 ? (ASSIGN_FINAL
2281 // first round: error if variable exists
2282 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002283 | ASSIGN_NO_MEMBER_TYPE
2284 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002285 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002286 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002287 int skip_assign = in_vim9script() && arg[0] == '_'
2288 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002289
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002290 if (fi->fi_blob != NULL)
2291 {
2292 typval_T tv;
2293
2294 if (fi->fi_bi >= blob_len(fi->fi_blob))
2295 return FALSE;
2296 tv.v_type = VAR_NUMBER;
2297 tv.v_lock = VAR_FIXED;
2298 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2299 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002300 if (skip_assign)
2301 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002302 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002303 fi->fi_varcount, flag, NULL) == OK;
2304 }
2305
2306 if (fi->fi_string != NULL)
2307 {
2308 typval_T tv;
2309 int len;
2310
2311 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2312 if (len == 0)
2313 return FALSE;
2314 tv.v_type = VAR_STRING;
2315 tv.v_lock = VAR_FIXED;
2316 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2317 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002318 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002319 if (skip_assign)
2320 result = TRUE;
2321 else
2322 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002323 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002324 vim_free(tv.vval.v_string);
2325 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002326 }
2327
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002328 item = fi->fi_lw.lw_item;
2329 if (item == NULL)
2330 result = FALSE;
2331 else
2332 {
2333 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002334 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002335 if (skip_assign)
2336 result = TRUE;
2337 else
2338 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002339 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002340 }
2341 return result;
2342}
2343
2344/*
2345 * Free the structure used to store info used by ":for".
2346 */
2347 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002348free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002349{
Bram Moolenaar33570922005-01-25 22:26:29 +00002350 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002351
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002352 if (fi == NULL)
2353 return;
2354 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002355 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002356 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002357 list_unref(fi->fi_list);
2358 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002359 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002360 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002361 else
2362 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002363 vim_free(fi);
2364}
2365
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002367set_context_for_expression(
2368 expand_T *xp,
2369 char_u *arg,
2370 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002372 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002374 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002376 if (cmdidx == CMD_let || cmdidx == CMD_var
2377 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002378 {
2379 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002380 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002381 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002382 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002383 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002384 {
2385 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002386 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002387 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002388 break;
2389 }
2390 return;
2391 }
2392 }
2393 else
2394 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2395 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 while ((xp->xp_pattern = vim_strpbrk(arg,
2397 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2398 {
2399 c = *xp->xp_pattern;
2400 if (c == '&')
2401 {
2402 c = xp->xp_pattern[1];
2403 if (c == '&')
2404 {
2405 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002406 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 }
2408 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002409 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002411 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2412 xp->xp_pattern += 2;
2413
2414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 }
2416 else if (c == '$')
2417 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002418 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 xp->xp_context = EXPAND_ENV_VARS;
2420 }
2421 else if (c == '=')
2422 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002423 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 xp->xp_context = EXPAND_EXPRESSION;
2425 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002426 else if (c == '#'
2427 && xp->xp_context == EXPAND_EXPRESSION)
2428 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002429 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002430 break;
2431 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002432 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 && xp->xp_context == EXPAND_FUNCTIONS
2434 && vim_strchr(xp->xp_pattern, '(') == NULL)
2435 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002436 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 break;
2438 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002439 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002441 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 {
2443 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2444 if (c == '\\' && xp->xp_pattern[1] != NUL)
2445 ++xp->xp_pattern;
2446 xp->xp_context = EXPAND_NOTHING;
2447 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002448 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002450 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2452 /* skip */ ;
2453 xp->xp_context = EXPAND_NOTHING;
2454 }
2455 else if (c == '|')
2456 {
2457 if (xp->xp_pattern[1] == '|')
2458 {
2459 ++xp->xp_pattern;
2460 xp->xp_context = EXPAND_EXPRESSION;
2461 }
2462 else
2463 xp->xp_context = EXPAND_COMMANDS;
2464 }
2465 else
2466 xp->xp_context = EXPAND_EXPRESSION;
2467 }
2468 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002469 // Doesn't look like something valid, expand as an expression
2470 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002471 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 arg = xp->xp_pattern;
2473 if (*arg != NUL)
2474 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2475 /* skip */ ;
2476 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002477
2478 // ":exe one two" completes "two"
2479 if ((cmdidx == CMD_execute
2480 || cmdidx == CMD_echo
2481 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002482 || cmdidx == CMD_echomsg
2483 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002484 && xp->xp_context == EXPAND_EXPRESSION)
2485 {
2486 for (;;)
2487 {
2488 char_u *n = skiptowhite(arg);
2489
2490 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2491 break;
2492 arg = skipwhite(n);
2493 }
2494 }
2495
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 xp->xp_pattern = arg;
2497}
2498
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002500 * Return TRUE if "pat" matches "text".
2501 * Does not use 'cpo' and always uses 'magic'.
2502 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002503 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002504pattern_match(char_u *pat, char_u *text, int ic)
2505{
2506 int matches = FALSE;
2507 char_u *save_cpo;
2508 regmatch_T regmatch;
2509
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002510 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002511 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002512 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002513 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2514 if (regmatch.regprog != NULL)
2515 {
2516 regmatch.rm_ic = ic;
2517 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2518 vim_regfree(regmatch.regprog);
2519 }
2520 p_cpo = save_cpo;
2521 return matches;
2522}
2523
2524/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002525 * Handle a name followed by "(". Both for just "name(arg)" and for
2526 * "expr->name(arg)".
2527 * Returns OK or FAIL.
2528 */
2529 static int
2530eval_func(
2531 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002532 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002533 char_u *name,
2534 int name_len,
2535 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002536 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002537 typval_T *basetv) // "expr" for "expr->name(arg)"
2538{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002539 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002540 char_u *s = name;
2541 int len = name_len;
2542 partial_T *partial;
2543 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002544 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002545 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002546
2547 if (!evaluate)
2548 check_vars(s, len);
2549
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002550 // If "s" is the name of a variable of type VAR_FUNC
2551 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002552 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002553 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002554
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002555 // Need to make a copy, in case evaluating the arguments makes
2556 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002557 s = vim_strsave(s);
zeertzjqb7f6f932023-04-13 22:12:50 +01002558 if (s == NULL || (evaluate && *s == NUL))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002559 ret = FAIL;
2560 else
2561 {
2562 funcexe_T funcexe;
2563
2564 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002565 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002566 funcexe.fe_firstline = curwin->w_cursor.lnum;
2567 funcexe.fe_lastline = curwin->w_cursor.lnum;
2568 funcexe.fe_evaluate = evaluate;
2569 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02002570 if (partial != NULL)
2571 {
2572 funcexe.fe_object = partial->pt_obj;
2573 if (funcexe.fe_object != NULL)
2574 ++funcexe.fe_object->obj_refcount;
2575 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002576 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
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02003540 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3541 if (evaluate && rettv->v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003542 {
3543 // left operand should be a number
3544 emsg(_(e_bitshift_ops_must_be_number));
3545 clear_tv(rettv);
3546 return FAIL;
3547 }
3548
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003549 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
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003578 if (evaluate)
3579 {
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02003580 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3581 {
3582 // right operand should be a positive number
3583 if (var2.v_type != VAR_NUMBER)
3584 emsg(_(e_bitshift_ops_must_be_number));
3585 else
3586 emsg(_(e_bitshift_ops_must_be_positive));
3587 clear_tv(rettv);
3588 clear_tv(&var2);
3589 return FAIL;
3590 }
3591
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003592 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 {
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02004055 if (want_type->tt_type == VAR_BOOL
4056 && actual->tt_type != VAR_BOOL
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004057 && (actual->tt_flags & TTFLAG_BOOL_OK))
4058 {
4059 int n = tv2bool(rettv);
4060
4061 // can use "0" and "1" for boolean in some places
4062 clear_tv(rettv);
4063 rettv->v_type = VAR_BOOL;
4064 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4065 }
4066 else
4067 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02004068 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004069
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004070 res = check_type(want_type, actual, TRUE, where);
4071 }
4072 }
4073 }
4074 clear_type_list(&type_list);
4075 }
4076
4077 return res;
4078}
4079
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004080 int
4081eval_leader(char_u **arg, int vim9)
4082{
4083 char_u *s = *arg;
4084 char_u *p = *arg;
4085
4086 while (*p == '!' || *p == '-' || *p == '+')
4087 {
4088 char_u *n = skipwhite(p + 1);
4089
4090 // ++, --, -+ and +- are not accepted in Vim9 script
4091 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
4092 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004093 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004094 return FAIL;
4095 }
4096 p = n;
4097 }
4098 *arg = p;
4099 return OK;
4100}
4101
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004103 * Check for a predefined value "true", "false" and "null.*".
4104 * Return OK when recognized.
4105 */
4106 int
4107handle_predefined(char_u *s, int len, typval_T *rettv)
4108{
4109 switch (len)
4110 {
4111 case 4: if (STRNCMP(s, "true", 4) == 0)
4112 {
4113 rettv->v_type = VAR_BOOL;
4114 rettv->vval.v_number = VVAL_TRUE;
4115 return OK;
4116 }
4117 if (STRNCMP(s, "null", 4) == 0)
4118 {
4119 rettv->v_type = VAR_SPECIAL;
4120 rettv->vval.v_number = VVAL_NULL;
4121 return OK;
4122 }
4123 break;
4124 case 5: if (STRNCMP(s, "false", 5) == 0)
4125 {
4126 rettv->v_type = VAR_BOOL;
4127 rettv->vval.v_number = VVAL_FALSE;
4128 return OK;
4129 }
4130 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004131 case 8: if (STRNCMP(s, "null_job", 8) == 0)
4132 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004133#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004134 rettv->v_type = VAR_JOB;
4135 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004136#else
4137 rettv->v_type = VAR_SPECIAL;
4138 rettv->vval.v_number = VVAL_NULL;
4139#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004140 return OK;
4141 }
4142 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004143 case 9:
4144 if (STRNCMP(s, "null_", 5) != 0)
4145 break;
4146 if (STRNCMP(s + 5, "list", 4) == 0)
4147 {
4148 rettv->v_type = VAR_LIST;
4149 rettv->vval.v_list = NULL;
4150 return OK;
4151 }
4152 if (STRNCMP(s + 5, "dict", 4) == 0)
4153 {
4154 rettv->v_type = VAR_DICT;
4155 rettv->vval.v_dict = NULL;
4156 return OK;
4157 }
4158 if (STRNCMP(s + 5, "blob", 4) == 0)
4159 {
4160 rettv->v_type = VAR_BLOB;
4161 rettv->vval.v_blob = NULL;
4162 return OK;
4163 }
4164 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004165 case 10: if (STRNCMP(s, "null_class", 10) == 0)
4166 {
4167 rettv->v_type = VAR_CLASS;
4168 rettv->vval.v_class = NULL;
4169 return OK;
4170 }
4171 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004172 case 11: if (STRNCMP(s, "null_string", 11) == 0)
4173 {
4174 rettv->v_type = VAR_STRING;
4175 rettv->vval.v_string = NULL;
4176 return OK;
4177 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004178 if (STRNCMP(s, "null_object", 11) == 0)
4179 {
4180 rettv->v_type = VAR_OBJECT;
4181 rettv->vval.v_object = NULL;
4182 return OK;
4183 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004184 break;
4185 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004186 if (STRNCMP(s, "null_channel", 12) == 0)
4187 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004188#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004189 rettv->v_type = VAR_CHANNEL;
4190 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004191#else
4192 rettv->v_type = VAR_SPECIAL;
4193 rettv->vval.v_number = VVAL_NULL;
4194#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004195 return OK;
4196 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004197 if (STRNCMP(s, "null_partial", 12) == 0)
4198 {
4199 rettv->v_type = VAR_PARTIAL;
4200 rettv->vval.v_partial = NULL;
4201 return OK;
4202 }
4203 break;
4204 case 13: if (STRNCMP(s, "null_function", 13) == 0)
4205 {
4206 rettv->v_type = VAR_FUNC;
4207 rettv->vval.v_string = NULL;
4208 return OK;
4209 }
4210 break;
4211 }
4212 return FAIL;
4213}
4214
4215/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 * Handle sixth level expression:
4217 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004218 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004219 * "string" string constant
4220 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 * &option-name option value
4222 * @r register contents
4223 * identifier variable value
4224 * function() function call
4225 * $VAR environment variable
4226 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004227 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004229 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004230 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 *
4232 * Also handle:
4233 * ! in front logical NOT
4234 * - in front unary minus
4235 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004236 * trailing [] subscript in String or List
4237 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004238 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 *
4240 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004241 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 *
4243 * Return OK or FAIL.
4244 */
4245 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004246eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004247 char_u **arg,
4248 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004249 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004250 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004252 int evaluate = evalarg != NULL
4253 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 int len;
4255 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004256 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 char_u *start_leader, *end_leader;
4258 int ret = OK;
4259 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004260 static int recurse = 0;
4261 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262
4263 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004264 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004265 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004267 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268
4269 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004270 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 */
4272 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004273 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004274 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 end_leader = *arg;
4276
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004277 if (**arg == '.' && (!isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004278 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004279 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004280 ++*arg;
4281 return FAIL;
4282 }
4283
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004284 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004285 // and crash. With MSVC the stack is smaller.
4286 if (recurse ==
4287#ifdef _MSC_VER
4288 300
4289#else
4290 1000
4291#endif
4292 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004293 {
4294 semsg(_(e_expression_too_recursive_str), *arg);
4295 return FAIL;
4296 }
4297 ++recurse;
4298
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 switch (**arg)
4300 {
4301 /*
4302 * Number constant.
4303 */
4304 case '0':
4305 case '1':
4306 case '2':
4307 case '3':
4308 case '4':
4309 case '5':
4310 case '6':
4311 case '7':
4312 case '8':
4313 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004314 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004315
4316 // Apply prefixed "-" and "+" now. Matters especially when
4317 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004318 if (ret == OK && evaluate && end_leader > start_leader
4319 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004320 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 break;
4322
4323 /*
4324 * String constant: "string".
4325 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004326 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 break;
4328
4329 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004330 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004332 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004333 break;
4334
4335 /*
4336 * List: [expr, expr]
4337 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004338 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 break;
4340
4341 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004342 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004343 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004344 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004345 {
4346 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4347 }
4348 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004349 {
4350 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004351 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004352 }
4353 else
4354 ret = NOTDONE;
4355 break;
4356
4357 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004358 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004359 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004360 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004361 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004362 ret = NOTDONE;
4363 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004364 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004365 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004366 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004367 break;
4368
4369 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004370 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004372 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 break;
4374
4375 /*
4376 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004377 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 */
LemonBoy2eaef102022-05-06 13:14:50 +01004379 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4380 ret = eval_interp_string(arg, rettv, evaluate);
4381 else
4382 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 break;
4384
4385 /*
4386 * Register contents: @r.
4387 */
4388 case '@': ++*arg;
4389 if (evaluate)
4390 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004391 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004392 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004393 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004394 emsg_invreg(**arg);
4395 else
4396 {
4397 rettv->v_type = VAR_STRING;
4398 rettv->vval.v_string = get_reg_contents(**arg,
4399 GREG_EXPR_SRC);
4400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 }
4402 if (**arg != NUL)
4403 ++*arg;
4404 break;
4405
4406 /*
4407 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004408 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004410 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004411 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004412 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004413 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004414 if (ret == OK && evaluate)
4415 {
4416 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4417
Bram Moolenaara9931532021-06-12 15:58:16 +02004418 // Compile it here to get the return type. The return
4419 // type is optional, when it's missing use t_unknown.
4420 // This is recognized in compile_return().
4421 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4422 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004423 if (compile_def_function(ufunc, FALSE,
4424 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004425 {
4426 clear_tv(rettv);
4427 ret = FAIL;
4428 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004429 }
4430 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004431 if (ret == NOTDONE)
4432 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004433 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004434 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004435
Bram Moolenaar9215f012020-06-27 21:18:00 +02004436 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004437 if (**arg == ')')
4438 ++*arg;
4439 else if (ret == OK)
4440 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004441 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004442 clear_tv(rettv);
4443 ret = FAIL;
4444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 }
4446 break;
4447
Bram Moolenaar8c711452005-01-14 21:53:12 +00004448 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 break;
4450 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004451
4452 if (ret == NOTDONE)
4453 {
4454 /*
4455 * Must be a variable or function name.
4456 * Can also be a curly-braces kind of name: {expr}.
4457 */
4458 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004459 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004460 if (alias != NULL)
4461 s = alias;
4462
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004463 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004464 ret = FAIL;
4465 else
4466 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004467 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4468
Bram Moolenaar4525a572022-02-13 11:57:33 +00004469 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004470 {
4471 emsg(_(e_cannot_use_underscore_here));
4472 ret = FAIL;
4473 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004474 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004475 && s[0] == 's' && s[1] == ':')
4476 {
4477 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4478 ret = FAIL;
4479 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004480 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004481 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004482 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004483 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004484 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004485 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004486 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004487 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004488 // get the value of "true", "false", etc. or a variable
4489 ret = FAIL;
4490 if (vim9script)
4491 ret = handle_predefined(s, len, rettv);
4492 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004493 {
4494 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004495 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004496 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004497 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004498 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004499 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004500 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004501 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004502 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004503 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004504 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004505 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004506 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004507 }
4508
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004509 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4510 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004511 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004512 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513
4514 /*
4515 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4516 */
4517 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004518 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004519
4520 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004521 return ret;
4522}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004523
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004524/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004525 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004526 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004527 * Adjusts "end_leaderp" until it is at "start_leader".
4528 */
4529 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004530eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004531 typval_T *rettv,
4532 int numeric_only,
4533 char_u *start_leader,
4534 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004535{
4536 char_u *end_leader = *end_leaderp;
4537 int ret = OK;
4538 int error = FALSE;
4539 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004540 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004541 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004542 float_T f = 0.0;
4543
4544 if (rettv->v_type == VAR_FLOAT)
4545 f = rettv->vval.v_float;
4546 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004547 {
4548 while (VIM_ISWHITE(end_leader[-1]))
4549 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004550 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004551 val = tv2bool(rettv);
4552 else
4553 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004554 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004555 if (error)
4556 {
4557 clear_tv(rettv);
4558 ret = FAIL;
4559 }
4560 else
4561 {
4562 while (end_leader > start_leader)
4563 {
4564 --end_leader;
4565 if (*end_leader == '!')
4566 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004567 if (numeric_only)
4568 {
4569 ++end_leader;
4570 break;
4571 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004572 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004573 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004574 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004575 {
4576 rettv->v_type = VAR_BOOL;
4577 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4578 }
4579 else
4580 f = !f;
4581 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004582 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004583 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004584 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004585 type = VAR_BOOL;
4586 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004587 }
4588 else if (*end_leader == '-')
4589 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004590 if (rettv->v_type == VAR_FLOAT)
4591 f = -f;
4592 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004593 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004594 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004595 type = VAR_NUMBER;
4596 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004597 }
4598 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004599 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004601 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004602 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004604 else
4605 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004606 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004607 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004608 rettv->v_type = type;
4609 else
4610 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004611 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004614 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 return ret;
4616}
4617
4618/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004619 * Call the function referred to in "rettv".
4620 */
4621 static int
4622call_func_rettv(
4623 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004624 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004625 typval_T *rettv,
4626 int evaluate,
4627 dict_T *selfdict,
4628 typval_T *basetv)
4629{
4630 partial_T *pt = NULL;
4631 funcexe_T funcexe;
4632 typval_T functv;
4633 char_u *s;
4634 int ret;
4635
4636 // need to copy the funcref so that we can clear rettv
4637 if (evaluate)
4638 {
4639 functv = *rettv;
4640 rettv->v_type = VAR_UNKNOWN;
4641
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004642 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004643 if (functv.v_type == VAR_PARTIAL)
4644 {
4645 pt = functv.vval.v_partial;
4646 s = partial_name(pt);
4647 }
4648 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004649 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004650 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004651 if (s == NULL || *s == NUL)
4652 {
4653 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004654 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004655 goto theend;
4656 }
4657 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004658 }
4659 else
4660 s = (char_u *)"";
4661
Bram Moolenaara80faa82020-04-12 19:37:17 +02004662 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004663 funcexe.fe_firstline = curwin->w_cursor.lnum;
4664 funcexe.fe_lastline = curwin->w_cursor.lnum;
4665 funcexe.fe_evaluate = evaluate;
4666 funcexe.fe_partial = pt;
4667 funcexe.fe_selfdict = selfdict;
4668 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004669 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004670
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004671theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004672 // Clear the funcref afterwards, so that deleting it while
4673 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004674 if (evaluate)
4675 clear_tv(&functv);
4676
4677 return ret;
4678}
4679
4680/*
4681 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004682 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004683 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4684 */
4685 static int
4686eval_lambda(
4687 char_u **arg,
4688 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004689 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004690 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004691{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004692 int evaluate = evalarg != NULL
4693 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004694 typval_T base = *rettv;
4695 int ret;
4696
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004697 rettv->v_type = VAR_UNKNOWN;
4698
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004699 if (**arg == '{')
4700 {
4701 // ->{lambda}()
4702 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4703 }
4704 else
4705 {
4706 // ->(lambda)()
4707 ++*arg;
4708 ret = eval1(arg, rettv, evalarg);
4709 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004710 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004711 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004712 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004713 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004714 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004715 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4716 && rettv->v_type != VAR_PARTIAL)
4717 {
4718 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4719 return FAIL;
4720 }
4721 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004722 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004723 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004724 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004725
4726 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004727 {
4728 if (verbose)
4729 {
4730 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004731 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004732 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004733 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004734 }
4735 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004736 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004737 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004738 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004739 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004740
4741 // Clear the funcref afterwards, so that deleting it while
4742 // evaluating the arguments is possible (see test55).
4743 if (evaluate)
4744 clear_tv(&base);
4745
4746 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004747}
4748
4749/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004750 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004751 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004752 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4753 */
4754 static int
4755eval_method(
4756 char_u **arg,
4757 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004758 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004759 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004760{
4761 char_u *name;
4762 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004763 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004764 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004765 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004766 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004767 int evaluate = evalarg != NULL
4768 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004769
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004770 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004771
Bram Moolenaarac92e252019-08-03 21:58:38 +02004772 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004773 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004774 if (alias != NULL)
4775 name = alias;
4776
4777 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004778 {
4779 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004780 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004781 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004782 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004783 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004784 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004785 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004786
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004787 // If there is no "(" immediately following, but there is further on,
4788 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4789 // Does not handle anything where "(" is part of the expression.
4790 *arg = skipwhite(*arg);
4791
4792 if (**arg != '(' && alias == NULL
4793 && (paren = vim_strchr(*arg, '(')) != NULL)
4794 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004795 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00004796
4797 // Truncate the name a the "(". Avoid trying to get another line
4798 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004799 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00004800 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
4801 if (evalarg != NULL)
4802 {
4803 getline = evalarg->eval_getline;
4804 evalarg->eval_getline = NULL;
4805 }
4806
4807 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004808 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004809 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004810 *arg = name + len;
4811 ret = FAIL;
4812 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004813 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004814 {
4815 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004816 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004817 }
Bram Moolenaar34820942022-12-19 20:28:38 +00004818
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004819 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00004820 if (getline != NULL)
4821 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004822 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004823
4824 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004825 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004826 *arg = skipwhite(*arg);
4827
4828 if (**arg != '(')
4829 {
4830 if (verbose)
4831 semsg(_(e_missing_parenthesis_str), name);
4832 ret = FAIL;
4833 }
4834 else if (VIM_ISWHITE((*arg)[-1]))
4835 {
4836 if (verbose)
4837 emsg(_(e_no_white_space_allowed_before_parenthesis));
4838 ret = FAIL;
4839 }
4840 else
4841 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004842 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004843 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004844 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004845
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004846 // Clear the funcref afterwards, so that deleting it while
4847 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004848 if (evaluate)
4849 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004850 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004851
Bram Moolenaarac92e252019-08-03 21:58:38 +02004852 return ret;
4853}
4854
4855/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004856 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4857 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004858 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4859 */
4860 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004861eval_index(
4862 char_u **arg,
4863 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004864 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004865 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004866{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004867 int evaluate = evalarg != NULL
4868 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004869 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004870 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004871 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004872 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004873 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004874 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004875
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004876 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4877 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004878
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004879 init_tv(&var1);
4880 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004881 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004882 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004883 /*
4884 * dict.name
4885 */
4886 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004887 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004888 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004889 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004890 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004891 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004892 }
4893 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004894 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004895 /*
4896 * something[idx]
4897 *
4898 * Get the (first) variable from inside the [].
4899 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004900 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004901 if (**arg == ':')
4902 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004903 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004904 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004905 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004906 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004907 semsg(_(e_white_space_required_before_and_after_str_at_str),
4908 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004909 clear_tv(&var1);
4910 return FAIL;
4911 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004912 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004913 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004914 int error = FALSE;
4915
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004916 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004917 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004918 && var1.v_type == VAR_FLOAT)
4919 {
4920 var1.vval.v_string = typval_tostring(&var1, TRUE);
4921 var1.v_type = VAR_STRING;
4922 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004923
Bram Moolenaar4525a572022-02-13 11:57:33 +00004924 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004925 tv_get_number_chk(&var1, &error);
4926 else
4927 error = tv_get_string_chk(&var1) == NULL;
4928 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004929 {
4930 // not a number or string
4931 clear_tv(&var1);
4932 return FAIL;
4933 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004934 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004935
4936 /*
4937 * Get the second variable from inside the [:].
4938 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004939 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004940 if (**arg == ':')
4941 {
4942 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004943 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004944 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004945 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004946 semsg(_(e_white_space_required_before_and_after_str_at_str),
4947 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004948 if (!empty1)
4949 clear_tv(&var1);
4950 return FAIL;
4951 }
4952 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004953 if (**arg == ']')
4954 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004955 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004956 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004957 if (!empty1)
4958 clear_tv(&var1);
4959 return FAIL;
4960 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004961 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004962 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004963 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004964 if (!empty1)
4965 clear_tv(&var1);
4966 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004967 return FAIL;
4968 }
4969 }
4970
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004971 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004972 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004973 if (**arg != ']')
4974 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004975 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004976 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004977 clear_tv(&var1);
4978 if (range)
4979 clear_tv(&var2);
4980 return FAIL;
4981 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004982 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004983 }
4984
4985 if (evaluate)
4986 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004987 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004988 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004989 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004990
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004991 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004992 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004993 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004994 clear_tv(&var2);
4995 return res;
4996 }
4997 return OK;
4998}
4999
5000/*
5001 * Check if "rettv" can have an [index] or [sli:ce]
5002 */
5003 int
5004check_can_index(typval_T *rettv, int evaluate, int verbose)
5005{
5006 switch (rettv->v_type)
5007 {
5008 case VAR_FUNC:
5009 case VAR_PARTIAL:
5010 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005011 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005012 return FAIL;
5013 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005014 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005015 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005016 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005017 case VAR_BOOL:
5018 case VAR_SPECIAL:
5019 case VAR_JOB:
5020 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005021 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005022 case VAR_CLASS:
5023 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02005024 case VAR_TYPEALIAS:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005025 if (verbose)
5026 emsg(_(e_cannot_index_special_variable));
5027 return FAIL;
5028 case VAR_UNKNOWN:
5029 case VAR_ANY:
5030 case VAR_VOID:
5031 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005032 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005033 emsg(_(e_cannot_index_special_variable));
5034 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005035 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005036 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005037
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005038 case VAR_STRING:
5039 case VAR_LIST:
5040 case VAR_DICT:
5041 case VAR_BLOB:
5042 break;
5043 case VAR_NUMBER:
5044 if (in_vim9script())
5045 emsg(_(e_cannot_index_number));
5046 break;
5047 }
5048 return OK;
5049}
5050
5051/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01005052 * slice() function
5053 */
5054 void
5055f_slice(typval_T *argvars, typval_T *rettv)
5056{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005057 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005058 && ((argvars[0].v_type != VAR_STRING
5059 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005060 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005061 && check_for_list_arg(argvars, 0) == FAIL)
5062 || check_for_number_arg(argvars, 1) == FAIL
5063 || check_for_opt_number_arg(argvars, 2) == FAIL))
5064 return;
5065
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005066 if (check_can_index(argvars, TRUE, FALSE) != OK)
5067 return;
5068
5069 copy_tv(argvars, rettv);
5070 eval_index_inner(rettv, TRUE, argvars + 1,
5071 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
5072 TRUE, NULL, 0, FALSE);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005073}
5074
5075/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005076 * Apply index or range to "rettv".
5077 * "var1" is the first index, NULL for [:expr].
5078 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01005079 * "exclusive" is TRUE for slice(): second index is exclusive, use character
5080 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005081 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
5082 */
5083 int
5084eval_index_inner(
5085 typval_T *rettv,
5086 int is_range,
5087 typval_T *var1,
5088 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005089 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005090 char_u *key,
5091 int keylen,
5092 int verbose)
5093{
Bram Moolenaar6601b622021-01-13 21:47:15 +01005094 varnumber_T n1, n2 = 0;
5095 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005096
5097 n1 = 0;
5098 if (var1 != NULL && rettv->v_type != VAR_DICT)
5099 n1 = tv_get_number(var1);
5100
5101 if (is_range)
5102 {
5103 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005104 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005105 if (verbose)
5106 emsg(_(e_cannot_slice_dictionary));
5107 return FAIL;
5108 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01005109 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005110 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005111 else
5112 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005113 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005114
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005115 switch (rettv->v_type)
5116 {
5117 case VAR_UNKNOWN:
5118 case VAR_ANY:
5119 case VAR_VOID:
5120 case VAR_FUNC:
5121 case VAR_PARTIAL:
5122 case VAR_FLOAT:
5123 case VAR_BOOL:
5124 case VAR_SPECIAL:
5125 case VAR_JOB:
5126 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005127 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005128 case VAR_CLASS:
5129 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02005130 case VAR_TYPEALIAS:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005131 break; // not evaluating, skipping over subscript
5132
5133 case VAR_NUMBER:
5134 case VAR_STRING:
5135 {
5136 char_u *s = tv_get_string(rettv);
5137
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005138 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005139 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005140 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005141 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005142 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005143 else
5144 s = char_from_string(s, n1);
5145 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005146 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005147 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005148 // The resulting variable is a substring. If the indexes
5149 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005150 if (n1 < 0)
5151 {
5152 n1 = len + n1;
5153 if (n1 < 0)
5154 n1 = 0;
5155 }
5156 if (n2 < 0)
5157 n2 = len + n2;
5158 else if (n2 >= len)
5159 n2 = len;
5160 if (n1 >= len || n2 < 0 || n1 > n2)
5161 s = NULL;
5162 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02005163 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005164 }
5165 else
5166 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005167 // The resulting variable is a string of a single
5168 // character. If the index is too big or negative the
5169 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005170 if (n1 >= len || n1 < 0)
5171 s = NULL;
5172 else
5173 s = vim_strnsave(s + n1, 1);
5174 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005175 clear_tv(rettv);
5176 rettv->v_type = VAR_STRING;
5177 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005178 }
5179 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005180
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005181 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005182 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
5183 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005184 break;
5185
5186 case VAR_LIST:
5187 if (var1 == NULL)
5188 n1 = 0;
5189 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005190 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005191 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005192 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005193 return FAIL;
5194 break;
5195
5196 case VAR_DICT:
5197 {
5198 dictitem_T *item;
5199 typval_T tmp;
5200
5201 if (key == NULL)
5202 {
5203 key = tv_get_string_chk(var1);
5204 if (key == NULL)
5205 return FAIL;
5206 }
5207
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005208 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005209
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005210 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005211 {
5212 if (verbose)
5213 {
5214 if (keylen > 0)
5215 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005216 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005217 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005218 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005219 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005220
5221 copy_tv(&item->di_tv, &tmp);
5222 clear_tv(rettv);
5223 *rettv = tmp;
5224 }
5225 break;
5226 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005227 return OK;
5228}
5229
5230/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005231 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005232 */
5233 char_u *
5234partial_name(partial_T *pt)
5235{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02005236 if (pt != NULL)
5237 {
5238 if (pt->pt_name != NULL)
5239 return pt->pt_name;
5240 if (pt->pt_func != NULL)
5241 return pt->pt_func->uf_name;
5242 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005243 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005244}
5245
Bram Moolenaarddecc252016-04-06 22:59:37 +02005246 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005247partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005248{
5249 int i;
5250
5251 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005252 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005253 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005254 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005255 if (pt->pt_name != NULL)
5256 {
5257 func_unref(pt->pt_name);
5258 vim_free(pt->pt_name);
5259 }
5260 else
5261 func_ptr_unref(pt->pt_func);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005262 object_unref(pt->pt_obj);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005263
Bram Moolenaar54656012021-06-09 20:50:46 +02005264 // "out_up" is no longer used, decrement refcount on partial that owns it.
5265 partial_unref(pt->pt_outer.out_up_partial);
5266
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005267 // Using pt_outer from another partial.
5268 partial_unref(pt->pt_outer_partial);
5269
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005270 // Decrease the reference count for the context of a closure. If down
5271 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005272 if (pt->pt_funcstack != NULL)
5273 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005274 --pt->pt_funcstack->fs_refcount;
5275 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005276 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005277 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005278 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5279 if (pt->pt_loopvars[i] != NULL)
5280 {
5281 --pt->pt_loopvars[i]->lvs_refcount;
5282 loopvars_check_refcount(pt->pt_loopvars[i]);
5283 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005284
Bram Moolenaarddecc252016-04-06 22:59:37 +02005285 vim_free(pt);
5286}
5287
5288/*
5289 * Unreference a closure: decrement the reference count and free it when it
5290 * becomes zero.
5291 */
5292 void
5293partial_unref(partial_T *pt)
5294{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005295 if (pt == NULL)
5296 return;
5297
5298 int done = FALSE;
5299
5300 if (--pt->pt_refcount <= 0)
5301 partial_free(pt);
5302
5303 // If the reference count goes down to one, the funcstack may be the
5304 // only reference and can be freed if no other partials reference it.
5305 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005306 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005307 // careful: if the funcstack is freed it may contain this partial
5308 // and it gets freed as well
5309 if (pt->pt_funcstack != NULL)
5310 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005311
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005312 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005313 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005314 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005315
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005316 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5317 if (pt->pt_loopvars[depth] != NULL
5318 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005319 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005320 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005321 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005322}
5323
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005324/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005325 * Return the next (unique) copy ID.
5326 * Used for serializing nested structures.
5327 */
5328 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005329get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005330{
5331 current_copyID += COPYID_INC;
5332 return current_copyID;
5333}
5334
5335/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005336 * Garbage collection for lists and dictionaries.
5337 *
5338 * We use reference counts to be able to free most items right away when they
5339 * are no longer used. But for composite items it's possible that it becomes
5340 * unused while the reference count is > 0: When there is a recursive
5341 * reference. Example:
5342 * :let l = [1, 2, 3]
5343 * :let d = {9: l}
5344 * :let l[1] = d
5345 *
5346 * Since this is quite unusual we handle this with garbage collection: every
5347 * once in a while find out which lists and dicts are not referenced from any
5348 * variable.
5349 *
5350 * Here is a good reference text about garbage collection (refers to Python
5351 * but it applies to all reference-counting mechanisms):
5352 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005353 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005354
5355/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005356 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005357 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005358 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005359 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005360 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005361garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005362{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005363 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005364 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005365 buf_T *buf;
5366 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005367 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005368 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005369
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005370 if (!testing)
5371 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005372 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005373 want_garbage_collect = FALSE;
5374 may_garbage_collect = FALSE;
5375 garbage_collect_at_exit = FALSE;
5376 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005377
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005378 // The execution stack can grow big, limit the size.
5379 if (exestack.ga_maxlen - exestack.ga_len > 500)
5380 {
5381 size_t new_len;
5382 char_u *pp;
5383 int n;
5384
5385 // Keep 150% of the current size, with a minimum of the growth size.
5386 n = exestack.ga_len / 2;
5387 if (n < exestack.ga_growsize)
5388 n = exestack.ga_growsize;
5389
5390 // Don't make it bigger though.
5391 if (exestack.ga_len + n < exestack.ga_maxlen)
5392 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005393 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005394 pp = vim_realloc(exestack.ga_data, new_len);
5395 if (pp == NULL)
5396 return FAIL;
5397 exestack.ga_maxlen = exestack.ga_len + n;
5398 exestack.ga_data = pp;
5399 }
5400 }
5401
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005402 // We advance by two because we add one for items referenced through
5403 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005404 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005405
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005406 /*
5407 * 1. Go through all accessible variables and mark all lists and dicts
5408 * with copyID.
5409 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005410
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005411 // Don't free variables in the previous_funccal list unless they are only
5412 // referenced through previous_funccal. This must be first, because if
5413 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005414 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005415
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005416 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005417 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005418
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005419 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005420 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005421 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5422 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005423
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005424 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005425 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005426 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5427 NULL, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005428 // window-local variables in autocmd windows
Bram Moolenaare76062c2022-11-28 18:51:43 +00005429 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005430 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00005431 abort = abort || set_ref_in_item(
5432 &aucmd_win[i].auc_win->w_winvar.di_tv, copyID, NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005433#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005434 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005435 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5436 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005437 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005438 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005439 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5440 NULL, NULL);
5441#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005442
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005443 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005444 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005445 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5446 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005447 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005448 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005449
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005450 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005451 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005452
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005453 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005454 abort = abort || set_ref_in_functions(copyID);
5455
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005456 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005457 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005458
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005459 // funcstacks keep variables for closures
5460 abort = abort || set_ref_in_funcstacks(copyID);
5461
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005462 // loopvars keep variables for loop blocks
5463 abort = abort || set_ref_in_loopvars(copyID);
5464
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005465 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005466 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005467
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005468 // callbacks in buffers
5469 abort = abort || set_ref_in_buffers(copyID);
5470
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005471 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5472 abort = abort || set_ref_in_insexpand_funcs(copyID);
5473
5474 // 'operatorfunc' callback
5475 abort = abort || set_ref_in_opfunc(copyID);
5476
5477 // 'tagfunc' callback
5478 abort = abort || set_ref_in_tagfunc(copyID);
5479
5480 // 'imactivatefunc' and 'imstatusfunc' callbacks
5481 abort = abort || set_ref_in_im_funcs(copyID);
5482
Bram Moolenaar1dced572012-04-05 16:54:08 +02005483#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005484 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005485#endif
5486
Bram Moolenaardb913952012-06-29 12:54:53 +02005487#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005488 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005489#endif
5490
5491#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005492 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005493#endif
5494
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005495#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005496 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005497 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005498#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005499#ifdef FEAT_NETBEANS_INTG
5500 abort = abort || set_ref_in_nb_channel(copyID);
5501#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005502
Bram Moolenaare3188e22016-05-31 21:13:04 +02005503#ifdef FEAT_TIMERS
5504 abort = abort || set_ref_in_timer(copyID);
5505#endif
5506
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005507#ifdef FEAT_QUICKFIX
5508 abort = abort || set_ref_in_quickfix(copyID);
5509#endif
5510
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005511#ifdef FEAT_TERMINAL
5512 abort = abort || set_ref_in_term(copyID);
5513#endif
5514
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005515#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005516 abort = abort || set_ref_in_popups(copyID);
5517#endif
5518
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005519 abort = abort || set_ref_in_classes(copyID);
5520
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005521 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005522 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005523 /*
5524 * 2. Free lists and dictionaries that are not referenced.
5525 */
5526 did_free = free_unref_items(copyID);
5527
5528 /*
5529 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005530 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005531 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005532 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005533 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005534 else if (p_verbose > 0)
5535 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005536 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005537 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005538
5539 return did_free;
5540}
5541
5542/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005543 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005544 */
5545 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005546free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005547{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005548 int did_free = FALSE;
5549
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005550 // Let all "free" functions know that we are here. This means no
5551 // dictionaries, lists, channels or jobs are to be freed, because we will
5552 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005553 in_free_unref_items = TRUE;
5554
5555 /*
5556 * PASS 1: free the contents of the items. We don't free the items
5557 * themselves yet, so that it is possible to decrement refcount counters
5558 */
5559
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005560 // Go through the list of dicts and free items without this copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005561 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005562
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005563 // Go through the list of lists and free items without this copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005564 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005565
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005566 // Go through the list of objects and free items without this copyID.
5567 did_free |= object_free_nonref(copyID);
5568
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005569 // Go through the list of classes and free items without this copyID.
5570 did_free |= class_free_nonref(copyID);
5571
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005572#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005573 // Go through the list of jobs and free items without the copyID. This
5574 // must happen before doing channels, because jobs refer to channels, but
5575 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005576 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5577
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005578 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005579 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5580#endif
5581
5582 /*
5583 * PASS 2: free the items themselves.
5584 */
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005585 object_free_items(copyID);
Bram Moolenaarcd524592016-07-17 14:57:05 +02005586 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005587 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005588
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005589#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005590 // Go through the list of jobs and free items without the copyID. This
5591 // must happen before doing channels, because jobs refer to channels, but
5592 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005593 free_unused_jobs(copyID, COPYID_MASK);
5594
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005595 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005596 free_unused_channels(copyID, COPYID_MASK);
5597#endif
5598
5599 in_free_unref_items = FALSE;
5600
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005601 return did_free;
5602}
5603
5604/*
5605 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005606 * "list_stack" is used to add lists to be marked. Can be NULL.
5607 *
5608 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005609 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005610 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005611set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005612{
5613 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005614 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005615 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005616 hashtab_T *cur_ht;
5617 ht_stack_T *ht_stack = NULL;
5618 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005619
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005620 cur_ht = ht;
5621 for (;;)
5622 {
5623 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005624 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005625 // Mark each item in the hashtab. If the item contains a hashtab
5626 // it is added to ht_stack, if it contains a list it is added to
5627 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005628 todo = (int)cur_ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00005629 FOR_ALL_HASHTAB_ITEMS(cur_ht, hi, todo)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005630 if (!HASHITEM_EMPTY(hi))
5631 {
5632 --todo;
5633 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5634 &ht_stack, list_stack);
5635 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005636 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005637
5638 if (ht_stack == NULL)
5639 break;
5640
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005641 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005642 cur_ht = ht_stack->ht;
5643 tempitem = ht_stack;
5644 ht_stack = ht_stack->prev;
5645 free(tempitem);
5646 }
5647
5648 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005649}
5650
Dominique Pelle748b3082022-01-08 12:41:16 +00005651#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5652 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005653/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005654 * Mark a dict and its items with "copyID".
5655 * Returns TRUE if setting references failed somehow.
5656 */
5657 int
5658set_ref_in_dict(dict_T *d, int copyID)
5659{
5660 if (d != NULL && d->dv_copyID != copyID)
5661 {
5662 d->dv_copyID = copyID;
5663 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5664 }
5665 return FALSE;
5666}
Dominique Pelle748b3082022-01-08 12:41:16 +00005667#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005668
5669/*
5670 * Mark a list and its items with "copyID".
5671 * Returns TRUE if setting references failed somehow.
5672 */
5673 int
5674set_ref_in_list(list_T *ll, int copyID)
5675{
5676 if (ll != NULL && ll->lv_copyID != copyID)
5677 {
5678 ll->lv_copyID = copyID;
5679 return set_ref_in_list_items(ll, copyID, NULL);
5680 }
5681 return FALSE;
5682}
5683
5684/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005685 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005686 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5687 *
5688 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005689 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005690 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005691set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005692{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005693 listitem_T *li;
5694 int abort = FALSE;
5695 list_T *cur_l;
5696 list_stack_T *list_stack = NULL;
5697 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005698
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005699 cur_l = l;
5700 for (;;)
5701 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005702 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005703 // Mark each item in the list. If the item contains a hashtab
5704 // it is added to ht_stack, if it contains a list it is added to
5705 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005706 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5707 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5708 ht_stack, &list_stack);
5709 if (list_stack == NULL)
5710 break;
5711
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005712 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005713 cur_l = list_stack->list;
5714 tempitem = list_stack;
5715 list_stack = list_stack->prev;
5716 free(tempitem);
5717 }
5718
5719 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005720}
5721
5722/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005723 * Mark the partial in callback 'cb' with "copyID".
5724 */
5725 int
5726set_ref_in_callback(callback_T *cb, int copyID)
5727{
5728 typval_T tv;
5729
5730 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5731 return FALSE;
5732
5733 tv.v_type = VAR_PARTIAL;
5734 tv.vval.v_partial = cb->cb_partial;
5735 return set_ref_in_item(&tv, copyID, NULL, NULL);
5736}
5737
5738/*
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005739 * Mark the dict "dd" with "copyID".
5740 * Also see set_ref_in_item().
5741 */
5742 static int
5743set_ref_in_item_dict(
5744 dict_T *dd,
5745 int copyID,
5746 ht_stack_T **ht_stack,
5747 list_stack_T **list_stack)
5748{
5749 if (dd == NULL || dd->dv_copyID == copyID)
5750 return FALSE;
5751
5752 // Didn't see this dict yet.
5753 dd->dv_copyID = copyID;
5754 if (ht_stack == NULL)
5755 return set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5756
5757 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5758 if (newitem == NULL)
5759 return TRUE;
5760
5761 newitem->ht = &dd->dv_hashtab;
5762 newitem->prev = *ht_stack;
5763 *ht_stack = newitem;
5764
5765 return FALSE;
5766}
5767
5768/*
5769 * Mark the list "ll" with "copyID".
5770 * Also see set_ref_in_item().
5771 */
5772 static int
5773set_ref_in_item_list(
5774 list_T *ll,
5775 int copyID,
5776 ht_stack_T **ht_stack,
5777 list_stack_T **list_stack)
5778{
5779 if (ll == NULL || ll->lv_copyID == copyID)
5780 return FALSE;
5781
5782 // Didn't see this list yet.
5783 ll->lv_copyID = copyID;
5784 if (list_stack == NULL)
5785 return set_ref_in_list_items(ll, copyID, ht_stack);
5786
5787 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5788 if (newitem == NULL)
5789 return TRUE;
5790
5791 newitem->list = ll;
5792 newitem->prev = *list_stack;
5793 *list_stack = newitem;
5794
5795 return FALSE;
5796}
5797
5798/*
5799 * Mark the partial "pt" with "copyID".
5800 * Also see set_ref_in_item().
5801 */
5802 static int
5803set_ref_in_item_partial(
5804 partial_T *pt,
5805 int copyID,
5806 ht_stack_T **ht_stack,
5807 list_stack_T **list_stack)
5808{
5809 if (pt == NULL || pt->pt_copyID == copyID)
5810 return FALSE;
5811
5812 // Didn't see this partial yet.
5813 pt->pt_copyID = copyID;
5814
5815 int abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
5816
5817 if (pt->pt_dict != NULL)
5818 {
5819 typval_T dtv;
5820
5821 dtv.v_type = VAR_DICT;
5822 dtv.vval.v_dict = pt->pt_dict;
5823 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5824 }
5825
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005826 if (pt->pt_obj != NULL)
5827 {
5828 typval_T objtv;
5829
5830 objtv.v_type = VAR_OBJECT;
5831 objtv.vval.v_object = pt->pt_obj;
5832 set_ref_in_item(&objtv, copyID, ht_stack, list_stack);
5833 }
5834
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005835 for (int i = 0; i < pt->pt_argc; ++i)
5836 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5837 ht_stack, list_stack);
5838 // pt_funcstack is handled in set_ref_in_funcstacks()
5839 // pt_loopvars is handled in set_ref_in_loopvars()
5840
5841 return abort;
5842}
5843
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005844#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005845/*
5846 * Mark the job "pt" with "copyID".
5847 * Also see set_ref_in_item().
5848 */
5849 static int
5850set_ref_in_item_job(
5851 job_T *job,
5852 int copyID,
5853 ht_stack_T **ht_stack,
5854 list_stack_T **list_stack)
5855{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005856 typval_T dtv;
5857
5858 if (job == NULL || job->jv_copyID == copyID)
5859 return FALSE;
5860
5861 job->jv_copyID = copyID;
5862 if (job->jv_channel != NULL)
5863 {
5864 dtv.v_type = VAR_CHANNEL;
5865 dtv.vval.v_channel = job->jv_channel;
5866 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5867 }
5868 if (job->jv_exit_cb.cb_partial != NULL)
5869 {
5870 dtv.v_type = VAR_PARTIAL;
5871 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
5872 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5873 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005874
5875 return FALSE;
5876}
5877
5878/*
5879 * Mark the channel "ch" with "copyID".
5880 * Also see set_ref_in_item().
5881 */
5882 static int
5883set_ref_in_item_channel(
5884 channel_T *ch,
5885 int copyID,
5886 ht_stack_T **ht_stack,
5887 list_stack_T **list_stack)
5888{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005889 typval_T dtv;
5890
5891 if (ch == NULL || ch->ch_copyID == copyID)
5892 return FALSE;
5893
5894 ch->ch_copyID = copyID;
5895 for (ch_part_T part = PART_SOCK; part < PART_COUNT; ++part)
5896 {
5897 for (jsonq_T *jq = ch->ch_part[part].ch_json_head.jq_next;
5898 jq != NULL; jq = jq->jq_next)
5899 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5900 for (cbq_T *cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5901 cq = cq->cq_next)
5902 if (cq->cq_callback.cb_partial != NULL)
5903 {
5904 dtv.v_type = VAR_PARTIAL;
5905 dtv.vval.v_partial = cq->cq_callback.cb_partial;
5906 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5907 }
5908 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
5909 {
5910 dtv.v_type = VAR_PARTIAL;
5911 dtv.vval.v_partial = ch->ch_part[part].ch_callback.cb_partial;
5912 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5913 }
5914 }
5915 if (ch->ch_callback.cb_partial != NULL)
5916 {
5917 dtv.v_type = VAR_PARTIAL;
5918 dtv.vval.v_partial = ch->ch_callback.cb_partial;
5919 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5920 }
5921 if (ch->ch_close_cb.cb_partial != NULL)
5922 {
5923 dtv.v_type = VAR_PARTIAL;
5924 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
5925 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5926 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005927
5928 return FALSE;
5929}
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005930#endif
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005931
5932/*
5933 * Mark the class "cl" with "copyID".
5934 * Also see set_ref_in_item().
5935 */
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005936 int
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005937set_ref_in_item_class(
5938 class_T *cl,
5939 int copyID,
5940 ht_stack_T **ht_stack,
5941 list_stack_T **list_stack)
5942{
5943 int abort = FALSE;
5944
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005945 if (cl == NULL || cl->class_copyID == copyID)
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005946 return FALSE;
5947
5948 cl->class_copyID = copyID;
Yegappan Lakshmanan544be0d2023-09-04 22:14:28 +02005949 if (cl->class_members_tv != NULL)
5950 {
5951 // The "class_members_tv" table is allocated only for regular classes
5952 // and not for interfaces.
5953 for (int i = 0; !abort && i < cl->class_class_member_count; ++i)
5954 abort = abort || set_ref_in_item(
5955 &cl->class_members_tv[i],
5956 copyID, ht_stack, list_stack);
5957 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005958
5959 for (int i = 0; !abort && i < cl->class_class_function_count; ++i)
5960 abort = abort || set_ref_in_func(NULL,
5961 cl->class_class_functions[i], copyID);
5962
5963 for (int i = 0; !abort && i < cl->class_obj_method_count; ++i)
5964 abort = abort || set_ref_in_func(NULL,
5965 cl->class_obj_methods[i], copyID);
5966
5967 return abort;
5968}
5969
5970/*
5971 * Mark the object "cl" with "copyID".
5972 * Also see set_ref_in_item().
5973 */
5974 static int
5975set_ref_in_item_object(
5976 object_T *obj,
5977 int copyID,
5978 ht_stack_T **ht_stack,
5979 list_stack_T **list_stack)
5980{
5981 int abort = FALSE;
5982
5983 if (obj == NULL || obj->obj_copyID == copyID)
5984 return FALSE;
5985
5986 obj->obj_copyID = copyID;
5987
5988 // The typval_T array is right after the object_T.
5989 typval_T *mtv = (typval_T *)(obj + 1);
5990 for (int i = 0; !abort
5991 && i < obj->obj_class->class_obj_member_count; ++i)
5992 abort = abort || set_ref_in_item(mtv + i, copyID,
5993 ht_stack, list_stack);
5994
5995 return abort;
5996}
5997
5998/*
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005999 * Mark all lists, dicts and other container types referenced through typval
6000 * "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006001 * "list_stack" is used to add lists to be marked. Can be NULL.
6002 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6003 *
6004 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006005 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006006 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006007set_ref_in_item(
6008 typval_T *tv,
6009 int copyID,
6010 ht_stack_T **ht_stack,
6011 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006012{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006013 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006014
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006015 switch (tv->v_type)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006016 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006017 case VAR_DICT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006018 return set_ref_in_item_dict(tv->vval.v_dict, copyID,
6019 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006020
6021 case VAR_LIST:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006022 return set_ref_in_item_list(tv->vval.v_list, copyID,
6023 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006024
6025 case VAR_FUNC:
6026 {
6027 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
6028 break;
6029 }
6030
6031 case VAR_PARTIAL:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006032 return set_ref_in_item_partial(tv->vval.v_partial, copyID,
6033 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006034
6035 case VAR_JOB:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006036#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006037 return set_ref_in_item_job(tv->vval.v_job, copyID,
6038 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006039#else
6040 break;
6041#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006042
6043 case VAR_CHANNEL:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006044#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006045 return set_ref_in_item_channel(tv->vval.v_channel, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006046 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006047#else
6048 break;
6049#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006050
6051 case VAR_CLASS:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006052 return set_ref_in_item_class(tv->vval.v_class, copyID,
6053 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006054
6055 case VAR_OBJECT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006056 return set_ref_in_item_object(tv->vval.v_object, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006057 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006058
6059 case VAR_UNKNOWN:
6060 case VAR_ANY:
6061 case VAR_VOID:
6062 case VAR_BOOL:
6063 case VAR_SPECIAL:
6064 case VAR_NUMBER:
6065 case VAR_FLOAT:
6066 case VAR_STRING:
6067 case VAR_BLOB:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006068 case VAR_TYPEALIAS:
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006069 case VAR_INSTR:
6070 // Types that do not contain any other item
6071 break;
6072 }
6073
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006074 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006075}
6076
Bram Moolenaar8c711452005-01-14 21:53:12 +00006077/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006078 * Return a string with the string representation of a variable.
6079 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006080 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006081 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006082 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006083 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02006084 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006085 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6086 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006087 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006088 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006089 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006090echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006091 typval_T *tv,
6092 char_u **tofree,
6093 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006094 int copyID,
6095 int echo_style,
6096 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006097 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006098{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006099 static int recurse = 0;
6100 char_u *r = NULL;
6101
Bram Moolenaar33570922005-01-25 22:26:29 +00006102 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006103 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006104 if (!did_echo_string_emsg)
6105 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006106 // Only give this message once for a recursive call to avoid
6107 // flooding the user with errors. And stop iterating over lists
6108 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02006109 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00006110 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006111 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006112 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006113 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006114 }
6115 ++recurse;
6116
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006117 switch (tv->v_type)
6118 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006119 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006120 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006121 {
6122 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006123 r = tv->vval.v_string;
6124 if (r == NULL)
6125 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006126 }
6127 else
6128 {
6129 *tofree = string_quote(tv->vval.v_string, FALSE);
6130 r = *tofree;
6131 }
6132 break;
6133
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006134 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006135 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01006136 char_u buf[MAX_FUNC_NAME_LEN];
6137
6138 if (echo_style)
6139 {
LemonBoya5d35902022-04-29 21:15:02 +01006140 r = tv->vval.v_string == NULL ? (char_u *)"function()"
6141 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01006142 buf, MAX_FUNC_NAME_LEN);
6143 if (r == buf)
6144 {
6145 r = vim_strsave(buf);
6146 *tofree = r;
6147 }
6148 else
6149 *tofree = NULL;
6150 }
6151 else
6152 {
6153 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
6154 : make_ufunc_name_readable(
6155 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
6156 TRUE);
6157 r = *tofree;
6158 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006159 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006160 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006161
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006162 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006163 {
6164 partial_T *pt = tv->vval.v_partial;
6165 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006166 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006167 garray_T ga;
6168 int i;
6169 char_u *tf;
6170
6171 ga_init2(&ga, 1, 100);
6172 ga_concat(&ga, (char_u *)"function(");
6173 if (fname != NULL)
6174 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006175 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00006176 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006177 && vim_isupper(fname[1]))
6178 {
6179 ga_concat(&ga, (char_u *)"'g:");
6180 ga_concat(&ga, fname + 1);
6181 }
6182 else
6183 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006184 vim_free(fname);
6185 }
6186 if (pt != NULL && pt->pt_argc > 0)
6187 {
6188 ga_concat(&ga, (char_u *)", [");
6189 for (i = 0; i < pt->pt_argc; ++i)
6190 {
6191 if (i > 0)
6192 ga_concat(&ga, (char_u *)", ");
6193 ga_concat(&ga,
6194 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6195 vim_free(tf);
6196 }
6197 ga_concat(&ga, (char_u *)"]");
6198 }
6199 if (pt != NULL && pt->pt_dict != NULL)
6200 {
6201 typval_T dtv;
6202
6203 ga_concat(&ga, (char_u *)", ");
6204 dtv.v_type = VAR_DICT;
6205 dtv.vval.v_dict = pt->pt_dict;
6206 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6207 vim_free(tf);
6208 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00006209 // terminate with ')' and a NUL
6210 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006211
6212 *tofree = ga.ga_data;
6213 r = *tofree;
6214 break;
6215 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006216
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006217 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006218 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006219 break;
6220
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006221 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006222 if (tv->vval.v_list == NULL)
6223 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02006224 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006225 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02006226 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006227 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006228 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6229 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006230 {
6231 *tofree = NULL;
6232 r = (char_u *)"[...]";
6233 }
6234 else
6235 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006236 int old_copyID = tv->vval.v_list->lv_copyID;
6237
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006238 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006239 *tofree = list2string(tv, copyID, restore_copyID);
6240 if (restore_copyID)
6241 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006242 r = *tofree;
6243 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006244 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006245
Bram Moolenaar8c711452005-01-14 21:53:12 +00006246 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006247 if (tv->vval.v_dict == NULL)
6248 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006249 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006250 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006251 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006252 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006253 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6254 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006255 {
6256 *tofree = NULL;
6257 r = (char_u *)"{...}";
6258 }
6259 else
6260 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006261 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006262
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006263 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006264 *tofree = dict2string(tv, copyID, restore_copyID);
6265 if (restore_copyID)
6266 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006267 r = *tofree;
6268 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006269 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006270
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006271 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006272 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006273 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006274 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006275 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006276 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006277 break;
6278
Bram Moolenaar835dc632016-02-07 14:27:38 +01006279 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006280 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006281#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00006282 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02006283 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
6284 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006285 if (composite_val)
6286 {
6287 *tofree = string_quote(r, FALSE);
6288 r = *tofree;
6289 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006290#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006291 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006292
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006293 case VAR_INSTR:
6294 *tofree = NULL;
6295 r = (char_u *)"instructions";
6296 break;
6297
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006298 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006299 {
6300 class_T *cl = tv->vval.v_class;
6301 size_t len = 6 + (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
6302 r = *tofree = alloc(len);
6303 vim_snprintf((char *)r, len, "class %s",
6304 cl == NULL ? "[unknown]" : (char *)cl->class_name);
6305 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006306 break;
6307
6308 case VAR_OBJECT:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006309 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006310 garray_T ga;
6311 ga_init2(&ga, 1, 50);
6312 ga_concat(&ga, (char_u *)"object of ");
6313 object_T *obj = tv->vval.v_object;
6314 class_T *cl = obj == NULL ? NULL : obj->obj_class;
6315 ga_concat(&ga, cl == NULL ? (char_u *)"[unknown]"
6316 : cl->class_name);
6317 if (cl != NULL)
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006318 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006319 ga_concat(&ga, (char_u *)" {");
6320 for (int i = 0; i < cl->class_obj_member_count; ++i)
6321 {
6322 if (i > 0)
6323 ga_concat(&ga, (char_u *)", ");
Bram Moolenaard505d172022-12-18 21:42:55 +00006324 ocmember_T *m = &cl->class_obj_members[i];
6325 ga_concat(&ga, m->ocm_name);
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006326 ga_concat(&ga, (char_u *)": ");
6327 char_u *tf = NULL;
6328 ga_concat(&ga, echo_string_core(
6329 (typval_T *)(obj + 1) + i,
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006330 &tf, numbuf, copyID, echo_style,
6331 restore_copyID, composite_val));
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006332 vim_free(tf);
6333 }
6334 ga_concat(&ga, (char_u *)"}");
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006335 }
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006336
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006337 *tofree = r = ga.ga_data;
6338 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006339 break;
6340
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006341 case VAR_FLOAT:
6342 *tofree = NULL;
6343 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6344 r = numbuf;
6345 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006346
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006347 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006348 case VAR_SPECIAL:
6349 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006350 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006351 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006352
6353 case VAR_TYPEALIAS:
6354 *tofree = vim_strsave(tv->vval.v_typealias->ta_name);
6355 r = *tofree;
6356 if (r == NULL)
6357 r = (char_u *)"";
6358 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006359 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006360
Bram Moolenaar8502c702014-06-17 12:51:16 +02006361 if (--recurse == 0)
6362 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006363 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006364}
6365
6366/*
6367 * Return a string with the string representation of a variable.
6368 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6369 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006370 * Does not put quotes around strings, as ":echo" displays values.
6371 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6372 * May return NULL.
6373 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006374 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006375echo_string(
6376 typval_T *tv,
6377 char_u **tofree,
6378 char_u *numbuf,
6379 int copyID)
6380{
6381 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6382}
6383
6384/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006385 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6386 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006387 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006388 */
6389 int
6390buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6391{
6392 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006393 char_u *t;
6394 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006395
6396 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6397 return -1;
6398
6399 if (lnum > buf->b_ml.ml_line_count)
6400 lnum = buf->b_ml.ml_line_count;
6401
6402 str = ml_get_buf(buf, lnum, FALSE);
6403 if (str == NULL)
6404 return -1;
6405
6406 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006407 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006408
Bram Moolenaar91458462021-01-13 20:08:38 +01006409 // count the number of characters
6410 t = str;
6411 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6412 t += mb_ptr2len(t);
6413
6414 // In insert mode, when the cursor is at the end of a non-empty line,
6415 // byteidx points to the NUL character immediately past the end of the
6416 // string. In this case, add one to the character count.
6417 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6418 count++;
6419
6420 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006421}
6422
6423/*
6424 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006425 * byte index. Works only for loaded buffers. Returns -1 on failure.
6426 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006427 */
6428 int
6429buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6430{
6431 char_u *str;
6432 char_u *t;
6433
6434 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6435 return -1;
6436
6437 if (lnum > buf->b_ml.ml_line_count)
6438 lnum = buf->b_ml.ml_line_count;
6439
6440 str = ml_get_buf(buf, lnum, FALSE);
6441 if (str == NULL)
6442 return -1;
6443
6444 // Convert the character offset to a byte offset
6445 t = str;
6446 while (*t != NUL && --charidx > 0)
6447 t += mb_ptr2len(t);
6448
Bram Moolenaar91458462021-01-13 20:08:38 +01006449 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006450}
6451
6452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006454 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006456 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006457var2fpos(
6458 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006459 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006460 int *fnum, // set to fnum for '0, 'A, etc.
6461 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006463 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006465 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006467 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006468 if (varp->v_type == VAR_LIST)
6469 {
6470 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006471 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006472 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006473 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006474
6475 l = varp->vval.v_list;
6476 if (l == NULL)
6477 return NULL;
6478
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006479 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006480 pos.lnum = list_find_nr(l, 0L, &error);
6481 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006482 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006483 if (charcol)
6484 len = (long)mb_charlen(ml_get(pos.lnum));
6485 else
6486 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006487
Bram Moolenaarec65d772020-08-20 22:29:12 +02006488 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006489 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006490 li = list_find(l, 1L);
6491 if (li != NULL && li->li_tv.v_type == VAR_STRING
6492 && li->li_tv.vval.v_string != NULL
6493 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006494 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006495 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006496 }
6497 else
6498 {
6499 pos.col = list_find_nr(l, 1L, &error);
6500 if (error)
6501 return NULL;
6502 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006503
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006504 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006505 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006506 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006507 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006508
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006509 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006510 pos.coladd = list_find_nr(l, 2L, &error);
6511 if (error)
6512 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006513
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006514 return &pos;
6515 }
6516
Bram Moolenaarc5809432021-03-27 21:23:30 +01006517 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6518 return NULL;
6519
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006520 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006521 if (name == NULL)
6522 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006523
6524 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006525 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006526 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006527 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006528 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006529 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006530 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006531 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006532 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006533 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006534 pos = VIsual;
6535 else
6536 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006537 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006538 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006539 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006541 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006542 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6544 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006545 pos = *pp;
6546 }
6547 if (pos.lnum != 0)
6548 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006549 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006550 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6551 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006553
Bram Moolenaara5525202006-03-02 22:52:09 +00006554 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006555
Bram Moolenaar477933c2007-07-17 14:32:23 +00006556 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006557 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006558 // the "w_valid" flags are not reset when moving the cursor, but they
6559 // do matter for update_topline() and validate_botline().
6560 check_cursor_moved(curwin);
6561
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006562 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006563 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006564 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006565 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006566 // In silent Ex mode topline is zero, but that's not a valid line
6567 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006568 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006569 return &pos;
6570 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006571 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006572 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006573 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006574 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006575 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006576 return &pos;
6577 }
6578 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006579 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006581 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 {
6583 pos.lnum = curbuf->b_ml.ml_line_count;
6584 pos.col = 0;
6585 }
6586 else
6587 {
6588 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006589 if (charcol)
6590 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6591 else
6592 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 }
6594 return &pos;
6595 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006596 if (in_vim9script())
6597 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 return NULL;
6599}
6600
6601/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006602 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006603 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006604 * Note that the column is passed on as-is, the caller may want to decrement
6605 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006606 * If "charcol" is TRUE use the column as the character index instead of the
6607 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006608 * Return FAIL when conversion is not possible, doesn't check the position for
6609 * validity.
6610 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006611 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006612list2fpos(
6613 typval_T *arg,
6614 pos_T *posp,
6615 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006616 colnr_T *curswantp,
6617 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006618{
6619 list_T *l = arg->vval.v_list;
6620 long i = 0;
6621 long n;
6622
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006623 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6624 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006625 if (arg->v_type != VAR_LIST
6626 || l == NULL
6627 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006628 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006629 return FAIL;
6630
6631 if (fnump != NULL)
6632 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006633 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006634 if (n < 0)
6635 return FAIL;
6636 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006637 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006638 *fnump = n;
6639 }
6640
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006641 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006642 if (n < 0)
6643 return FAIL;
6644 posp->lnum = n;
6645
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006646 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006647 if (n < 0)
6648 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006649 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006650 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006651 if (charcol)
6652 {
6653 buf_T *buf;
6654
6655 // Get the text for the specified line in a loaded buffer
6656 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6657 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6658 return FAIL;
6659
Bram Moolenaar79f23442022-10-10 12:42:57 +01006660 n = buf_charidx_to_byteidx(buf,
6661 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006662 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006663 posp->col = n;
6664
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006665 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006666 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006667 posp->coladd = 0;
6668 else
6669 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006670
Bram Moolenaar493c1782014-05-28 14:34:46 +02006671 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006672 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006673
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006674 return OK;
6675}
6676
6677/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 * Get the length of an environment variable name.
6679 * Advance "arg" to the first character after the name.
6680 * Return 0 for error.
6681 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006682 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006683get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684{
6685 char_u *p;
6686 int len;
6687
6688 for (p = *arg; vim_isIDc(*p); ++p)
6689 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006690 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 return 0;
6692
6693 len = (int)(p - *arg);
6694 *arg = p;
6695 return len;
6696}
6697
6698/*
6699 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006700 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 * Return 0 if something is wrong.
6702 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006703 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006704get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705{
6706 char_u *p;
6707 int len;
6708
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006709 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006711 {
6712 if (*p == ':')
6713 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006714 // "s:" is start of "s:var", but "n:" is not and can be used in
6715 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006716 len = (int)(p - *arg);
6717 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6718 || len > 1)
6719 break;
6720 }
6721 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006722 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 return 0;
6724
6725 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006726 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727
6728 return len;
6729}
6730
6731/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006732 * Get the length of the name of a variable or function.
6733 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006735 * Return -1 if curly braces expansion failed.
6736 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 * If the name contains 'magic' {}'s, expand them and return the
6738 * expanded name in an allocated string via 'alias' - caller must free.
6739 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006740 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006741get_name_len(
6742 char_u **arg,
6743 char_u **alias,
6744 int evaluate,
6745 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746{
6747 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 char_u *p;
6749 char_u *expr_start;
6750 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006752 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753
6754 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6755 && (*arg)[2] == (int)KE_SNR)
6756 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006757 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 *arg += 3;
6759 return get_id_len(arg) + 3;
6760 }
6761 len = eval_fname_script(*arg);
6762 if (len > 0)
6763 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006764 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 *arg += len;
6766 }
6767
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006769 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006771 p = find_name_end(*arg, &expr_start, &expr_end,
6772 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 if (expr_start != NULL)
6774 {
6775 char_u *temp_string;
6776
6777 if (!evaluate)
6778 {
6779 len += (int)(p - *arg);
6780 *arg = skipwhite(p);
6781 return len;
6782 }
6783
6784 /*
6785 * Include any <SID> etc in the expanded string:
6786 * Thus the -len here.
6787 */
6788 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6789 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006790 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 *alias = temp_string;
6792 *arg = skipwhite(p);
6793 return (int)STRLEN(temp_string);
6794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795
6796 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006797 // Only give an error when there is something, otherwise it will be
6798 // reported at a higher level.
6799 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006800 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801
6802 return len;
6803}
6804
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006805/*
6806 * Find the end of a variable or function name, taking care of magic braces.
6807 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6808 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006809 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006810 * Return a pointer to just after the name. Equal to "arg" if there is no
6811 * valid name.
6812 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006813 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006814find_name_end(
6815 char_u *arg,
6816 char_u **expr_start,
6817 char_u **expr_end,
6818 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006820 int mb_nest = 0;
6821 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006823 int len;
zeertzjqa93d9cd2023-05-02 16:25:47 +01006824 int allow_curly = (flags & FNE_ALLOW_CURLY) || !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006826 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006828 *expr_start = NULL;
6829 *expr_end = NULL;
6830 }
6831
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006832 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006833 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006834 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006835 return arg;
6836
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006837 for (p = arg; *p != NUL
6838 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006839 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006840 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006841 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006842 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006843 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006844 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006845 if (*p == '\'')
6846 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006847 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006848 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006849 ;
6850 if (*p == NUL)
6851 break;
6852 }
6853 else if (*p == '"')
6854 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006855 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006856 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006857 if (*p == '\\' && p[1] != NUL)
6858 ++p;
6859 if (*p == NUL)
6860 break;
6861 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006862 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6863 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006864 // "s:" is start of "s:var", but "n:" is not and can be used in
6865 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006866 len = (int)(p - arg);
6867 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006868 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006869 break;
6870 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006871
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006872 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006874 if (*p == '[')
6875 ++br_nest;
6876 else if (*p == ']')
6877 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006879
zeertzjqa93d9cd2023-05-02 16:25:47 +01006880 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006882 if (*p == '{')
6883 {
6884 mb_nest++;
6885 if (expr_start != NULL && *expr_start == NULL)
6886 *expr_start = p;
6887 }
6888 else if (*p == '}')
6889 {
6890 mb_nest--;
6891 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6892 *expr_end = p;
6893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 }
6896
6897 return p;
6898}
6899
6900/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006901 * Expands out the 'magic' {}'s in a variable/function name.
6902 * Note that this can call itself recursively, to deal with
6903 * constructs like foo{bar}{baz}{bam}
6904 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6905 * "in_start" ^
6906 * "expr_start" ^
6907 * "expr_end" ^
6908 * "in_end" ^
6909 *
6910 * Returns a new allocated string, which the caller must free.
6911 * Returns NULL for failure.
6912 */
6913 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006914make_expanded_name(
6915 char_u *in_start,
6916 char_u *expr_start,
6917 char_u *expr_end,
6918 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006919{
6920 char_u c1;
6921 char_u *retval = NULL;
6922 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006923
6924 if (expr_end == NULL || in_end == NULL)
6925 return NULL;
6926 *expr_start = NUL;
6927 *expr_end = NUL;
6928 c1 = *in_end;
6929 *in_end = NUL;
6930
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006931 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006932 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006933 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006934 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6935 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006936 if (retval != NULL)
6937 {
6938 STRCPY(retval, in_start);
6939 STRCAT(retval, temp_result);
6940 STRCAT(retval, expr_end + 1);
6941 }
6942 }
6943 vim_free(temp_result);
6944
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006945 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006946 *expr_start = '{';
6947 *expr_end = '}';
6948
6949 if (retval != NULL)
6950 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006951 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006952 if (expr_start != NULL)
6953 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006954 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006955 temp_result = make_expanded_name(retval, expr_start,
6956 expr_end, temp_result);
6957 vim_free(retval);
6958 retval = temp_result;
6959 }
6960 }
6961
6962 return retval;
6963}
6964
6965/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006967 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006969 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006970eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006972 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006973}
6974
6975/*
6976 * Return TRUE if character "c" can be used as the first character in a
6977 * variable or function name (excluding '{' and '}').
6978 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006979 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006980eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006981{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006982 return ASCII_ISALPHA(c) || c == '_';
6983}
6984
6985/*
6986 * Return TRUE if character "c" can be used as the first character of a
6987 * dictionary key.
6988 */
6989 int
6990eval_isdictc(int c)
6991{
6992 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993}
6994
6995/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006996 * Handle:
6997 * - expr[expr], expr[expr:expr] subscript
6998 * - ".name" lookup
6999 * - function call with Funcref variable: func(expr)
7000 * - method call: var->method()
7001 *
7002 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007003 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007004 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007005 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007006handle_subscript(
7007 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007008 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007009 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007010 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02007011 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007012{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007013 int evaluate = evalarg != NULL
7014 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007015 int ret = OK;
7016 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007017 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007018 int getnext;
7019 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007020
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007021 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007022 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007023 // When at the end of the line and ".name" or "->{" or "->X" follows in
7024 // the next line then consume the line break.
7025 p = eval_next_non_blank(*arg, evalarg, &getnext);
7026 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00007027 && ((*p == '.'
7028 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
7029 || rettv->v_type == VAR_CLASS
7030 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02007031 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
7032 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
7033 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007034 {
Bram Moolenaare442d592022-05-05 12:20:28 +01007035 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02007036 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007037 check_white = FALSE;
7038 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007039
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007040 if (rettv->v_type == VAR_ANY)
7041 {
7042 char_u *exp_name;
7043 int cc;
7044 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01007045 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007046 type_T *type;
7047
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007048 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00007049 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007050 if (**arg != '.')
7051 {
7052 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007053 semsg(_(e_expected_dot_after_name_str),
7054 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007055 ret = FAIL;
7056 break;
7057 }
7058 ++*arg;
7059 if (IS_WHITE_OR_NUL(**arg))
7060 {
7061 if (verbose)
7062 emsg(_(e_no_white_space_allowed_after_dot));
7063 ret = FAIL;
7064 break;
7065 }
7066
7067 // isolate the name
7068 exp_name = *arg;
7069 while (eval_isnamec(**arg))
7070 ++*arg;
7071 cc = **arg;
7072 **arg = NUL;
7073
7074 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01007075 evalarg == NULL ? NULL : evalarg->eval_cctx,
7076 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007077 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007078
7079 if (idx < 0 && ufunc == NULL)
7080 {
7081 ret = FAIL;
7082 break;
7083 }
7084 if (idx >= 0)
7085 {
7086 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
7087 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
7088
7089 copy_tv(sv->sv_tv, rettv);
7090 }
7091 else
7092 {
7093 rettv->v_type = VAR_FUNC;
7094 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
7095 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007096 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007097 }
7098
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007099 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7100 || rettv->v_type == VAR_PARTIAL))
7101 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007102 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02007103 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
7104 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007105
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007106 // Stop the expression evaluation when immediately aborting on
7107 // error, or when an interrupt occurred or an exception was thrown
7108 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007109 if (aborting())
7110 {
7111 if (ret == OK)
7112 clear_tv(rettv);
7113 ret = FAIL;
7114 }
7115 dict_unref(selfdict);
7116 selfdict = NULL;
7117 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02007118 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02007119 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02007120 if (in_vim9script())
7121 *arg = skipwhite(p + 2);
7122 else
7123 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00007124 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007125 {
zeertzjqc481ad32023-03-11 16:18:51 +00007126 emsg(_(e_no_white_space_allowed_before_parenthesis));
7127 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007128 }
zeertzjqc481ad32023-03-11 16:18:51 +00007129 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
7130 // expr->{lambda}() or expr->(lambda)()
7131 ret = eval_lambda(arg, rettv, evalarg, verbose);
7132 else
7133 // expr->name()
7134 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02007135 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007136 // "." is ".name" lookup when we found a dict or when evaluating and
7137 // scriptversion is at least 2, where string concatenation is "..".
7138 else if (**arg == '['
7139 || (**arg == '.' && (rettv->v_type == VAR_DICT
7140 || (!evaluate
7141 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02007142 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007143 {
7144 dict_unref(selfdict);
7145 if (rettv->v_type == VAR_DICT)
7146 {
7147 selfdict = rettv->vval.v_dict;
7148 if (selfdict != NULL)
7149 ++selfdict->dv_refcount;
7150 }
7151 else
7152 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007153 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007154 {
7155 clear_tv(rettv);
7156 ret = FAIL;
7157 }
7158 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007159 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
7160 || rettv->v_type == VAR_OBJECT))
7161 {
7162 // class member: SomeClass.varname
7163 // class method: SomeClass.SomeMethod()
7164 // class constructor: SomeClass.new()
7165 // object member: someObject.varname
7166 // object method: someObject.SomeMethod()
7167 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
7168 {
7169 clear_tv(rettv);
7170 ret = FAIL;
7171 }
7172 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007173 else
7174 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007175 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007176
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007177 // Turn "dict.Func" into a partial for "Func" bound to "dict".
7178 // Don't do this when "Func" is already a partial that was bound
7179 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02007180 if (selfdict != NULL
7181 && (rettv->v_type == VAR_FUNC
7182 || (rettv->v_type == VAR_PARTIAL
7183 && (rettv->vval.v_partial->pt_auto
7184 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007185 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007186
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007187 dict_unref(selfdict);
7188 return ret;
7189}
7190
7191/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007192 * Make a copy of an item.
7193 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00007194 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007195 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7196 * reference to an already copied list/dict can be used.
7197 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007198 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007199 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007200item_copy(
7201 typval_T *from,
7202 typval_T *to,
7203 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00007204 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007205 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007206{
7207 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007208 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007209
Bram Moolenaar33570922005-01-25 22:26:29 +00007210 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007211 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007212 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007213 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007214 }
7215 ++recurse;
7216
7217 switch (from->v_type)
7218 {
7219 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007220 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007221 case VAR_STRING:
7222 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007223 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01007224 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007225 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007226 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007227 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007228 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007229 case VAR_CLASS:
7230 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02007231 case VAR_TYPEALIAS:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007232 copy_tv(from, to);
7233 break;
7234 case VAR_LIST:
7235 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007236 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007237 if (from->vval.v_list == NULL)
7238 to->vval.v_list = NULL;
7239 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7240 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007241 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007242 to->vval.v_list = from->vval.v_list->lv_copylist;
7243 ++to->vval.v_list->lv_refcount;
7244 }
7245 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007246 to->vval.v_list = list_copy(from->vval.v_list,
7247 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007248 if (to->vval.v_list == NULL)
7249 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007250 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007251 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007252 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007253 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007254 case VAR_DICT:
7255 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007256 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007257 if (from->vval.v_dict == NULL)
7258 to->vval.v_dict = NULL;
7259 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7260 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007261 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007262 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7263 ++to->vval.v_dict->dv_refcount;
7264 }
7265 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007266 to->vval.v_dict = dict_copy(from->vval.v_dict,
7267 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007268 if (to->vval.v_dict == NULL)
7269 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007270 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007271 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007272 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007273 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007274 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007275 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007276 }
7277 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007278 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007279}
7280
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007281 void
7282echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7283{
7284 char_u *tofree;
7285 char_u numbuf[NUMBUFLEN];
7286 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7287
7288 if (*atstart)
7289 {
7290 *atstart = FALSE;
7291 // Call msg_start() after eval1(), evaluating the expression
7292 // may cause a message to appear.
7293 if (with_space)
7294 {
7295 // Mark the saved text as finishing the line, so that what
7296 // follows is displayed on a new line when scrolling back
7297 // at the more prompt.
7298 msg_sb_eol();
7299 msg_start();
7300 }
7301 }
7302 else if (with_space)
7303 msg_puts_attr(" ", echo_attr);
7304
7305 if (p != NULL)
7306 for ( ; *p != NUL && !got_int; ++p)
7307 {
7308 if (*p == '\n' || *p == '\r' || *p == TAB)
7309 {
7310 if (*p != TAB && *needclr)
7311 {
7312 // remove any text still there from the command
7313 msg_clr_eos();
7314 *needclr = FALSE;
7315 }
7316 msg_putchar_attr(*p, echo_attr);
7317 }
7318 else
7319 {
7320 if (has_mbyte)
7321 {
7322 int i = (*mb_ptr2len)(p);
7323
7324 (void)msg_outtrans_len_attr(p, i, echo_attr);
7325 p += i - 1;
7326 }
7327 else
7328 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7329 }
7330 }
7331 vim_free(tofree);
7332}
7333
Bram Moolenaare9a41262005-01-15 22:18:47 +00007334/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 * ":echo expr1 ..." print each argument separated with a space, add a
7336 * newline at the end.
7337 * ":echon expr1 ..." print each argument plain.
7338 */
7339 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007340ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341{
7342 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007343 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007344 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 int needclr = TRUE;
7346 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007347 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007348 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007349 evalarg_T evalarg;
7350
Bram Moolenaare707c882020-07-01 13:07:37 +02007351 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352
7353 if (eap->skip)
7354 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007355 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007357 // If eval1() causes an error message the text from the command may
7358 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007359 need_clr_eos = needclr;
7360
Bram Moolenaar68db9962021-05-09 23:19:22 +02007361 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007362 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 {
7364 /*
7365 * Report the invalid expression unless the expression evaluation
7366 * has been cancelled due to an aborting error, an interrupt, or an
7367 * exception.
7368 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007369 if (!aborting() && did_emsg == did_emsg_before
7370 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007371 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007372 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 break;
7374 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007375 need_clr_eos = FALSE;
7376
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007378 {
7379 if (rettv.v_type == VAR_VOID)
7380 {
7381 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7382 break;
7383 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007384 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007385 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007386
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007387 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 arg = skipwhite(arg);
7389 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007390 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007391 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392
7393 if (eap->skip)
7394 --emsg_skip;
7395 else
7396 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007397 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 if (needclr)
7399 msg_clr_eos();
7400 if (eap->cmdidx == CMD_echo)
7401 msg_end();
7402 }
7403}
7404
7405/*
7406 * ":echohl {name}".
7407 */
7408 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007409ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007411 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412}
7413
7414/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007415 * Returns the :echo attribute
7416 */
7417 int
7418get_echo_attr(void)
7419{
7420 return echo_attr;
7421}
7422
7423/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 * ":execute expr1 ..." execute the result of an expression.
7425 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007426 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007428 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 * Each gets spaces around each argument and a newline at the end for
7430 * echo commands
7431 */
7432 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007433ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434{
7435 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007436 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 int ret = OK;
7438 char_u *p;
7439 garray_T ga;
7440 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007441 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442
7443 ga_init2(&ga, 1, 80);
7444
7445 if (eap->skip)
7446 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007447 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007449 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007450 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452
7453 if (!eap->skip)
7454 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007455 char_u buf[NUMBUFLEN];
7456
7457 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007458 {
7459 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7460 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007461 semsg(_(e_using_invalid_value_as_string_str),
7462 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007463 p = NULL;
7464 }
7465 else
7466 p = tv_get_string_buf(&rettv, buf);
7467 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007468 else
7469 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007470 if (p == NULL)
7471 {
7472 clear_tv(&rettv);
7473 ret = FAIL;
7474 break;
7475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 len = (int)STRLEN(p);
7477 if (ga_grow(&ga, len + 2) == FAIL)
7478 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007479 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 ret = FAIL;
7481 break;
7482 }
7483 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 ga.ga_len += len;
7487 }
7488
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007489 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 arg = skipwhite(arg);
7491 }
7492
7493 if (ret != FAIL && ga.ga_data != NULL)
7494 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007495 // use the first line of continuation lines for messages
7496 SOURCING_LNUM = start_lnum;
7497
Bram Moolenaar37fef162022-08-29 18:16:32 +01007498 if (eap->cmdidx == CMD_echomsg
7499 || eap->cmdidx == CMD_echowindow
7500 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007501 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007502 // Mark the already saved text as finishing the line, so that what
7503 // follows is displayed on a new line when scrolling back at the
7504 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007505 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007506 }
7507
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007508 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007509 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007510 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007511 out_flush();
7512 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007513 else if (eap->cmdidx == CMD_echowindow)
7514 {
7515#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007516 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007517#endif
7518 msg_attr(ga.ga_data, echo_attr);
7519#ifdef HAS_MESSAGE_WINDOW
7520 end_echowindow();
7521#endif
7522 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007523 else if (eap->cmdidx == CMD_echoconsole)
7524 {
7525 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7526 ui_write((char_u *)"\r\n", 2, TRUE);
7527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 else if (eap->cmdidx == CMD_echoerr)
7529 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007530 int save_did_emsg = did_emsg;
7531
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007532 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007533 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 if (!force_abort)
7535 did_emsg = save_did_emsg;
7536 }
7537 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007538 {
7539 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7540
7541 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7542 sticky_cmdmod_flags = cmdmod.cmod_flags
7543 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 do_cmdline((char_u *)ga.ga_data,
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01007545 eap->ea_getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007546 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 }
7549
7550 ga_clear(&ga);
7551
7552 if (eap->skip)
7553 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007554 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555}
7556
7557/*
7558 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7559 * "arg" points to the "&" or '+' when called, to "option" when returning.
7560 * Returns NULL when no option name found. Otherwise pointer to the char
7561 * after the option name.
7562 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007563 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007564find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565{
7566 char_u *p = *arg;
7567
7568 ++p;
7569 if (*p == 'g' && p[1] == ':')
7570 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007571 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 p += 2;
7573 }
7574 else if (*p == 'l' && p[1] == ':')
7575 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007576 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 p += 2;
7578 }
7579 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007580 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581
7582 if (!ASCII_ISALPHA(*p))
7583 return NULL;
7584 *arg = p;
7585
7586 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007587 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 else
7589 while (ASCII_ISALPHA(*p))
7590 ++p;
7591 return p;
7592}
7593
7594/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007595 * Display script name where an item was last set.
7596 * Should only be invoked when 'verbose' is non-zero.
7597 */
7598 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007599last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007600{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007601 char_u *p;
7602
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007603 if (script_ctx.sc_sid == 0)
7604 return;
7605
7606 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7607 if (p == NULL)
7608 return;
7609
7610 verbose_enter();
7611 msg_puts(_("\n\tLast set from "));
7612 msg_puts((char *)p);
7613 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007614 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007615 msg_puts(_(line_msg));
7616 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007617 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007618 verbose_leave();
7619 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007620}
7621
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007622#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623
7624/*
7625 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007626 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 * "flags" can be "g" to do a global substitute.
7628 * Returns an allocated string, NULL for error.
7629 */
7630 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007631do_string_sub(
7632 char_u *str,
7633 char_u *pat,
7634 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007635 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007636 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637{
7638 int sublen;
7639 regmatch_T regmatch;
7640 int i;
7641 int do_all;
7642 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007643 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 garray_T ga;
7645 char_u *ret;
7646 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007647 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007649 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007651 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652
7653 ga_init2(&ga, 1, 200);
7654
7655 do_all = (flags[0] == 'g');
7656
7657 regmatch.rm_ic = p_ic;
7658 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7659 if (regmatch.regprog != NULL)
7660 {
7661 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007662 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7664 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007665 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007666 if (regmatch.startp[0] == regmatch.endp[0])
7667 {
7668 if (zero_width == regmatch.startp[0])
7669 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007670 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007671 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007672 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7673 (size_t)i);
7674 ga.ga_len += i;
7675 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007676 continue;
7677 }
7678 zero_width = regmatch.startp[0];
7679 }
7680
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 /*
7682 * Get some space for a temporary buffer to do the substitution
7683 * into. It will contain:
7684 * - The text up to where the match is.
7685 * - The substituted text.
7686 * - The text after the match.
7687 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007688 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007689 if (sublen <= 0)
7690 {
7691 ga_clear(&ga);
7692 break;
7693 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007694 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7696 {
7697 ga_clear(&ga);
7698 break;
7699 }
7700
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007701 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 i = (int)(regmatch.startp[0] - tail);
7703 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007704 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007705 (void)vim_regsub(&regmatch, sub, expr,
7706 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7707 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007709 tail = regmatch.endp[0];
7710 if (*tail == NUL)
7711 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 if (!do_all)
7713 break;
7714 }
7715
7716 if (ga.ga_data != NULL)
7717 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7718
Bram Moolenaar473de612013-06-08 18:19:48 +02007719 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 }
7721
7722 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7723 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007724 if (p_cpo == empty_option)
7725 p_cpo = save_cpo;
7726 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007727 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007728 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007729 // If it's still empty it was changed and restored, need to restore in
7730 // the complicated way.
7731 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007732 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007733 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735
7736 return ret;
7737}