blob: d81ef17526ab5c4e6689a4b41064a9066c7d8b79 [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);
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +0200552 ga_clear(freegap);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200553 }
554 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200555 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200556 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200557
558 // free lines that were explicitly marked for freeing
559 ga_clear_strings(freegap);
560 }
561
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200562 gap->ga_itemsize = 0;
563 if (p == NULL)
564 return FAIL;
565 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200566 vim_free(evalarg->eval_tofree_lambda);
567 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200568 // Compute "end" relative to the end.
569 *end = *start + STRLEN(*start) - endoff;
570 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200571 }
572
573 return res;
574}
575
576/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100577 * Convert "tv" to a string.
Yegappan Lakshmananf01493c2024-04-14 23:21:02 +0200578 * When "convert" is TRUE convert a List into a sequence of lines and a Dict
579 * into a textual representation of the Dict.
Bram Moolenaar883cf972021-01-15 18:04:43 +0100580 * Returns an allocated string (NULL when out of memory).
581 */
582 char_u *
583typval2string(typval_T *tv, int convert)
584{
585 garray_T ga;
586 char_u *retval;
Bram Moolenaar883cf972021-01-15 18:04:43 +0100587
588 if (convert && tv->v_type == VAR_LIST)
589 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000590 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100591 if (tv->vval.v_list != NULL)
592 {
593 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
594 if (tv->vval.v_list->lv_len > 0)
595 ga_append(&ga, NL);
596 }
597 ga_append(&ga, NUL);
598 retval = (char_u *)ga.ga_data;
599 }
Yegappan Lakshmananf01493c2024-04-14 23:21:02 +0200600 else if (convert && tv->v_type == VAR_DICT)
601 retval = dict2string(tv, get_copyID(), FALSE);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100602 else
603 retval = vim_strsave(tv_get_string(tv));
604 return retval;
605}
606
607/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200608 * Top level evaluation function, returning a string. Does not handle line
609 * breaks.
zeertzjq19dfa272023-06-15 10:56:41 +0100610 * When "convert" is TRUE convert a List into a sequence of lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 * Return pointer to allocated memory, or NULL for failure.
612 */
613 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100614eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100615 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100616 int convert,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100617 exarg_T *eap,
618 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619{
Bram Moolenaar33570922005-01-25 22:26:29 +0000620 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100622 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100623 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100625 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100626 if (use_simple_function)
627 r = eval0_simple_funccal(arg, &tv, NULL, &evalarg);
628 else
629 r = eval0(arg, &tv, NULL, &evalarg);
630 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 retval = NULL;
632 else
633 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100634 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000635 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100637 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638
639 return retval;
640}
641
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100642 char_u *
643eval_to_string(
644 char_u *arg,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100645 int convert,
646 int use_simple_function)
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100647{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100648 return eval_to_string_eap(arg, convert, NULL, use_simple_function);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100649}
650
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000652 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100653 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200654 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 */
656 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100657eval_to_string_safe(
658 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000659 int use_sandbox,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100660 int keep_script_version,
661 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662{
663 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200664 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200665 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200666 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
Bram Moolenaar9530b582022-01-22 13:39:08 +0000668 if (!keep_script_version)
669 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200670 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000671 if (use_sandbox)
672 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100673 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200674 may_garbage_collect = FALSE;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100675 retval = eval_to_string(arg, FALSE, use_simple_function);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000676 if (use_sandbox)
677 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100678 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200679 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200680 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200681 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 return retval;
683}
684
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685/*
686 * Top level evaluation function, returning a number.
687 * Evaluates "expr" silently.
688 * Returns -1 for an error.
689 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200690 varnumber_T
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100691eval_to_number(char_u *expr, int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692{
Bram Moolenaar33570922005-01-25 22:26:29 +0000693 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200694 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000695 char_u *p = skipwhite(expr);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100696 int r = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697
698 ++emsg_off;
699
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100700 if (use_simple_function)
701 r = may_call_simple_func(expr, &rettv);
702 if (r == NOTDONE)
703 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
704 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 retval = -1;
706 else
707 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100708 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000709 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 }
711 --emsg_off;
712
713 return retval;
714}
715
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000716/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000717 * Top level evaluation function.
718 * Returns an allocated typval_T with the result.
719 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000720 */
721 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200722eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000723{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100724 return eval_expr_ext(arg, eap, FALSE);
725}
726
727 typval_T *
728eval_expr_ext(char_u *arg, exarg_T *eap, int use_simple_function)
729{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000730 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200731 evalarg_T evalarg;
732
733 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000734
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200735 tv = ALLOC_ONE(typval_T);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100736 if (tv != NULL)
737 {
738 int r = NOTDONE;
739
740 if (use_simple_function)
741 r = eval0_simple_funccal(arg, tv, eap, &evalarg);
742 if (r == NOTDONE)
743 r = eval0(arg, tv, eap, &evalarg);
744
745 if (r == FAIL)
746 VIM_CLEAR(tv);
747 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000748
Bram Moolenaar37c83712020-06-30 21:18:36 +0200749 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000750 return tv;
751}
752
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000754 * "*arg" points to what can be a function name in the form of "import.Name" or
755 * "Funcref". Return the name of the function. Set "tofree" to something that
756 * was allocated.
757 * If "verbose" is FALSE no errors are given.
758 * Return NULL for any failure.
759 */
760 static char_u *
761deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000762 char_u **arg,
763 char_u **tofree,
764 evalarg_T *evalarg,
765 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000766{
767 typval_T ref;
768 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100769 int save_flags = 0;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000770
771 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100772 if (evalarg != NULL)
773 {
774 // need to evaluate this to get an import, like in "a.Func"
775 save_flags = evalarg->eval_flags;
776 evalarg->eval_flags |= EVAL_EVALUATE;
777 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100778 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100779 {
780 dictitem_T *v;
781
782 // If <SID>VarName was used it would not be found, try another way.
783 v = find_var_also_in_script(name, NULL, FALSE);
784 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100785 {
786 name = NULL;
787 goto theend;
788 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100789 copy_tv(&v->di_tv, &ref);
790 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000791 if (*skipwhite(*arg) != NUL)
792 {
793 if (verbose)
794 semsg(_(e_trailing_characters_str), *arg);
795 name = NULL;
796 }
797 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
798 {
799 name = ref.vval.v_string;
800 ref.vval.v_string = NULL;
801 *tofree = name;
802 }
803 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
804 {
805 if (ref.vval.v_partial->pt_argc > 0
806 || ref.vval.v_partial->pt_dict != NULL)
807 {
808 if (verbose)
809 emsg(_(e_cannot_use_partial_here));
810 name = NULL;
811 }
812 else
813 {
814 name = vim_strsave(partial_name(ref.vval.v_partial));
815 *tofree = name;
816 }
817 }
818 else
819 {
820 if (verbose)
821 semsg(_(e_not_callable_type_str), name);
822 name = NULL;
823 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100824
825theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000826 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100827 if (evalarg != NULL)
828 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000829 return name;
830}
831
832/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100833 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200834 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
835 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000836 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200838 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100839call_vim_function(
840 char_u *func,
841 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200842 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200843 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000845 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200846 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000847 char_u *arg;
848 char_u *name;
849 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000850 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100852 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200853 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000854 funcexe.fe_firstline = curwin->w_cursor.lnum;
855 funcexe.fe_lastline = curwin->w_cursor.lnum;
856 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000857
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000858 // The name might be "import.Func" or "Funcref". We don't know, we need to
859 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000860 // autoload script has errors. Guess that when there is a dot in the name
861 // showing errors is the right choice.
862 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000863 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000864 if (ignore_errors)
865 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000866 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000867 if (ignore_errors)
868 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000869 if (name == NULL)
870 name = func;
871
872 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
873
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000874 if (ret == FAIL)
875 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000876 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000877
878 return ret;
879}
880
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100881/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100882 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000883 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
884 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000885 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000886 */
887 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100888call_func_retstr(
889 char_u *func,
890 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200891 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000892{
893 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000894 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000895
Bram Moolenaarded27a12018-08-01 19:06:03 +0200896 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000897 return NULL;
898
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100899 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000900 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 return retval;
902}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000903
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000904/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100905 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000906 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000907 * Returns NULL when there is something wrong.
Bram Moolenaar55e93662022-09-10 13:52:26 +0100908 * Gives an error when the returned value is not a list.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000909 */
910 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100911call_func_retlist(
912 char_u *func,
913 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200914 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000915{
916 typval_T rettv;
917
Bram Moolenaarded27a12018-08-01 19:06:03 +0200918 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000919 return NULL;
920
921 if (rettv.v_type != VAR_LIST)
922 {
Bram Moolenaar55e93662022-09-10 13:52:26 +0100923 semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
924 vartype_name(rettv.v_type));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000925 clear_tv(&rettv);
926 return NULL;
927 }
928
929 return rettv.vval.v_list;
930}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931
Bram Moolenaare70dd112022-01-21 16:31:11 +0000932#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100934 * Evaluate "arg", which is 'foldexpr'.
935 * Note: caller must set "curwin" to match "arg".
936 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
937 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 */
939 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000940eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000942 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000943 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200944 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000946 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000947 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100948 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100950 arg = skipwhite(wp->w_p_fde);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000951 current_sctx = wp->w_p_script_ctx[WV_FDE];
952
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000954 if (use_sandbox)
955 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100956 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 *cp = NUL;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100958
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100959 // Evaluate the expression. If the expression is "FuncName()" call the
960 // function directly.
961 if (eval0_simple_funccal(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 retval = 0;
963 else
964 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100965 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000966 if (tv.v_type == VAR_NUMBER)
967 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000968 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 retval = 0;
970 else
971 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100972 // If the result is a string, check if there is a non-digit before
973 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000974 s = tv.vval.v_string;
zeertzjqa991ce92023-10-06 19:16:36 +0200975 if (*s != NUL && !VIM_ISDIGIT(*s) && *s != '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 *cp = *s++;
977 retval = atol((char *)s);
978 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000979 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 }
981 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000982 if (use_sandbox)
983 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100984 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200985 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000986 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200988 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989}
990#endif
991
Ernie Rael64885642023-10-04 20:16:22 +0200992#ifdef LOG_LOCKVAR
993typedef struct flag_string_S
994{
995 int flag;
996 char *str;
997} flag_string_T;
998
999 static char *
1000flags_tostring(int flags, flag_string_T *_fstring, char *buf, size_t n)
1001{
1002 char *p = buf;
1003 *p = NUL;
1004 for (flag_string_T *fstring = _fstring; fstring->flag; ++fstring)
1005 {
1006 if ((fstring->flag & flags) != 0)
1007 {
1008 size_t len = STRLEN(fstring->str);
1009 if (n > p - buf + len + 7)
1010 {
1011 STRCAT(p, fstring->str);
1012 p += len;
1013 STRCAT(p, " ");
1014 ++p;
1015 }
1016 else
1017 {
1018 STRCAT(buf, "...");
1019 break;
1020 }
1021 }
1022 }
1023 return buf;
1024}
1025
1026flag_string_T glv_flag_strings[] = {
1027 { GLV_QUIET, "QUIET" },
1028 { GLV_NO_AUTOLOAD, "NO_AUTOLOAD" },
1029 { GLV_READ_ONLY, "READ_ONLY" },
1030 { GLV_NO_DECL, "NO_DECL" },
1031 { GLV_COMPILING, "COMPILING" },
1032 { GLV_ASSIGN_WITH_OP, "ASSIGN_WITH_OP" },
1033 { GLV_PREFER_FUNC, "PREFER_FUNC" },
1034 { 0, NULL }
1035};
1036#endif
1037
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038/*
Ernie Raelee865f32023-09-29 19:53:55 +02001039 * Fill in "lp" using "root". This is used in a special case when
1040 * "get_lval()" parses a bare word when "lval_root" is not NULL.
1041 *
1042 * This is typically called with "lval_root" as "root". For a class, find
1043 * the name from lp in the class from root, fill in lval_T if found. For a
1044 * complex type, list/dict use it as the result; just put the root into
1045 * ll_tv.
1046 *
1047 * "lval_root" is a hack used during run-time/instr-execution to provide the
1048 * starting point for "get_lval()" to traverse a chain of indexes. In some
1049 * cases get_lval sees a bare name and uses this function to populate the
1050 * lval_T.
1051 *
1052 * For setting up "lval_root" (currently only used with lockvar)
1053 * compile_lock_unlock - pushes object on stack (which becomes lval_root)
1054 * execute_instructions: ISN_LOCKUNLOCK - sets lval_root from stack.
1055 */
1056 static void
Ernie Rael4c8da022023-10-11 21:35:11 +02001057fill_lval_from_lval_root(lval_T *lp, lval_root_T *lr)
Ernie Raelee865f32023-09-29 19:53:55 +02001058{
1059#ifdef LOG_LOCKVAR
Ernie Rael4c8da022023-10-11 21:35:11 +02001060 ch_log(NULL, "LKVAR: fill_lval_from_lval_root(): name %s, tv %p",
1061 lp->ll_name, (void*)lr->lr_tv);
Ernie Raelee865f32023-09-29 19:53:55 +02001062#endif
Ernie Rael4c8da022023-10-11 21:35:11 +02001063 if (lr->lr_tv == NULL)
1064 return;
Ernie Rael64885642023-10-04 20:16:22 +02001065 if (!lr->lr_is_arg && lr->lr_tv->v_type == VAR_CLASS)
Ernie Raelee865f32023-09-29 19:53:55 +02001066 {
Ernie Rael64885642023-10-04 20:16:22 +02001067 if (lr->lr_tv->vval.v_class != NULL)
Ernie Raelee865f32023-09-29 19:53:55 +02001068 {
1069 // Special special case. Look for a bare class variable reference.
Ernie Rael64885642023-10-04 20:16:22 +02001070 class_T *cl = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001071 int m_idx;
1072 ocmember_T *m = class_member_lookup(cl, lp->ll_name,
1073 lp->ll_name_end - lp->ll_name, &m_idx);
1074 if (m != NULL)
1075 {
1076 // Assuming "inside class" since bare reference.
Ernie Rael64885642023-10-04 20:16:22 +02001077 lp->ll_class = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001078 lp->ll_oi = m_idx;
1079 lp->ll_valtype = m->ocm_type;
1080 lp->ll_tv = &lp->ll_class->class_members_tv[m_idx];
1081#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001082 ch_log(NULL, "LKVAR: ... class member %s.%s",
1083 lp->ll_class->class_name, lp->ll_name);
Ernie Raelee865f32023-09-29 19:53:55 +02001084#endif
1085 return;
1086 }
1087 }
1088 }
1089
1090#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001091 ch_log(NULL, "LKVAR: ... type: %s", vartype_name(lr->lr_tv->v_type));
Ernie Raelee865f32023-09-29 19:53:55 +02001092#endif
Ernie Rael64885642023-10-04 20:16:22 +02001093 lp->ll_tv = lr->lr_tv;
Ernie Raelee865f32023-09-29 19:53:55 +02001094 lp->ll_is_root = TRUE;
1095}
1096
1097/*
Ernie Rael64885642023-10-04 20:16:22 +02001098 * Check if the class has permission to access the member.
1099 * Returns OK or FAIL.
1100 */
1101 static int
1102get_lval_check_access(
1103 class_T *cl_exec, // executing class, NULL if :def or script level
1104 class_T *cl, // class which contains the member
1105 ocmember_T *om, // member being accessed
1106 char_u *p, // char after member name
1107 int flags) // GLV flags to check if writing to lval
1108{
1109#ifdef LOG_LOCKVAR
1110 ch_log(NULL, "LKVAR: get_lval_check_access(), cl_exec %p, cl %p, %c",
1111 (void*)cl_exec, (void*)cl, *p);
1112#endif
1113 if (cl_exec == NULL || cl_exec != cl)
1114 {
Ernie Raele6c9aa52023-10-06 19:55:52 +02001115 char *msg = NULL;
Ernie Rael64885642023-10-04 20:16:22 +02001116 switch (om->ocm_access)
1117 {
1118 case VIM_ACCESS_PRIVATE:
Ernie Rael03042a22023-11-11 08:53:32 +01001119 msg = e_cannot_access_protected_variable_str;
Ernie Raele6c9aa52023-10-06 19:55:52 +02001120 break;
Ernie Rael64885642023-10-04 20:16:22 +02001121 case VIM_ACCESS_READ:
1122 // If [idx] or .key following, read only OK.
1123 if (*p == '[' || *p == '.')
1124 break;
1125 if ((flags & GLV_READ_ONLY) == 0)
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001126 {
1127 if (IS_ENUM(cl))
1128 {
1129 if (om->ocm_type->tt_type == VAR_OBJECT)
1130 semsg(_(e_enumvalue_str_cannot_be_modified),
1131 cl->class_name, om->ocm_name);
1132 else
1133 msg = e_variable_is_not_writable_str;
1134 }
1135 else
1136 msg = e_variable_is_not_writable_str;
1137 }
Ernie Rael64885642023-10-04 20:16:22 +02001138 break;
1139 case VIM_ACCESS_ALL:
1140 break;
1141 }
Ernie Raele6c9aa52023-10-06 19:55:52 +02001142 if (msg != NULL)
1143 {
1144 emsg_var_cl_define(msg, om->ocm_name, 0, cl);
1145 return FAIL;
1146 }
1147
Ernie Rael64885642023-10-04 20:16:22 +02001148 }
1149 return OK;
1150}
1151
1152/*
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001153 * Get lval information for a variable imported from script "imp_sid". On
1154 * success, updates "lp" with the variable name, type, script ID and typval.
1155 * The variable name starts at or after "p".
1156 * If "rettv" is not NULL it points to the value to be assigned. This used to
1157 * match the rhs and lhs types.
1158 * Returns a pointer to the character after the variable name if the imported
1159 * variable is valid and writable.
1160 * Returns NULL if the variable is not exported or typval is not found or the
1161 * rhs type doesn't match the lhs type or the variable is not writable.
1162 */
1163 static char_u *
1164get_lval_imported(
1165 lval_T *lp,
1166 typval_T *rettv,
1167 scid_T imp_sid,
1168 char_u *p,
1169 dictitem_T **dip,
1170 int fne_flags,
1171 int vim9script)
1172{
1173 ufunc_T *ufunc;
1174 type_T *type = NULL;
1175 int cc;
1176 int rc = FAIL;
1177
1178 p = skipwhite(p);
1179
1180 import_check_sourced_sid(&imp_sid);
1181 lp->ll_sid = imp_sid;
1182 lp->ll_name = p;
1183 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1184 lp->ll_name_end = p;
1185
1186 // check the item is exported
1187 cc = *p;
1188 *p = NUL;
1189 if (find_exported(imp_sid, lp->ll_name, &ufunc, &type, NULL, NULL,
1190 TRUE) == -1)
1191 goto failed;
1192
1193 if (vim9script && type != NULL)
1194 {
1195 where_T where = WHERE_INIT;
1196
1197 // In a vim9 script, do type check and make sure the variable is
1198 // writable.
1199 if (check_typval_type(type, rettv, where) == FAIL)
1200 goto failed;
1201 }
1202
1203 // Get the typval for the exported item
1204 hashtab_T *ht = &SCRIPT_VARS(imp_sid);
1205 if (ht == NULL)
1206 goto failed;
1207
1208 dictitem_T *di = find_var_in_ht(ht, 0, lp->ll_name, TRUE);
1209 if (di == NULL)
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001210 // script is autoloaded. So variable will be found later
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001211 goto success;
1212
1213 *dip = di;
1214
1215 // Check whether the variable is writable.
1216 svar_T *sv = find_typval_in_script(&di->di_tv, imp_sid, FALSE);
1217 if (sv != NULL && sv->sv_const != 0)
1218 {
1219 semsg(_(e_cannot_change_readonly_variable_str), lp->ll_name);
1220 goto failed;
1221 }
1222
1223 // check whether variable is locked
1224 if (value_check_lock(di->di_tv.v_lock, lp->ll_name, FALSE))
1225 goto failed;
1226
1227 lp->ll_tv = &di->di_tv;
1228
1229success:
1230 rc = OK;
1231
1232failed:
1233 *p = cc;
1234 return rc == OK ? p : NULL;
1235}
1236
1237/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001238 * Get an lval: variable, Dict item or List item that can be assigned a value
1239 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1240 * "name.key", "name.key[expr]" etc.
1241 * Indexing only works if "name" is an existing List or Dictionary.
1242 * "name" points to the start of the name.
1243 * If "rettv" is not NULL it points to the value to be assigned.
1244 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1245 * wrong; must end in space or cmd separator.
1246 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001247 * flags:
1248 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001249 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001250 * GLV_NO_AUTOLOAD: do not use script autoloading
1251 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001252 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001253 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001254 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001255 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001256 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001257get_lval(
1258 char_u *name,
1259 typval_T *rettv,
1260 lval_T *lp,
1261 int unlet,
1262 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001263 int flags, // GLV_ values
1264 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001265{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001266 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001267 char_u *expr_start, *expr_end;
1268 int cc;
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001269 dictitem_T *v = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001270 typval_T var1;
1271 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001272 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001273 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001274 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01001275 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001276 int quiet = flags & GLV_QUIET;
Ernie Rael86eda072023-09-05 07:33:48 +02001277 int writing = 0;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001278 int vim9script = in_vim9script();
Ernie Rael64885642023-10-04 20:16:22 +02001279 class_T *cl_exec = NULL; // class that is executing, or NULL.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001280
Ernie Raelee865f32023-09-29 19:53:55 +02001281#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001282 if (lval_root == NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001283 ch_log(NULL, "LKVAR: get_lval(): name: %s, lval_root (nil)", name);
Ernie Rael64885642023-10-04 20:16:22 +02001284 else
Ernie Rael4c8da022023-10-11 21:35:11 +02001285 ch_log(NULL, "LKVAR: get_lval(): name: %s, lr_tv %p lr_is_arg %d",
1286 name, (void*)lval_root->lr_tv, lval_root->lr_is_arg);
Ernie Rael64885642023-10-04 20:16:22 +02001287 char buf[80];
Ernie Rael4c8da022023-10-11 21:35:11 +02001288 ch_log(NULL, "LKVAR: ...: GLV flags: %s",
Ernie Rael64885642023-10-04 20:16:22 +02001289 flags_tostring(flags, glv_flag_strings, buf, sizeof(buf)));
Ernie Raelee865f32023-09-29 19:53:55 +02001290#endif
1291
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001292 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02001293 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001294
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001295 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001296 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001297 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001298 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02001299 lp->ll_name_end = find_name_end(name, NULL, NULL,
1300 FNE_INCL_BR | fne_flags);
1301 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001302 }
1303
Bram Moolenaara749a422022-02-12 19:52:25 +00001304 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001305 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00001306 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
1307 {
1308 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
1309 return NULL;
1310 }
1311
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001312 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001313 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001314 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001315 if (expr_start != NULL)
1316 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001317 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01001318 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001319 && *p != '[' && *p != '.')
1320 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001321 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001322 return NULL;
1323 }
1324
1325 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1326 if (lp->ll_exp_name == NULL)
1327 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001328 // Report an invalid expression in braces, unless the
1329 // expression evaluation has been cancelled due to an
1330 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001331 if (!aborting() && !quiet)
1332 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001333 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001334 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001335 return NULL;
1336 }
1337 }
1338 lp->ll_name = lp->ll_exp_name;
1339 }
1340 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001341 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001342 lp->ll_name = name;
1343
Bram Moolenaar4525a572022-02-13 11:57:33 +00001344 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001346 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001347 // However, "g:[key]" is indexing a dictionary.
1348 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001349 {
1350 --p;
1351 lp->ll_name_end = p;
1352 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001353 if (*skipwhite(p) == ':')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001354 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001355 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001356
Bram Moolenaar9510d222022-09-11 15:14:05 +01001357 if (is_scoped_variable(name))
1358 {
1359 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1360 return NULL;
1361 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001362 if (VIM_ISWHITE(*p))
1363 {
1364 semsg(_(e_no_white_space_allowed_before_colon_str), p);
1365 return NULL;
1366 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001367 if (tp == p + 1 && !quiet)
1368 {
1369 semsg(_(e_white_space_required_after_str_str), ":", p);
1370 return NULL;
1371 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001372 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001373 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001374 semsg(_(e_using_type_not_in_script_context_str), p);
1375 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001376 }
Yegappan Lakshmananb5a07192023-10-05 20:14:43 +02001377 if (vim9script && (flags & GLV_NO_DECL) &&
1378 !(flags & GLV_FOR_LOOP))
1379 {
1380 // Using a type and not in a "var" declaration.
1381 semsg(_(e_trailing_characters_str), p);
1382 return NULL;
1383 }
1384
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001385
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001386 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001387 lp->ll_type = parse_type(&tp,
1388 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1389 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001390 if (lp->ll_type == NULL && !quiet)
1391 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001392 lp->ll_name_end = tp;
1393 }
Ernie Raelee865f32023-09-29 19:53:55 +02001394 // TODO: check inside class?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395 }
1396 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001397 if (lp->ll_name == NULL)
1398 return p;
1399
Bram Moolenaar62aec932022-01-29 21:45:34 +00001400 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001401 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001402 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001403 if (import != NULL)
1404 {
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001405 p++; // skip '.'
1406 p = get_lval_imported(lp, rettv, import->imp_sid, p, &v,
1407 fne_flags, vim9script);
1408 if (p == NULL)
Bram Moolenaar5d982692022-01-12 15:15:27 +00001409 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001410 }
1411 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412
Ernie Rael0f058d12023-10-14 11:25:04 +02001413 // Without [idx] or .key we are done.
1414 if (*p != '[' && *p != '.')
Ernie Raelee865f32023-09-29 19:53:55 +02001415 {
1416 if (lval_root != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001417 fill_lval_from_lval_root(lp, lval_root);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001418 return p;
Ernie Raelee865f32023-09-29 19:53:55 +02001419 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001420
Ernie Rael0f058d12023-10-14 11:25:04 +02001421 if (vim9script && lval_root != NULL)
1422 cl_exec = lval_root->lr_cl_exec;
Ernie Rael4c8da022023-10-11 21:35:11 +02001423 if (vim9script && lval_root != NULL && lval_root->lr_tv != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001424 {
1425 // using local variable
Ernie Rael64885642023-10-04 20:16:22 +02001426 lp->ll_tv = lval_root->lr_tv;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001427 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001428 }
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001429 else if (lp->ll_tv == NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001430 {
1431 cc = *p;
1432 *p = NUL;
1433 // When we would write to the variable pass &ht and prevent autoload.
1434 writing = !(flags & GLV_READ_ONLY);
1435 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001436 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001437 if (v == NULL && !quiet)
1438 semsg(_(e_undefined_variable_str), lp->ll_name);
1439 *p = cc;
1440 if (v == NULL)
1441 return NULL;
1442 lp->ll_tv = &v->di_tv;
1443 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001444
Bram Moolenaar4525a572022-02-13 11:57:33 +00001445 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001446 {
1447 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001448 semsg(_(e_variable_already_declared_str), lp->ll_name);
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001449 return NULL;
1450 }
1451
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001452 /*
1453 * Loop until no more [idx] or .key is following.
1454 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001455 var1.v_type = VAR_UNKNOWN;
1456 var2.v_type = VAR_UNKNOWN;
Ernie Rael0f058d12023-10-14 11:25:04 +02001457 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001458 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001459 vartype_T v_type = lp->ll_tv->v_type;
1460
1461 if (*p == '.' && v_type != VAR_DICT
1462 && v_type != VAR_OBJECT
1463 && v_type != VAR_CLASS)
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001464 {
1465 if (!quiet)
Ernie Raelf8da3242023-10-11 21:22:12 +02001466 semsg(_(e_dot_not_allowed_after_str_str),
1467 vartype_name(v_type), name);
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001468 return NULL;
1469 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001470 if (v_type != VAR_LIST
1471 && v_type != VAR_DICT
1472 && v_type != VAR_BLOB
1473 && v_type != VAR_OBJECT
1474 && v_type != VAR_CLASS)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001475 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001476 if (!quiet)
Ernie Raelf8da3242023-10-11 21:22:12 +02001477 semsg(_(e_index_not_allowed_after_str_str),
1478 vartype_name(v_type), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001479 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001480 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001481
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001482 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001483 int r = OK;
Bram Moolenaard505d172022-12-18 21:42:55 +00001484 if (v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001485 r = rettv_list_alloc(lp->ll_tv);
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +02001486 else if (v_type == VAR_BLOB && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001487 r = rettv_blob_alloc(lp->ll_tv);
1488 if (r == FAIL)
1489 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001490
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001491 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001492 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001493 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001494 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001495 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001496 }
Ernie Rael4c8da022023-10-11 21:35:11 +02001497#ifdef LOG_LOCKVAR
1498 ch_log(NULL, "LKVAR: get_lval() loop: p: %s, type: %s", p,
1499 vartype_name(v_type));
1500#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001501
Bram Moolenaar4525a572022-02-13 11:57:33 +00001502 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001503 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001504 && lp->ll_tv == &v->di_tv
1505 && ht != NULL && ht == get_script_local_ht())
1506 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001507 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001508
1509 // Vim9 script local variable: get the type
1510 if (sv != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001511 {
Bram Moolenaar10c65862020-10-08 21:16:42 +02001512 lp->ll_valtype = sv->sv_type;
Ernie Rael4c8da022023-10-11 21:35:11 +02001513#ifdef LOG_LOCKVAR
1514 ch_log(NULL, "LKVAR: ... loop: vim9 assign type: %s",
1515 vartype_name(lp->ll_valtype->tt_type));
1516#endif
1517 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001518 }
1519
Bram Moolenaar8c711452005-01-14 21:53:12 +00001520 len = -1;
Ernie Rael0f058d12023-10-14 11:25:04 +02001521 if (*p == '.')
Bram Moolenaar8c711452005-01-14 21:53:12 +00001522 {
1523 key = p + 1;
1524 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1525 ;
1526 if (len == 0)
1527 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001528 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001529 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001530 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001531 }
1532 p = key + len;
1533 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001534 else
1535 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001536 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001537 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001538 if (*p == ':')
1539 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001540 else
1541 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001542 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001543 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001544 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001545 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001546 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001547 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001548 clear_tv(&var1);
1549 return NULL;
1550 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001551 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001552 }
1553
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001554 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001555 if (*p == ':')
1556 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001557 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001558 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001559 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001560 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001561 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001562 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001563 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001564 if (rettv != NULL
1565 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001566 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001567 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001568 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001569 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001570 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001571 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001572 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001573 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001574 }
1575 p = skipwhite(p + 1);
1576 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001577 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001578 else
1579 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001580 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001581 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001582 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001583 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001584 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001585 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001586 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001587 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001588 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001589 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001590 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001591 clear_tv(&var2);
1592 return NULL;
1593 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001594 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001595 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001596 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001597 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001598 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001599
Bram Moolenaar8c711452005-01-14 21:53:12 +00001600 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001601 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001602 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001603 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001604 clear_tv(&var1);
1605 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001606 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001607 }
1608
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001609 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001610 ++p;
1611 }
Ernie Rael4c8da022023-10-11 21:35:11 +02001612#ifdef LOG_LOCKVAR
Ernie Rael0f058d12023-10-14 11:25:04 +02001613 if (len == -1)
Ernie Rael4c8da022023-10-11 21:35:11 +02001614 ch_log(NULL, "LKVAR: ... loop: p: %s, '[' key: %s", p,
1615 empty1 ? ":" : (char*)tv_get_string(&var1));
1616 else
1617 ch_log(NULL, "LKVAR: ... loop: p: %s, '.' key: %s", p, key);
Ernie Rael4c8da022023-10-11 21:35:11 +02001618#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00001619
Bram Moolenaard505d172022-12-18 21:42:55 +00001620 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001621 {
1622 if (len == -1)
1623 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001624 // "[key]": get key from "var1"
1625 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001626 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001627 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001628 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001629 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001630 }
1631 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001632 lp->ll_list = NULL;
Ernie Raelee865f32023-09-29 19:53:55 +02001633 lp->ll_object = NULL;
1634 lp->ll_class = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001635
1636 // a NULL dict is equivalent with an empty dict
1637 if (lp->ll_tv->vval.v_dict == NULL)
1638 {
1639 lp->ll_tv->vval.v_dict = dict_alloc();
1640 if (lp->ll_tv->vval.v_dict == NULL)
1641 {
1642 clear_tv(&var1);
1643 return NULL;
1644 }
1645 ++lp->ll_tv->vval.v_dict->dv_refcount;
1646 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001647 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001648
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001649 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001650
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001651 // When assigning to a scope dictionary check that a function and
1652 // variable name is valid (only variable name unless it is l: or
1653 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001654 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001655 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001656 int prevval;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001657
1658 if (len != -1)
1659 {
1660 prevval = key[len];
1661 key[len] = NUL;
1662 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001663 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001664 prevval = 0; // avoid compiler warning
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001665 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
zeertzjq91c75d12022-11-05 20:21:58 +00001666 && (rettv->v_type == VAR_FUNC
1667 || rettv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001668 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001669 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001670 if (len != -1)
1671 key[len] = prevval;
1672 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001673 {
1674 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001675 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001676 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001677 }
1678
Bram Moolenaar10c65862020-10-08 21:16:42 +02001679 if (lp->ll_valtype != NULL)
1680 // use the type of the member
1681 lp->ll_valtype = lp->ll_valtype->tt_member;
1682
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001683 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001684 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001685 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001686 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001687 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001688 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001689 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001690 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001691 return NULL;
1692 }
1693
Bram Moolenaar31b81602019-02-10 22:14:27 +01001694 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001695 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001696 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001697 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001698 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001699 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001700 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001701 }
1702 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001703 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001704 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001705 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001706 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001707 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001708 p = NULL;
1709 break;
1710 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001711 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001712 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001713 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1714 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001715 {
1716 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001717 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001718 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001719
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001720 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001721 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001722 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001723 else if (v_type == VAR_BLOB)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001724 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001725 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1726
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001727 /*
1728 * Get the number and item for the only or first index of the List.
1729 */
1730 if (empty1)
1731 lp->ll_n1 = 0;
1732 else
1733 // is number or string
1734 lp->ll_n1 = (long)tv_get_number(&var1);
1735 clear_tv(&var1);
1736
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001737 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001738 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001739 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001740 return NULL;
1741 }
1742 if (lp->ll_range && !lp->ll_empty2)
1743 {
1744 lp->ll_n2 = (long)tv_get_number(&var2);
1745 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001746 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1747 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001748 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001749 }
1750 lp->ll_blob = lp->ll_tv->vval.v_blob;
1751 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001752 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001753 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001754 else if (v_type == VAR_LIST)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001755 {
1756 /*
1757 * Get the number and item for the only or first index of the List.
1758 */
1759 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001760 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001761 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001762 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001763 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001764 clear_tv(&var1);
1765
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001766 lp->ll_dict = NULL;
Ernie Raelee865f32023-09-29 19:53:55 +02001767 lp->ll_object = NULL;
1768 lp->ll_class = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001769 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001770 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1771 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001772 if (lp->ll_li == NULL)
1773 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001774 clear_tv(&var2);
1775 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001776 }
1777
Bram Moolenaar10c65862020-10-08 21:16:42 +02001778 if (lp->ll_valtype != NULL)
1779 // use the type of the member
1780 lp->ll_valtype = lp->ll_valtype->tt_member;
1781
Bram Moolenaar8c711452005-01-14 21:53:12 +00001782 /*
1783 * May need to find the item or absolute index for the second
1784 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001785 * When no index given: "lp->ll_empty2" is TRUE.
1786 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001787 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001788 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001789 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001790 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001791 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001792 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001793 if (check_range_index_two(lp->ll_list,
1794 &lp->ll_n1, lp->ll_li,
1795 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001796 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001797 }
1798
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001799 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001800 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001801 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001802 {
Ernie Raelee865f32023-09-29 19:53:55 +02001803 lp->ll_dict = NULL;
1804 lp->ll_list = NULL;
1805
1806 class_T *cl;
Ernie Rael4c8da022023-10-11 21:35:11 +02001807 if (v_type == VAR_OBJECT)
Ernie Raelee865f32023-09-29 19:53:55 +02001808 {
Ernie Rael4c8da022023-10-11 21:35:11 +02001809 if (lp->ll_tv->vval.v_object == NULL)
1810 {
1811 if (!quiet)
1812 emsg(_(e_using_null_object));
1813 return NULL;
1814 }
Ernie Raelee865f32023-09-29 19:53:55 +02001815 cl = lp->ll_tv->vval.v_object->obj_class;
1816 lp->ll_object = lp->ll_tv->vval.v_object;
1817 }
1818 else
1819 {
1820 cl = lp->ll_tv->vval.v_class;
1821 lp->ll_object = NULL;
1822 }
1823 lp->ll_class = cl;
1824
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001825 // TODO: what if class is NULL?
1826 if (cl != NULL)
1827 {
1828 lp->ll_valtype = NULL;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001829
1830 if (flags & GLV_PREFER_FUNC)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001831 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001832 // First look for a function with this name.
1833 // round 1: class functions (skipped for an object)
1834 // round 2: object methods
1835 for (int round = v_type == VAR_OBJECT ? 2 : 1;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001836 round <= 2; ++round)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001837 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001838 int m_idx;
1839 ufunc_T *fp;
1840
1841 fp = method_lookup(cl,
1842 round == 1 ? VAR_CLASS : VAR_OBJECT,
1843 key, p - key, &m_idx);
Ernie Raelee865f32023-09-29 19:53:55 +02001844 lp->ll_oi = m_idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001845 if (fp != NULL)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001846 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001847 lp->ll_ufunc = fp;
1848 lp->ll_valtype = fp->uf_func_type;
1849 break;
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001850 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001851 }
1852 }
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001853
1854 if (lp->ll_valtype == NULL)
1855 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001856 int m_idx;
Ernie Raelee865f32023-09-29 19:53:55 +02001857 ocmember_T *om
1858 = member_lookup(cl, v_type, key, p - key, &m_idx);
1859 lp->ll_oi = m_idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001860 if (om != NULL)
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001861 {
Ernie Rael64885642023-10-04 20:16:22 +02001862 if (get_lval_check_access(cl_exec, cl, om,
1863 p, flags) == FAIL)
1864 return NULL;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001865
Yegappan Lakshmanane5437c52023-12-16 14:11:19 +01001866 // When lhs is used to modify the variable, check it is
1867 // not a read-only variable.
1868 if ((flags & GLV_READ_ONLY) == 0
1869 && (*p != '.' && *p != '[')
1870 && oc_var_check_ro(cl, om))
1871 return NULL;
1872
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001873 lp->ll_valtype = om->ocm_type;
1874
1875 if (v_type == VAR_OBJECT)
1876 lp->ll_tv = ((typval_T *)(
1877 lp->ll_tv->vval.v_object + 1)) + m_idx;
1878 else
1879 lp->ll_tv = &cl->class_members_tv[m_idx];
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001880 }
1881 }
1882
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001883 if (lp->ll_valtype == NULL)
1884 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001885 member_not_found_msg(cl, v_type, key, p - key);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001886 return NULL;
1887 }
1888 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001889 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001890 }
1891
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001892 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001893 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001894 return p;
1895}
1896
1897/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001898 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001899 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001900 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001901clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001902{
1903 vim_free(lp->ll_exp_name);
1904 vim_free(lp->ll_newkey);
1905}
1906
1907/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001908 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001909 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001910 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1911 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001912 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001913 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001914set_var_lval(
1915 lval_T *lp,
1916 char_u *endp,
1917 typval_T *rettv,
1918 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001919 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1920 char_u *op,
1921 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001922{
1923 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001924 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001925
1926 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001927 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001928 cc = *endp;
1929 *endp = NUL;
h-east2bd6a092023-05-19 19:01:17 +01001930 if (in_vim9script() && check_reserved_name(lp->ll_name, FALSE) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001931 return;
1932
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001933 if (lp->ll_blob != NULL)
1934 {
1935 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001936
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001937 if (op != NULL && *op != '=')
1938 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001939 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001940 return;
1941 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001942 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001943 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001944
1945 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1946 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001947 if (lp->ll_empty2)
1948 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1949
Bram Moolenaar68452172021-04-12 21:21:02 +02001950 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1951 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001952 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001953 }
1954 else
1955 {
1956 val = (int)tv_get_number_chk(rettv, &error);
1957 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001958 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001959 }
1960 }
1961 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001962 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001963 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001964
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001965 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1966 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001967 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001968 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001969 *endp = cc;
1970 return;
1971 }
1972
Bram Moolenaarff697e62019-02-12 22:28:33 +01001973 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001974 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001975 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001976 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001977 {
Ernie Raele75fde62023-12-21 17:18:54 +01001978 if (di != NULL && check_typval_is_value(&di->di_tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001979 {
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001980 clear_tv(&tv);
1981 return;
1982 }
1983
Bram Moolenaar79518e22017-02-17 16:31:35 +01001984 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001985 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1986 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001987 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001988 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01001989 ASSIGN_NO_DECL | ASSIGN_COMPOUND_OP, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001990 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001991 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001992 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001993 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001994 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001995 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1996 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001997 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001998 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001999 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002000 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002001 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002002 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002003 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002004 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002005 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002006 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002007 else if (lp->ll_range)
2008 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002009 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2010 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002011 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002012 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002013 return;
2014 }
2015
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002016 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
2017 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002018 }
2019 else
2020 {
2021 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00002022 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002023 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002024 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2025 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002026 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002027 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002028 return;
2029 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02002030
2031 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002032 && check_typval_arg_type(lp->ll_valtype, rettv,
2033 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02002034 return;
2035
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002036 if (lp->ll_newkey != NULL)
2037 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002038 if (op != NULL && *op != '=')
2039 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002040 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002041 return;
2042 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02002043 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
2044 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02002045 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002046
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002047 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002048 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002049 if (di == NULL)
2050 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002051 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2052 {
2053 vim_free(di);
2054 return;
2055 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002056 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002057 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002058 else if (op != NULL && *op != '=')
2059 {
2060 tv_op(lp->ll_tv, rettv, op);
2061 return;
2062 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002063 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002064 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002065
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002066 /*
2067 * Assign the value to the variable or list item.
2068 */
2069 if (copy)
2070 copy_tv(rettv, lp->ll_tv);
2071 else
2072 {
2073 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002074 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002075 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002076 }
2077 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002078}
2079
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002080/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002081 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2082 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002083 * Returns OK or FAIL.
2084 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002085 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002086tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002087{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002088 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002089 char_u numbuf[NUMBUFLEN];
2090 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002091 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002092
Ernie Raele75fde62023-12-21 17:18:54 +01002093 // Can't do anything with a Funcref or Dict or Type on the right.
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02002094 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002095 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Ernie Raele75fde62023-12-21 17:18:54 +01002096 && tv2->v_type != VAR_CLASS && tv2->v_type != VAR_TYPEALIAS
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02002097 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
2098 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002099 {
2100 switch (tv1->v_type)
2101 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002102 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002103 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002104 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002105 case VAR_DICT:
2106 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002107 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002108 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002109 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002110 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002111 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002112 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002113 case VAR_OBJECT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002114 break;
Ernie Raele75fde62023-12-21 17:18:54 +01002115 case VAR_CLASS:
2116 case VAR_TYPEALIAS:
2117 check_typval_is_value(tv1);
2118 return FAIL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002119
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002120 case VAR_BLOB:
2121 if (*op != '+' || tv2->v_type != VAR_BLOB)
2122 break;
2123 // BLOB += BLOB
2124 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2125 {
2126 blob_T *b1 = tv1->vval.v_blob;
2127 blob_T *b2 = tv2->vval.v_blob;
2128 int i, len = blob_len(b2);
2129 for (i = 0; i < len; i++)
2130 ga_append(&b1->bv_ga, blob_get(b2, i));
2131 }
2132 return OK;
2133
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002134 case VAR_LIST:
2135 if (*op != '+' || tv2->v_type != VAR_LIST)
2136 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002137 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02002138 if (tv2->vval.v_list != NULL)
2139 {
2140 if (tv1->vval.v_list == NULL)
2141 {
2142 tv1->vval.v_list = tv2->vval.v_list;
2143 ++tv1->vval.v_list->lv_refcount;
2144 }
2145 else
2146 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2147 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002148 return OK;
2149
2150 case VAR_NUMBER:
2151 case VAR_STRING:
2152 if (tv2->v_type == VAR_LIST)
2153 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002154 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002155 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002156 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002157 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002158 if (tv2->v_type == VAR_FLOAT)
2159 {
2160 float_T f = n;
2161
Bram Moolenaarff697e62019-02-12 22:28:33 +01002162 if (*op == '%')
2163 break;
2164 switch (*op)
2165 {
2166 case '+': f += tv2->vval.v_float; break;
2167 case '-': f -= tv2->vval.v_float; break;
2168 case '*': f *= tv2->vval.v_float; break;
2169 case '/': f /= tv2->vval.v_float; break;
2170 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002171 clear_tv(tv1);
2172 tv1->v_type = VAR_FLOAT;
2173 tv1->vval.v_float = f;
2174 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002175 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002176 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002177 switch (*op)
2178 {
2179 case '+': n += tv_get_number(tv2); break;
2180 case '-': n -= tv_get_number(tv2); break;
2181 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002182 case '/': n = num_divide(n, tv_get_number(tv2),
2183 &failed); break;
2184 case '%': n = num_modulus(n, tv_get_number(tv2),
2185 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002186 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002187 clear_tv(tv1);
2188 tv1->v_type = VAR_NUMBER;
2189 tv1->vval.v_number = n;
2190 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002191 }
2192 else
2193 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002194 if (tv2->v_type == VAR_FLOAT)
2195 break;
2196
Bram Moolenaarff697e62019-02-12 22:28:33 +01002197 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002198 s = tv_get_string(tv1);
2199 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002200 clear_tv(tv1);
2201 tv1->v_type = VAR_STRING;
2202 tv1->vval.v_string = s;
2203 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002204 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002205
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002206 case VAR_FLOAT:
2207 {
2208 float_T f;
2209
Bram Moolenaarff697e62019-02-12 22:28:33 +01002210 if (*op == '%' || *op == '.'
2211 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002212 && tv2->v_type != VAR_NUMBER
2213 && tv2->v_type != VAR_STRING))
2214 break;
2215 if (tv2->v_type == VAR_FLOAT)
2216 f = tv2->vval.v_float;
2217 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002218 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002219 switch (*op)
2220 {
2221 case '+': tv1->vval.v_float += f; break;
2222 case '-': tv1->vval.v_float -= f; break;
2223 case '*': tv1->vval.v_float *= f; break;
2224 case '/': tv1->vval.v_float /= f; break;
2225 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002226 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002227 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002228 }
2229 }
2230
Ernie Raele75fde62023-12-21 17:18:54 +01002231 if (check_typval_is_value(tv2) == OK)
2232 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002233 return FAIL;
2234}
2235
2236/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002237 * Evaluate the expression used in a ":for var in expr" command.
2238 * "arg" points to "var".
2239 * Set "*errp" to TRUE for an error, FALSE otherwise;
2240 * Return a pointer that holds the info. Null when there is an error.
2241 */
2242 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002243eval_for_line(
2244 char_u *arg,
2245 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002246 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002247 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002248{
Bram Moolenaar33570922005-01-25 22:26:29 +00002249 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02002250 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002251 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002252 typval_T tv;
2253 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002254 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002255
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002256 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002257
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002258 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002259 if (fi == NULL)
2260 return NULL;
2261
Bram Moolenaar404557e2021-07-05 21:41:48 +02002262 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
2263 &fi->fi_semicolon, FALSE);
2264 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002265 return fi;
2266
Bram Moolenaar404557e2021-07-05 21:41:48 +02002267 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002268 if (expr[0] != 'i' || expr[1] != 'n'
2269 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002270 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02002271 if (in_vim9script() && *expr == ':' && expr != var_list_end)
2272 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
2273 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002274 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002275 return fi;
2276 }
2277
2278 if (skip)
2279 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002280 expr = skipwhite_and_linebreak(expr + 2, evalarg);
2281 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002282 {
2283 *errp = FALSE;
2284 if (!skip)
2285 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002286 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002287 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002288 l = tv.vval.v_list;
2289 if (l == NULL)
2290 {
2291 // a null list is like an empty list: do nothing
2292 clear_tv(&tv);
2293 }
2294 else
2295 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002296 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002297 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002298
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002299 // No need to increment the refcount, it's already set for
2300 // the list being used in "tv".
2301 fi->fi_list = l;
2302 list_add_watch(l, &fi->fi_lw);
2303 fi->fi_lw.lw_item = l->lv_first;
2304 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002305 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002306 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002307 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002308 fi->fi_bi = 0;
2309 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002310 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002311 typval_T btv;
2312
2313 // Make a copy, so that the iteration still works when the
2314 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002315 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002316 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002317 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002318 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002319 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002320 else if (tv.v_type == VAR_STRING)
2321 {
2322 fi->fi_byte_idx = 0;
2323 fi->fi_string = tv.vval.v_string;
2324 tv.vval.v_string = NULL;
2325 if (fi->fi_string == NULL)
2326 fi->fi_string = vim_strsave((char_u *)"");
2327 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002328 else
2329 {
Sean Dewar80d73952021-08-04 19:25:54 +02002330 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002331 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002332 }
2333 }
2334 }
2335 if (skip)
2336 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002337 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002338
2339 return fi;
2340}
2341
2342/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002343 * Used when looping over a :for line, skip the "in expr" part.
2344 */
2345 void
2346skip_for_lines(void *fi_void, evalarg_T *evalarg)
2347{
2348 forinfo_T *fi = (forinfo_T *)fi_void;
2349 int i;
2350
2351 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002352 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002353}
2354
2355/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002356 * Use the first item in a ":for" list. Advance to the next.
2357 * Assign the values to the variable (list). "arg" points to the first one.
2358 * Return TRUE when a valid item was found, FALSE when at end of list or
2359 * something wrong.
2360 */
2361 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002362next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002363{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002364 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002365 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002366 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002367 ? (ASSIGN_FINAL
2368 // first round: error if variable exists
2369 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002370 | ASSIGN_NO_MEMBER_TYPE
2371 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002372 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002373 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002374 int skip_assign = in_vim9script() && arg[0] == '_'
2375 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002376
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002377 if (fi->fi_blob != NULL)
2378 {
2379 typval_T tv;
2380
2381 if (fi->fi_bi >= blob_len(fi->fi_blob))
2382 return FALSE;
2383 tv.v_type = VAR_NUMBER;
2384 tv.v_lock = VAR_FIXED;
2385 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2386 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002387 if (skip_assign)
2388 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002389 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002390 fi->fi_varcount, flag, NULL) == OK;
2391 }
2392
2393 if (fi->fi_string != NULL)
2394 {
2395 typval_T tv;
2396 int len;
2397
2398 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2399 if (len == 0)
2400 return FALSE;
2401 tv.v_type = VAR_STRING;
2402 tv.v_lock = VAR_FIXED;
2403 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2404 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002405 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002406 if (skip_assign)
2407 result = TRUE;
2408 else
2409 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002410 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002411 vim_free(tv.vval.v_string);
2412 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002413 }
2414
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002415 item = fi->fi_lw.lw_item;
2416 if (item == NULL)
2417 result = FALSE;
2418 else
2419 {
2420 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002421 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002422 if (skip_assign)
2423 result = TRUE;
2424 else
2425 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002426 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002427 }
2428 return result;
2429}
2430
2431/*
2432 * Free the structure used to store info used by ":for".
2433 */
2434 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002435free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002436{
Bram Moolenaar33570922005-01-25 22:26:29 +00002437 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002438
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002439 if (fi == NULL)
2440 return;
2441 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002442 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002443 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002444 list_unref(fi->fi_list);
2445 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002446 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002447 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002448 else
2449 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002450 vim_free(fi);
2451}
2452
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002454set_context_for_expression(
2455 expand_T *xp,
2456 char_u *arg,
2457 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002459 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002461 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002463 if (cmdidx == CMD_let || cmdidx == CMD_var
2464 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002465 {
2466 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002467 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002468 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002469 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002470 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002471 {
2472 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002473 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002474 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002475 break;
2476 }
2477 return;
2478 }
2479 }
2480 else
2481 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2482 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 while ((xp->xp_pattern = vim_strpbrk(arg,
2484 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2485 {
2486 c = *xp->xp_pattern;
2487 if (c == '&')
2488 {
2489 c = xp->xp_pattern[1];
2490 if (c == '&')
2491 {
2492 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002493 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 }
2495 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002496 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002498 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2499 xp->xp_pattern += 2;
2500
2501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 }
2503 else if (c == '$')
2504 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002505 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 xp->xp_context = EXPAND_ENV_VARS;
2507 }
2508 else if (c == '=')
2509 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002510 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 xp->xp_context = EXPAND_EXPRESSION;
2512 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002513 else if (c == '#'
2514 && xp->xp_context == EXPAND_EXPRESSION)
2515 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002516 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002517 break;
2518 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002519 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 && xp->xp_context == EXPAND_FUNCTIONS
2521 && vim_strchr(xp->xp_pattern, '(') == NULL)
2522 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002523 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 break;
2525 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002526 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002528 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 {
2530 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2531 if (c == '\\' && xp->xp_pattern[1] != NUL)
2532 ++xp->xp_pattern;
2533 xp->xp_context = EXPAND_NOTHING;
2534 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002535 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002537 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2539 /* skip */ ;
2540 xp->xp_context = EXPAND_NOTHING;
2541 }
2542 else if (c == '|')
2543 {
2544 if (xp->xp_pattern[1] == '|')
2545 {
2546 ++xp->xp_pattern;
2547 xp->xp_context = EXPAND_EXPRESSION;
2548 }
2549 else
2550 xp->xp_context = EXPAND_COMMANDS;
2551 }
2552 else
2553 xp->xp_context = EXPAND_EXPRESSION;
2554 }
2555 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002556 // Doesn't look like something valid, expand as an expression
2557 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002558 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 arg = xp->xp_pattern;
2560 if (*arg != NUL)
2561 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2562 /* skip */ ;
2563 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002564
2565 // ":exe one two" completes "two"
2566 if ((cmdidx == CMD_execute
2567 || cmdidx == CMD_echo
2568 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002569 || cmdidx == CMD_echomsg
2570 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002571 && xp->xp_context == EXPAND_EXPRESSION)
2572 {
2573 for (;;)
2574 {
2575 char_u *n = skiptowhite(arg);
2576
2577 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2578 break;
2579 arg = skipwhite(n);
2580 }
2581 }
2582
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 xp->xp_pattern = arg;
2584}
2585
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002587 * Return TRUE if "pat" matches "text".
2588 * Does not use 'cpo' and always uses 'magic'.
2589 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002590 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002591pattern_match(char_u *pat, char_u *text, int ic)
2592{
2593 int matches = FALSE;
2594 char_u *save_cpo;
2595 regmatch_T regmatch;
2596
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002597 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002598 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002599 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002600 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2601 if (regmatch.regprog != NULL)
2602 {
2603 regmatch.rm_ic = ic;
2604 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2605 vim_regfree(regmatch.regprog);
2606 }
2607 p_cpo = save_cpo;
2608 return matches;
2609}
2610
2611/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002612 * Handle a name followed by "(". Both for just "name(arg)" and for
2613 * "expr->name(arg)".
2614 * Returns OK or FAIL.
2615 */
2616 static int
2617eval_func(
2618 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002619 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002620 char_u *name,
2621 int name_len,
2622 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002623 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002624 typval_T *basetv) // "expr" for "expr->name(arg)"
2625{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002626 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002627 char_u *s = name;
2628 int len = name_len;
2629 partial_T *partial;
2630 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002631 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002632 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002633
2634 if (!evaluate)
2635 check_vars(s, len);
2636
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002637 // If "s" is the name of a variable of type VAR_FUNC
2638 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002639 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002640 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002641
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002642 // Need to make a copy, in case evaluating the arguments makes
2643 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002644 s = vim_strsave(s);
zeertzjqb7f6f932023-04-13 22:12:50 +01002645 if (s == NULL || (evaluate && *s == NUL))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002646 ret = FAIL;
2647 else
2648 {
2649 funcexe_T funcexe;
2650
2651 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002652 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002653 funcexe.fe_firstline = curwin->w_cursor.lnum;
2654 funcexe.fe_lastline = curwin->w_cursor.lnum;
2655 funcexe.fe_evaluate = evaluate;
2656 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02002657 if (partial != NULL)
2658 {
2659 funcexe.fe_object = partial->pt_obj;
2660 if (funcexe.fe_object != NULL)
2661 ++funcexe.fe_object->obj_refcount;
2662 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002663 funcexe.fe_basetv = basetv;
2664 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002665 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002666 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002667 }
2668 vim_free(s);
2669
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002670 // If evaluate is FALSE rettv->v_type was not set in
2671 // get_func_tv, but it's needed in handle_subscript() to parse
2672 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002673 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2674 {
2675 rettv->vval.v_string = NULL;
2676 rettv->v_type = VAR_FUNC;
2677 }
2678
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002679 // Stop the expression evaluation when immediately
2680 // aborting on error, or when an interrupt occurred or
2681 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002682 if (evaluate && aborting())
2683 {
2684 if (ret == OK)
2685 clear_tv(rettv);
2686 ret = FAIL;
2687 }
2688 return ret;
2689}
2690
2691/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002692 * After a NL, skip over empty lines and comment-only lines.
2693 */
2694 static char_u *
2695newline_skip_comments(char_u *arg)
2696{
2697 char_u *p = arg + 1;
2698
2699 for (;;)
2700 {
2701 p = skipwhite(p);
2702
2703 if (*p == NUL)
2704 break;
2705 if (vim9_comment_start(p))
2706 {
2707 char_u *nl = vim_strchr(p, NL);
2708
2709 if (nl == NULL)
2710 break;
2711 p = nl;
2712 }
2713 if (*p != NL)
2714 break;
2715 ++p; // skip another NL
2716 }
2717 return p;
2718}
2719
2720/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002721 * Get the next line source line without advancing. But do skip over comment
2722 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002723 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002724 */
2725 static char_u *
2726getline_peek_skip_comments(evalarg_T *evalarg)
2727{
2728 for (;;)
2729 {
2730 char_u *next = getline_peek(evalarg->eval_getline,
2731 evalarg->eval_cookie);
2732 char_u *p;
2733
2734 if (next == NULL)
2735 break;
2736 p = skipwhite(next);
2737 if (*p != NUL && !vim9_comment_start(p))
2738 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002739 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002740 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002741 }
2742 return NULL;
2743}
2744
2745/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002746 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2747 * comment) and there is a next line, return the next line (skipping blanks)
2748 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002749 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2750 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002751 * "arg" must point somewhere inside a line, not at the start.
2752 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002753 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002754eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2755{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002756 char_u *p = skipwhite(arg);
2757
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002758 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002759 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002760 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002761 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2762 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002763 && (*p == NUL || *p == NL
2764 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002765 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002766 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002767
Bram Moolenaare442d592022-05-05 12:20:28 +01002768 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002769 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002770 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002771 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002772 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002773 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002774
Bram Moolenaar9d489562020-07-30 20:08:50 +02002775 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002776 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002777 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002778 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002779 }
2780 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002781 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002782}
2783
2784/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002785 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002786 * Only called for Vim9 script.
Yegappan Lakshmanan28d71b52024-01-12 17:21:55 +01002787 *
2788 * If "arg" is not NULL, then the caller should assign the return value to
2789 * "arg".
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002790 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002791 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002792eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002793{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002794 garray_T *gap = &evalarg->eval_ga;
2795 char_u *line;
2796
Bram Moolenaar39be4982022-05-06 21:51:50 +01002797 if (arg != NULL)
2798 {
2799 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002800 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002801 // Truncate before a trailing comment, so that concatenating the lines
2802 // won't turn the rest into a comment.
2803 if (*skipwhite(arg) == '#')
2804 *arg = NUL;
2805 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002806
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002807 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002808 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2809 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002810 else
2811 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002812 if (line == NULL)
2813 return NULL;
2814
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002815 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002816 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2817 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002818 char_u *p = skipwhite(line);
2819
2820 // Going to concatenate the lines after parsing. For an empty or
2821 // comment line use an empty string.
2822 if (*p == NUL || vim9_comment_start(p))
2823 {
2824 vim_free(line);
2825 line = vim_strsave((char_u *)"");
2826 }
2827
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002828 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2829 ++gap->ga_len;
2830 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002831 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002832 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002833 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002834 evalarg->eval_tofree = line;
2835 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002836
2837 // Advanced to the next line, "arg" no longer points into the previous
Yegappan Lakshmanan28d71b52024-01-12 17:21:55 +01002838 // line. The caller assigns the return value to "arg".
2839 // If "arg" is NULL, then the return value is discarded. In that case,
2840 // "arg" still points to the previous line. So don't reset
2841 // "eval_using_cmdline".
2842 if (arg != NULL)
2843 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002844 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002845}
2846
Bram Moolenaare6b53242020-07-01 17:28:33 +02002847/*
2848 * Call eval_next_non_blank() and get the next line if needed.
2849 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002850 char_u *
2851skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2852{
2853 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002854 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002855
Bram Moolenaar962d7212020-07-04 14:15:00 +02002856 if (evalarg == NULL)
2857 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002858 eval_next_non_blank(p, evalarg, &getnext);
2859 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002860 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002861 return p;
2862}
2863
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002864/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002865 * The "eval" functions have an "evalarg" argument: When NULL or
2866 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
2867 * parsed but not executed. The functions may return OK, but the rettv will be
2868 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 */
2870
2871/*
2872 * Handle zero level expression.
2873 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002874 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002875 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002876 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 * Return OK or FAIL.
2878 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002879 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002880eval0(
2881 char_u *arg,
2882 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002883 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002884 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002886 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2887}
2888
2889/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002890 * If "arg" is a simple function call without arguments then call it and return
2891 * the result. Otherwise return NOTDONE.
2892 */
2893 int
2894may_call_simple_func(
2895 char_u *arg,
2896 typval_T *rettv)
2897{
2898 char_u *parens = (char_u *)strstr((char *)arg, "()");
2899 int r = NOTDONE;
2900
2901 // If the expression is "FuncName()" then we can skip a lot of overhead.
2902 if (parens != NULL && *skipwhite(parens + 2) == NUL)
2903 {
2904 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
2905
2906 if (to_name_end(p, TRUE) == parens)
2907 r = call_simple_func(arg, (int)(parens - arg), rettv);
2908 }
2909 return r;
2910}
2911
2912/*
2913 * Handle zero level expression with optimization for a simple function call.
2914 * Same arguments and return value as eval0().
2915 */
2916 int
2917eval0_simple_funccal(
2918 char_u *arg,
2919 typval_T *rettv,
2920 exarg_T *eap,
2921 evalarg_T *evalarg)
2922{
2923 int r = may_call_simple_func(arg, rettv);
2924
2925 if (r == NOTDONE)
2926 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
2927 return r;
2928}
2929
2930/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002931 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2932 * expression and don't check what comes after the expression.
2933 */
2934 int
2935eval0_retarg(
2936 char_u *arg,
2937 typval_T *rettv,
2938 exarg_T *eap,
2939 evalarg_T *evalarg,
2940 char_u **retarg)
2941{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 int ret;
2943 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002944 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002945 int did_emsg_before = did_emsg;
2946 int called_emsg_before = called_emsg;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002947 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002948 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949
2950 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002951 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002952
Bram Moolenaar79481362022-06-27 20:15:10 +01002953 if (ret != FAIL)
2954 {
2955 expr_end = p;
2956 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002957
Bram Moolenaar79481362022-06-27 20:15:10 +01002958 // In Vim9 script a command block is not split at NL characters for
2959 // commands using an expression argument. Skip over a '#' comment to
2960 // check for a following NL. Require white space before the '#'.
2961 if (in_vim9script() && p > expr_end && retarg == NULL)
2962 while (*p == '#')
2963 {
2964 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002965
Bram Moolenaar79481362022-06-27 20:15:10 +01002966 if (nl == NULL)
2967 break;
2968 p = skipwhite(nl + 1);
2969 if (eap != NULL && *p != NUL)
2970 eap->nextcmd = p;
2971 check_for_end = FALSE;
2972 }
2973
2974 if (check_for_end)
2975 end_error = !ends_excmd2(arg, p);
2976 }
2977
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002978 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 {
2980 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002981 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 /*
2983 * Report the invalid expression unless the expression evaluation has
2984 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002985 * exception, or we already gave a more specific error.
2986 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002988 if (!aborting()
2989 && did_emsg == did_emsg_before
2990 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002991 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002992 {
2993 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002994 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002995 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002996 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002997 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002998
zeertzjqf2588b62023-05-05 17:22:35 +01002999 if (eap != NULL && p != NULL)
3000 {
3001 // Some of the expression may not have been consumed.
3002 // Only execute a next command if it cannot be a "||" operator.
3003 // The next command may be "catch".
3004 char_u *nextcmd = check_nextcmd(p);
3005 if (nextcmd != NULL && *nextcmd != '|')
3006 eap->nextcmd = nextcmd;
3007 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01003008 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003010
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003011 if (retarg != NULL)
3012 *retarg = p;
3013 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02003014 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003015
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 return ret;
3017}
3018
3019/*
3020 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003021 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003022 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 *
3024 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003025 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003027 * Note: "rettv.v_lock" is not set.
3028 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 * Return OK or FAIL.
3030 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003031 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003032eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033{
Bram Moolenaar793648f2020-06-26 21:28:25 +02003034 char_u *p;
3035 int getnext;
3036
Bram Moolenaar4a091b92020-09-12 22:10:00 +02003037 CLEAR_POINTER(rettv);
3038
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 /*
3040 * Get the first variable.
3041 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003042 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 return FAIL;
3044
Bram Moolenaar793648f2020-06-26 21:28:25 +02003045 p = eval_next_non_blank(*arg, evalarg, &getnext);
3046 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003048 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003049 int result;
3050 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003051 evalarg_T *evalarg_used = evalarg;
3052 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003053 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02003054 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02003055 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003056
3057 if (evalarg == NULL)
3058 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003059 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003060 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003061 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003062 orig_flags = evalarg_used->eval_flags;
3063 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003064
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003065 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003066 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003067 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003068 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003069 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003070 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003071 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003072 clear_tv(rettv);
3073 return FAIL;
3074 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003075 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003076 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003077
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003079 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003081 int error = FALSE;
3082
Bram Moolenaar13106602020-10-04 16:06:05 +02003083 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003084 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02003085 else if (vim9script)
3086 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02003087 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003089 if (error || !op_falsy || !result)
3090 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003091 if (error)
3092 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 }
3094
3095 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02003096 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003098 if (op_falsy)
3099 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02003100 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003101 {
mityu4ac198c2021-05-28 17:52:40 +02003102 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003103 clear_tv(rettv);
3104 return FAIL;
3105 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003106 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003107 evalarg_used->eval_flags = (op_falsy ? !result : result)
zeertzjqb7f6f932023-04-13 22:12:50 +01003108 ? orig_flags : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003109 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02003110 {
3111 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02003113 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003114 if (!op_falsy || !result)
3115 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003117 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003119 /*
3120 * Check for the ":".
3121 */
3122 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
3123 if (*p != ':')
3124 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003125 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003126 if (evaluate && result)
3127 clear_tv(rettv);
3128 evalarg_used->eval_flags = orig_flags;
3129 return FAIL;
3130 }
3131 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003132 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003133 else
3134 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003135 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003136 {
3137 error_white_both(p, 1);
3138 clear_tv(rettv);
3139 evalarg_used->eval_flags = orig_flags;
3140 return FAIL;
3141 }
3142 *arg = p;
3143 }
3144
3145 /*
3146 * Get the third variable. Recursive!
3147 */
Bram Moolenaar13106602020-10-04 16:06:05 +02003148 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003149 {
mityu4ac198c2021-05-28 17:52:40 +02003150 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003151 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02003152 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003153 return FAIL;
3154 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003155 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
3156 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003157 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003158 if (eval1(arg, &var2, evalarg_used) == FAIL)
3159 {
3160 if (evaluate && result)
3161 clear_tv(rettv);
3162 evalarg_used->eval_flags = orig_flags;
3163 return FAIL;
3164 }
3165 if (evaluate && !result)
3166 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003168
3169 if (evalarg == NULL)
3170 clear_evalarg(&local_evalarg, NULL);
3171 else
3172 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 }
3174
3175 return OK;
3176}
3177
3178/*
3179 * Handle first level expression:
3180 * expr2 || expr2 || expr2 logical OR
3181 *
3182 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003183 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 *
3185 * Return OK or FAIL.
3186 */
3187 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003188eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003190 char_u *p;
3191 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192
3193 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003194 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003196 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 return FAIL;
3198
3199 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003200 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003202 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003203 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003205 evalarg_T *evalarg_used = evalarg;
3206 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003207 int evaluate;
3208 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003209 long result = FALSE;
3210 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003211 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003212 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003213
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003214 if (evalarg == NULL)
3215 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003216 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003217 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003218 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003219 orig_flags = evalarg_used->eval_flags;
3220 evaluate = orig_flags & EVAL_EVALUATE;
3221 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003222 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003223 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003224 result = tv_get_bool_chk(rettv, &error);
3225 else if (tv_get_number_chk(rettv, &error) != 0)
3226 result = TRUE;
3227 clear_tv(rettv);
3228 if (error)
3229 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 }
3231
3232 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003233 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003235 while (p[0] == '|' && p[1] == '|')
3236 {
3237 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003238 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003239 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003240 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003241 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003242 {
3243 error_white_both(p, 2);
3244 clear_tv(rettv);
3245 return FAIL;
3246 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003247 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003248 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003249
3250 /*
3251 * Get the second variable.
3252 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003253 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003254 {
mityu4ac198c2021-05-28 17:52:40 +02003255 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003256 clear_tv(rettv);
3257 return FAIL;
3258 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003259 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3260 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003261 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003262 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003263 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003264
3265 /*
3266 * Compute the result.
3267 */
3268 if (evaluate && !result)
3269 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003270 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003271 result = tv_get_bool_chk(&var2, &error);
3272 else if (tv_get_number_chk(&var2, &error) != 0)
3273 result = TRUE;
3274 clear_tv(&var2);
3275 if (error)
3276 return FAIL;
3277 }
3278 if (evaluate)
3279 {
3280 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003281 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003282 rettv->v_type = VAR_BOOL;
3283 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003284 }
3285 else
3286 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003287 rettv->v_type = VAR_NUMBER;
3288 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003289 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003290 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003291
3292 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003294
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003295 if (evalarg == NULL)
3296 clear_evalarg(&local_evalarg, NULL);
3297 else
3298 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 }
3300
3301 return OK;
3302}
3303
3304/*
3305 * Handle second level expression:
3306 * expr3 && expr3 && expr3 logical AND
3307 *
3308 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003309 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 *
3311 * Return OK or FAIL.
3312 */
3313 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003314eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003316 char_u *p;
3317 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318
3319 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003320 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003322 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 return FAIL;
3324
3325 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003326 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003328 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003329 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003331 evalarg_T *evalarg_used = evalarg;
3332 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003333 int orig_flags;
3334 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003335 long result = TRUE;
3336 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003337 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003338 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003339
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003340 if (evalarg == NULL)
3341 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003342 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003343 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003344 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003345 orig_flags = evalarg_used->eval_flags;
3346 evaluate = orig_flags & EVAL_EVALUATE;
3347 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003348 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003349 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003350 result = tv_get_bool_chk(rettv, &error);
3351 else if (tv_get_number_chk(rettv, &error) == 0)
3352 result = FALSE;
3353 clear_tv(rettv);
3354 if (error)
3355 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 }
3357
3358 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003359 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003361 while (p[0] == '&' && p[1] == '&')
3362 {
3363 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003364 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003365 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003366 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003367 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003368 {
3369 error_white_both(p, 2);
3370 clear_tv(rettv);
3371 return FAIL;
3372 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003373 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003374 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003375
3376 /*
3377 * Get the second variable.
3378 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003379 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003380 {
mityu4ac198c2021-05-28 17:52:40 +02003381 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003382 clear_tv(rettv);
3383 return FAIL;
3384 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003385 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3386 evalarg_used->eval_flags = result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003387 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003388 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003389 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003390 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003391
3392 /*
3393 * Compute the result.
3394 */
3395 if (evaluate && result)
3396 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003397 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003398 result = tv_get_bool_chk(&var2, &error);
3399 else if (tv_get_number_chk(&var2, &error) == 0)
3400 result = FALSE;
3401 clear_tv(&var2);
3402 if (error)
3403 return FAIL;
3404 }
3405 if (evaluate)
3406 {
3407 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003408 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003409 rettv->v_type = VAR_BOOL;
3410 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003411 }
3412 else
3413 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003414 rettv->v_type = VAR_NUMBER;
3415 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003416 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003417 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003418
3419 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003421
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003422 if (evalarg == NULL)
3423 clear_evalarg(&local_evalarg, NULL);
3424 else
3425 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 }
3427
3428 return OK;
3429}
3430
3431/*
3432 * Handle third level expression:
3433 * var1 == var2
3434 * var1 =~ var2
3435 * var1 != var2
3436 * var1 !~ var2
3437 * var1 > var2
3438 * var1 >= var2
3439 * var1 < var2
3440 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003441 * var1 is var2
3442 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 *
3444 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003445 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 *
3447 * Return OK or FAIL.
3448 */
3449 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003450eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003453 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003454 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003456 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457
3458 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003459 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003461 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 return FAIL;
3463
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003464 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003465
Bram Moolenaar696ba232020-07-29 21:20:41 +02003466 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467
3468 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003469 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003471 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003473 typval_T var2;
3474 int ic;
3475 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003476 int evaluate = evalarg == NULL
3477 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003478 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003479
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003480 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003481 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003482 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003483 p = *arg;
3484 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003485 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3486 {
mityu4ac198c2021-05-28 17:52:40 +02003487 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003488 clear_tv(rettv);
3489 return FAIL;
3490 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003491
Bram Moolenaar696ba232020-07-29 21:20:41 +02003492 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3493 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003494 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003495 clear_tv(rettv);
3496 return FAIL;
3497 }
3498
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003499 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 if (p[len] == '?')
3501 {
3502 ic = TRUE;
3503 ++len;
3504 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003505 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 else if (p[len] == '#')
3507 {
3508 ic = FALSE;
3509 ++len;
3510 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003511 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003513 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514
3515 /*
3516 * Get the second variable.
3517 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003518 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3519 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003520 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003521 clear_tv(rettv);
3522 return FAIL;
3523 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003524 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003525 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003527 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 return FAIL;
3529 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003530 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003531 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003532 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003533
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003534 // use the line of the comparison for messages
3535 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003536 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003537 {
3538 ret = FAIL;
3539 clear_tv(rettv);
3540 }
3541 else
3542 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003543 clear_tv(&var2);
3544 return ret;
3545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 }
3547
3548 return OK;
3549}
3550
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003551/*
3552 * Make a copy of blob "tv1" and append blob "tv2".
3553 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003554 void
3555eval_addblob(typval_T *tv1, typval_T *tv2)
3556{
3557 blob_T *b1 = tv1->vval.v_blob;
3558 blob_T *b2 = tv2->vval.v_blob;
3559 blob_T *b = blob_alloc();
3560 int i;
3561
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003562 if (b == NULL)
3563 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003565 for (i = 0; i < blob_len(b1); i++)
3566 ga_append(&b->bv_ga, blob_get(b1, i));
3567 for (i = 0; i < blob_len(b2); i++)
3568 ga_append(&b->bv_ga, blob_get(b2, i));
3569
3570 clear_tv(tv1);
3571 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572}
3573
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003574/*
3575 * Make a copy of list "tv1" and append list "tv2".
3576 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577 int
3578eval_addlist(typval_T *tv1, typval_T *tv2)
3579{
3580 typval_T var3;
3581
3582 // concatenate Lists
3583 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3584 {
3585 clear_tv(tv1);
3586 clear_tv(tv2);
3587 return FAIL;
3588 }
3589 clear_tv(tv1);
3590 *tv1 = var3;
3591 return OK;
3592}
3593
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003595 * Handle the bitwise left/right shift operator expression:
3596 * var1 << var2
3597 * var1 >> var2
3598 *
3599 * "arg" must point to the first non-white of the expression.
3600 * "arg" is advanced to just after the recognized expression.
3601 *
3602 * Return OK or FAIL.
3603 */
3604 static int
3605eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3606{
3607 /*
3608 * Get the first expression.
3609 */
3610 if (eval6(arg, rettv, evalarg) == FAIL)
3611 return FAIL;
3612
3613 /*
3614 * Repeat computing, until no '<<' or '>>' is following.
3615 */
3616 for (;;)
3617 {
3618 char_u *p;
3619 int getnext;
3620 exprtype_T type;
3621 int evaluate;
3622 typval_T var2;
3623 int vim9script;
3624
3625 p = eval_next_non_blank(*arg, evalarg, &getnext);
3626 if (p[0] == '<' && p[1] == '<')
3627 type = EXPR_LSHIFT;
3628 else if (p[0] == '>' && p[1] == '>')
3629 type = EXPR_RSHIFT;
3630 else
3631 return OK;
3632
3633 // Handle a bitwise left or right shift operator
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02003634 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3635 if (evaluate && rettv->v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003636 {
3637 // left operand should be a number
3638 emsg(_(e_bitshift_ops_must_be_number));
3639 clear_tv(rettv);
3640 return FAIL;
3641 }
3642
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003643 vim9script = in_vim9script();
3644 if (getnext)
3645 {
3646 *arg = eval_next_line(*arg, evalarg);
3647 p = *arg;
3648 }
3649 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3650 {
3651 error_white_both(*arg, 2);
3652 clear_tv(rettv);
3653 return FAIL;
3654 }
3655
3656 /*
3657 * Get the second variable.
3658 */
3659 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3660 {
3661 error_white_both(p, 2);
3662 clear_tv(rettv);
3663 return FAIL;
3664 }
3665 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3666 if (eval6(arg, &var2, evalarg) == FAIL)
3667 {
3668 clear_tv(rettv);
3669 return FAIL;
3670 }
3671
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003672 if (evaluate)
3673 {
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02003674 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3675 {
3676 // right operand should be a positive number
3677 if (var2.v_type != VAR_NUMBER)
3678 emsg(_(e_bitshift_ops_must_be_number));
3679 else
3680 emsg(_(e_bitshift_ops_must_be_positive));
3681 clear_tv(rettv);
3682 clear_tv(&var2);
3683 return FAIL;
3684 }
3685
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003686 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3687 // shifting more bits than we have always results in zero
3688 rettv->vval.v_number = 0;
3689 else if (type == EXPR_LSHIFT)
3690 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003691 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003692 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003693 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003694 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003695 }
3696
3697 clear_tv(&var2);
3698 }
3699
3700 return OK;
3701}
3702
3703/*
3704 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003705 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003707 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003708 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 *
3710 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003711 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 *
3713 * Return OK or FAIL.
3714 */
3715 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003716eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003719 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003721 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 return FAIL;
3723
3724 /*
3725 * Repeat computing, until no '+', '-' or '.' is following.
3726 */
3727 for (;;)
3728 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003729 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003730 int getnext;
3731 char_u *p;
3732 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003733 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003734 int concat;
3735 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003736 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003737
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003738 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003739 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003740 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003741 p = eval_next_non_blank(*arg, evalarg, &getnext);
3742 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003743 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003744 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3745 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003747 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3748 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003749
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003750 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003751 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003752 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003753 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003754 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003755 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003756 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003757 {
mityu4ac198c2021-05-28 17:52:40 +02003758 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003759 clear_tv(rettv);
3760 return FAIL;
3761 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003762 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003763 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003764 if ((op != '+' || (rettv->v_type != VAR_LIST
3765 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003766 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003767 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003768 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003769 int error = FALSE;
3770
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003771 // For "list + ...", an illegal use of the first operand as
3772 // a number cannot be determined before evaluating the 2nd
3773 // operand: if this is also a list, all is ok.
3774 // For "something . ...", "something - ..." or "non-list + ...",
3775 // we know that the first operand needs to be a string or number
3776 // without evaluating the 2nd operand. So check before to avoid
3777 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003778 if (op != '.')
3779 tv_get_number_chk(rettv, &error);
3780 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003781 {
3782 clear_tv(rettv);
3783 return FAIL;
3784 }
3785 }
3786
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 /*
3788 * Get the second variable.
3789 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003790 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003791 {
mityu89dcb4d2021-05-28 13:50:17 +02003792 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003793 clear_tv(rettv);
3794 return FAIL;
3795 }
3796 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003797 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003799 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 return FAIL;
3801 }
3802
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003803 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 {
3805 /*
3806 * Compute the result.
3807 */
3808 if (op == '.')
3809 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003810 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3811 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003812 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003813
Bram Moolenaar2e086612020-08-25 22:37:48 +02003814 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003815 || var2.v_type == VAR_CHANNEL
3816 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003817 semsg(_(e_using_invalid_value_as_string_str),
3818 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003819 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003820 {
3821 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3822 var2.vval.v_float);
3823 s2 = buf2;
3824 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003825 else
3826 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003827 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003828 {
3829 clear_tv(rettv);
3830 clear_tv(&var2);
3831 return FAIL;
3832 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003833 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003834 clear_tv(rettv);
3835 rettv->v_type = VAR_STRING;
3836 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003838 else if (op == '+' && rettv->v_type == VAR_BLOB
3839 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003841 else if (op == '+' && rettv->v_type == VAR_LIST
3842 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003843 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003845 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 else
3848 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003849 int error = FALSE;
3850 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003851 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003852
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003853 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003854 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003855 f1 = rettv->vval.v_float;
3856 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003857 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003858 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003859 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003860 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003861 if (error)
3862 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003863 // This can only happen for "list + non-list" or
3864 // "blob + non-blob". For "non-list + ..." or
3865 // "something - ...", we returned before evaluating the
3866 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003867 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003868 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003869 return FAIL;
3870 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003871 if (var2.v_type == VAR_FLOAT)
3872 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003873 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003874 if (var2.v_type == VAR_FLOAT)
3875 {
3876 f2 = var2.vval.v_float;
3877 n2 = 0;
3878 }
3879 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003880 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003881 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003882 if (error)
3883 {
3884 clear_tv(rettv);
3885 clear_tv(&var2);
3886 return FAIL;
3887 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003888 if (rettv->v_type == VAR_FLOAT)
3889 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003890 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003891 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003892
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003893 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003894 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3895 {
3896 if (op == '+')
3897 f1 = f1 + f2;
3898 else
3899 f1 = f1 - f2;
3900 rettv->v_type = VAR_FLOAT;
3901 rettv->vval.v_float = f1;
3902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003904 {
3905 if (op == '+')
3906 n1 = n1 + n2;
3907 else
3908 n1 = n1 - n2;
3909 rettv->v_type = VAR_NUMBER;
3910 rettv->vval.v_number = n1;
3911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003913 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 }
3915 }
3916 return OK;
3917}
3918
3919/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003920 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 * * number multiplication
3922 * / number division
3923 * % number modulo
3924 *
3925 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003926 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 *
3928 * Return OK or FAIL.
3929 */
3930 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003931eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003932 char_u **arg,
3933 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003934 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003935 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003937 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938
3939 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003940 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003942 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 return FAIL;
3944
3945 /*
3946 * Repeat computing, until no '*', '/' or '%' is following.
3947 */
3948 for (;;)
3949 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003950 int evaluate;
3951 int getnext;
3952 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003953 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003954 int op;
3955 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003956 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003957 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003958
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003959 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003960 p = eval_next_non_blank(*arg, evalarg, &getnext);
3961 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003962 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003964
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003965 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003966 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003967 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003968 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003969 {
3970 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3971 {
mityu4ac198c2021-05-28 17:52:40 +02003972 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003973 clear_tv(rettv);
3974 return FAIL;
3975 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003976 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003979 f1 = 0;
3980 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003981 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003982 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003984 if (rettv->v_type == VAR_FLOAT)
3985 {
3986 f1 = rettv->vval.v_float;
3987 use_float = TRUE;
3988 n1 = 0;
3989 }
3990 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003991 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003992 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003993 if (error)
3994 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 }
3996 else
3997 n1 = 0;
3998
3999 /*
4000 * Get the second variable.
4001 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004002 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
4003 {
Bram Moolenaara9749532021-03-06 18:18:19 +01004004 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004005 clear_tv(rettv);
4006 return FAIL;
4007 }
4008 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004009 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 return FAIL;
4011
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004012 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004014 if (var2.v_type == VAR_FLOAT)
4015 {
4016 if (!use_float)
4017 {
4018 f1 = n1;
4019 use_float = TRUE;
4020 }
4021 f2 = var2.vval.v_float;
4022 n2 = 0;
4023 }
4024 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004025 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004026 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004027 clear_tv(&var2);
4028 if (error)
4029 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004030 if (use_float)
4031 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033
4034 /*
4035 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004036 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004038 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004040 if (op == '*')
4041 f1 = f1 * f2;
4042 else if (op == '/')
4043 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004044#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004045 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004046 if (f2 == 0.0)
4047 {
4048 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004049 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004050 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004051 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004052 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004053 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004054 }
4055 else
4056 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004057#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004058 // We rely on the floating point library to handle divide
4059 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004060 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004061#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004064 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004065 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004066 return FAIL;
4067 }
4068 rettv->v_type = VAR_FLOAT;
4069 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 }
4071 else
4072 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004073 int failed = FALSE;
4074
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004075 if (op == '*')
4076 n1 = n1 * n2;
4077 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004078 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004080 n1 = num_modulus(n1, n2, &failed);
4081 if (failed)
4082 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01004083
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004084 rettv->v_type = VAR_NUMBER;
4085 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 }
4088 }
4089
4090 return OK;
4091}
4092
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004093/*
4094 * Handle a type cast before a base level expression.
4095 * "arg" must point to the first non-white of the expression.
4096 * "arg" is advanced to just after the recognized expression.
4097 * Return OK or FAIL.
4098 */
4099 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004100eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004101 char_u **arg,
4102 typval_T *rettv,
4103 evalarg_T *evalarg,
4104 int want_string) // after "." operator
4105{
4106 type_T *want_type = NULL;
4107 garray_T type_list; // list of pointers to allocated types
4108 int res;
4109 int evaluate = evalarg == NULL ? 0
4110 : (evalarg->eval_flags & EVAL_EVALUATE);
4111
4112 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02004113 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
4114 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004115 {
4116 ++*arg;
4117 ga_init2(&type_list, sizeof(type_T *), 10);
4118 want_type = parse_type(arg, &type_list, TRUE);
4119 if (want_type == NULL && (evaluate || **arg != '>'))
4120 {
4121 clear_type_list(&type_list);
4122 return FAIL;
4123 }
4124
4125 if (**arg != '>')
4126 {
4127 if (*skipwhite(*arg) == '>')
4128 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
4129 else
4130 emsg(_(e_missing_gt));
4131 clear_type_list(&type_list);
4132 return FAIL;
4133 }
4134 ++*arg;
4135 *arg = skipwhite_and_linebreak(*arg, evalarg);
4136 }
4137
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004138 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004139
4140 if (want_type != NULL && evaluate)
4141 {
4142 if (res == OK)
4143 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00004144 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
4145 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004146
Bram Moolenaar60dc8272021-07-29 22:48:54 +02004147 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004148 {
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02004149 if (want_type->tt_type == VAR_BOOL
4150 && actual->tt_type != VAR_BOOL
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004151 && (actual->tt_flags & TTFLAG_BOOL_OK))
4152 {
4153 int n = tv2bool(rettv);
4154
4155 // can use "0" and "1" for boolean in some places
4156 clear_tv(rettv);
4157 rettv->v_type = VAR_BOOL;
4158 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4159 }
4160 else
4161 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02004162 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004163
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004164 res = check_type(want_type, actual, TRUE, where);
4165 }
4166 }
4167 }
4168 clear_type_list(&type_list);
4169 }
4170
4171 return res;
4172}
4173
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004174 int
4175eval_leader(char_u **arg, int vim9)
4176{
4177 char_u *s = *arg;
4178 char_u *p = *arg;
4179
4180 while (*p == '!' || *p == '-' || *p == '+')
4181 {
4182 char_u *n = skipwhite(p + 1);
4183
4184 // ++, --, -+ and +- are not accepted in Vim9 script
4185 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
4186 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004187 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004188 return FAIL;
4189 }
4190 p = n;
4191 }
4192 *arg = p;
4193 return OK;
4194}
4195
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004197 * Check for a predefined value "true", "false" and "null.*".
4198 * Return OK when recognized.
4199 */
4200 int
4201handle_predefined(char_u *s, int len, typval_T *rettv)
4202{
4203 switch (len)
4204 {
4205 case 4: if (STRNCMP(s, "true", 4) == 0)
4206 {
4207 rettv->v_type = VAR_BOOL;
4208 rettv->vval.v_number = VVAL_TRUE;
4209 return OK;
4210 }
4211 if (STRNCMP(s, "null", 4) == 0)
4212 {
4213 rettv->v_type = VAR_SPECIAL;
4214 rettv->vval.v_number = VVAL_NULL;
4215 return OK;
4216 }
4217 break;
4218 case 5: if (STRNCMP(s, "false", 5) == 0)
4219 {
4220 rettv->v_type = VAR_BOOL;
4221 rettv->vval.v_number = VVAL_FALSE;
4222 return OK;
4223 }
4224 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004225 case 8: if (STRNCMP(s, "null_job", 8) == 0)
4226 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004227#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004228 rettv->v_type = VAR_JOB;
4229 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004230#else
4231 rettv->v_type = VAR_SPECIAL;
4232 rettv->vval.v_number = VVAL_NULL;
4233#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004234 return OK;
4235 }
4236 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004237 case 9:
4238 if (STRNCMP(s, "null_", 5) != 0)
4239 break;
4240 if (STRNCMP(s + 5, "list", 4) == 0)
4241 {
4242 rettv->v_type = VAR_LIST;
4243 rettv->vval.v_list = NULL;
4244 return OK;
4245 }
4246 if (STRNCMP(s + 5, "dict", 4) == 0)
4247 {
4248 rettv->v_type = VAR_DICT;
4249 rettv->vval.v_dict = NULL;
4250 return OK;
4251 }
4252 if (STRNCMP(s + 5, "blob", 4) == 0)
4253 {
4254 rettv->v_type = VAR_BLOB;
4255 rettv->vval.v_blob = NULL;
4256 return OK;
4257 }
4258 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004259 case 10: if (STRNCMP(s, "null_class", 10) == 0)
4260 {
4261 rettv->v_type = VAR_CLASS;
4262 rettv->vval.v_class = NULL;
4263 return OK;
4264 }
4265 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004266 case 11: if (STRNCMP(s, "null_string", 11) == 0)
4267 {
4268 rettv->v_type = VAR_STRING;
4269 rettv->vval.v_string = NULL;
4270 return OK;
4271 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004272 if (STRNCMP(s, "null_object", 11) == 0)
4273 {
4274 rettv->v_type = VAR_OBJECT;
4275 rettv->vval.v_object = NULL;
4276 return OK;
4277 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004278 break;
4279 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004280 if (STRNCMP(s, "null_channel", 12) == 0)
4281 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004282#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004283 rettv->v_type = VAR_CHANNEL;
4284 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004285#else
4286 rettv->v_type = VAR_SPECIAL;
4287 rettv->vval.v_number = VVAL_NULL;
4288#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004289 return OK;
4290 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004291 if (STRNCMP(s, "null_partial", 12) == 0)
4292 {
4293 rettv->v_type = VAR_PARTIAL;
4294 rettv->vval.v_partial = NULL;
4295 return OK;
4296 }
4297 break;
4298 case 13: if (STRNCMP(s, "null_function", 13) == 0)
4299 {
4300 rettv->v_type = VAR_FUNC;
4301 rettv->vval.v_string = NULL;
4302 return OK;
4303 }
4304 break;
4305 }
4306 return FAIL;
4307}
4308
4309/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 * Handle sixth level expression:
4311 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004312 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004313 * "string" string constant
4314 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 * &option-name option value
4316 * @r register contents
4317 * identifier variable value
4318 * function() function call
4319 * $VAR environment variable
4320 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004321 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004322 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004323 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004324 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 *
4326 * Also handle:
4327 * ! in front logical NOT
4328 * - in front unary minus
4329 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004330 * trailing [] subscript in String or List
4331 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004332 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 *
4334 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004335 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 *
4337 * Return OK or FAIL.
4338 */
4339 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004340eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004341 char_u **arg,
4342 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004343 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004346 int evaluate = evalarg != NULL
4347 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 int len;
4349 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004350 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 char_u *start_leader, *end_leader;
4352 int ret = OK;
4353 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004354 static int recurse = 0;
4355 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356
4357 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004358 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004359 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004361 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362
4363 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004364 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 */
4366 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004367 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004368 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 end_leader = *arg;
4370
Keith Thompson184f71c2024-01-04 21:19:04 +01004371 if (**arg == '.' && (!SAFE_isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004372 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004373 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004374 ++*arg;
4375 return FAIL;
4376 }
4377
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004378 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004379 // and crash. With MSVC the stack is smaller.
4380 if (recurse ==
4381#ifdef _MSC_VER
4382 300
4383#else
4384 1000
4385#endif
4386 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004387 {
4388 semsg(_(e_expression_too_recursive_str), *arg);
4389 return FAIL;
4390 }
4391 ++recurse;
4392
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 switch (**arg)
4394 {
4395 /*
4396 * Number constant.
4397 */
4398 case '0':
4399 case '1':
4400 case '2':
4401 case '3':
4402 case '4':
4403 case '5':
4404 case '6':
4405 case '7':
4406 case '8':
4407 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004408 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004409
4410 // Apply prefixed "-" and "+" now. Matters especially when
4411 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004412 if (ret == OK && evaluate && end_leader > start_leader
4413 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004414 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 break;
4416
4417 /*
4418 * String constant: "string".
4419 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004420 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 break;
4422
4423 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004424 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004426 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004427 break;
4428
4429 /*
4430 * List: [expr, expr]
4431 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004432 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 break;
4434
4435 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004436 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004437 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004438 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004439 {
4440 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4441 }
4442 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004443 {
4444 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004445 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004446 }
4447 else
4448 ret = NOTDONE;
4449 break;
4450
4451 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004452 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004453 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004454 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004455 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004456 ret = NOTDONE;
4457 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004458 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004459 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004460 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004461 break;
4462
4463 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004464 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004466 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 break;
4468
4469 /*
4470 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004471 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 */
LemonBoy2eaef102022-05-06 13:14:50 +01004473 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4474 ret = eval_interp_string(arg, rettv, evaluate);
4475 else
4476 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 break;
4478
4479 /*
4480 * Register contents: @r.
4481 */
4482 case '@': ++*arg;
4483 if (evaluate)
4484 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004485 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004486 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004487 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004488 emsg_invreg(**arg);
4489 else
4490 {
4491 rettv->v_type = VAR_STRING;
4492 rettv->vval.v_string = get_reg_contents(**arg,
4493 GREG_EXPR_SRC);
4494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 }
4496 if (**arg != NUL)
4497 ++*arg;
4498 break;
4499
4500 /*
4501 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004502 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004504 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004505 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004506 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004507 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004508 if (ret == OK && evaluate)
4509 {
4510 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4511
Bram Moolenaara9931532021-06-12 15:58:16 +02004512 // Compile it here to get the return type. The return
4513 // type is optional, when it's missing use t_unknown.
4514 // This is recognized in compile_return().
4515 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4516 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004517 if (compile_def_function(ufunc, FALSE,
4518 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004519 {
4520 clear_tv(rettv);
4521 ret = FAIL;
4522 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004523 }
4524 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004525 if (ret == NOTDONE)
4526 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004527 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004528 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004529
Bram Moolenaar9215f012020-06-27 21:18:00 +02004530 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004531 if (**arg == ')')
4532 ++*arg;
4533 else if (ret == OK)
4534 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004535 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004536 clear_tv(rettv);
4537 ret = FAIL;
4538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 }
4540 break;
4541
Bram Moolenaar8c711452005-01-14 21:53:12 +00004542 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 break;
4544 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004545
4546 if (ret == NOTDONE)
4547 {
4548 /*
4549 * Must be a variable or function name.
4550 * Can also be a curly-braces kind of name: {expr}.
4551 */
4552 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004553 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004554 if (alias != NULL)
4555 s = alias;
4556
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004557 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004558 ret = FAIL;
4559 else
4560 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004561 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4562
Bram Moolenaar4525a572022-02-13 11:57:33 +00004563 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004564 {
4565 emsg(_(e_cannot_use_underscore_here));
4566 ret = FAIL;
4567 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004568 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004569 && s[0] == 's' && s[1] == ':')
4570 {
4571 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4572 ret = FAIL;
4573 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004574 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004575 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004576 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004577 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004578 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004579 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004580 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004581 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004582 // get the value of "true", "false", etc. or a variable
4583 ret = FAIL;
4584 if (vim9script)
4585 ret = handle_predefined(s, len, rettv);
4586 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004587 {
4588 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004589 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004590 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004591 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004592 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004593 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004594 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004595 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004596 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004597 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004598 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004599 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004600 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004601 }
4602
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004603 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4604 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004605 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004606 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607
4608 /*
4609 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4610 */
4611 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004612 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004613
4614 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004615 return ret;
4616}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004617
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004618/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004619 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004620 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004621 * Adjusts "end_leaderp" until it is at "start_leader".
4622 */
4623 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004624eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004625 typval_T *rettv,
4626 int numeric_only,
4627 char_u *start_leader,
4628 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004629{
4630 char_u *end_leader = *end_leaderp;
4631 int ret = OK;
4632 int error = FALSE;
4633 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004634 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004635 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004636 float_T f = 0.0;
4637
4638 if (rettv->v_type == VAR_FLOAT)
4639 f = rettv->vval.v_float;
4640 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004641 {
4642 while (VIM_ISWHITE(end_leader[-1]))
4643 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004644 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004645 val = tv2bool(rettv);
4646 else
4647 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004648 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004649 if (error)
4650 {
4651 clear_tv(rettv);
4652 ret = FAIL;
4653 }
4654 else
4655 {
4656 while (end_leader > start_leader)
4657 {
4658 --end_leader;
4659 if (*end_leader == '!')
4660 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004661 if (numeric_only)
4662 {
4663 ++end_leader;
4664 break;
4665 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004666 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004667 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004668 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004669 {
4670 rettv->v_type = VAR_BOOL;
4671 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4672 }
4673 else
4674 f = !f;
4675 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004676 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004677 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004678 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004679 type = VAR_BOOL;
4680 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004681 }
4682 else if (*end_leader == '-')
4683 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004684 if (rettv->v_type == VAR_FLOAT)
4685 f = -f;
4686 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004687 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004688 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004689 type = VAR_NUMBER;
4690 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004691 }
4692 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004693 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004695 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004696 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004698 else
4699 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004700 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004701 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004702 rettv->v_type = type;
4703 else
4704 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004705 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004708 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 return ret;
4710}
4711
4712/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004713 * Call the function referred to in "rettv".
4714 */
4715 static int
4716call_func_rettv(
4717 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004718 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004719 typval_T *rettv,
4720 int evaluate,
4721 dict_T *selfdict,
4722 typval_T *basetv)
4723{
4724 partial_T *pt = NULL;
4725 funcexe_T funcexe;
4726 typval_T functv;
4727 char_u *s;
4728 int ret;
4729
4730 // need to copy the funcref so that we can clear rettv
4731 if (evaluate)
4732 {
4733 functv = *rettv;
4734 rettv->v_type = VAR_UNKNOWN;
4735
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004736 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004737 if (functv.v_type == VAR_PARTIAL)
4738 {
4739 pt = functv.vval.v_partial;
4740 s = partial_name(pt);
4741 }
4742 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004743 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004744 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004745 if (s == NULL || *s == NUL)
4746 {
4747 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004748 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004749 goto theend;
4750 }
4751 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004752 }
4753 else
4754 s = (char_u *)"";
4755
Bram Moolenaara80faa82020-04-12 19:37:17 +02004756 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004757 funcexe.fe_firstline = curwin->w_cursor.lnum;
4758 funcexe.fe_lastline = curwin->w_cursor.lnum;
4759 funcexe.fe_evaluate = evaluate;
4760 funcexe.fe_partial = pt;
4761 funcexe.fe_selfdict = selfdict;
4762 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004763 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004764
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004765theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004766 // Clear the funcref afterwards, so that deleting it while
4767 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004768 if (evaluate)
4769 clear_tv(&functv);
4770
4771 return ret;
4772}
4773
4774/*
4775 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004776 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004777 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4778 */
4779 static int
4780eval_lambda(
4781 char_u **arg,
4782 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004783 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004784 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004785{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004786 int evaluate = evalarg != NULL
4787 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004788 typval_T base = *rettv;
4789 int ret;
4790
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004791 rettv->v_type = VAR_UNKNOWN;
4792
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004793 if (**arg == '{')
4794 {
4795 // ->{lambda}()
4796 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4797 }
4798 else
4799 {
4800 // ->(lambda)()
4801 ++*arg;
4802 ret = eval1(arg, rettv, evalarg);
4803 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004804 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004805 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004806 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004807 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004808 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004809 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4810 && rettv->v_type != VAR_PARTIAL)
4811 {
4812 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4813 return FAIL;
4814 }
4815 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004816 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004817 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004818 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004819
4820 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004821 {
4822 if (verbose)
4823 {
4824 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004825 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004826 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004827 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004828 }
4829 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004830 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004831 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004832 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004833 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004834
4835 // Clear the funcref afterwards, so that deleting it while
4836 // evaluating the arguments is possible (see test55).
4837 if (evaluate)
4838 clear_tv(&base);
4839
4840 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004841}
4842
4843/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004844 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004845 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004846 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4847 */
4848 static int
4849eval_method(
4850 char_u **arg,
4851 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004852 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004853 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004854{
4855 char_u *name;
4856 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004857 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004858 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004859 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004860 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004861 int evaluate = evalarg != NULL
4862 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004863
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004864 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004865
Bram Moolenaarac92e252019-08-03 21:58:38 +02004866 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004867 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004868 if (alias != NULL)
4869 name = alias;
4870
4871 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004872 {
4873 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004874 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004875 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004876 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004877 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004878 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004879 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004880
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004881 // If there is no "(" immediately following, but there is further on,
4882 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4883 // Does not handle anything where "(" is part of the expression.
4884 *arg = skipwhite(*arg);
4885
4886 if (**arg != '(' && alias == NULL
4887 && (paren = vim_strchr(*arg, '(')) != NULL)
4888 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004889 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00004890
4891 // Truncate the name a the "(". Avoid trying to get another line
4892 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004893 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00004894 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
4895 if (evalarg != NULL)
4896 {
4897 getline = evalarg->eval_getline;
4898 evalarg->eval_getline = NULL;
4899 }
4900
4901 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004902 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004903 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004904 *arg = name + len;
4905 ret = FAIL;
4906 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004907 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004908 {
4909 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004910 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004911 }
Bram Moolenaar34820942022-12-19 20:28:38 +00004912
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004913 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00004914 if (getline != NULL)
4915 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004916 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004917
4918 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004919 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004920 *arg = skipwhite(*arg);
4921
4922 if (**arg != '(')
4923 {
4924 if (verbose)
4925 semsg(_(e_missing_parenthesis_str), name);
4926 ret = FAIL;
4927 }
4928 else if (VIM_ISWHITE((*arg)[-1]))
4929 {
4930 if (verbose)
4931 emsg(_(e_no_white_space_allowed_before_parenthesis));
4932 ret = FAIL;
4933 }
4934 else
4935 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004936 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004937 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004938 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004939
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004940 // Clear the funcref afterwards, so that deleting it while
4941 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004942 if (evaluate)
4943 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004944 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004945
Bram Moolenaarac92e252019-08-03 21:58:38 +02004946 return ret;
4947}
4948
4949/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004950 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4951 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004952 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4953 */
4954 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004955eval_index(
4956 char_u **arg,
4957 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004958 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004959 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004960{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004961 int evaluate = evalarg != NULL
4962 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004963 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004964 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004965 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004966 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004967 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004968 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004969
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004970 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4971 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004972
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004973 init_tv(&var1);
4974 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004975 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004976 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004977 /*
4978 * dict.name
4979 */
4980 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004981 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004982 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004983 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004984 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004985 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004986 }
4987 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004988 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004989 /*
4990 * something[idx]
4991 *
4992 * Get the (first) variable from inside the [].
4993 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004994 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004995 if (**arg == ':')
4996 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004997 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004998 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004999 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005000 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005001 semsg(_(e_white_space_required_before_and_after_str_at_str),
5002 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005003 clear_tv(&var1);
5004 return FAIL;
5005 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005006 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005007 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005008 int error = FALSE;
5009
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005010 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00005011 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005012 && var1.v_type == VAR_FLOAT)
5013 {
5014 var1.vval.v_string = typval_tostring(&var1, TRUE);
5015 var1.v_type = VAR_STRING;
5016 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01005017
Bram Moolenaar4525a572022-02-13 11:57:33 +00005018 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005019 tv_get_number_chk(&var1, &error);
5020 else
5021 error = tv_get_string_chk(&var1) == NULL;
5022 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005023 {
5024 // not a number or string
5025 clear_tv(&var1);
5026 return FAIL;
5027 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005028 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005029
5030 /*
5031 * Get the second variable from inside the [:].
5032 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005033 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005034 if (**arg == ':')
5035 {
5036 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005037 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005038 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005039 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005040 semsg(_(e_white_space_required_before_and_after_str_at_str),
5041 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005042 if (!empty1)
5043 clear_tv(&var1);
5044 return FAIL;
5045 }
5046 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005047 if (**arg == ']')
5048 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005049 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00005050 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005051 if (!empty1)
5052 clear_tv(&var1);
5053 return FAIL;
5054 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005055 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005056 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005057 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005058 if (!empty1)
5059 clear_tv(&var1);
5060 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005061 return FAIL;
5062 }
5063 }
5064
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005065 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005066 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005067 if (**arg != ']')
5068 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005069 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00005070 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005071 clear_tv(&var1);
5072 if (range)
5073 clear_tv(&var2);
5074 return FAIL;
5075 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02005076 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005077 }
5078
5079 if (evaluate)
5080 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005081 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005082 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005083 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005084
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005085 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005086 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005087 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005088 clear_tv(&var2);
5089 return res;
5090 }
5091 return OK;
5092}
5093
5094/*
5095 * Check if "rettv" can have an [index] or [sli:ce]
5096 */
5097 int
5098check_can_index(typval_T *rettv, int evaluate, int verbose)
5099{
5100 switch (rettv->v_type)
5101 {
5102 case VAR_FUNC:
5103 case VAR_PARTIAL:
5104 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005105 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005106 return FAIL;
5107 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005108 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005109 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005110 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005111 case VAR_BOOL:
5112 case VAR_SPECIAL:
5113 case VAR_JOB:
5114 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005115 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005116 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005117 if (verbose)
5118 emsg(_(e_cannot_index_special_variable));
5119 return FAIL;
Ernie Raele75fde62023-12-21 17:18:54 +01005120 case VAR_CLASS:
5121 case VAR_TYPEALIAS:
5122 if (verbose)
5123 check_typval_is_value(rettv);
5124 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005125 case VAR_UNKNOWN:
5126 case VAR_ANY:
5127 case VAR_VOID:
5128 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005129 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005130 emsg(_(e_cannot_index_special_variable));
5131 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005132 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005133 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005134
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005135 case VAR_STRING:
5136 case VAR_LIST:
5137 case VAR_DICT:
5138 case VAR_BLOB:
5139 break;
5140 case VAR_NUMBER:
5141 if (in_vim9script())
5142 emsg(_(e_cannot_index_number));
5143 break;
5144 }
5145 return OK;
5146}
5147
5148/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01005149 * slice() function
5150 */
5151 void
5152f_slice(typval_T *argvars, typval_T *rettv)
5153{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005154 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005155 && ((argvars[0].v_type != VAR_STRING
5156 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005157 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005158 && check_for_list_arg(argvars, 0) == FAIL)
5159 || check_for_number_arg(argvars, 1) == FAIL
5160 || check_for_opt_number_arg(argvars, 2) == FAIL))
5161 return;
5162
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005163 if (check_can_index(argvars, TRUE, FALSE) != OK)
5164 return;
5165
5166 copy_tv(argvars, rettv);
5167 eval_index_inner(rettv, TRUE, argvars + 1,
5168 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
5169 TRUE, NULL, 0, FALSE);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005170}
5171
5172/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005173 * Apply index or range to "rettv".
5174 * "var1" is the first index, NULL for [:expr].
5175 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01005176 * "exclusive" is TRUE for slice(): second index is exclusive, use character
5177 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005178 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
5179 */
5180 int
5181eval_index_inner(
5182 typval_T *rettv,
5183 int is_range,
5184 typval_T *var1,
5185 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005186 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005187 char_u *key,
5188 int keylen,
5189 int verbose)
5190{
Bram Moolenaar6601b622021-01-13 21:47:15 +01005191 varnumber_T n1, n2 = 0;
5192 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005193
5194 n1 = 0;
5195 if (var1 != NULL && rettv->v_type != VAR_DICT)
5196 n1 = tv_get_number(var1);
5197
5198 if (is_range)
5199 {
5200 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005201 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005202 if (verbose)
5203 emsg(_(e_cannot_slice_dictionary));
5204 return FAIL;
5205 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01005206 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005207 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005208 else
5209 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005210 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005211
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005212 switch (rettv->v_type)
5213 {
5214 case VAR_UNKNOWN:
5215 case VAR_ANY:
5216 case VAR_VOID:
5217 case VAR_FUNC:
5218 case VAR_PARTIAL:
5219 case VAR_FLOAT:
5220 case VAR_BOOL:
5221 case VAR_SPECIAL:
5222 case VAR_JOB:
5223 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005224 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005225 case VAR_CLASS:
5226 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02005227 case VAR_TYPEALIAS:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005228 break; // not evaluating, skipping over subscript
5229
5230 case VAR_NUMBER:
5231 case VAR_STRING:
5232 {
5233 char_u *s = tv_get_string(rettv);
5234
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005235 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005236 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005237 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005238 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005239 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005240 else
5241 s = char_from_string(s, n1);
5242 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005243 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005244 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005245 // The resulting variable is a substring. If the indexes
5246 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005247 if (n1 < 0)
5248 {
5249 n1 = len + n1;
5250 if (n1 < 0)
5251 n1 = 0;
5252 }
5253 if (n2 < 0)
5254 n2 = len + n2;
5255 else if (n2 >= len)
5256 n2 = len;
5257 if (n1 >= len || n2 < 0 || n1 > n2)
5258 s = NULL;
5259 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02005260 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005261 }
5262 else
5263 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005264 // The resulting variable is a string of a single
5265 // character. If the index is too big or negative the
5266 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005267 if (n1 >= len || n1 < 0)
5268 s = NULL;
5269 else
5270 s = vim_strnsave(s + n1, 1);
5271 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005272 clear_tv(rettv);
5273 rettv->v_type = VAR_STRING;
5274 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005275 }
5276 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005277
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005278 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005279 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
5280 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005281 break;
5282
5283 case VAR_LIST:
5284 if (var1 == NULL)
5285 n1 = 0;
5286 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005287 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005288 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005289 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005290 return FAIL;
5291 break;
5292
5293 case VAR_DICT:
5294 {
5295 dictitem_T *item;
5296 typval_T tmp;
5297
5298 if (key == NULL)
5299 {
5300 key = tv_get_string_chk(var1);
5301 if (key == NULL)
5302 return FAIL;
5303 }
5304
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005305 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005306
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005307 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005308 {
5309 if (verbose)
5310 {
5311 if (keylen > 0)
5312 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005313 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005314 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005315 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005316 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005317
5318 copy_tv(&item->di_tv, &tmp);
5319 clear_tv(rettv);
5320 *rettv = tmp;
5321 }
5322 break;
5323 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005324 return OK;
5325}
5326
5327/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005328 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005329 */
5330 char_u *
5331partial_name(partial_T *pt)
5332{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02005333 if (pt != NULL)
5334 {
5335 if (pt->pt_name != NULL)
5336 return pt->pt_name;
5337 if (pt->pt_func != NULL)
5338 return pt->pt_func->uf_name;
5339 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005340 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005341}
5342
Bram Moolenaarddecc252016-04-06 22:59:37 +02005343 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005344partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005345{
5346 int i;
5347
5348 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005349 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005350 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005351 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005352 if (pt->pt_name != NULL)
5353 {
5354 func_unref(pt->pt_name);
5355 vim_free(pt->pt_name);
5356 }
5357 else
5358 func_ptr_unref(pt->pt_func);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005359 object_unref(pt->pt_obj);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005360
Bram Moolenaar54656012021-06-09 20:50:46 +02005361 // "out_up" is no longer used, decrement refcount on partial that owns it.
5362 partial_unref(pt->pt_outer.out_up_partial);
5363
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005364 // Using pt_outer from another partial.
5365 partial_unref(pt->pt_outer_partial);
5366
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005367 // Decrease the reference count for the context of a closure. If down
5368 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005369 if (pt->pt_funcstack != NULL)
5370 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005371 --pt->pt_funcstack->fs_refcount;
5372 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005373 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005374 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005375 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5376 if (pt->pt_loopvars[i] != NULL)
5377 {
5378 --pt->pt_loopvars[i]->lvs_refcount;
5379 loopvars_check_refcount(pt->pt_loopvars[i]);
5380 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005381
Bram Moolenaarddecc252016-04-06 22:59:37 +02005382 vim_free(pt);
5383}
5384
5385/*
5386 * Unreference a closure: decrement the reference count and free it when it
5387 * becomes zero.
5388 */
5389 void
5390partial_unref(partial_T *pt)
5391{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005392 if (pt == NULL)
5393 return;
5394
5395 int done = FALSE;
5396
5397 if (--pt->pt_refcount <= 0)
5398 partial_free(pt);
5399
5400 // If the reference count goes down to one, the funcstack may be the
5401 // only reference and can be freed if no other partials reference it.
5402 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005403 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005404 // careful: if the funcstack is freed it may contain this partial
5405 // and it gets freed as well
5406 if (pt->pt_funcstack != NULL)
5407 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005408
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005409 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005410 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005411 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005412
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005413 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5414 if (pt->pt_loopvars[depth] != NULL
5415 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005416 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005417 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005418 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005419}
5420
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005421/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005422 * Return the next (unique) copy ID.
5423 * Used for serializing nested structures.
5424 */
5425 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005426get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005427{
5428 current_copyID += COPYID_INC;
5429 return current_copyID;
5430}
5431
5432/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005433 * Garbage collection for lists and dictionaries.
5434 *
5435 * We use reference counts to be able to free most items right away when they
5436 * are no longer used. But for composite items it's possible that it becomes
5437 * unused while the reference count is > 0: When there is a recursive
5438 * reference. Example:
5439 * :let l = [1, 2, 3]
5440 * :let d = {9: l}
5441 * :let l[1] = d
5442 *
5443 * Since this is quite unusual we handle this with garbage collection: every
5444 * once in a while find out which lists and dicts are not referenced from any
5445 * variable.
5446 *
5447 * Here is a good reference text about garbage collection (refers to Python
5448 * but it applies to all reference-counting mechanisms):
5449 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005450 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005451
5452/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005453 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005454 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005455 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005456 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005457 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005458garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005459{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005460 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005461 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005462 buf_T *buf;
5463 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005464 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005465 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005466
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005467 if (!testing)
5468 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005469 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005470 want_garbage_collect = FALSE;
5471 may_garbage_collect = FALSE;
5472 garbage_collect_at_exit = FALSE;
5473 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005474
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005475 // The execution stack can grow big, limit the size.
5476 if (exestack.ga_maxlen - exestack.ga_len > 500)
5477 {
5478 size_t new_len;
5479 char_u *pp;
5480 int n;
5481
5482 // Keep 150% of the current size, with a minimum of the growth size.
5483 n = exestack.ga_len / 2;
5484 if (n < exestack.ga_growsize)
5485 n = exestack.ga_growsize;
5486
5487 // Don't make it bigger though.
5488 if (exestack.ga_len + n < exestack.ga_maxlen)
5489 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005490 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005491 pp = vim_realloc(exestack.ga_data, new_len);
5492 if (pp == NULL)
5493 return FAIL;
5494 exestack.ga_maxlen = exestack.ga_len + n;
5495 exestack.ga_data = pp;
5496 }
5497 }
5498
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005499 // We advance by two because we add one for items referenced through
5500 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005501 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005502
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005503 /*
5504 * 1. Go through all accessible variables and mark all lists and dicts
5505 * with copyID.
5506 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005507
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005508 // Don't free variables in the previous_funccal list unless they are only
5509 // referenced through previous_funccal. This must be first, because if
5510 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005511 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005512
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005513 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005514 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005515
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005516 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005517 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005518 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5519 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005520
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005521 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005522 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005523 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5524 NULL, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005525 // window-local variables in autocmd windows
Bram Moolenaare76062c2022-11-28 18:51:43 +00005526 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005527 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00005528 abort = abort || set_ref_in_item(
5529 &aucmd_win[i].auc_win->w_winvar.di_tv, copyID, NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005530#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005531 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005532 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5533 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005534 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005535 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005536 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5537 NULL, NULL);
5538#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005539
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005540 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005541 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005542 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5543 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005544 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005545 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005546
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005547 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005548 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005549
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005550 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005551 abort = abort || set_ref_in_functions(copyID);
5552
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005553 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005554 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005555
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005556 // funcstacks keep variables for closures
5557 abort = abort || set_ref_in_funcstacks(copyID);
5558
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005559 // loopvars keep variables for loop blocks
5560 abort = abort || set_ref_in_loopvars(copyID);
5561
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005562 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005563 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005564
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005565 // callbacks in buffers
5566 abort = abort || set_ref_in_buffers(copyID);
5567
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005568 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5569 abort = abort || set_ref_in_insexpand_funcs(copyID);
5570
5571 // 'operatorfunc' callback
5572 abort = abort || set_ref_in_opfunc(copyID);
5573
5574 // 'tagfunc' callback
5575 abort = abort || set_ref_in_tagfunc(copyID);
5576
5577 // 'imactivatefunc' and 'imstatusfunc' callbacks
5578 abort = abort || set_ref_in_im_funcs(copyID);
5579
Bram Moolenaar1dced572012-04-05 16:54:08 +02005580#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005581 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005582#endif
5583
Bram Moolenaardb913952012-06-29 12:54:53 +02005584#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005585 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005586#endif
5587
5588#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005589 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005590#endif
5591
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005592#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005593 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005594 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005595#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005596#ifdef FEAT_NETBEANS_INTG
5597 abort = abort || set_ref_in_nb_channel(copyID);
5598#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005599
Bram Moolenaare3188e22016-05-31 21:13:04 +02005600#ifdef FEAT_TIMERS
5601 abort = abort || set_ref_in_timer(copyID);
5602#endif
5603
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005604#ifdef FEAT_QUICKFIX
5605 abort = abort || set_ref_in_quickfix(copyID);
5606#endif
5607
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005608#ifdef FEAT_TERMINAL
5609 abort = abort || set_ref_in_term(copyID);
5610#endif
5611
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005612#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005613 abort = abort || set_ref_in_popups(copyID);
5614#endif
5615
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005616 abort = abort || set_ref_in_classes(copyID);
5617
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005618 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005619 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005620 /*
5621 * 2. Free lists and dictionaries that are not referenced.
5622 */
5623 did_free = free_unref_items(copyID);
5624
5625 /*
5626 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005627 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005628 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005629 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005630 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005631 else if (p_verbose > 0)
5632 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005633 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005634 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005635
5636 return did_free;
5637}
5638
5639/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005640 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005641 */
5642 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005643free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005644{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005645 int did_free = FALSE;
5646
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005647 // Let all "free" functions know that we are here. This means no
5648 // dictionaries, lists, channels or jobs are to be freed, because we will
5649 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005650 in_free_unref_items = TRUE;
5651
5652 /*
5653 * PASS 1: free the contents of the items. We don't free the items
5654 * themselves yet, so that it is possible to decrement refcount counters
5655 */
5656
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005657 // Go through the list of dicts and free items without this copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005658 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005659
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005660 // Go through the list of lists and free items without this copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005661 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005662
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005663 // Go through the list of objects and free items without this copyID.
5664 did_free |= object_free_nonref(copyID);
5665
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005666 // Go through the list of classes and free items without this copyID.
5667 did_free |= class_free_nonref(copyID);
5668
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005669#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005670 // Go through the list of jobs and free items without the copyID. This
5671 // must happen before doing channels, because jobs refer to channels, but
5672 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005673 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5674
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005675 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005676 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5677#endif
5678
5679 /*
5680 * PASS 2: free the items themselves.
5681 */
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005682 object_free_items(copyID);
Bram Moolenaarcd524592016-07-17 14:57:05 +02005683 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005684 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005685
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005686#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005687 // Go through the list of jobs and free items without the copyID. This
5688 // must happen before doing channels, because jobs refer to channels, but
5689 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005690 free_unused_jobs(copyID, COPYID_MASK);
5691
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005692 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005693 free_unused_channels(copyID, COPYID_MASK);
5694#endif
5695
5696 in_free_unref_items = FALSE;
5697
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005698 return did_free;
5699}
5700
5701/*
5702 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005703 * "list_stack" is used to add lists to be marked. Can be NULL.
5704 *
5705 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005706 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005707 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005708set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005709{
5710 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005711 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005712 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005713 hashtab_T *cur_ht;
5714 ht_stack_T *ht_stack = NULL;
5715 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005716
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005717 cur_ht = ht;
5718 for (;;)
5719 {
5720 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005721 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005722 // Mark each item in the hashtab. If the item contains a hashtab
5723 // it is added to ht_stack, if it contains a list it is added to
5724 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005725 todo = (int)cur_ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00005726 FOR_ALL_HASHTAB_ITEMS(cur_ht, hi, todo)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005727 if (!HASHITEM_EMPTY(hi))
5728 {
5729 --todo;
5730 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5731 &ht_stack, list_stack);
5732 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005733 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005734
5735 if (ht_stack == NULL)
5736 break;
5737
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005738 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005739 cur_ht = ht_stack->ht;
5740 tempitem = ht_stack;
5741 ht_stack = ht_stack->prev;
5742 free(tempitem);
5743 }
5744
5745 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005746}
5747
Dominique Pelle748b3082022-01-08 12:41:16 +00005748#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5749 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005750/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005751 * Mark a dict and its items with "copyID".
5752 * Returns TRUE if setting references failed somehow.
5753 */
5754 int
5755set_ref_in_dict(dict_T *d, int copyID)
5756{
5757 if (d != NULL && d->dv_copyID != copyID)
5758 {
5759 d->dv_copyID = copyID;
5760 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5761 }
5762 return FALSE;
5763}
Dominique Pelle748b3082022-01-08 12:41:16 +00005764#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005765
5766/*
5767 * Mark a list and its items with "copyID".
5768 * Returns TRUE if setting references failed somehow.
5769 */
5770 int
5771set_ref_in_list(list_T *ll, int copyID)
5772{
5773 if (ll != NULL && ll->lv_copyID != copyID)
5774 {
5775 ll->lv_copyID = copyID;
5776 return set_ref_in_list_items(ll, copyID, NULL);
5777 }
5778 return FALSE;
5779}
5780
5781/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005782 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005783 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5784 *
5785 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005786 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005787 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005788set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005789{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005790 listitem_T *li;
5791 int abort = FALSE;
5792 list_T *cur_l;
5793 list_stack_T *list_stack = NULL;
5794 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005795
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005796 cur_l = l;
5797 for (;;)
5798 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005799 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005800 // Mark each item in the list. If the item contains a hashtab
5801 // it is added to ht_stack, if it contains a list it is added to
5802 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005803 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5804 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5805 ht_stack, &list_stack);
5806 if (list_stack == NULL)
5807 break;
5808
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005809 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005810 cur_l = list_stack->list;
5811 tempitem = list_stack;
5812 list_stack = list_stack->prev;
5813 free(tempitem);
5814 }
5815
5816 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005817}
5818
5819/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005820 * Mark the partial in callback 'cb' with "copyID".
5821 */
5822 int
5823set_ref_in_callback(callback_T *cb, int copyID)
5824{
5825 typval_T tv;
5826
5827 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5828 return FALSE;
5829
5830 tv.v_type = VAR_PARTIAL;
5831 tv.vval.v_partial = cb->cb_partial;
5832 return set_ref_in_item(&tv, copyID, NULL, NULL);
5833}
5834
5835/*
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005836 * Mark the dict "dd" with "copyID".
5837 * Also see set_ref_in_item().
5838 */
5839 static int
5840set_ref_in_item_dict(
5841 dict_T *dd,
5842 int copyID,
5843 ht_stack_T **ht_stack,
5844 list_stack_T **list_stack)
5845{
5846 if (dd == NULL || dd->dv_copyID == copyID)
5847 return FALSE;
5848
5849 // Didn't see this dict yet.
5850 dd->dv_copyID = copyID;
5851 if (ht_stack == NULL)
5852 return set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5853
5854 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5855 if (newitem == NULL)
5856 return TRUE;
5857
5858 newitem->ht = &dd->dv_hashtab;
5859 newitem->prev = *ht_stack;
5860 *ht_stack = newitem;
5861
5862 return FALSE;
5863}
5864
5865/*
5866 * Mark the list "ll" with "copyID".
5867 * Also see set_ref_in_item().
5868 */
5869 static int
5870set_ref_in_item_list(
5871 list_T *ll,
5872 int copyID,
5873 ht_stack_T **ht_stack,
5874 list_stack_T **list_stack)
5875{
5876 if (ll == NULL || ll->lv_copyID == copyID)
5877 return FALSE;
5878
5879 // Didn't see this list yet.
5880 ll->lv_copyID = copyID;
5881 if (list_stack == NULL)
5882 return set_ref_in_list_items(ll, copyID, ht_stack);
5883
5884 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5885 if (newitem == NULL)
5886 return TRUE;
5887
5888 newitem->list = ll;
5889 newitem->prev = *list_stack;
5890 *list_stack = newitem;
5891
5892 return FALSE;
5893}
5894
5895/*
5896 * Mark the partial "pt" with "copyID".
5897 * Also see set_ref_in_item().
5898 */
5899 static int
5900set_ref_in_item_partial(
5901 partial_T *pt,
5902 int copyID,
5903 ht_stack_T **ht_stack,
5904 list_stack_T **list_stack)
5905{
5906 if (pt == NULL || pt->pt_copyID == copyID)
5907 return FALSE;
5908
5909 // Didn't see this partial yet.
5910 pt->pt_copyID = copyID;
5911
5912 int abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
5913
5914 if (pt->pt_dict != NULL)
5915 {
5916 typval_T dtv;
5917
5918 dtv.v_type = VAR_DICT;
5919 dtv.vval.v_dict = pt->pt_dict;
5920 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5921 }
5922
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005923 if (pt->pt_obj != NULL)
5924 {
5925 typval_T objtv;
5926
5927 objtv.v_type = VAR_OBJECT;
5928 objtv.vval.v_object = pt->pt_obj;
5929 set_ref_in_item(&objtv, copyID, ht_stack, list_stack);
5930 }
5931
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005932 for (int i = 0; i < pt->pt_argc; ++i)
5933 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5934 ht_stack, list_stack);
5935 // pt_funcstack is handled in set_ref_in_funcstacks()
5936 // pt_loopvars is handled in set_ref_in_loopvars()
5937
5938 return abort;
5939}
5940
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005941#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005942/*
5943 * Mark the job "pt" with "copyID".
5944 * Also see set_ref_in_item().
5945 */
5946 static int
5947set_ref_in_item_job(
5948 job_T *job,
5949 int copyID,
5950 ht_stack_T **ht_stack,
5951 list_stack_T **list_stack)
5952{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005953 typval_T dtv;
5954
5955 if (job == NULL || job->jv_copyID == copyID)
5956 return FALSE;
5957
5958 job->jv_copyID = copyID;
5959 if (job->jv_channel != NULL)
5960 {
5961 dtv.v_type = VAR_CHANNEL;
5962 dtv.vval.v_channel = job->jv_channel;
5963 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5964 }
5965 if (job->jv_exit_cb.cb_partial != NULL)
5966 {
5967 dtv.v_type = VAR_PARTIAL;
5968 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
5969 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5970 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005971
5972 return FALSE;
5973}
5974
5975/*
5976 * Mark the channel "ch" with "copyID".
5977 * Also see set_ref_in_item().
5978 */
5979 static int
5980set_ref_in_item_channel(
5981 channel_T *ch,
5982 int copyID,
5983 ht_stack_T **ht_stack,
5984 list_stack_T **list_stack)
5985{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005986 typval_T dtv;
5987
5988 if (ch == NULL || ch->ch_copyID == copyID)
5989 return FALSE;
5990
5991 ch->ch_copyID = copyID;
5992 for (ch_part_T part = PART_SOCK; part < PART_COUNT; ++part)
5993 {
5994 for (jsonq_T *jq = ch->ch_part[part].ch_json_head.jq_next;
5995 jq != NULL; jq = jq->jq_next)
5996 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5997 for (cbq_T *cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5998 cq = cq->cq_next)
5999 if (cq->cq_callback.cb_partial != NULL)
6000 {
6001 dtv.v_type = VAR_PARTIAL;
6002 dtv.vval.v_partial = cq->cq_callback.cb_partial;
6003 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6004 }
6005 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
6006 {
6007 dtv.v_type = VAR_PARTIAL;
6008 dtv.vval.v_partial = ch->ch_part[part].ch_callback.cb_partial;
6009 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6010 }
6011 }
6012 if (ch->ch_callback.cb_partial != NULL)
6013 {
6014 dtv.v_type = VAR_PARTIAL;
6015 dtv.vval.v_partial = ch->ch_callback.cb_partial;
6016 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6017 }
6018 if (ch->ch_close_cb.cb_partial != NULL)
6019 {
6020 dtv.v_type = VAR_PARTIAL;
6021 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
6022 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6023 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006024
6025 return FALSE;
6026}
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006027#endif
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006028
6029/*
6030 * Mark the class "cl" with "copyID".
6031 * Also see set_ref_in_item().
6032 */
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02006033 int
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006034set_ref_in_item_class(
6035 class_T *cl,
6036 int copyID,
6037 ht_stack_T **ht_stack,
6038 list_stack_T **list_stack)
6039{
6040 int abort = FALSE;
6041
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02006042 if (cl == NULL || cl->class_copyID == copyID)
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006043 return FALSE;
6044
6045 cl->class_copyID = copyID;
Yegappan Lakshmanan544be0d2023-09-04 22:14:28 +02006046 if (cl->class_members_tv != NULL)
6047 {
6048 // The "class_members_tv" table is allocated only for regular classes
6049 // and not for interfaces.
6050 for (int i = 0; !abort && i < cl->class_class_member_count; ++i)
6051 abort = abort || set_ref_in_item(
6052 &cl->class_members_tv[i],
6053 copyID, ht_stack, list_stack);
6054 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006055
6056 for (int i = 0; !abort && i < cl->class_class_function_count; ++i)
6057 abort = abort || set_ref_in_func(NULL,
6058 cl->class_class_functions[i], copyID);
6059
6060 for (int i = 0; !abort && i < cl->class_obj_method_count; ++i)
6061 abort = abort || set_ref_in_func(NULL,
6062 cl->class_obj_methods[i], copyID);
6063
6064 return abort;
6065}
6066
6067/*
6068 * Mark the object "cl" with "copyID".
6069 * Also see set_ref_in_item().
6070 */
6071 static int
6072set_ref_in_item_object(
6073 object_T *obj,
6074 int copyID,
6075 ht_stack_T **ht_stack,
6076 list_stack_T **list_stack)
6077{
6078 int abort = FALSE;
6079
6080 if (obj == NULL || obj->obj_copyID == copyID)
6081 return FALSE;
6082
6083 obj->obj_copyID = copyID;
6084
6085 // The typval_T array is right after the object_T.
6086 typval_T *mtv = (typval_T *)(obj + 1);
6087 for (int i = 0; !abort
6088 && i < obj->obj_class->class_obj_member_count; ++i)
6089 abort = abort || set_ref_in_item(mtv + i, copyID,
6090 ht_stack, list_stack);
6091
6092 return abort;
6093}
6094
6095/*
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006096 * Mark all lists, dicts and other container types referenced through typval
6097 * "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006098 * "list_stack" is used to add lists to be marked. Can be NULL.
6099 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6100 *
6101 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006102 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006103 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006104set_ref_in_item(
6105 typval_T *tv,
6106 int copyID,
6107 ht_stack_T **ht_stack,
6108 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006109{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006110 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006111
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006112 switch (tv->v_type)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006113 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006114 case VAR_DICT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006115 return set_ref_in_item_dict(tv->vval.v_dict, copyID,
6116 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006117
6118 case VAR_LIST:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006119 return set_ref_in_item_list(tv->vval.v_list, copyID,
6120 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006121
6122 case VAR_FUNC:
6123 {
6124 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
6125 break;
6126 }
6127
6128 case VAR_PARTIAL:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006129 return set_ref_in_item_partial(tv->vval.v_partial, copyID,
6130 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006131
6132 case VAR_JOB:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006133#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006134 return set_ref_in_item_job(tv->vval.v_job, copyID,
6135 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006136#else
6137 break;
6138#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006139
6140 case VAR_CHANNEL:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006141#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006142 return set_ref_in_item_channel(tv->vval.v_channel, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006143 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006144#else
6145 break;
6146#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006147
6148 case VAR_CLASS:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006149 return set_ref_in_item_class(tv->vval.v_class, copyID,
6150 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006151
6152 case VAR_OBJECT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006153 return set_ref_in_item_object(tv->vval.v_object, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006154 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006155
6156 case VAR_UNKNOWN:
6157 case VAR_ANY:
6158 case VAR_VOID:
6159 case VAR_BOOL:
6160 case VAR_SPECIAL:
6161 case VAR_NUMBER:
6162 case VAR_FLOAT:
6163 case VAR_STRING:
6164 case VAR_BLOB:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006165 case VAR_TYPEALIAS:
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006166 case VAR_INSTR:
6167 // Types that do not contain any other item
6168 break;
6169 }
6170
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006171 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006172}
6173
Bram Moolenaar8c711452005-01-14 21:53:12 +00006174/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006175 * Return a string with the string representation of a variable.
6176 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006177 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006178 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006179 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006180 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02006181 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006182 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6183 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006184 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006185 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006186 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006187echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006188 typval_T *tv,
6189 char_u **tofree,
6190 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006191 int copyID,
6192 int echo_style,
6193 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006194 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006195{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006196 static int recurse = 0;
6197 char_u *r = NULL;
6198
Bram Moolenaar33570922005-01-25 22:26:29 +00006199 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006200 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006201 if (!did_echo_string_emsg)
6202 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006203 // Only give this message once for a recursive call to avoid
6204 // flooding the user with errors. And stop iterating over lists
6205 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02006206 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00006207 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006208 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006209 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006210 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006211 }
6212 ++recurse;
6213
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006214 switch (tv->v_type)
6215 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006216 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006217 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006218 {
6219 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006220 r = tv->vval.v_string;
6221 if (r == NULL)
6222 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006223 }
6224 else
6225 {
6226 *tofree = string_quote(tv->vval.v_string, FALSE);
6227 r = *tofree;
6228 }
6229 break;
6230
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006231 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006232 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01006233 char_u buf[MAX_FUNC_NAME_LEN];
6234
6235 if (echo_style)
6236 {
LemonBoya5d35902022-04-29 21:15:02 +01006237 r = tv->vval.v_string == NULL ? (char_u *)"function()"
6238 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01006239 buf, MAX_FUNC_NAME_LEN);
6240 if (r == buf)
6241 {
6242 r = vim_strsave(buf);
6243 *tofree = r;
6244 }
6245 else
6246 *tofree = NULL;
6247 }
6248 else
6249 {
6250 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
6251 : make_ufunc_name_readable(
6252 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
6253 TRUE);
6254 r = *tofree;
6255 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006256 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006257 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006258
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006259 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006260 {
6261 partial_T *pt = tv->vval.v_partial;
6262 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006263 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006264 garray_T ga;
6265 int i;
6266 char_u *tf;
6267
6268 ga_init2(&ga, 1, 100);
6269 ga_concat(&ga, (char_u *)"function(");
6270 if (fname != NULL)
6271 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006272 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00006273 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006274 && vim_isupper(fname[1]))
6275 {
6276 ga_concat(&ga, (char_u *)"'g:");
6277 ga_concat(&ga, fname + 1);
6278 }
6279 else
6280 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006281 vim_free(fname);
6282 }
6283 if (pt != NULL && pt->pt_argc > 0)
6284 {
6285 ga_concat(&ga, (char_u *)", [");
6286 for (i = 0; i < pt->pt_argc; ++i)
6287 {
6288 if (i > 0)
6289 ga_concat(&ga, (char_u *)", ");
6290 ga_concat(&ga,
6291 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6292 vim_free(tf);
6293 }
6294 ga_concat(&ga, (char_u *)"]");
6295 }
6296 if (pt != NULL && pt->pt_dict != NULL)
6297 {
6298 typval_T dtv;
6299
6300 ga_concat(&ga, (char_u *)", ");
6301 dtv.v_type = VAR_DICT;
6302 dtv.vval.v_dict = pt->pt_dict;
6303 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6304 vim_free(tf);
6305 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00006306 // terminate with ')' and a NUL
6307 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006308
6309 *tofree = ga.ga_data;
6310 r = *tofree;
6311 break;
6312 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006313
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006314 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006315 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006316 break;
6317
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006318 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006319 if (tv->vval.v_list == NULL)
6320 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02006321 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006322 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02006323 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006324 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006325 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6326 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006327 {
6328 *tofree = NULL;
6329 r = (char_u *)"[...]";
6330 }
6331 else
6332 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006333 int old_copyID = tv->vval.v_list->lv_copyID;
6334
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006335 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006336 *tofree = list2string(tv, copyID, restore_copyID);
6337 if (restore_copyID)
6338 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006339 r = *tofree;
6340 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006341 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006342
Bram Moolenaar8c711452005-01-14 21:53:12 +00006343 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006344 if (tv->vval.v_dict == NULL)
6345 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006346 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006347 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006348 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006349 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006350 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6351 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006352 {
6353 *tofree = NULL;
6354 r = (char_u *)"{...}";
6355 }
6356 else
6357 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006358 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006359
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006360 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006361 *tofree = dict2string(tv, copyID, restore_copyID);
6362 if (restore_copyID)
6363 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006364 r = *tofree;
6365 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006366 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006367
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006368 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006369 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006370 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006371 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006372 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006373 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006374 break;
6375
Bram Moolenaar835dc632016-02-07 14:27:38 +01006376 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006377 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006378#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00006379 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02006380 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
6381 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006382 if (composite_val)
6383 {
6384 *tofree = string_quote(r, FALSE);
6385 r = *tofree;
6386 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006387#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006388 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006389
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006390 case VAR_INSTR:
6391 *tofree = NULL;
6392 r = (char_u *)"instructions";
6393 break;
6394
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006395 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006396 {
6397 class_T *cl = tv->vval.v_class;
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01006398 char *s = "class";
6399 if (IS_INTERFACE(cl))
6400 s = "interface";
6401 else if (IS_ENUM(cl))
6402 s = "enum";
6403 size_t len = STRLEN(s) + 1 +
6404 (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006405 r = *tofree = alloc(len);
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01006406 vim_snprintf((char *)r, len, "%s %s", s,
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006407 cl == NULL ? "[unknown]" : (char *)cl->class_name);
6408 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006409 break;
6410
6411 case VAR_OBJECT:
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01006412 *tofree = r = object_string(tv->vval.v_object, numbuf, copyID,
6413 echo_style, restore_copyID,
6414 composite_val);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006415 break;
6416
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006417 case VAR_FLOAT:
6418 *tofree = NULL;
6419 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6420 r = numbuf;
6421 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006422
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006423 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006424 case VAR_SPECIAL:
6425 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006426 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006427 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006428
6429 case VAR_TYPEALIAS:
6430 *tofree = vim_strsave(tv->vval.v_typealias->ta_name);
6431 r = *tofree;
6432 if (r == NULL)
6433 r = (char_u *)"";
6434 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006435 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006436
Bram Moolenaar8502c702014-06-17 12:51:16 +02006437 if (--recurse == 0)
6438 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006439 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006440}
6441
6442/*
6443 * Return a string with the string representation of a variable.
6444 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6445 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006446 * Does not put quotes around strings, as ":echo" displays values.
6447 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6448 * May return NULL.
6449 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006450 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006451echo_string(
6452 typval_T *tv,
6453 char_u **tofree,
6454 char_u *numbuf,
6455 int copyID)
6456{
6457 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6458}
6459
6460/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006461 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6462 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006463 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006464 */
6465 int
6466buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6467{
6468 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006469 char_u *t;
6470 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006471
6472 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6473 return -1;
6474
6475 if (lnum > buf->b_ml.ml_line_count)
6476 lnum = buf->b_ml.ml_line_count;
6477
6478 str = ml_get_buf(buf, lnum, FALSE);
6479 if (str == NULL)
6480 return -1;
6481
6482 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006483 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006484
Bram Moolenaar91458462021-01-13 20:08:38 +01006485 // count the number of characters
6486 t = str;
6487 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6488 t += mb_ptr2len(t);
6489
6490 // In insert mode, when the cursor is at the end of a non-empty line,
6491 // byteidx points to the NUL character immediately past the end of the
6492 // string. In this case, add one to the character count.
6493 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6494 count++;
6495
6496 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006497}
6498
6499/*
6500 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006501 * byte index. Works only for loaded buffers. Returns -1 on failure.
6502 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006503 */
6504 int
6505buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6506{
6507 char_u *str;
6508 char_u *t;
6509
6510 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6511 return -1;
6512
6513 if (lnum > buf->b_ml.ml_line_count)
6514 lnum = buf->b_ml.ml_line_count;
6515
6516 str = ml_get_buf(buf, lnum, FALSE);
6517 if (str == NULL)
6518 return -1;
6519
6520 // Convert the character offset to a byte offset
6521 t = str;
6522 while (*t != NUL && --charidx > 0)
6523 t += mb_ptr2len(t);
6524
Bram Moolenaar91458462021-01-13 20:08:38 +01006525 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006526}
6527
6528/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006530 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006532 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006533var2fpos(
6534 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006535 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006536 int *fnum, // set to fnum for '0, 'A, etc.
6537 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006539 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006541 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006543 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006544 if (varp->v_type == VAR_LIST)
6545 {
6546 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006547 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006548 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006549 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006550
6551 l = varp->vval.v_list;
6552 if (l == NULL)
6553 return NULL;
6554
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006555 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006556 pos.lnum = list_find_nr(l, 0L, &error);
6557 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006558 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006559 if (charcol)
6560 len = (long)mb_charlen(ml_get(pos.lnum));
6561 else
John Marriottbfcc8952024-03-11 22:04:45 +01006562 len = (long)ml_get_len(pos.lnum);
Bram Moolenaar477933c2007-07-17 14:32:23 +00006563
Bram Moolenaarec65d772020-08-20 22:29:12 +02006564 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006565 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006566 li = list_find(l, 1L);
6567 if (li != NULL && li->li_tv.v_type == VAR_STRING
6568 && li->li_tv.vval.v_string != NULL
6569 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006570 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006571 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006572 }
6573 else
6574 {
6575 pos.col = list_find_nr(l, 1L, &error);
6576 if (error)
6577 return NULL;
6578 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006579
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006580 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006581 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006582 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006583 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006584
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006585 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006586 pos.coladd = list_find_nr(l, 2L, &error);
6587 if (error)
6588 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006589
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006590 return &pos;
6591 }
6592
Bram Moolenaarc5809432021-03-27 21:23:30 +01006593 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6594 return NULL;
6595
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006596 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006597 if (name == NULL)
6598 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006599
6600 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006601 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006602 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006603 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006604 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006605 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006606 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006607 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006608 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006609 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006610 pos = VIsual;
6611 else
6612 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006613 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006614 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006615 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006617 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006618 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6620 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006621 pos = *pp;
6622 }
6623 if (pos.lnum != 0)
6624 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006625 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006626 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6627 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006629
Bram Moolenaara5525202006-03-02 22:52:09 +00006630 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006631
Bram Moolenaar477933c2007-07-17 14:32:23 +00006632 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006633 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006634 // the "w_valid" flags are not reset when moving the cursor, but they
6635 // do matter for update_topline() and validate_botline().
6636 check_cursor_moved(curwin);
6637
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006638 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006639 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006640 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006641 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006642 // In silent Ex mode topline is zero, but that's not a valid line
6643 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006644 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006645 return &pos;
6646 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006647 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006648 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006649 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006650 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006651 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006652 return &pos;
6653 }
6654 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006655 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006657 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 {
6659 pos.lnum = curbuf->b_ml.ml_line_count;
6660 pos.col = 0;
6661 }
6662 else
6663 {
6664 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006665 if (charcol)
6666 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6667 else
John Marriottbfcc8952024-03-11 22:04:45 +01006668 pos.col = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 }
6670 return &pos;
6671 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006672 if (in_vim9script())
6673 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 return NULL;
6675}
6676
6677/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006678 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006679 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006680 * Note that the column is passed on as-is, the caller may want to decrement
6681 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006682 * If "charcol" is TRUE use the column as the character index instead of the
6683 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006684 * Return FAIL when conversion is not possible, doesn't check the position for
6685 * validity.
6686 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006687 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006688list2fpos(
6689 typval_T *arg,
6690 pos_T *posp,
6691 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006692 colnr_T *curswantp,
6693 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006694{
6695 list_T *l = arg->vval.v_list;
6696 long i = 0;
6697 long n;
6698
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006699 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6700 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006701 if (arg->v_type != VAR_LIST
6702 || l == NULL
6703 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006704 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006705 return FAIL;
6706
6707 if (fnump != NULL)
6708 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006709 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006710 if (n < 0)
6711 return FAIL;
6712 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006713 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006714 *fnump = n;
6715 }
6716
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006717 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006718 if (n < 0)
6719 return FAIL;
6720 posp->lnum = n;
6721
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006722 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006723 if (n < 0)
6724 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006725 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006726 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006727 if (charcol)
6728 {
6729 buf_T *buf;
6730
6731 // Get the text for the specified line in a loaded buffer
6732 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6733 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6734 return FAIL;
6735
Bram Moolenaar79f23442022-10-10 12:42:57 +01006736 n = buf_charidx_to_byteidx(buf,
6737 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006738 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006739 posp->col = n;
6740
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006741 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006742 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006743 posp->coladd = 0;
6744 else
6745 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006746
Bram Moolenaar493c1782014-05-28 14:34:46 +02006747 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006748 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006749
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006750 return OK;
6751}
6752
6753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 * Get the length of an environment variable name.
6755 * Advance "arg" to the first character after the name.
6756 * Return 0 for error.
6757 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006758 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006759get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760{
6761 char_u *p;
6762 int len;
6763
6764 for (p = *arg; vim_isIDc(*p); ++p)
6765 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006766 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 return 0;
6768
6769 len = (int)(p - *arg);
6770 *arg = p;
6771 return len;
6772}
6773
6774/*
6775 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006776 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 * Return 0 if something is wrong.
6778 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006779 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006780get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781{
6782 char_u *p;
6783 int len;
6784
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006785 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006787 {
6788 if (*p == ':')
6789 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006790 // "s:" is start of "s:var", but "n:" is not and can be used in
6791 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006792 len = (int)(p - *arg);
6793 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6794 || len > 1)
6795 break;
6796 }
6797 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006798 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 return 0;
6800
6801 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006802 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803
6804 return len;
6805}
6806
6807/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006808 * Get the length of the name of a variable or function.
6809 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006811 * Return -1 if curly braces expansion failed.
6812 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 * If the name contains 'magic' {}'s, expand them and return the
6814 * expanded name in an allocated string via 'alias' - caller must free.
6815 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006816 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006817get_name_len(
6818 char_u **arg,
6819 char_u **alias,
6820 int evaluate,
6821 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822{
6823 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 char_u *p;
6825 char_u *expr_start;
6826 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006828 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829
6830 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6831 && (*arg)[2] == (int)KE_SNR)
6832 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006833 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 *arg += 3;
6835 return get_id_len(arg) + 3;
6836 }
6837 len = eval_fname_script(*arg);
6838 if (len > 0)
6839 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006840 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 *arg += len;
6842 }
6843
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006845 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006847 p = find_name_end(*arg, &expr_start, &expr_end,
6848 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 if (expr_start != NULL)
6850 {
6851 char_u *temp_string;
6852
6853 if (!evaluate)
6854 {
6855 len += (int)(p - *arg);
6856 *arg = skipwhite(p);
6857 return len;
6858 }
6859
6860 /*
6861 * Include any <SID> etc in the expanded string:
6862 * Thus the -len here.
6863 */
6864 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6865 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006866 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 *alias = temp_string;
6868 *arg = skipwhite(p);
6869 return (int)STRLEN(temp_string);
6870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871
6872 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006873 // Only give an error when there is something, otherwise it will be
6874 // reported at a higher level.
6875 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006876 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877
6878 return len;
6879}
6880
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006881/*
6882 * Find the end of a variable or function name, taking care of magic braces.
6883 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6884 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006885 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006886 * Return a pointer to just after the name. Equal to "arg" if there is no
6887 * valid name.
6888 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006889 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006890find_name_end(
6891 char_u *arg,
6892 char_u **expr_start,
6893 char_u **expr_end,
6894 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006896 int mb_nest = 0;
6897 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006899 int len;
zeertzjq9a91d2b2024-04-09 21:47:10 +02006900 int allow_curly = !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006902 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006904 *expr_start = NULL;
6905 *expr_end = NULL;
6906 }
6907
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006908 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006909 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006910 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006911 return arg;
6912
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006913 for (p = arg; *p != NUL
6914 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006915 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006916 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006917 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006918 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006919 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006920 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006921 if (*p == '\'')
6922 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006923 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006924 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006925 ;
6926 if (*p == NUL)
6927 break;
6928 }
6929 else if (*p == '"')
6930 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006931 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006932 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006933 if (*p == '\\' && p[1] != NUL)
6934 ++p;
6935 if (*p == NUL)
6936 break;
6937 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006938 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6939 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006940 // "s:" is start of "s:var", but "n:" is not and can be used in
6941 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006942 len = (int)(p - arg);
6943 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006944 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006945 break;
6946 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006947
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006948 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006950 if (*p == '[')
6951 ++br_nest;
6952 else if (*p == ']')
6953 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006955
zeertzjqa93d9cd2023-05-02 16:25:47 +01006956 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006958 if (*p == '{')
6959 {
6960 mb_nest++;
6961 if (expr_start != NULL && *expr_start == NULL)
6962 *expr_start = p;
6963 }
6964 else if (*p == '}')
6965 {
6966 mb_nest--;
6967 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6968 *expr_end = p;
6969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 }
6972
6973 return p;
6974}
6975
6976/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006977 * Expands out the 'magic' {}'s in a variable/function name.
6978 * Note that this can call itself recursively, to deal with
6979 * constructs like foo{bar}{baz}{bam}
6980 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6981 * "in_start" ^
6982 * "expr_start" ^
6983 * "expr_end" ^
6984 * "in_end" ^
6985 *
6986 * Returns a new allocated string, which the caller must free.
6987 * Returns NULL for failure.
6988 */
6989 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006990make_expanded_name(
6991 char_u *in_start,
6992 char_u *expr_start,
6993 char_u *expr_end,
6994 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006995{
6996 char_u c1;
6997 char_u *retval = NULL;
6998 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006999
7000 if (expr_end == NULL || in_end == NULL)
7001 return NULL;
7002 *expr_start = NUL;
7003 *expr_end = NUL;
7004 c1 = *in_end;
7005 *in_end = NUL;
7006
Bram Moolenaara4e0b972022-10-01 19:43:52 +01007007 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02007008 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007009 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007010 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
7011 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007012 if (retval != NULL)
7013 {
7014 STRCPY(retval, in_start);
7015 STRCAT(retval, temp_result);
7016 STRCAT(retval, expr_end + 1);
7017 }
7018 }
7019 vim_free(temp_result);
7020
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007021 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007022 *expr_start = '{';
7023 *expr_end = '}';
7024
7025 if (retval != NULL)
7026 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007027 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007028 if (expr_start != NULL)
7029 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007030 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007031 temp_result = make_expanded_name(retval, expr_start,
7032 expr_end, temp_result);
7033 vim_free(retval);
7034 retval = temp_result;
7035 }
7036 }
7037
7038 return retval;
7039}
7040
7041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007043 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007045 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007046eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02007048 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007049}
7050
7051/*
7052 * Return TRUE if character "c" can be used as the first character in a
7053 * variable or function name (excluding '{' and '}').
7054 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007055 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007056eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007057{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02007058 return ASCII_ISALPHA(c) || c == '_';
7059}
7060
7061/*
7062 * Return TRUE if character "c" can be used as the first character of a
7063 * dictionary key.
7064 */
7065 int
7066eval_isdictc(int c)
7067{
7068 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069}
7070
7071/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02007072 * Handle:
7073 * - expr[expr], expr[expr:expr] subscript
7074 * - ".name" lookup
7075 * - function call with Funcref variable: func(expr)
7076 * - method call: var->method()
7077 *
7078 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007079 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007080 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007081 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007082handle_subscript(
7083 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007084 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007085 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007086 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02007087 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007088{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007089 int evaluate = evalarg != NULL
7090 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007091 int ret = OK;
7092 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007093 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007094 int getnext;
7095 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007096
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007097 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007098 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007099 // When at the end of the line and ".name" or "->{" or "->X" follows in
7100 // the next line then consume the line break.
7101 p = eval_next_non_blank(*arg, evalarg, &getnext);
7102 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00007103 && ((*p == '.'
7104 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
7105 || rettv->v_type == VAR_CLASS
7106 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02007107 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
7108 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
7109 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007110 {
Bram Moolenaare442d592022-05-05 12:20:28 +01007111 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02007112 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007113 check_white = FALSE;
7114 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007115
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007116 if (rettv->v_type == VAR_ANY)
7117 {
7118 char_u *exp_name;
7119 int cc;
7120 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01007121 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007122 type_T *type;
7123
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007124 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00007125 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007126 if (**arg != '.')
7127 {
7128 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007129 semsg(_(e_expected_dot_after_name_str),
7130 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007131 ret = FAIL;
7132 break;
7133 }
7134 ++*arg;
7135 if (IS_WHITE_OR_NUL(**arg))
7136 {
7137 if (verbose)
7138 emsg(_(e_no_white_space_allowed_after_dot));
7139 ret = FAIL;
7140 break;
7141 }
7142
7143 // isolate the name
7144 exp_name = *arg;
7145 while (eval_isnamec(**arg))
7146 ++*arg;
7147 cc = **arg;
7148 **arg = NUL;
7149
7150 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01007151 evalarg == NULL ? NULL : evalarg->eval_cctx,
7152 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007153 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007154
7155 if (idx < 0 && ufunc == NULL)
7156 {
7157 ret = FAIL;
7158 break;
7159 }
7160 if (idx >= 0)
7161 {
7162 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
7163 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
7164
7165 copy_tv(sv->sv_tv, rettv);
7166 }
7167 else
7168 {
7169 rettv->v_type = VAR_FUNC;
7170 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
7171 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007172 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007173 }
7174
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007175 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7176 || rettv->v_type == VAR_PARTIAL))
7177 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007178 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02007179 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
7180 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007181
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007182 // Stop the expression evaluation when immediately aborting on
7183 // error, or when an interrupt occurred or an exception was thrown
7184 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007185 if (aborting())
7186 {
7187 if (ret == OK)
7188 clear_tv(rettv);
7189 ret = FAIL;
7190 }
7191 dict_unref(selfdict);
7192 selfdict = NULL;
7193 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02007194 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02007195 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02007196 if (in_vim9script())
7197 *arg = skipwhite(p + 2);
7198 else
7199 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00007200 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007201 {
zeertzjqc481ad32023-03-11 16:18:51 +00007202 emsg(_(e_no_white_space_allowed_before_parenthesis));
7203 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007204 }
zeertzjqc481ad32023-03-11 16:18:51 +00007205 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
7206 // expr->{lambda}() or expr->(lambda)()
7207 ret = eval_lambda(arg, rettv, evalarg, verbose);
7208 else
7209 // expr->name()
7210 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02007211 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007212 // "." is ".name" lookup when we found a dict or when evaluating and
7213 // scriptversion is at least 2, where string concatenation is "..".
7214 else if (**arg == '['
7215 || (**arg == '.' && (rettv->v_type == VAR_DICT
7216 || (!evaluate
7217 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02007218 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007219 {
7220 dict_unref(selfdict);
7221 if (rettv->v_type == VAR_DICT)
7222 {
7223 selfdict = rettv->vval.v_dict;
7224 if (selfdict != NULL)
7225 ++selfdict->dv_refcount;
7226 }
7227 else
7228 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007229 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007230 {
7231 clear_tv(rettv);
7232 ret = FAIL;
7233 }
7234 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007235 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
7236 || rettv->v_type == VAR_OBJECT))
7237 {
7238 // class member: SomeClass.varname
7239 // class method: SomeClass.SomeMethod()
7240 // class constructor: SomeClass.new()
7241 // object member: someObject.varname
7242 // object method: someObject.SomeMethod()
7243 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
7244 {
7245 clear_tv(rettv);
7246 ret = FAIL;
7247 }
7248 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007249 else
7250 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007251 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007252
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007253 // Turn "dict.Func" into a partial for "Func" bound to "dict".
7254 // Don't do this when "Func" is already a partial that was bound
7255 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02007256 if (selfdict != NULL
7257 && (rettv->v_type == VAR_FUNC
7258 || (rettv->v_type == VAR_PARTIAL
7259 && (rettv->vval.v_partial->pt_auto
7260 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007261 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007262
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007263 dict_unref(selfdict);
7264 return ret;
7265}
7266
7267/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007268 * Make a copy of an item.
7269 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00007270 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007271 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7272 * reference to an already copied list/dict can be used.
7273 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007274 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007275 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007276item_copy(
7277 typval_T *from,
7278 typval_T *to,
7279 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00007280 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007281 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007282{
7283 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007284 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007285
Bram Moolenaar33570922005-01-25 22:26:29 +00007286 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007287 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007288 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007289 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007290 }
7291 ++recurse;
7292
7293 switch (from->v_type)
7294 {
7295 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007296 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007297 case VAR_STRING:
7298 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007299 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01007300 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007301 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007302 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007303 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007304 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007305 case VAR_CLASS:
7306 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02007307 case VAR_TYPEALIAS:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007308 copy_tv(from, to);
7309 break;
7310 case VAR_LIST:
7311 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007312 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007313 if (from->vval.v_list == NULL)
7314 to->vval.v_list = NULL;
7315 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7316 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007317 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007318 to->vval.v_list = from->vval.v_list->lv_copylist;
7319 ++to->vval.v_list->lv_refcount;
7320 }
7321 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007322 to->vval.v_list = list_copy(from->vval.v_list,
7323 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007324 if (to->vval.v_list == NULL)
7325 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007326 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007327 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007328 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007329 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007330 case VAR_DICT:
7331 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007332 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007333 if (from->vval.v_dict == NULL)
7334 to->vval.v_dict = NULL;
7335 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7336 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007337 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007338 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7339 ++to->vval.v_dict->dv_refcount;
7340 }
7341 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007342 to->vval.v_dict = dict_copy(from->vval.v_dict,
7343 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007344 if (to->vval.v_dict == NULL)
7345 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007346 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007347 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007348 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007349 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007350 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007351 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007352 }
7353 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007354 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007355}
7356
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007357 void
7358echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7359{
7360 char_u *tofree;
7361 char_u numbuf[NUMBUFLEN];
7362 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7363
7364 if (*atstart)
7365 {
7366 *atstart = FALSE;
7367 // Call msg_start() after eval1(), evaluating the expression
7368 // may cause a message to appear.
7369 if (with_space)
7370 {
7371 // Mark the saved text as finishing the line, so that what
7372 // follows is displayed on a new line when scrolling back
7373 // at the more prompt.
7374 msg_sb_eol();
7375 msg_start();
7376 }
7377 }
7378 else if (with_space)
7379 msg_puts_attr(" ", echo_attr);
7380
7381 if (p != NULL)
7382 for ( ; *p != NUL && !got_int; ++p)
7383 {
7384 if (*p == '\n' || *p == '\r' || *p == TAB)
7385 {
7386 if (*p != TAB && *needclr)
7387 {
7388 // remove any text still there from the command
7389 msg_clr_eos();
7390 *needclr = FALSE;
7391 }
7392 msg_putchar_attr(*p, echo_attr);
7393 }
7394 else
7395 {
7396 if (has_mbyte)
7397 {
7398 int i = (*mb_ptr2len)(p);
7399
7400 (void)msg_outtrans_len_attr(p, i, echo_attr);
7401 p += i - 1;
7402 }
7403 else
7404 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7405 }
7406 }
7407 vim_free(tofree);
7408}
7409
Bram Moolenaare9a41262005-01-15 22:18:47 +00007410/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 * ":echo expr1 ..." print each argument separated with a space, add a
7412 * newline at the end.
7413 * ":echon expr1 ..." print each argument plain.
7414 */
7415 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007416ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417{
7418 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007419 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007420 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 int needclr = TRUE;
7422 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007423 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007424 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007425 evalarg_T evalarg;
7426
Bram Moolenaare707c882020-07-01 13:07:37 +02007427 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428
7429 if (eap->skip)
7430 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007431 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007433 // If eval1() causes an error message the text from the command may
7434 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007435 need_clr_eos = needclr;
7436
Bram Moolenaar68db9962021-05-09 23:19:22 +02007437 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007438 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 {
7440 /*
7441 * Report the invalid expression unless the expression evaluation
7442 * has been cancelled due to an aborting error, an interrupt, or an
7443 * exception.
7444 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007445 if (!aborting() && did_emsg == did_emsg_before
7446 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007447 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007448 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 break;
7450 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007451 need_clr_eos = FALSE;
7452
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007454 {
7455 if (rettv.v_type == VAR_VOID)
7456 {
7457 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7458 break;
7459 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007460 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007461 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007462
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007463 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 arg = skipwhite(arg);
7465 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007466 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007467 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468
7469 if (eap->skip)
7470 --emsg_skip;
7471 else
7472 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007473 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 if (needclr)
7475 msg_clr_eos();
7476 if (eap->cmdidx == CMD_echo)
7477 msg_end();
7478 }
7479}
7480
7481/*
7482 * ":echohl {name}".
7483 */
7484 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007485ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007487 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488}
7489
7490/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007491 * Returns the :echo attribute
7492 */
7493 int
7494get_echo_attr(void)
7495{
7496 return echo_attr;
7497}
7498
7499/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 * ":execute expr1 ..." execute the result of an expression.
7501 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007502 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007504 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 * Each gets spaces around each argument and a newline at the end for
7506 * echo commands
7507 */
7508 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007509ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510{
7511 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007512 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 int ret = OK;
7514 char_u *p;
7515 garray_T ga;
7516 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007517 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518
7519 ga_init2(&ga, 1, 80);
7520
7521 if (eap->skip)
7522 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007523 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007525 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007526 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528
7529 if (!eap->skip)
7530 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007531 char_u buf[NUMBUFLEN];
7532
7533 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007534 {
7535 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7536 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007537 semsg(_(e_using_invalid_value_as_string_str),
7538 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007539 p = NULL;
7540 }
7541 else
7542 p = tv_get_string_buf(&rettv, buf);
7543 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007544 else
7545 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007546 if (p == NULL)
7547 {
7548 clear_tv(&rettv);
7549 ret = FAIL;
7550 break;
7551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 len = (int)STRLEN(p);
7553 if (ga_grow(&ga, len + 2) == FAIL)
7554 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007555 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 ret = FAIL;
7557 break;
7558 }
7559 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 ga.ga_len += len;
7563 }
7564
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007565 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 arg = skipwhite(arg);
7567 }
7568
7569 if (ret != FAIL && ga.ga_data != NULL)
7570 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007571 // use the first line of continuation lines for messages
7572 SOURCING_LNUM = start_lnum;
7573
Bram Moolenaar37fef162022-08-29 18:16:32 +01007574 if (eap->cmdidx == CMD_echomsg
7575 || eap->cmdidx == CMD_echowindow
7576 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007577 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007578 // Mark the already saved text as finishing the line, so that what
7579 // follows is displayed on a new line when scrolling back at the
7580 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007581 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007582 }
7583
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007584 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007585 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007586 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007587 out_flush();
7588 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007589 else if (eap->cmdidx == CMD_echowindow)
7590 {
7591#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007592 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007593#endif
7594 msg_attr(ga.ga_data, echo_attr);
7595#ifdef HAS_MESSAGE_WINDOW
7596 end_echowindow();
7597#endif
7598 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007599 else if (eap->cmdidx == CMD_echoconsole)
7600 {
7601 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7602 ui_write((char_u *)"\r\n", 2, TRUE);
7603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 else if (eap->cmdidx == CMD_echoerr)
7605 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007606 int save_did_emsg = did_emsg;
7607
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007608 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007609 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 if (!force_abort)
7611 did_emsg = save_did_emsg;
7612 }
7613 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007614 {
7615 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7616
7617 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7618 sticky_cmdmod_flags = cmdmod.cmod_flags
7619 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 do_cmdline((char_u *)ga.ga_data,
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01007621 eap->ea_getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007622 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 }
7625
7626 ga_clear(&ga);
7627
7628 if (eap->skip)
7629 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007630 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631}
7632
7633/*
7634 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7635 * "arg" points to the "&" or '+' when called, to "option" when returning.
7636 * Returns NULL when no option name found. Otherwise pointer to the char
7637 * after the option name.
7638 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007639 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007640find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641{
7642 char_u *p = *arg;
7643
7644 ++p;
7645 if (*p == 'g' && p[1] == ':')
7646 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007647 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 p += 2;
7649 }
7650 else if (*p == 'l' && p[1] == ':')
7651 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007652 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 p += 2;
7654 }
7655 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007656 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657
7658 if (!ASCII_ISALPHA(*p))
7659 return NULL;
7660 *arg = p;
7661
7662 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007663 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 else
7665 while (ASCII_ISALPHA(*p))
7666 ++p;
7667 return p;
7668}
7669
7670/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007671 * Display script name where an item was last set.
7672 * Should only be invoked when 'verbose' is non-zero.
7673 */
7674 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007675last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007676{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007677 char_u *p;
7678
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007679 if (script_ctx.sc_sid == 0)
7680 return;
7681
7682 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7683 if (p == NULL)
7684 return;
7685
7686 verbose_enter();
7687 msg_puts(_("\n\tLast set from "));
7688 msg_puts((char *)p);
7689 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007690 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007691 msg_puts(_(line_msg));
7692 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007693 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007694 verbose_leave();
7695 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007696}
7697
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007698#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699
7700/*
7701 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007702 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 * "flags" can be "g" to do a global substitute.
7704 * Returns an allocated string, NULL for error.
7705 */
7706 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007707do_string_sub(
7708 char_u *str,
7709 char_u *pat,
7710 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007711 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007712 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713{
7714 int sublen;
7715 regmatch_T regmatch;
7716 int i;
7717 int do_all;
7718 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007719 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 garray_T ga;
7721 char_u *ret;
7722 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007723 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007725 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007727 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728
7729 ga_init2(&ga, 1, 200);
7730
7731 do_all = (flags[0] == 'g');
7732
7733 regmatch.rm_ic = p_ic;
7734 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7735 if (regmatch.regprog != NULL)
7736 {
7737 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007738 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7740 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007741 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007742 if (regmatch.startp[0] == regmatch.endp[0])
7743 {
7744 if (zero_width == regmatch.startp[0])
7745 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007746 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007747 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007748 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7749 (size_t)i);
7750 ga.ga_len += i;
7751 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007752 continue;
7753 }
7754 zero_width = regmatch.startp[0];
7755 }
7756
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 /*
7758 * Get some space for a temporary buffer to do the substitution
7759 * into. It will contain:
7760 * - The text up to where the match is.
7761 * - The substituted text.
7762 * - The text after the match.
7763 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007764 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007765 if (sublen <= 0)
7766 {
7767 ga_clear(&ga);
7768 break;
7769 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007770 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7772 {
7773 ga_clear(&ga);
7774 break;
7775 }
7776
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007777 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 i = (int)(regmatch.startp[0] - tail);
7779 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007780 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007781 (void)vim_regsub(&regmatch, sub, expr,
7782 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7783 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007785 tail = regmatch.endp[0];
7786 if (*tail == NUL)
7787 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 if (!do_all)
7789 break;
7790 }
7791
7792 if (ga.ga_data != NULL)
7793 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7794
Bram Moolenaar473de612013-06-08 18:19:48 +02007795 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 }
7797
7798 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7799 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007800 if (p_cpo == empty_option)
7801 p_cpo = save_cpo;
7802 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007803 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007804 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007805 // If it's still empty it was changed and restored, need to restore in
7806 // the complicated way.
7807 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007808 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007809 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811
7812 return ret;
7813}