blob: 8563aa6d79f5dc745eee9319c604954c4fe82f96 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020032static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
33static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
34static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
35static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010036static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020037static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010038static int eval8(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
39static int eval9(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
40static int eval9_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000041
Bram Moolenaar48e697e2016-01-23 22:17:30 +010042static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020043static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020044
Bram Moolenaare21c1582019-03-02 11:57:09 +010045/*
46 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010047 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010048 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020049 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010050num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010051{
52 varnumber_T result;
53
Bram Moolenaar99880f92021-01-20 21:23:14 +010054 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010055 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010056 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010057 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010058 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010059 if (failed != NULL)
60 *failed = TRUE;
61 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010062 if (n1 == 0)
63 result = VARNUM_MIN; // similar to NaN
64 else if (n1 < 0)
65 result = -VARNUM_MAX;
66 else
67 result = VARNUM_MAX;
68 }
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +010069 else if (n1 == VARNUM_MIN && n2 == -1)
70 {
71 // specific case: trying to do VARNUM_MIN / -1 results in a positive
72 // number that doesn't fit in varnumber_T and causes an FPE
73 result = VARNUM_MAX;
74 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010075 else
76 result = n1 / n2;
77
78 return result;
79}
80
81/*
82 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010083 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010084 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020085 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010086num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010087{
Bram Moolenaar99880f92021-01-20 21:23:14 +010088 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010089 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010090 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010091 if (failed != NULL)
92 *failed = TRUE;
93 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010094 return (n2 == 0) ? 0 : (n1 % n2);
95}
96
Bram Moolenaar33570922005-01-25 22:26:29 +000097/*
98 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +000099 */
100 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100101eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000102{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200103 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200104 func_init();
Bram Moolenaara7043832005-01-21 11:56:39 +0000105}
106
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000107#if defined(EXITFREE) || defined(PROTO)
108 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100109eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000110{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200111 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100112 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200113 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000114
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200115 // autoloaded script names
116 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000117
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200118 // unreferenced lists and dicts
119 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200120
121 // functions not garbage collected
122 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000123}
124#endif
125
Bram Moolenaare6b53242020-07-01 17:28:33 +0200126 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200127fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
128{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100129 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200130 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000131
132 if (eap == NULL)
133 return;
134
135 evalarg->eval_cstack = eap->cstack;
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100136 if (sourcing_a_script(eap) || eap->ea_getline == get_list_line)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200137 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100138 evalarg->eval_getline = eap->ea_getline;
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000139 evalarg->eval_cookie = eap->cookie;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200140 }
141}
142
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143/*
144 * Top level evaluation function, returning a boolean.
145 * Sets "error" to TRUE if there was an error.
146 * Return TRUE or FALSE.
147 */
148 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100149eval_to_bool(
150 char_u *arg,
151 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200152 exarg_T *eap,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100153 int skip, // only parse, don't execute
154 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155{
Bram Moolenaar33570922005-01-25 22:26:29 +0000156 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200157 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200158 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100159 int r;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200160
Bram Moolenaar37c83712020-06-30 21:18:36 +0200161 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162
163 if (skip)
164 ++emsg_skip;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100165 if (use_simple_function)
166 r = eval0_simple_funccal(arg, &tv, eap, &evalarg);
167 else
168 r = eval0(arg, &tv, eap, &evalarg);
169 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 else
172 {
173 *error = FALSE;
174 if (!skip)
175 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200176 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200177 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200178 else
179 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000180 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 }
182 }
183 if (skip)
184 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200185 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200187 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188}
189
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100190/*
191 * Call eval1() and give an error message if not done at a lower level.
192 */
193 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200194eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100195{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100196 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100197 int ret;
198 int did_emsg_before = did_emsg;
199 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200200 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100201
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200202 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
203
204 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100205 if (ret == FAIL)
206 {
207 // Report the invalid expression unless the expression evaluation has
208 // been cancelled due to an aborting error, an interrupt, or an
209 // exception, or we already gave a more specific error.
210 // Also check called_emsg for when using assert_fails().
211 if (!aborting() && did_emsg == did_emsg_before
212 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200213 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100214 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200215 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100216 return ret;
217}
218
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200220 * Return whether a typval is a valid expression to pass to eval_expr_typval()
221 * or eval_expr_to_bool(). An empty string returns FALSE;
222 */
223 int
224eval_expr_valid_arg(typval_T *tv)
225{
226 return tv->v_type != VAR_UNKNOWN
227 && (tv->v_type != VAR_STRING
228 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
229}
230
231/*
Bram Moolenaar82418262022-09-28 16:16:15 +0100232 * When calling eval_expr_typval() many times we only need one funccall_T.
233 * Returns NULL when no funccall_T is to be used.
234 * When returning non-NULL remove_funccal() must be called later.
235 */
236 funccall_T *
237eval_expr_get_funccal(typval_T *expr, typval_T *rettv)
238{
239 if (expr->v_type != VAR_PARTIAL)
240 return NULL;
241
242 partial_T *partial = expr->vval.v_partial;
243 if (partial == NULL)
244 return NULL;
245 if (partial->pt_func == NULL
246 || partial->pt_func->uf_def_status == UF_NOT_COMPILED)
247 return NULL;
248
249 return create_funccal(partial->pt_func, rettv);
250}
251
252/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253 * Evaluate an expression, which can be a function, partial or string.
254 * Pass arguments "argv[argc]".
zeertzjqad0c4422023-08-17 22:15:47 +0200255 * If "want_func" is TRUE treat a string as a function name, not an expression.
Bram Moolenaar82418262022-09-28 16:16:15 +0100256 * "fc_arg" is from eval_expr_get_funccal() or NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100257 * Return the result in "rettv" and OK or FAIL.
258 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200259 int
Bram Moolenaar82418262022-09-28 16:16:15 +0100260eval_expr_typval(
261 typval_T *expr,
zeertzjqad0c4422023-08-17 22:15:47 +0200262 int want_func,
Bram Moolenaar82418262022-09-28 16:16:15 +0100263 typval_T *argv,
264 int argc,
265 funccall_T *fc_arg,
266 typval_T *rettv)
Bram Moolenaar48570482017-10-30 21:48:41 +0100267{
268 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100269 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200270 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100271
zeertzjqad0c4422023-08-17 22:15:47 +0200272 if (expr->v_type == VAR_PARTIAL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100273 {
274 partial_T *partial = expr->vval.v_partial;
275
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200276 if (partial == NULL)
277 return FAIL;
278
Bram Moolenaar822ba242020-05-24 23:00:18 +0200279 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200280 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100281 {
Bram Moolenaar82418262022-09-28 16:16:15 +0100282 funccall_T *fc = fc_arg != NULL ? fc_arg
283 : create_funccal(partial->pt_func, rettv);
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100284 int r;
285
286 if (fc == NULL)
287 return FAIL;
288
Bram Moolenaar69082912022-09-22 21:35:19 +0100289 // Shortcut to call a compiled function with minimal overhead.
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100290 r = call_def_function(partial->pt_func, argc, argv,
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000291 DEF_USE_PT_ARGV, partial, NULL, fc, rettv);
Bram Moolenaar82418262022-09-28 16:16:15 +0100292 if (fc_arg == NULL)
293 remove_funccal();
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100294 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100295 return FAIL;
296 }
297 else
298 {
299 s = partial_name(partial);
300 if (s == NULL || *s == NUL)
301 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200302 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000303 funcexe.fe_evaluate = TRUE;
304 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
306 return FAIL;
307 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100308 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200309 else if (expr->v_type == VAR_INSTR)
310 {
311 return exe_typval_instr(expr, rettv);
312 }
zeertzjqad0c4422023-08-17 22:15:47 +0200313 else if (expr->v_type == VAR_FUNC || want_func)
314 {
315 s = expr->v_type == VAR_FUNC
316 ? expr->vval.v_string
317 : tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
318 if (s == NULL || *s == NUL)
319 return FAIL;
320 CLEAR_FIELD(funcexe);
321 funcexe.fe_evaluate = TRUE;
322 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
323 return FAIL;
324 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100325 else
326 {
Bram Moolenaarfc4c4482022-01-26 21:17:04 +0000327 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
Bram Moolenaar48570482017-10-30 21:48:41 +0100328 if (s == NULL)
329 return FAIL;
330 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200331 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100332 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200333 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100334 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200335 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200336 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100337 return FAIL;
338 }
339 }
340 return OK;
341}
342
343/*
344 * Like eval_to_bool() but using a typval_T instead of a string.
345 * Works for string, funcref and partial.
346 */
347 int
348eval_expr_to_bool(typval_T *expr, int *error)
349{
350 typval_T rettv;
351 int res;
352
zeertzjqad0c4422023-08-17 22:15:47 +0200353 if (eval_expr_typval(expr, FALSE, NULL, 0, NULL, &rettv) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100354 {
355 *error = TRUE;
356 return FALSE;
357 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200358 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100359 clear_tv(&rettv);
360 return res;
361}
362
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363/*
364 * Top level evaluation function, returning a string. If "skip" is TRUE,
365 * only parsing to "nextcmd" is done, without reporting errors. Return
366 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
367 */
368 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100369eval_to_string_skip(
370 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200371 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100372 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373{
Bram Moolenaar33570922005-01-25 22:26:29 +0000374 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200376 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377
Bram Moolenaar37c83712020-06-30 21:18:36 +0200378 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 if (skip)
380 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200381 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 retval = NULL;
383 else
384 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100385 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000386 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 }
388 if (skip)
389 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200390 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391
392 return retval;
393}
394
395/*
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100396 * Initialize "evalarg" for use.
397 */
398 void
399init_evalarg(evalarg_T *evalarg)
400{
401 CLEAR_POINTER(evalarg);
402 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
403}
404
405/*
406 * If "evalarg->eval_tofree" is not NULL free it later.
407 * Caller is expected to overwrite "evalarg->eval_tofree" next.
408 */
409 static void
410free_eval_tofree_later(evalarg_T *evalarg)
411{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000412 if (evalarg->eval_tofree == NULL)
413 return;
414
415 if (ga_grow(&evalarg->eval_tofree_ga, 1) == OK)
416 ((char_u **)evalarg->eval_tofree_ga.ga_data)
417 [evalarg->eval_tofree_ga.ga_len++]
418 = evalarg->eval_tofree;
419 else
420 vim_free(evalarg->eval_tofree);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100421}
422
423/*
424 * After using "evalarg" filled from "eap": free the memory.
425 */
426 void
427clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
428{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000429 if (evalarg == NULL)
430 return;
431
432 garray_T *etga = &evalarg->eval_tofree_ga;
433
434 if (evalarg->eval_tofree != NULL || evalarg->eval_using_cmdline)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100435 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000436 if (eap != NULL)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100437 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000438 // We may need to keep the original command line, e.g. for
439 // ":let" it has the variable names. But we may also need
440 // the new one, "nextcmd" points into it. Keep both.
441 vim_free(eap->cmdline_tofree);
442 eap->cmdline_tofree = *eap->cmdlinep;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100443
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000444 if (evalarg->eval_using_cmdline && etga->ga_len > 0)
445 {
446 // "nextcmd" points into the last line in eval_tofree_ga,
447 // need to keep it around.
448 --etga->ga_len;
449 *eap->cmdlinep = ((char_u **)etga->ga_data)[etga->ga_len];
450 vim_free(evalarg->eval_tofree);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100451 }
452 else
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000453 *eap->cmdlinep = evalarg->eval_tofree;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100454 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000455 else
456 vim_free(evalarg->eval_tofree);
457 evalarg->eval_tofree = NULL;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100458 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000459
460 ga_clear_strings(etga);
461 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100462}
463
464/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000465 * Skip over an expression at "*pp".
466 * Return FAIL for an error, OK otherwise.
467 */
468 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200469skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000470{
Bram Moolenaar33570922005-01-25 22:26:29 +0000471 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000472
473 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200474 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000475}
476
477/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200478 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200479 * If in Vim9 script and line breaks are encountered, the lines are
480 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200481 * "arg" is advanced to just after the expression.
482 * "start" is set to the start of the expression, "end" to just after the end.
483 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200484 * Return FAIL for an error, OK otherwise.
485 */
486 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200487skip_expr_concatenate(
488 char_u **arg,
489 char_u **start,
490 char_u **end,
491 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200492{
493 typval_T rettv;
494 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200495 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200496 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200497 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200498 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200499 int evaluate = evalarg == NULL
500 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200501
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200502 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200503 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200504 {
505 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200506 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200507 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200508 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200509 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200510 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200511 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200512
513 // Don't evaluate the expression.
514 if (evalarg != NULL)
515 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200516 *arg = skipwhite(*arg);
517 res = eval1(arg, &rettv, evalarg);
518 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200519 if (evalarg != NULL)
520 evalarg->eval_flags = save_flags;
521
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200522 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200523 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200524 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200525 if (evalarg->eval_ga.ga_len == 1)
526 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200527 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200528 ga_clear(gap);
529 gap->ga_itemsize = 0;
530 }
531 else
532 {
533 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200534 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200535
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200536 // Line breaks encountered, concatenate all the lines.
537 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200538 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200539
540 // free the lines only when using getsourceline()
541 if (evalarg->eval_cookie != NULL)
542 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200543 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200544 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200545 // Do not free the last line, "arg" points into it, free it
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100546 // later. Also free "eval_tofree" later if needed.
547 free_eval_tofree_later(evalarg);
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200548 evalarg->eval_tofree =
549 ((char_u **)gap->ga_data)[gap->ga_len - 1];
550 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200551 ga_clear_strings(gap);
552 }
553 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200554 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200555 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200556
557 // free lines that were explicitly marked for freeing
558 ga_clear_strings(freegap);
559 }
560
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200561 gap->ga_itemsize = 0;
562 if (p == NULL)
563 return FAIL;
564 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200565 vim_free(evalarg->eval_tofree_lambda);
566 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200567 // Compute "end" relative to the end.
568 *end = *start + STRLEN(*start) - endoff;
569 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200570 }
571
572 return res;
573}
574
575/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100576 * Convert "tv" to a string.
zeertzjq19dfa272023-06-15 10:56:41 +0100577 * When "convert" is TRUE convert a List into a sequence of lines.
Bram Moolenaar883cf972021-01-15 18:04:43 +0100578 * Returns an allocated string (NULL when out of memory).
579 */
580 char_u *
581typval2string(typval_T *tv, int convert)
582{
583 garray_T ga;
584 char_u *retval;
Bram Moolenaar883cf972021-01-15 18:04:43 +0100585
586 if (convert && tv->v_type == VAR_LIST)
587 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000588 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100589 if (tv->vval.v_list != NULL)
590 {
591 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
592 if (tv->vval.v_list->lv_len > 0)
593 ga_append(&ga, NL);
594 }
595 ga_append(&ga, NUL);
596 retval = (char_u *)ga.ga_data;
597 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100598 else
599 retval = vim_strsave(tv_get_string(tv));
600 return retval;
601}
602
603/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200604 * Top level evaluation function, returning a string. Does not handle line
605 * breaks.
zeertzjq19dfa272023-06-15 10:56:41 +0100606 * When "convert" is TRUE convert a List into a sequence of lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 * Return pointer to allocated memory, or NULL for failure.
608 */
609 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100610eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100611 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100612 int convert,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100613 exarg_T *eap,
614 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615{
Bram Moolenaar33570922005-01-25 22:26:29 +0000616 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100618 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100619 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100621 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100622 if (use_simple_function)
623 r = eval0_simple_funccal(arg, &tv, NULL, &evalarg);
624 else
625 r = eval0(arg, &tv, NULL, &evalarg);
626 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 retval = NULL;
628 else
629 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100630 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000631 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100633 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634
635 return retval;
636}
637
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100638 char_u *
639eval_to_string(
640 char_u *arg,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100641 int convert,
642 int use_simple_function)
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100643{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100644 return eval_to_string_eap(arg, convert, NULL, use_simple_function);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100645}
646
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000648 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100649 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200650 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 */
652 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100653eval_to_string_safe(
654 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000655 int use_sandbox,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100656 int keep_script_version,
657 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658{
659 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200660 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200661 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200662 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663
Bram Moolenaar9530b582022-01-22 13:39:08 +0000664 if (!keep_script_version)
665 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200666 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000667 if (use_sandbox)
668 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100669 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200670 may_garbage_collect = FALSE;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100671 retval = eval_to_string(arg, FALSE, use_simple_function);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000672 if (use_sandbox)
673 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100674 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200675 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200676 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200677 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 return retval;
679}
680
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681/*
682 * Top level evaluation function, returning a number.
683 * Evaluates "expr" silently.
684 * Returns -1 for an error.
685 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200686 varnumber_T
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100687eval_to_number(char_u *expr, int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688{
Bram Moolenaar33570922005-01-25 22:26:29 +0000689 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200690 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000691 char_u *p = skipwhite(expr);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100692 int r = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693
694 ++emsg_off;
695
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100696 if (use_simple_function)
697 r = may_call_simple_func(expr, &rettv);
698 if (r == NOTDONE)
699 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
700 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 retval = -1;
702 else
703 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100704 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000705 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 }
707 --emsg_off;
708
709 return retval;
710}
711
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000712/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000713 * Top level evaluation function.
714 * Returns an allocated typval_T with the result.
715 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000716 */
717 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200718eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000719{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100720 return eval_expr_ext(arg, eap, FALSE);
721}
722
723 typval_T *
724eval_expr_ext(char_u *arg, exarg_T *eap, int use_simple_function)
725{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000726 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200727 evalarg_T evalarg;
728
729 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000730
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200731 tv = ALLOC_ONE(typval_T);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100732 if (tv != NULL)
733 {
734 int r = NOTDONE;
735
736 if (use_simple_function)
737 r = eval0_simple_funccal(arg, tv, eap, &evalarg);
738 if (r == NOTDONE)
739 r = eval0(arg, tv, eap, &evalarg);
740
741 if (r == FAIL)
742 VIM_CLEAR(tv);
743 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000744
Bram Moolenaar37c83712020-06-30 21:18:36 +0200745 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000746 return tv;
747}
748
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000750 * "*arg" points to what can be a function name in the form of "import.Name" or
751 * "Funcref". Return the name of the function. Set "tofree" to something that
752 * was allocated.
753 * If "verbose" is FALSE no errors are given.
754 * Return NULL for any failure.
755 */
756 static char_u *
757deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000758 char_u **arg,
759 char_u **tofree,
760 evalarg_T *evalarg,
761 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000762{
763 typval_T ref;
764 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100765 int save_flags = 0;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000766
767 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100768 if (evalarg != NULL)
769 {
770 // need to evaluate this to get an import, like in "a.Func"
771 save_flags = evalarg->eval_flags;
772 evalarg->eval_flags |= EVAL_EVALUATE;
773 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100774 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100775 {
776 dictitem_T *v;
777
778 // If <SID>VarName was used it would not be found, try another way.
779 v = find_var_also_in_script(name, NULL, FALSE);
780 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100781 {
782 name = NULL;
783 goto theend;
784 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100785 copy_tv(&v->di_tv, &ref);
786 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000787 if (*skipwhite(*arg) != NUL)
788 {
789 if (verbose)
790 semsg(_(e_trailing_characters_str), *arg);
791 name = NULL;
792 }
793 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
794 {
795 name = ref.vval.v_string;
796 ref.vval.v_string = NULL;
797 *tofree = name;
798 }
799 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
800 {
801 if (ref.vval.v_partial->pt_argc > 0
802 || ref.vval.v_partial->pt_dict != NULL)
803 {
804 if (verbose)
805 emsg(_(e_cannot_use_partial_here));
806 name = NULL;
807 }
808 else
809 {
810 name = vim_strsave(partial_name(ref.vval.v_partial));
811 *tofree = name;
812 }
813 }
814 else
815 {
816 if (verbose)
817 semsg(_(e_not_callable_type_str), name);
818 name = NULL;
819 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100820
821theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000822 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100823 if (evalarg != NULL)
824 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000825 return name;
826}
827
828/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100829 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200830 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
831 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000832 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200834 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100835call_vim_function(
836 char_u *func,
837 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200838 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200839 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000841 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200842 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000843 char_u *arg;
844 char_u *name;
845 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000846 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100848 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200849 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000850 funcexe.fe_firstline = curwin->w_cursor.lnum;
851 funcexe.fe_lastline = curwin->w_cursor.lnum;
852 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000853
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000854 // The name might be "import.Func" or "Funcref". We don't know, we need to
855 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000856 // autoload script has errors. Guess that when there is a dot in the name
857 // showing errors is the right choice.
858 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000859 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000860 if (ignore_errors)
861 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000862 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000863 if (ignore_errors)
864 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000865 if (name == NULL)
866 name = func;
867
868 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
869
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000870 if (ret == FAIL)
871 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000872 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000873
874 return ret;
875}
876
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100877/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100878 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000879 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
880 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000881 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000882 */
883 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100884call_func_retstr(
885 char_u *func,
886 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200887 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000888{
889 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000890 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000891
Bram Moolenaarded27a12018-08-01 19:06:03 +0200892 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000893 return NULL;
894
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100895 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000896 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 return retval;
898}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000899
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000900/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100901 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000902 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000903 * Returns NULL when there is something wrong.
Bram Moolenaar55e93662022-09-10 13:52:26 +0100904 * Gives an error when the returned value is not a list.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000905 */
906 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100907call_func_retlist(
908 char_u *func,
909 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200910 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000911{
912 typval_T rettv;
913
Bram Moolenaarded27a12018-08-01 19:06:03 +0200914 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000915 return NULL;
916
917 if (rettv.v_type != VAR_LIST)
918 {
Bram Moolenaar55e93662022-09-10 13:52:26 +0100919 semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
920 vartype_name(rettv.v_type));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000921 clear_tv(&rettv);
922 return NULL;
923 }
924
925 return rettv.vval.v_list;
926}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927
Bram Moolenaare70dd112022-01-21 16:31:11 +0000928#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100930 * Evaluate "arg", which is 'foldexpr'.
931 * Note: caller must set "curwin" to match "arg".
932 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
933 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 */
935 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000936eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000938 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000939 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200940 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000942 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000943 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100944 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100946 arg = skipwhite(wp->w_p_fde);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000947 current_sctx = wp->w_p_script_ctx[WV_FDE];
948
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000950 if (use_sandbox)
951 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100952 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 *cp = NUL;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100954
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100955 // Evaluate the expression. If the expression is "FuncName()" call the
956 // function directly.
957 if (eval0_simple_funccal(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 retval = 0;
959 else
960 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100961 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000962 if (tv.v_type == VAR_NUMBER)
963 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000964 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 retval = 0;
966 else
967 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100968 // If the result is a string, check if there is a non-digit before
969 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000970 s = tv.vval.v_string;
zeertzjqa991ce92023-10-06 19:16:36 +0200971 if (*s != NUL && !VIM_ISDIGIT(*s) && *s != '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 *cp = *s++;
973 retval = atol((char *)s);
974 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000975 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 }
977 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000978 if (use_sandbox)
979 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100980 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200981 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000982 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200984 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985}
986#endif
987
Ernie Rael64885642023-10-04 20:16:22 +0200988#ifdef LOG_LOCKVAR
989typedef struct flag_string_S
990{
991 int flag;
992 char *str;
993} flag_string_T;
994
995 static char *
996flags_tostring(int flags, flag_string_T *_fstring, char *buf, size_t n)
997{
998 char *p = buf;
999 *p = NUL;
1000 for (flag_string_T *fstring = _fstring; fstring->flag; ++fstring)
1001 {
1002 if ((fstring->flag & flags) != 0)
1003 {
1004 size_t len = STRLEN(fstring->str);
1005 if (n > p - buf + len + 7)
1006 {
1007 STRCAT(p, fstring->str);
1008 p += len;
1009 STRCAT(p, " ");
1010 ++p;
1011 }
1012 else
1013 {
1014 STRCAT(buf, "...");
1015 break;
1016 }
1017 }
1018 }
1019 return buf;
1020}
1021
1022flag_string_T glv_flag_strings[] = {
1023 { GLV_QUIET, "QUIET" },
1024 { GLV_NO_AUTOLOAD, "NO_AUTOLOAD" },
1025 { GLV_READ_ONLY, "READ_ONLY" },
1026 { GLV_NO_DECL, "NO_DECL" },
1027 { GLV_COMPILING, "COMPILING" },
1028 { GLV_ASSIGN_WITH_OP, "ASSIGN_WITH_OP" },
1029 { GLV_PREFER_FUNC, "PREFER_FUNC" },
1030 { 0, NULL }
1031};
1032#endif
1033
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034/*
Ernie Raelee865f32023-09-29 19:53:55 +02001035 * Fill in "lp" using "root". This is used in a special case when
1036 * "get_lval()" parses a bare word when "lval_root" is not NULL.
1037 *
1038 * This is typically called with "lval_root" as "root". For a class, find
1039 * the name from lp in the class from root, fill in lval_T if found. For a
1040 * complex type, list/dict use it as the result; just put the root into
1041 * ll_tv.
1042 *
1043 * "lval_root" is a hack used during run-time/instr-execution to provide the
1044 * starting point for "get_lval()" to traverse a chain of indexes. In some
1045 * cases get_lval sees a bare name and uses this function to populate the
1046 * lval_T.
1047 *
1048 * For setting up "lval_root" (currently only used with lockvar)
1049 * compile_lock_unlock - pushes object on stack (which becomes lval_root)
1050 * execute_instructions: ISN_LOCKUNLOCK - sets lval_root from stack.
1051 */
1052 static void
Ernie Rael4c8da022023-10-11 21:35:11 +02001053fill_lval_from_lval_root(lval_T *lp, lval_root_T *lr)
Ernie Raelee865f32023-09-29 19:53:55 +02001054{
1055#ifdef LOG_LOCKVAR
Ernie Rael4c8da022023-10-11 21:35:11 +02001056 ch_log(NULL, "LKVAR: fill_lval_from_lval_root(): name %s, tv %p",
1057 lp->ll_name, (void*)lr->lr_tv);
Ernie Raelee865f32023-09-29 19:53:55 +02001058#endif
Ernie Rael4c8da022023-10-11 21:35:11 +02001059 if (lr->lr_tv == NULL)
1060 return;
Ernie Rael64885642023-10-04 20:16:22 +02001061 if (!lr->lr_is_arg && lr->lr_tv->v_type == VAR_CLASS)
Ernie Raelee865f32023-09-29 19:53:55 +02001062 {
Ernie Rael64885642023-10-04 20:16:22 +02001063 if (lr->lr_tv->vval.v_class != NULL)
Ernie Raelee865f32023-09-29 19:53:55 +02001064 {
1065 // Special special case. Look for a bare class variable reference.
Ernie Rael64885642023-10-04 20:16:22 +02001066 class_T *cl = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001067 int m_idx;
1068 ocmember_T *m = class_member_lookup(cl, lp->ll_name,
1069 lp->ll_name_end - lp->ll_name, &m_idx);
1070 if (m != NULL)
1071 {
1072 // Assuming "inside class" since bare reference.
Ernie Rael64885642023-10-04 20:16:22 +02001073 lp->ll_class = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001074 lp->ll_oi = m_idx;
1075 lp->ll_valtype = m->ocm_type;
1076 lp->ll_tv = &lp->ll_class->class_members_tv[m_idx];
1077#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001078 ch_log(NULL, "LKVAR: ... class member %s.%s",
1079 lp->ll_class->class_name, lp->ll_name);
Ernie Raelee865f32023-09-29 19:53:55 +02001080#endif
1081 return;
1082 }
1083 }
1084 }
1085
1086#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001087 ch_log(NULL, "LKVAR: ... type: %s", vartype_name(lr->lr_tv->v_type));
Ernie Raelee865f32023-09-29 19:53:55 +02001088#endif
Ernie Rael64885642023-10-04 20:16:22 +02001089 lp->ll_tv = lr->lr_tv;
Ernie Raelee865f32023-09-29 19:53:55 +02001090 lp->ll_is_root = TRUE;
1091}
1092
1093/*
Ernie Rael64885642023-10-04 20:16:22 +02001094 * Check if the class has permission to access the member.
1095 * Returns OK or FAIL.
1096 */
1097 static int
1098get_lval_check_access(
1099 class_T *cl_exec, // executing class, NULL if :def or script level
1100 class_T *cl, // class which contains the member
1101 ocmember_T *om, // member being accessed
1102 char_u *p, // char after member name
1103 int flags) // GLV flags to check if writing to lval
1104{
1105#ifdef LOG_LOCKVAR
1106 ch_log(NULL, "LKVAR: get_lval_check_access(), cl_exec %p, cl %p, %c",
1107 (void*)cl_exec, (void*)cl, *p);
1108#endif
1109 if (cl_exec == NULL || cl_exec != cl)
1110 {
Ernie Raele6c9aa52023-10-06 19:55:52 +02001111 char *msg = NULL;
Ernie Rael64885642023-10-04 20:16:22 +02001112 switch (om->ocm_access)
1113 {
1114 case VIM_ACCESS_PRIVATE:
Ernie Rael03042a22023-11-11 08:53:32 +01001115 msg = e_cannot_access_protected_variable_str;
Ernie Raele6c9aa52023-10-06 19:55:52 +02001116 break;
Ernie Rael64885642023-10-04 20:16:22 +02001117 case VIM_ACCESS_READ:
1118 // If [idx] or .key following, read only OK.
1119 if (*p == '[' || *p == '.')
1120 break;
1121 if ((flags & GLV_READ_ONLY) == 0)
Ernie Raele6c9aa52023-10-06 19:55:52 +02001122 msg = e_variable_is_not_writable_str;
Ernie Rael64885642023-10-04 20:16:22 +02001123 break;
1124 case VIM_ACCESS_ALL:
1125 break;
1126 }
Ernie Raele6c9aa52023-10-06 19:55:52 +02001127 if (msg != NULL)
1128 {
1129 emsg_var_cl_define(msg, om->ocm_name, 0, cl);
1130 return FAIL;
1131 }
1132
Ernie Rael64885642023-10-04 20:16:22 +02001133 }
1134 return OK;
1135}
1136
1137/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001138 * Get an lval: variable, Dict item or List item that can be assigned a value
1139 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1140 * "name.key", "name.key[expr]" etc.
1141 * Indexing only works if "name" is an existing List or Dictionary.
1142 * "name" points to the start of the name.
1143 * If "rettv" is not NULL it points to the value to be assigned.
1144 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1145 * wrong; must end in space or cmd separator.
1146 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001147 * flags:
1148 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001149 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001150 * GLV_NO_AUTOLOAD: do not use script autoloading
1151 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001152 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001153 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001154 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001155 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001156 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001157get_lval(
1158 char_u *name,
1159 typval_T *rettv,
1160 lval_T *lp,
1161 int unlet,
1162 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001163 int flags, // GLV_ values
1164 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001165{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001166 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001167 char_u *expr_start, *expr_end;
1168 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001169 dictitem_T *v;
1170 typval_T var1;
1171 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001172 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001173 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001174 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01001175 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001176 int quiet = flags & GLV_QUIET;
Ernie Rael86eda072023-09-05 07:33:48 +02001177 int writing = 0;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001178 int vim9script = in_vim9script();
Ernie Rael64885642023-10-04 20:16:22 +02001179 class_T *cl_exec = NULL; // class that is executing, or NULL.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001180
Ernie Raelee865f32023-09-29 19:53:55 +02001181#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001182 if (lval_root == NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001183 ch_log(NULL, "LKVAR: get_lval(): name: %s, lval_root (nil)", name);
Ernie Rael64885642023-10-04 20:16:22 +02001184 else
Ernie Rael4c8da022023-10-11 21:35:11 +02001185 ch_log(NULL, "LKVAR: get_lval(): name: %s, lr_tv %p lr_is_arg %d",
1186 name, (void*)lval_root->lr_tv, lval_root->lr_is_arg);
Ernie Rael64885642023-10-04 20:16:22 +02001187 char buf[80];
Ernie Rael4c8da022023-10-11 21:35:11 +02001188 ch_log(NULL, "LKVAR: ...: GLV flags: %s",
Ernie Rael64885642023-10-04 20:16:22 +02001189 flags_tostring(flags, glv_flag_strings, buf, sizeof(buf)));
Ernie Raelee865f32023-09-29 19:53:55 +02001190#endif
1191
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001192 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02001193 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001194
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001195 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001196 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001197 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001198 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02001199 lp->ll_name_end = find_name_end(name, NULL, NULL,
1200 FNE_INCL_BR | fne_flags);
1201 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001202 }
1203
Bram Moolenaara749a422022-02-12 19:52:25 +00001204 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001205 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00001206 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
1207 {
1208 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
1209 return NULL;
1210 }
1211
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001212 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001213 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001214 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001215 if (expr_start != NULL)
1216 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001217 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01001218 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001219 && *p != '[' && *p != '.')
1220 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001221 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001222 return NULL;
1223 }
1224
1225 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1226 if (lp->ll_exp_name == NULL)
1227 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001228 // Report an invalid expression in braces, unless the
1229 // expression evaluation has been cancelled due to an
1230 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001231 if (!aborting() && !quiet)
1232 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001233 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001234 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001235 return NULL;
1236 }
1237 }
1238 lp->ll_name = lp->ll_exp_name;
1239 }
1240 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001241 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001242 lp->ll_name = name;
1243
Bram Moolenaar4525a572022-02-13 11:57:33 +00001244 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001245 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001246 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001247 // However, "g:[key]" is indexing a dictionary.
1248 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001249 {
1250 --p;
1251 lp->ll_name_end = p;
1252 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001253 if (*skipwhite(p) == ':')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001254 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001255 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001256
Bram Moolenaar9510d222022-09-11 15:14:05 +01001257 if (is_scoped_variable(name))
1258 {
1259 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1260 return NULL;
1261 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001262 if (VIM_ISWHITE(*p))
1263 {
1264 semsg(_(e_no_white_space_allowed_before_colon_str), p);
1265 return NULL;
1266 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001267 if (tp == p + 1 && !quiet)
1268 {
1269 semsg(_(e_white_space_required_after_str_str), ":", p);
1270 return NULL;
1271 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001272 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001273 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001274 semsg(_(e_using_type_not_in_script_context_str), p);
1275 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001276 }
Yegappan Lakshmananb5a07192023-10-05 20:14:43 +02001277 if (vim9script && (flags & GLV_NO_DECL) &&
1278 !(flags & GLV_FOR_LOOP))
1279 {
1280 // Using a type and not in a "var" declaration.
1281 semsg(_(e_trailing_characters_str), p);
1282 return NULL;
1283 }
1284
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001285
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001286 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001287 lp->ll_type = parse_type(&tp,
1288 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1289 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001290 if (lp->ll_type == NULL && !quiet)
1291 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001292 lp->ll_name_end = tp;
1293 }
Ernie Raelee865f32023-09-29 19:53:55 +02001294 // TODO: check inside class?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001295 }
1296 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001297 if (lp->ll_name == NULL)
1298 return p;
1299
Bram Moolenaar62aec932022-01-29 21:45:34 +00001300 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001301 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001302 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001303
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001304 if (import != NULL)
1305 {
1306 ufunc_T *ufunc;
1307 type_T *type;
1308
Bram Moolenaar753885b2022-08-24 16:30:36 +01001309 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001310 lp->ll_sid = import->imp_sid;
1311 lp->ll_name = skipwhite(p + 1);
1312 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1313 lp->ll_name_end = p;
1314
1315 // check the item is exported
1316 cc = *p;
1317 *p = NUL;
1318 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001319 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001320 {
1321 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001322 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001323 }
1324 *p = cc;
1325 }
1326 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001327
Ernie Rael0f058d12023-10-14 11:25:04 +02001328 // Without [idx] or .key we are done.
1329 if (*p != '[' && *p != '.')
Ernie Raelee865f32023-09-29 19:53:55 +02001330 {
1331 if (lval_root != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001332 fill_lval_from_lval_root(lp, lval_root);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001333 return p;
Ernie Raelee865f32023-09-29 19:53:55 +02001334 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001335
Ernie Rael0f058d12023-10-14 11:25:04 +02001336 if (vim9script && lval_root != NULL)
1337 cl_exec = lval_root->lr_cl_exec;
Ernie Rael4c8da022023-10-11 21:35:11 +02001338 if (vim9script && lval_root != NULL && lval_root->lr_tv != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001339 {
1340 // using local variable
Ernie Rael64885642023-10-04 20:16:22 +02001341 lp->ll_tv = lval_root->lr_tv;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001342 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001343 }
1344 else
1345 {
1346 cc = *p;
1347 *p = NUL;
1348 // When we would write to the variable pass &ht and prevent autoload.
1349 writing = !(flags & GLV_READ_ONLY);
1350 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001351 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001352 if (v == NULL && !quiet)
1353 semsg(_(e_undefined_variable_str), lp->ll_name);
1354 *p = cc;
1355 if (v == NULL)
1356 return NULL;
1357 lp->ll_tv = &v->di_tv;
1358 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001359
Bram Moolenaar4525a572022-02-13 11:57:33 +00001360 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001361 {
1362 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001363 semsg(_(e_variable_already_declared_str), lp->ll_name);
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001364 return NULL;
1365 }
1366
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001367 /*
1368 * Loop until no more [idx] or .key is following.
1369 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001370 var1.v_type = VAR_UNKNOWN;
1371 var2.v_type = VAR_UNKNOWN;
Ernie Rael0f058d12023-10-14 11:25:04 +02001372 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001373 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001374 vartype_T v_type = lp->ll_tv->v_type;
1375
1376 if (*p == '.' && v_type != VAR_DICT
1377 && v_type != VAR_OBJECT
1378 && v_type != VAR_CLASS)
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001379 {
1380 if (!quiet)
Ernie Raelf8da3242023-10-11 21:22:12 +02001381 semsg(_(e_dot_not_allowed_after_str_str),
1382 vartype_name(v_type), name);
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001383 return NULL;
1384 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001385 if (v_type != VAR_LIST
1386 && v_type != VAR_DICT
1387 && v_type != VAR_BLOB
1388 && v_type != VAR_OBJECT
1389 && v_type != VAR_CLASS)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001390 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001391 if (!quiet)
Ernie Raelf8da3242023-10-11 21:22:12 +02001392 semsg(_(e_index_not_allowed_after_str_str),
1393 vartype_name(v_type), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001394 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001395 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001396
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001397 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001398 int r = OK;
Bram Moolenaard505d172022-12-18 21:42:55 +00001399 if (v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001400 r = rettv_list_alloc(lp->ll_tv);
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +02001401 else if (v_type == VAR_BLOB && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001402 r = rettv_blob_alloc(lp->ll_tv);
1403 if (r == FAIL)
1404 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001405
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001406 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001407 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001408 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001409 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001410 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001411 }
Ernie Rael4c8da022023-10-11 21:35:11 +02001412#ifdef LOG_LOCKVAR
1413 ch_log(NULL, "LKVAR: get_lval() loop: p: %s, type: %s", p,
1414 vartype_name(v_type));
1415#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001416
Bram Moolenaar4525a572022-02-13 11:57:33 +00001417 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001418 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001419 && lp->ll_tv == &v->di_tv
1420 && ht != NULL && ht == get_script_local_ht())
1421 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001422 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001423
1424 // Vim9 script local variable: get the type
1425 if (sv != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001426 {
Bram Moolenaar10c65862020-10-08 21:16:42 +02001427 lp->ll_valtype = sv->sv_type;
Ernie Rael4c8da022023-10-11 21:35:11 +02001428#ifdef LOG_LOCKVAR
1429 ch_log(NULL, "LKVAR: ... loop: vim9 assign type: %s",
1430 vartype_name(lp->ll_valtype->tt_type));
1431#endif
1432 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001433 }
1434
Bram Moolenaar8c711452005-01-14 21:53:12 +00001435 len = -1;
Ernie Rael0f058d12023-10-14 11:25:04 +02001436 if (*p == '.')
Bram Moolenaar8c711452005-01-14 21:53:12 +00001437 {
1438 key = p + 1;
1439 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1440 ;
1441 if (len == 0)
1442 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001443 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001444 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001445 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001446 }
1447 p = key + len;
1448 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001449 else
1450 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001451 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001452 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001453 if (*p == ':')
1454 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001455 else
1456 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001457 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001458 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001459 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001460 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001461 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001462 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001463 clear_tv(&var1);
1464 return NULL;
1465 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001466 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001467 }
1468
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001469 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001470 if (*p == ':')
1471 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001472 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001473 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001474 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001475 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001476 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001477 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001478 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001479 if (rettv != NULL
1480 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001481 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001482 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001483 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001484 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001485 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001486 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001487 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001488 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001489 }
1490 p = skipwhite(p + 1);
1491 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001492 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001493 else
1494 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001495 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001496 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001497 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001498 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001499 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001500 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001501 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001502 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001503 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001504 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001505 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001506 clear_tv(&var2);
1507 return NULL;
1508 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001509 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001510 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001511 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001512 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001513 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001514
Bram Moolenaar8c711452005-01-14 21:53:12 +00001515 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001516 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001517 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001518 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001519 clear_tv(&var1);
1520 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001521 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001522 }
1523
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001524 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001525 ++p;
1526 }
Ernie Rael4c8da022023-10-11 21:35:11 +02001527#ifdef LOG_LOCKVAR
Ernie Rael0f058d12023-10-14 11:25:04 +02001528 if (len == -1)
Ernie Rael4c8da022023-10-11 21:35:11 +02001529 ch_log(NULL, "LKVAR: ... loop: p: %s, '[' key: %s", p,
1530 empty1 ? ":" : (char*)tv_get_string(&var1));
1531 else
1532 ch_log(NULL, "LKVAR: ... loop: p: %s, '.' key: %s", p, key);
Ernie Rael4c8da022023-10-11 21:35:11 +02001533#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00001534
Bram Moolenaard505d172022-12-18 21:42:55 +00001535 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001536 {
1537 if (len == -1)
1538 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001539 // "[key]": get key from "var1"
1540 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001541 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001542 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001543 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001544 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001545 }
1546 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001547 lp->ll_list = NULL;
Ernie Raelee865f32023-09-29 19:53:55 +02001548 lp->ll_object = NULL;
1549 lp->ll_class = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001550
1551 // a NULL dict is equivalent with an empty dict
1552 if (lp->ll_tv->vval.v_dict == NULL)
1553 {
1554 lp->ll_tv->vval.v_dict = dict_alloc();
1555 if (lp->ll_tv->vval.v_dict == NULL)
1556 {
1557 clear_tv(&var1);
1558 return NULL;
1559 }
1560 ++lp->ll_tv->vval.v_dict->dv_refcount;
1561 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001562 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001563
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001564 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001565
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001566 // When assigning to a scope dictionary check that a function and
1567 // variable name is valid (only variable name unless it is l: or
1568 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001569 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001570 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001571 int prevval;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001572
1573 if (len != -1)
1574 {
1575 prevval = key[len];
1576 key[len] = NUL;
1577 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001578 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001579 prevval = 0; // avoid compiler warning
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001580 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
zeertzjq91c75d12022-11-05 20:21:58 +00001581 && (rettv->v_type == VAR_FUNC
1582 || rettv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001583 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001584 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001585 if (len != -1)
1586 key[len] = prevval;
1587 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001588 {
1589 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001590 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001591 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001592 }
1593
Bram Moolenaar10c65862020-10-08 21:16:42 +02001594 if (lp->ll_valtype != NULL)
1595 // use the type of the member
1596 lp->ll_valtype = lp->ll_valtype->tt_member;
1597
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001598 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001599 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001600 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001601 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001602 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001603 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001604 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001605 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001606 return NULL;
1607 }
1608
Bram Moolenaar31b81602019-02-10 22:14:27 +01001609 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001610 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001611 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001612 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001613 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001614 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001615 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001616 }
1617 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001618 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001619 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001620 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001621 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001622 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001623 p = NULL;
1624 break;
1625 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001626 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001627 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001628 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1629 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001630 {
1631 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001632 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001633 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001634
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001635 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001636 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001637 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001638 else if (v_type == VAR_BLOB)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001639 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001640 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1641
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001642 /*
1643 * Get the number and item for the only or first index of the List.
1644 */
1645 if (empty1)
1646 lp->ll_n1 = 0;
1647 else
1648 // is number or string
1649 lp->ll_n1 = (long)tv_get_number(&var1);
1650 clear_tv(&var1);
1651
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001652 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001653 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001654 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001655 return NULL;
1656 }
1657 if (lp->ll_range && !lp->ll_empty2)
1658 {
1659 lp->ll_n2 = (long)tv_get_number(&var2);
1660 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001661 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1662 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001663 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001664 }
1665 lp->ll_blob = lp->ll_tv->vval.v_blob;
1666 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001667 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001668 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001669 else if (v_type == VAR_LIST)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001670 {
1671 /*
1672 * Get the number and item for the only or first index of the List.
1673 */
1674 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001675 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001676 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001677 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001678 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001679 clear_tv(&var1);
1680
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001681 lp->ll_dict = NULL;
Ernie Raelee865f32023-09-29 19:53:55 +02001682 lp->ll_object = NULL;
1683 lp->ll_class = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001684 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001685 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1686 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001687 if (lp->ll_li == NULL)
1688 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001689 clear_tv(&var2);
1690 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001691 }
1692
Bram Moolenaar10c65862020-10-08 21:16:42 +02001693 if (lp->ll_valtype != NULL)
1694 // use the type of the member
1695 lp->ll_valtype = lp->ll_valtype->tt_member;
1696
Bram Moolenaar8c711452005-01-14 21:53:12 +00001697 /*
1698 * May need to find the item or absolute index for the second
1699 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001700 * When no index given: "lp->ll_empty2" is TRUE.
1701 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001702 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001703 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001704 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001705 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001706 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001707 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001708 if (check_range_index_two(lp->ll_list,
1709 &lp->ll_n1, lp->ll_li,
1710 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001711 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001712 }
1713
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001714 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001715 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001716 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001717 {
Ernie Raelee865f32023-09-29 19:53:55 +02001718 lp->ll_dict = NULL;
1719 lp->ll_list = NULL;
1720
1721 class_T *cl;
Ernie Rael4c8da022023-10-11 21:35:11 +02001722 if (v_type == VAR_OBJECT)
Ernie Raelee865f32023-09-29 19:53:55 +02001723 {
Ernie Rael4c8da022023-10-11 21:35:11 +02001724 if (lp->ll_tv->vval.v_object == NULL)
1725 {
1726 if (!quiet)
1727 emsg(_(e_using_null_object));
1728 return NULL;
1729 }
Ernie Raelee865f32023-09-29 19:53:55 +02001730 cl = lp->ll_tv->vval.v_object->obj_class;
1731 lp->ll_object = lp->ll_tv->vval.v_object;
1732 }
1733 else
1734 {
1735 cl = lp->ll_tv->vval.v_class;
1736 lp->ll_object = NULL;
1737 }
1738 lp->ll_class = cl;
1739
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001740 // TODO: what if class is NULL?
1741 if (cl != NULL)
1742 {
1743 lp->ll_valtype = NULL;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001744
1745 if (flags & GLV_PREFER_FUNC)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001746 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001747 // First look for a function with this name.
1748 // round 1: class functions (skipped for an object)
1749 // round 2: object methods
1750 for (int round = v_type == VAR_OBJECT ? 2 : 1;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001751 round <= 2; ++round)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001752 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001753 int m_idx;
1754 ufunc_T *fp;
1755
1756 fp = method_lookup(cl,
1757 round == 1 ? VAR_CLASS : VAR_OBJECT,
1758 key, p - key, &m_idx);
Ernie Raelee865f32023-09-29 19:53:55 +02001759 lp->ll_oi = m_idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001760 if (fp != NULL)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001761 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001762 lp->ll_ufunc = fp;
1763 lp->ll_valtype = fp->uf_func_type;
1764 break;
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001765 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001766 }
1767 }
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001768
1769 if (lp->ll_valtype == NULL)
1770 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001771 int m_idx;
Ernie Raelee865f32023-09-29 19:53:55 +02001772 ocmember_T *om
1773 = member_lookup(cl, v_type, key, p - key, &m_idx);
1774 lp->ll_oi = m_idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001775 if (om != NULL)
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001776 {
Ernie Rael64885642023-10-04 20:16:22 +02001777 if (get_lval_check_access(cl_exec, cl, om,
1778 p, flags) == FAIL)
1779 return NULL;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001780
Yegappan Lakshmanane5437c52023-12-16 14:11:19 +01001781 // When lhs is used to modify the variable, check it is
1782 // not a read-only variable.
1783 if ((flags & GLV_READ_ONLY) == 0
1784 && (*p != '.' && *p != '[')
1785 && oc_var_check_ro(cl, om))
1786 return NULL;
1787
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001788 lp->ll_valtype = om->ocm_type;
1789
1790 if (v_type == VAR_OBJECT)
1791 lp->ll_tv = ((typval_T *)(
1792 lp->ll_tv->vval.v_object + 1)) + m_idx;
1793 else
1794 lp->ll_tv = &cl->class_members_tv[m_idx];
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001795 }
1796 }
1797
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001798 if (lp->ll_valtype == NULL)
1799 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001800 member_not_found_msg(cl, v_type, key, p - key);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001801 return NULL;
1802 }
1803 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001804 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001805 }
1806
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001807 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001809 return p;
1810}
1811
1812/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001813 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001814 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001815 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001816clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001817{
1818 vim_free(lp->ll_exp_name);
1819 vim_free(lp->ll_newkey);
1820}
1821
1822/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001823 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001824 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001825 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1826 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001827 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001828 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001829set_var_lval(
1830 lval_T *lp,
1831 char_u *endp,
1832 typval_T *rettv,
1833 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001834 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1835 char_u *op,
1836 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001837{
1838 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001839 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001840
1841 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001842 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001843 cc = *endp;
1844 *endp = NUL;
h-east2bd6a092023-05-19 19:01:17 +01001845 if (in_vim9script() && check_reserved_name(lp->ll_name, FALSE) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001846 return;
1847
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001848 if (lp->ll_blob != NULL)
1849 {
1850 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001851
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001852 if (op != NULL && *op != '=')
1853 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001854 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001855 return;
1856 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001857 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001858 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001859
1860 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1861 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001862 if (lp->ll_empty2)
1863 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1864
Bram Moolenaar68452172021-04-12 21:21:02 +02001865 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1866 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001867 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001868 }
1869 else
1870 {
1871 val = (int)tv_get_number_chk(rettv, &error);
1872 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001873 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001874 }
1875 }
1876 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001877 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001878 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001879
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001880 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1881 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001882 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001883 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001884 *endp = cc;
1885 return;
1886 }
1887
Bram Moolenaarff697e62019-02-12 22:28:33 +01001888 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001889 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001890 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001891 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001892 {
Ernie Raele75fde62023-12-21 17:18:54 +01001893 if (di != NULL && check_typval_is_value(&di->di_tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001894 {
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001895 clear_tv(&tv);
1896 return;
1897 }
1898
Bram Moolenaar79518e22017-02-17 16:31:35 +01001899 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001900 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1901 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001902 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001903 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001904 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001905 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001906 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001908 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001909 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001910 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1911 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001912 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001913 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001914 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001915 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001916 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001917 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001918 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001919 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001920 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001921 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001922 else if (lp->ll_range)
1923 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001924 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1925 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001926 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001927 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001928 return;
1929 }
1930
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001931 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1932 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001933 }
1934 else
1935 {
1936 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001937 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001938 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001939 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1940 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001941 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001942 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001943 return;
1944 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001945
1946 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001947 && check_typval_arg_type(lp->ll_valtype, rettv,
1948 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001949 return;
1950
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001951 if (lp->ll_newkey != NULL)
1952 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001953 if (op != NULL && *op != '=')
1954 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001955 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001956 return;
1957 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001958 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1959 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001960 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001961
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001962 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001963 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001964 if (di == NULL)
1965 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001966 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1967 {
1968 vim_free(di);
1969 return;
1970 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001971 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001972 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001973 else if (op != NULL && *op != '=')
1974 {
1975 tv_op(lp->ll_tv, rettv, op);
1976 return;
1977 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001978 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001979 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001980
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001981 /*
1982 * Assign the value to the variable or list item.
1983 */
1984 if (copy)
1985 copy_tv(rettv, lp->ll_tv);
1986 else
1987 {
1988 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001989 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001990 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001991 }
1992 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001993}
1994
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001995/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001996 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1997 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001998 * Returns OK or FAIL.
1999 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002000 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002001tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002002{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002003 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002004 char_u numbuf[NUMBUFLEN];
2005 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002006 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002007
Ernie Raele75fde62023-12-21 17:18:54 +01002008 // Can't do anything with a Funcref or Dict or Type on the right.
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02002009 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002010 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Ernie Raele75fde62023-12-21 17:18:54 +01002011 && tv2->v_type != VAR_CLASS && tv2->v_type != VAR_TYPEALIAS
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02002012 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
2013 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002014 {
2015 switch (tv1->v_type)
2016 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002017 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002018 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002019 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002020 case VAR_DICT:
2021 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002022 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002023 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002024 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002025 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002026 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002027 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002028 case VAR_OBJECT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002029 break;
Ernie Raele75fde62023-12-21 17:18:54 +01002030 case VAR_CLASS:
2031 case VAR_TYPEALIAS:
2032 check_typval_is_value(tv1);
2033 return FAIL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002034
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002035 case VAR_BLOB:
2036 if (*op != '+' || tv2->v_type != VAR_BLOB)
2037 break;
2038 // BLOB += BLOB
2039 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2040 {
2041 blob_T *b1 = tv1->vval.v_blob;
2042 blob_T *b2 = tv2->vval.v_blob;
2043 int i, len = blob_len(b2);
2044 for (i = 0; i < len; i++)
2045 ga_append(&b1->bv_ga, blob_get(b2, i));
2046 }
2047 return OK;
2048
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002049 case VAR_LIST:
2050 if (*op != '+' || tv2->v_type != VAR_LIST)
2051 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002052 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02002053 if (tv2->vval.v_list != NULL)
2054 {
2055 if (tv1->vval.v_list == NULL)
2056 {
2057 tv1->vval.v_list = tv2->vval.v_list;
2058 ++tv1->vval.v_list->lv_refcount;
2059 }
2060 else
2061 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2062 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002063 return OK;
2064
2065 case VAR_NUMBER:
2066 case VAR_STRING:
2067 if (tv2->v_type == VAR_LIST)
2068 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002069 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002070 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002071 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002072 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002073 if (tv2->v_type == VAR_FLOAT)
2074 {
2075 float_T f = n;
2076
Bram Moolenaarff697e62019-02-12 22:28:33 +01002077 if (*op == '%')
2078 break;
2079 switch (*op)
2080 {
2081 case '+': f += tv2->vval.v_float; break;
2082 case '-': f -= tv2->vval.v_float; break;
2083 case '*': f *= tv2->vval.v_float; break;
2084 case '/': f /= tv2->vval.v_float; break;
2085 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002086 clear_tv(tv1);
2087 tv1->v_type = VAR_FLOAT;
2088 tv1->vval.v_float = f;
2089 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002090 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002091 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002092 switch (*op)
2093 {
2094 case '+': n += tv_get_number(tv2); break;
2095 case '-': n -= tv_get_number(tv2); break;
2096 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002097 case '/': n = num_divide(n, tv_get_number(tv2),
2098 &failed); break;
2099 case '%': n = num_modulus(n, tv_get_number(tv2),
2100 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002101 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002102 clear_tv(tv1);
2103 tv1->v_type = VAR_NUMBER;
2104 tv1->vval.v_number = n;
2105 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002106 }
2107 else
2108 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002109 if (tv2->v_type == VAR_FLOAT)
2110 break;
2111
Bram Moolenaarff697e62019-02-12 22:28:33 +01002112 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002113 s = tv_get_string(tv1);
2114 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002115 clear_tv(tv1);
2116 tv1->v_type = VAR_STRING;
2117 tv1->vval.v_string = s;
2118 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002119 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002120
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002121 case VAR_FLOAT:
2122 {
2123 float_T f;
2124
Bram Moolenaarff697e62019-02-12 22:28:33 +01002125 if (*op == '%' || *op == '.'
2126 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002127 && tv2->v_type != VAR_NUMBER
2128 && tv2->v_type != VAR_STRING))
2129 break;
2130 if (tv2->v_type == VAR_FLOAT)
2131 f = tv2->vval.v_float;
2132 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002133 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002134 switch (*op)
2135 {
2136 case '+': tv1->vval.v_float += f; break;
2137 case '-': tv1->vval.v_float -= f; break;
2138 case '*': tv1->vval.v_float *= f; break;
2139 case '/': tv1->vval.v_float /= f; break;
2140 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002141 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002142 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002143 }
2144 }
2145
Ernie Raele75fde62023-12-21 17:18:54 +01002146 if (check_typval_is_value(tv2) == OK)
2147 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002148 return FAIL;
2149}
2150
2151/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002152 * Evaluate the expression used in a ":for var in expr" command.
2153 * "arg" points to "var".
2154 * Set "*errp" to TRUE for an error, FALSE otherwise;
2155 * Return a pointer that holds the info. Null when there is an error.
2156 */
2157 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002158eval_for_line(
2159 char_u *arg,
2160 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002161 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002162 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002163{
Bram Moolenaar33570922005-01-25 22:26:29 +00002164 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02002165 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002166 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002167 typval_T tv;
2168 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002169 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002170
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002171 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002172
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002173 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002174 if (fi == NULL)
2175 return NULL;
2176
Bram Moolenaar404557e2021-07-05 21:41:48 +02002177 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
2178 &fi->fi_semicolon, FALSE);
2179 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002180 return fi;
2181
Bram Moolenaar404557e2021-07-05 21:41:48 +02002182 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002183 if (expr[0] != 'i' || expr[1] != 'n'
2184 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002185 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02002186 if (in_vim9script() && *expr == ':' && expr != var_list_end)
2187 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
2188 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002189 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002190 return fi;
2191 }
2192
2193 if (skip)
2194 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002195 expr = skipwhite_and_linebreak(expr + 2, evalarg);
2196 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002197 {
2198 *errp = FALSE;
2199 if (!skip)
2200 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002201 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002202 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002203 l = tv.vval.v_list;
2204 if (l == NULL)
2205 {
2206 // a null list is like an empty list: do nothing
2207 clear_tv(&tv);
2208 }
2209 else
2210 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002211 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002212 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002213
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002214 // No need to increment the refcount, it's already set for
2215 // the list being used in "tv".
2216 fi->fi_list = l;
2217 list_add_watch(l, &fi->fi_lw);
2218 fi->fi_lw.lw_item = l->lv_first;
2219 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002220 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002221 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002222 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002223 fi->fi_bi = 0;
2224 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002225 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002226 typval_T btv;
2227
2228 // Make a copy, so that the iteration still works when the
2229 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002230 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002231 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002232 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002233 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002234 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002235 else if (tv.v_type == VAR_STRING)
2236 {
2237 fi->fi_byte_idx = 0;
2238 fi->fi_string = tv.vval.v_string;
2239 tv.vval.v_string = NULL;
2240 if (fi->fi_string == NULL)
2241 fi->fi_string = vim_strsave((char_u *)"");
2242 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002243 else
2244 {
Sean Dewar80d73952021-08-04 19:25:54 +02002245 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002246 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002247 }
2248 }
2249 }
2250 if (skip)
2251 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002252 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002253
2254 return fi;
2255}
2256
2257/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002258 * Used when looping over a :for line, skip the "in expr" part.
2259 */
2260 void
2261skip_for_lines(void *fi_void, evalarg_T *evalarg)
2262{
2263 forinfo_T *fi = (forinfo_T *)fi_void;
2264 int i;
2265
2266 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002267 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002268}
2269
2270/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002271 * Use the first item in a ":for" list. Advance to the next.
2272 * Assign the values to the variable (list). "arg" points to the first one.
2273 * Return TRUE when a valid item was found, FALSE when at end of list or
2274 * something wrong.
2275 */
2276 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002277next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002278{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002279 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002280 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002281 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002282 ? (ASSIGN_FINAL
2283 // first round: error if variable exists
2284 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002285 | ASSIGN_NO_MEMBER_TYPE
2286 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002287 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002288 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002289 int skip_assign = in_vim9script() && arg[0] == '_'
2290 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002291
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002292 if (fi->fi_blob != NULL)
2293 {
2294 typval_T tv;
2295
2296 if (fi->fi_bi >= blob_len(fi->fi_blob))
2297 return FALSE;
2298 tv.v_type = VAR_NUMBER;
2299 tv.v_lock = VAR_FIXED;
2300 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2301 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002302 if (skip_assign)
2303 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002304 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002305 fi->fi_varcount, flag, NULL) == OK;
2306 }
2307
2308 if (fi->fi_string != NULL)
2309 {
2310 typval_T tv;
2311 int len;
2312
2313 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2314 if (len == 0)
2315 return FALSE;
2316 tv.v_type = VAR_STRING;
2317 tv.v_lock = VAR_FIXED;
2318 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2319 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002320 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002321 if (skip_assign)
2322 result = TRUE;
2323 else
2324 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002325 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002326 vim_free(tv.vval.v_string);
2327 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002328 }
2329
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002330 item = fi->fi_lw.lw_item;
2331 if (item == NULL)
2332 result = FALSE;
2333 else
2334 {
2335 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002336 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002337 if (skip_assign)
2338 result = TRUE;
2339 else
2340 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002341 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002342 }
2343 return result;
2344}
2345
2346/*
2347 * Free the structure used to store info used by ":for".
2348 */
2349 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002350free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002351{
Bram Moolenaar33570922005-01-25 22:26:29 +00002352 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002353
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002354 if (fi == NULL)
2355 return;
2356 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002357 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002358 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002359 list_unref(fi->fi_list);
2360 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002361 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002362 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002363 else
2364 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002365 vim_free(fi);
2366}
2367
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002369set_context_for_expression(
2370 expand_T *xp,
2371 char_u *arg,
2372 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002374 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002376 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002378 if (cmdidx == CMD_let || cmdidx == CMD_var
2379 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002380 {
2381 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002382 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002383 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002384 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002385 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002386 {
2387 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002388 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002389 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002390 break;
2391 }
2392 return;
2393 }
2394 }
2395 else
2396 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2397 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 while ((xp->xp_pattern = vim_strpbrk(arg,
2399 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2400 {
2401 c = *xp->xp_pattern;
2402 if (c == '&')
2403 {
2404 c = xp->xp_pattern[1];
2405 if (c == '&')
2406 {
2407 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002408 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 }
2410 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002411 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002413 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2414 xp->xp_pattern += 2;
2415
2416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 }
2418 else if (c == '$')
2419 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002420 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 xp->xp_context = EXPAND_ENV_VARS;
2422 }
2423 else if (c == '=')
2424 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002425 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 xp->xp_context = EXPAND_EXPRESSION;
2427 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002428 else if (c == '#'
2429 && xp->xp_context == EXPAND_EXPRESSION)
2430 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002431 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002432 break;
2433 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002434 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 && xp->xp_context == EXPAND_FUNCTIONS
2436 && vim_strchr(xp->xp_pattern, '(') == NULL)
2437 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002438 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 break;
2440 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002441 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002443 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 {
2445 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2446 if (c == '\\' && xp->xp_pattern[1] != NUL)
2447 ++xp->xp_pattern;
2448 xp->xp_context = EXPAND_NOTHING;
2449 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002450 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002452 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2454 /* skip */ ;
2455 xp->xp_context = EXPAND_NOTHING;
2456 }
2457 else if (c == '|')
2458 {
2459 if (xp->xp_pattern[1] == '|')
2460 {
2461 ++xp->xp_pattern;
2462 xp->xp_context = EXPAND_EXPRESSION;
2463 }
2464 else
2465 xp->xp_context = EXPAND_COMMANDS;
2466 }
2467 else
2468 xp->xp_context = EXPAND_EXPRESSION;
2469 }
2470 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002471 // Doesn't look like something valid, expand as an expression
2472 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002473 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 arg = xp->xp_pattern;
2475 if (*arg != NUL)
2476 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2477 /* skip */ ;
2478 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002479
2480 // ":exe one two" completes "two"
2481 if ((cmdidx == CMD_execute
2482 || cmdidx == CMD_echo
2483 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002484 || cmdidx == CMD_echomsg
2485 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002486 && xp->xp_context == EXPAND_EXPRESSION)
2487 {
2488 for (;;)
2489 {
2490 char_u *n = skiptowhite(arg);
2491
2492 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2493 break;
2494 arg = skipwhite(n);
2495 }
2496 }
2497
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 xp->xp_pattern = arg;
2499}
2500
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002502 * Return TRUE if "pat" matches "text".
2503 * Does not use 'cpo' and always uses 'magic'.
2504 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002505 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002506pattern_match(char_u *pat, char_u *text, int ic)
2507{
2508 int matches = FALSE;
2509 char_u *save_cpo;
2510 regmatch_T regmatch;
2511
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002512 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002513 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002514 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002515 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2516 if (regmatch.regprog != NULL)
2517 {
2518 regmatch.rm_ic = ic;
2519 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2520 vim_regfree(regmatch.regprog);
2521 }
2522 p_cpo = save_cpo;
2523 return matches;
2524}
2525
2526/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002527 * Handle a name followed by "(". Both for just "name(arg)" and for
2528 * "expr->name(arg)".
2529 * Returns OK or FAIL.
2530 */
2531 static int
2532eval_func(
2533 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002534 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002535 char_u *name,
2536 int name_len,
2537 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002538 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002539 typval_T *basetv) // "expr" for "expr->name(arg)"
2540{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002541 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002542 char_u *s = name;
2543 int len = name_len;
2544 partial_T *partial;
2545 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002546 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002547 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002548
2549 if (!evaluate)
2550 check_vars(s, len);
2551
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002552 // If "s" is the name of a variable of type VAR_FUNC
2553 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002554 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002555 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002556
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002557 // Need to make a copy, in case evaluating the arguments makes
2558 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002559 s = vim_strsave(s);
zeertzjqb7f6f932023-04-13 22:12:50 +01002560 if (s == NULL || (evaluate && *s == NUL))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002561 ret = FAIL;
2562 else
2563 {
2564 funcexe_T funcexe;
2565
2566 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002567 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002568 funcexe.fe_firstline = curwin->w_cursor.lnum;
2569 funcexe.fe_lastline = curwin->w_cursor.lnum;
2570 funcexe.fe_evaluate = evaluate;
2571 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02002572 if (partial != NULL)
2573 {
2574 funcexe.fe_object = partial->pt_obj;
2575 if (funcexe.fe_object != NULL)
2576 ++funcexe.fe_object->obj_refcount;
2577 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002578 funcexe.fe_basetv = basetv;
2579 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002580 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002581 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002582 }
2583 vim_free(s);
2584
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002585 // If evaluate is FALSE rettv->v_type was not set in
2586 // get_func_tv, but it's needed in handle_subscript() to parse
2587 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002588 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2589 {
2590 rettv->vval.v_string = NULL;
2591 rettv->v_type = VAR_FUNC;
2592 }
2593
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002594 // Stop the expression evaluation when immediately
2595 // aborting on error, or when an interrupt occurred or
2596 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002597 if (evaluate && aborting())
2598 {
2599 if (ret == OK)
2600 clear_tv(rettv);
2601 ret = FAIL;
2602 }
2603 return ret;
2604}
2605
2606/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002607 * After a NL, skip over empty lines and comment-only lines.
2608 */
2609 static char_u *
2610newline_skip_comments(char_u *arg)
2611{
2612 char_u *p = arg + 1;
2613
2614 for (;;)
2615 {
2616 p = skipwhite(p);
2617
2618 if (*p == NUL)
2619 break;
2620 if (vim9_comment_start(p))
2621 {
2622 char_u *nl = vim_strchr(p, NL);
2623
2624 if (nl == NULL)
2625 break;
2626 p = nl;
2627 }
2628 if (*p != NL)
2629 break;
2630 ++p; // skip another NL
2631 }
2632 return p;
2633}
2634
2635/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002636 * Get the next line source line without advancing. But do skip over comment
2637 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002638 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002639 */
2640 static char_u *
2641getline_peek_skip_comments(evalarg_T *evalarg)
2642{
2643 for (;;)
2644 {
2645 char_u *next = getline_peek(evalarg->eval_getline,
2646 evalarg->eval_cookie);
2647 char_u *p;
2648
2649 if (next == NULL)
2650 break;
2651 p = skipwhite(next);
2652 if (*p != NUL && !vim9_comment_start(p))
2653 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002654 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002655 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002656 }
2657 return NULL;
2658}
2659
2660/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002661 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2662 * comment) and there is a next line, return the next line (skipping blanks)
2663 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002664 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2665 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002666 * "arg" must point somewhere inside a line, not at the start.
2667 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002668 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002669eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2670{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002671 char_u *p = skipwhite(arg);
2672
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002673 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002674 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002675 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002676 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2677 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002678 && (*p == NUL || *p == NL
2679 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002680 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002681 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002682
Bram Moolenaare442d592022-05-05 12:20:28 +01002683 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002684 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002685 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002686 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002687 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002688 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002689
Bram Moolenaar9d489562020-07-30 20:08:50 +02002690 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002691 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002692 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002693 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002694 }
2695 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002696 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002697}
2698
2699/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002700 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002701 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002702 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002703 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002704eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002705{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002706 garray_T *gap = &evalarg->eval_ga;
2707 char_u *line;
2708
Bram Moolenaar39be4982022-05-06 21:51:50 +01002709 if (arg != NULL)
2710 {
2711 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002712 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002713 // Truncate before a trailing comment, so that concatenating the lines
2714 // won't turn the rest into a comment.
2715 if (*skipwhite(arg) == '#')
2716 *arg = NUL;
2717 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002718
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002719 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002720 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2721 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002722 else
2723 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002724 if (line == NULL)
2725 return NULL;
2726
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002727 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002728 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2729 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002730 char_u *p = skipwhite(line);
2731
2732 // Going to concatenate the lines after parsing. For an empty or
2733 // comment line use an empty string.
2734 if (*p == NUL || vim9_comment_start(p))
2735 {
2736 vim_free(line);
2737 line = vim_strsave((char_u *)"");
2738 }
2739
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002740 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2741 ++gap->ga_len;
2742 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002743 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002744 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002745 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002746 evalarg->eval_tofree = line;
2747 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002748
2749 // Advanced to the next line, "arg" no longer points into the previous
2750 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002751 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002752 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002753}
2754
Bram Moolenaare6b53242020-07-01 17:28:33 +02002755/*
2756 * Call eval_next_non_blank() and get the next line if needed.
2757 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002758 char_u *
2759skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2760{
2761 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002762 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002763
Bram Moolenaar962d7212020-07-04 14:15:00 +02002764 if (evalarg == NULL)
2765 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002766 eval_next_non_blank(p, evalarg, &getnext);
2767 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002768 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002769 return p;
2770}
2771
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002772/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002773 * The "eval" functions have an "evalarg" argument: When NULL or
2774 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
2775 * parsed but not executed. The functions may return OK, but the rettv will be
2776 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 */
2778
2779/*
2780 * Handle zero level expression.
2781 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002782 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002783 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002784 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 * Return OK or FAIL.
2786 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002787 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002788eval0(
2789 char_u *arg,
2790 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002791 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002792 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002794 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2795}
2796
2797/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002798 * If "arg" is a simple function call without arguments then call it and return
2799 * the result. Otherwise return NOTDONE.
2800 */
2801 int
2802may_call_simple_func(
2803 char_u *arg,
2804 typval_T *rettv)
2805{
2806 char_u *parens = (char_u *)strstr((char *)arg, "()");
2807 int r = NOTDONE;
2808
2809 // If the expression is "FuncName()" then we can skip a lot of overhead.
2810 if (parens != NULL && *skipwhite(parens + 2) == NUL)
2811 {
2812 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
2813
2814 if (to_name_end(p, TRUE) == parens)
2815 r = call_simple_func(arg, (int)(parens - arg), rettv);
2816 }
2817 return r;
2818}
2819
2820/*
2821 * Handle zero level expression with optimization for a simple function call.
2822 * Same arguments and return value as eval0().
2823 */
2824 int
2825eval0_simple_funccal(
2826 char_u *arg,
2827 typval_T *rettv,
2828 exarg_T *eap,
2829 evalarg_T *evalarg)
2830{
2831 int r = may_call_simple_func(arg, rettv);
2832
2833 if (r == NOTDONE)
2834 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
2835 return r;
2836}
2837
2838/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002839 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2840 * expression and don't check what comes after the expression.
2841 */
2842 int
2843eval0_retarg(
2844 char_u *arg,
2845 typval_T *rettv,
2846 exarg_T *eap,
2847 evalarg_T *evalarg,
2848 char_u **retarg)
2849{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 int ret;
2851 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002852 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002853 int did_emsg_before = did_emsg;
2854 int called_emsg_before = called_emsg;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002855 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002856 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857
2858 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002859 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002860
Bram Moolenaar79481362022-06-27 20:15:10 +01002861 if (ret != FAIL)
2862 {
2863 expr_end = p;
2864 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002865
Bram Moolenaar79481362022-06-27 20:15:10 +01002866 // In Vim9 script a command block is not split at NL characters for
2867 // commands using an expression argument. Skip over a '#' comment to
2868 // check for a following NL. Require white space before the '#'.
2869 if (in_vim9script() && p > expr_end && retarg == NULL)
2870 while (*p == '#')
2871 {
2872 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002873
Bram Moolenaar79481362022-06-27 20:15:10 +01002874 if (nl == NULL)
2875 break;
2876 p = skipwhite(nl + 1);
2877 if (eap != NULL && *p != NUL)
2878 eap->nextcmd = p;
2879 check_for_end = FALSE;
2880 }
2881
2882 if (check_for_end)
2883 end_error = !ends_excmd2(arg, p);
2884 }
2885
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002886 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 {
2888 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002889 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 /*
2891 * Report the invalid expression unless the expression evaluation has
2892 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002893 * exception, or we already gave a more specific error.
2894 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002896 if (!aborting()
2897 && did_emsg == did_emsg_before
2898 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002899 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002900 {
2901 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002902 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002903 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002904 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002905 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002906
zeertzjqf2588b62023-05-05 17:22:35 +01002907 if (eap != NULL && p != NULL)
2908 {
2909 // Some of the expression may not have been consumed.
2910 // Only execute a next command if it cannot be a "||" operator.
2911 // The next command may be "catch".
2912 char_u *nextcmd = check_nextcmd(p);
2913 if (nextcmd != NULL && *nextcmd != '|')
2914 eap->nextcmd = nextcmd;
2915 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002916 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002918
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002919 if (retarg != NULL)
2920 *retarg = p;
2921 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002922 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002923
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 return ret;
2925}
2926
2927/*
2928 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002929 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002930 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 *
2932 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002933 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002935 * Note: "rettv.v_lock" is not set.
2936 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 * Return OK or FAIL.
2938 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002939 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002940eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002942 char_u *p;
2943 int getnext;
2944
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002945 CLEAR_POINTER(rettv);
2946
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 /*
2948 * Get the first variable.
2949 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002950 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 return FAIL;
2952
Bram Moolenaar793648f2020-06-26 21:28:25 +02002953 p = eval_next_non_blank(*arg, evalarg, &getnext);
2954 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002956 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002957 int result;
2958 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002959 evalarg_T *evalarg_used = evalarg;
2960 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002961 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002962 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002963 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002964
2965 if (evalarg == NULL)
2966 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002967 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002968 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002969 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002970 orig_flags = evalarg_used->eval_flags;
2971 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002972
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002973 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002974 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002975 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002976 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002977 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002978 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002979 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002980 clear_tv(rettv);
2981 return FAIL;
2982 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002983 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002984 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002985
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002987 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002989 int error = FALSE;
2990
Bram Moolenaar13106602020-10-04 16:06:05 +02002991 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002992 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002993 else if (vim9script)
2994 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002995 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002997 if (error || !op_falsy || !result)
2998 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002999 if (error)
3000 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 }
3002
3003 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02003004 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003006 if (op_falsy)
3007 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02003008 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003009 {
mityu4ac198c2021-05-28 17:52:40 +02003010 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003011 clear_tv(rettv);
3012 return FAIL;
3013 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003014 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003015 evalarg_used->eval_flags = (op_falsy ? !result : result)
zeertzjqb7f6f932023-04-13 22:12:50 +01003016 ? orig_flags : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003017 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02003018 {
3019 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02003021 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003022 if (!op_falsy || !result)
3023 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003025 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003027 /*
3028 * Check for the ":".
3029 */
3030 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
3031 if (*p != ':')
3032 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003033 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003034 if (evaluate && result)
3035 clear_tv(rettv);
3036 evalarg_used->eval_flags = orig_flags;
3037 return FAIL;
3038 }
3039 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003040 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003041 else
3042 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003043 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003044 {
3045 error_white_both(p, 1);
3046 clear_tv(rettv);
3047 evalarg_used->eval_flags = orig_flags;
3048 return FAIL;
3049 }
3050 *arg = p;
3051 }
3052
3053 /*
3054 * Get the third variable. Recursive!
3055 */
Bram Moolenaar13106602020-10-04 16:06:05 +02003056 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003057 {
mityu4ac198c2021-05-28 17:52:40 +02003058 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003059 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02003060 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003061 return FAIL;
3062 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003063 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
3064 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003065 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003066 if (eval1(arg, &var2, evalarg_used) == FAIL)
3067 {
3068 if (evaluate && result)
3069 clear_tv(rettv);
3070 evalarg_used->eval_flags = orig_flags;
3071 return FAIL;
3072 }
3073 if (evaluate && !result)
3074 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003076
3077 if (evalarg == NULL)
3078 clear_evalarg(&local_evalarg, NULL);
3079 else
3080 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 }
3082
3083 return OK;
3084}
3085
3086/*
3087 * Handle first level expression:
3088 * expr2 || expr2 || expr2 logical OR
3089 *
3090 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003091 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 *
3093 * Return OK or FAIL.
3094 */
3095 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003096eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003098 char_u *p;
3099 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100
3101 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003102 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003104 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 return FAIL;
3106
3107 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003108 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003110 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003111 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003113 evalarg_T *evalarg_used = evalarg;
3114 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003115 int evaluate;
3116 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003117 long result = FALSE;
3118 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003119 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003120 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003121
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003122 if (evalarg == NULL)
3123 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003124 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003125 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003126 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003127 orig_flags = evalarg_used->eval_flags;
3128 evaluate = orig_flags & EVAL_EVALUATE;
3129 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003130 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003131 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003132 result = tv_get_bool_chk(rettv, &error);
3133 else if (tv_get_number_chk(rettv, &error) != 0)
3134 result = TRUE;
3135 clear_tv(rettv);
3136 if (error)
3137 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 }
3139
3140 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003141 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003143 while (p[0] == '|' && p[1] == '|')
3144 {
3145 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003146 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003147 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003148 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003149 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003150 {
3151 error_white_both(p, 2);
3152 clear_tv(rettv);
3153 return FAIL;
3154 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003155 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003156 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003157
3158 /*
3159 * Get the second variable.
3160 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003161 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003162 {
mityu4ac198c2021-05-28 17:52:40 +02003163 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003164 clear_tv(rettv);
3165 return FAIL;
3166 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003167 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3168 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003169 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003170 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003171 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003172
3173 /*
3174 * Compute the result.
3175 */
3176 if (evaluate && !result)
3177 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003178 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003179 result = tv_get_bool_chk(&var2, &error);
3180 else if (tv_get_number_chk(&var2, &error) != 0)
3181 result = TRUE;
3182 clear_tv(&var2);
3183 if (error)
3184 return FAIL;
3185 }
3186 if (evaluate)
3187 {
3188 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003189 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003190 rettv->v_type = VAR_BOOL;
3191 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003192 }
3193 else
3194 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003195 rettv->v_type = VAR_NUMBER;
3196 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003197 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003198 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003199
3200 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003202
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003203 if (evalarg == NULL)
3204 clear_evalarg(&local_evalarg, NULL);
3205 else
3206 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 }
3208
3209 return OK;
3210}
3211
3212/*
3213 * Handle second level expression:
3214 * expr3 && expr3 && expr3 logical AND
3215 *
3216 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003217 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 *
3219 * Return OK or FAIL.
3220 */
3221 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003222eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003224 char_u *p;
3225 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226
3227 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003228 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003230 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 return FAIL;
3232
3233 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003234 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003236 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003237 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003239 evalarg_T *evalarg_used = evalarg;
3240 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003241 int orig_flags;
3242 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003243 long result = TRUE;
3244 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003245 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003246 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003247
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003248 if (evalarg == NULL)
3249 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003250 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003251 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003252 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003253 orig_flags = evalarg_used->eval_flags;
3254 evaluate = orig_flags & EVAL_EVALUATE;
3255 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003256 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003257 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003258 result = tv_get_bool_chk(rettv, &error);
3259 else if (tv_get_number_chk(rettv, &error) == 0)
3260 result = FALSE;
3261 clear_tv(rettv);
3262 if (error)
3263 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 }
3265
3266 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003267 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003269 while (p[0] == '&' && p[1] == '&')
3270 {
3271 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003272 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003273 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003274 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003275 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003276 {
3277 error_white_both(p, 2);
3278 clear_tv(rettv);
3279 return FAIL;
3280 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003281 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003282 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003283
3284 /*
3285 * Get the second variable.
3286 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003287 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003288 {
mityu4ac198c2021-05-28 17:52:40 +02003289 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003290 clear_tv(rettv);
3291 return FAIL;
3292 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003293 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3294 evalarg_used->eval_flags = result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003295 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003296 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003297 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003298 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003299
3300 /*
3301 * Compute the result.
3302 */
3303 if (evaluate && result)
3304 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003305 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003306 result = tv_get_bool_chk(&var2, &error);
3307 else if (tv_get_number_chk(&var2, &error) == 0)
3308 result = FALSE;
3309 clear_tv(&var2);
3310 if (error)
3311 return FAIL;
3312 }
3313 if (evaluate)
3314 {
3315 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003316 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003317 rettv->v_type = VAR_BOOL;
3318 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003319 }
3320 else
3321 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003322 rettv->v_type = VAR_NUMBER;
3323 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003324 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003325 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003326
3327 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003329
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003330 if (evalarg == NULL)
3331 clear_evalarg(&local_evalarg, NULL);
3332 else
3333 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 }
3335
3336 return OK;
3337}
3338
3339/*
3340 * Handle third level expression:
3341 * var1 == var2
3342 * var1 =~ var2
3343 * var1 != var2
3344 * var1 !~ var2
3345 * var1 > var2
3346 * var1 >= var2
3347 * var1 < var2
3348 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003349 * var1 is var2
3350 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 *
3352 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003353 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 *
3355 * Return OK or FAIL.
3356 */
3357 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003358eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003361 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003362 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003364 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365
3366 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003367 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003369 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 return FAIL;
3371
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003372 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003373
Bram Moolenaar696ba232020-07-29 21:20:41 +02003374 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375
3376 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003377 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003379 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003381 typval_T var2;
3382 int ic;
3383 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003384 int evaluate = evalarg == NULL
3385 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003386 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003387
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003388 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003389 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003390 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003391 p = *arg;
3392 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003393 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3394 {
mityu4ac198c2021-05-28 17:52:40 +02003395 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003396 clear_tv(rettv);
3397 return FAIL;
3398 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003399
Bram Moolenaar696ba232020-07-29 21:20:41 +02003400 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3401 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003402 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003403 clear_tv(rettv);
3404 return FAIL;
3405 }
3406
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003407 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 if (p[len] == '?')
3409 {
3410 ic = TRUE;
3411 ++len;
3412 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003413 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 else if (p[len] == '#')
3415 {
3416 ic = FALSE;
3417 ++len;
3418 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003419 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003421 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422
3423 /*
3424 * Get the second variable.
3425 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003426 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3427 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003428 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003429 clear_tv(rettv);
3430 return FAIL;
3431 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003432 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003433 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003435 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 return FAIL;
3437 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003438 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003439 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003440 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003441
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003442 // use the line of the comparison for messages
3443 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003444 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003445 {
3446 ret = FAIL;
3447 clear_tv(rettv);
3448 }
3449 else
3450 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003451 clear_tv(&var2);
3452 return ret;
3453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 }
3455
3456 return OK;
3457}
3458
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003459/*
3460 * Make a copy of blob "tv1" and append blob "tv2".
3461 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003462 void
3463eval_addblob(typval_T *tv1, typval_T *tv2)
3464{
3465 blob_T *b1 = tv1->vval.v_blob;
3466 blob_T *b2 = tv2->vval.v_blob;
3467 blob_T *b = blob_alloc();
3468 int i;
3469
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003470 if (b == NULL)
3471 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003472
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003473 for (i = 0; i < blob_len(b1); i++)
3474 ga_append(&b->bv_ga, blob_get(b1, i));
3475 for (i = 0; i < blob_len(b2); i++)
3476 ga_append(&b->bv_ga, blob_get(b2, i));
3477
3478 clear_tv(tv1);
3479 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003480}
3481
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003482/*
3483 * Make a copy of list "tv1" and append list "tv2".
3484 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485 int
3486eval_addlist(typval_T *tv1, typval_T *tv2)
3487{
3488 typval_T var3;
3489
3490 // concatenate Lists
3491 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3492 {
3493 clear_tv(tv1);
3494 clear_tv(tv2);
3495 return FAIL;
3496 }
3497 clear_tv(tv1);
3498 *tv1 = var3;
3499 return OK;
3500}
3501
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003503 * Handle the bitwise left/right shift operator expression:
3504 * var1 << var2
3505 * var1 >> var2
3506 *
3507 * "arg" must point to the first non-white of the expression.
3508 * "arg" is advanced to just after the recognized expression.
3509 *
3510 * Return OK or FAIL.
3511 */
3512 static int
3513eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3514{
3515 /*
3516 * Get the first expression.
3517 */
3518 if (eval6(arg, rettv, evalarg) == FAIL)
3519 return FAIL;
3520
3521 /*
3522 * Repeat computing, until no '<<' or '>>' is following.
3523 */
3524 for (;;)
3525 {
3526 char_u *p;
3527 int getnext;
3528 exprtype_T type;
3529 int evaluate;
3530 typval_T var2;
3531 int vim9script;
3532
3533 p = eval_next_non_blank(*arg, evalarg, &getnext);
3534 if (p[0] == '<' && p[1] == '<')
3535 type = EXPR_LSHIFT;
3536 else if (p[0] == '>' && p[1] == '>')
3537 type = EXPR_RSHIFT;
3538 else
3539 return OK;
3540
3541 // Handle a bitwise left or right shift operator
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02003542 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3543 if (evaluate && rettv->v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003544 {
3545 // left operand should be a number
3546 emsg(_(e_bitshift_ops_must_be_number));
3547 clear_tv(rettv);
3548 return FAIL;
3549 }
3550
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003551 vim9script = in_vim9script();
3552 if (getnext)
3553 {
3554 *arg = eval_next_line(*arg, evalarg);
3555 p = *arg;
3556 }
3557 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3558 {
3559 error_white_both(*arg, 2);
3560 clear_tv(rettv);
3561 return FAIL;
3562 }
3563
3564 /*
3565 * Get the second variable.
3566 */
3567 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3568 {
3569 error_white_both(p, 2);
3570 clear_tv(rettv);
3571 return FAIL;
3572 }
3573 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3574 if (eval6(arg, &var2, evalarg) == FAIL)
3575 {
3576 clear_tv(rettv);
3577 return FAIL;
3578 }
3579
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003580 if (evaluate)
3581 {
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02003582 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3583 {
3584 // right operand should be a positive number
3585 if (var2.v_type != VAR_NUMBER)
3586 emsg(_(e_bitshift_ops_must_be_number));
3587 else
3588 emsg(_(e_bitshift_ops_must_be_positive));
3589 clear_tv(rettv);
3590 clear_tv(&var2);
3591 return FAIL;
3592 }
3593
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003594 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3595 // shifting more bits than we have always results in zero
3596 rettv->vval.v_number = 0;
3597 else if (type == EXPR_LSHIFT)
3598 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003599 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003600 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003601 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003602 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003603 }
3604
3605 clear_tv(&var2);
3606 }
3607
3608 return OK;
3609}
3610
3611/*
3612 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003613 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003615 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003616 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 *
3618 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003619 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 *
3621 * Return OK or FAIL.
3622 */
3623 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003624eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003627 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003629 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 return FAIL;
3631
3632 /*
3633 * Repeat computing, until no '+', '-' or '.' is following.
3634 */
3635 for (;;)
3636 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003637 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003638 int getnext;
3639 char_u *p;
3640 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003641 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003642 int concat;
3643 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003644 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003645
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003646 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003647 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003648 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003649 p = eval_next_non_blank(*arg, evalarg, &getnext);
3650 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003651 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003652 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3653 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003655 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3656 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003657
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003658 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003659 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003660 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003661 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003662 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003663 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003664 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003665 {
mityu4ac198c2021-05-28 17:52:40 +02003666 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003667 clear_tv(rettv);
3668 return FAIL;
3669 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003670 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003671 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003672 if ((op != '+' || (rettv->v_type != VAR_LIST
3673 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003674 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003675 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003676 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003677 int error = FALSE;
3678
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003679 // For "list + ...", an illegal use of the first operand as
3680 // a number cannot be determined before evaluating the 2nd
3681 // operand: if this is also a list, all is ok.
3682 // For "something . ...", "something - ..." or "non-list + ...",
3683 // we know that the first operand needs to be a string or number
3684 // without evaluating the 2nd operand. So check before to avoid
3685 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003686 if (op != '.')
3687 tv_get_number_chk(rettv, &error);
3688 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003689 {
3690 clear_tv(rettv);
3691 return FAIL;
3692 }
3693 }
3694
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 /*
3696 * Get the second variable.
3697 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003698 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003699 {
mityu89dcb4d2021-05-28 13:50:17 +02003700 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003701 clear_tv(rettv);
3702 return FAIL;
3703 }
3704 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003705 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003707 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 return FAIL;
3709 }
3710
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003711 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 {
3713 /*
3714 * Compute the result.
3715 */
3716 if (op == '.')
3717 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003718 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3719 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003720 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003721
Bram Moolenaar2e086612020-08-25 22:37:48 +02003722 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003723 || var2.v_type == VAR_CHANNEL
3724 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003725 semsg(_(e_using_invalid_value_as_string_str),
3726 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003727 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003728 {
3729 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3730 var2.vval.v_float);
3731 s2 = buf2;
3732 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003733 else
3734 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003735 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003736 {
3737 clear_tv(rettv);
3738 clear_tv(&var2);
3739 return FAIL;
3740 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003741 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003742 clear_tv(rettv);
3743 rettv->v_type = VAR_STRING;
3744 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003746 else if (op == '+' && rettv->v_type == VAR_BLOB
3747 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003749 else if (op == '+' && rettv->v_type == VAR_LIST
3750 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003751 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003752 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003753 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 else
3756 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003757 int error = FALSE;
3758 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003759 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003760
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003761 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003762 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003763 f1 = rettv->vval.v_float;
3764 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003765 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003766 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003767 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003768 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003769 if (error)
3770 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003771 // This can only happen for "list + non-list" or
3772 // "blob + non-blob". For "non-list + ..." or
3773 // "something - ...", we returned before evaluating the
3774 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003775 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003776 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003777 return FAIL;
3778 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003779 if (var2.v_type == VAR_FLOAT)
3780 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003781 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003782 if (var2.v_type == VAR_FLOAT)
3783 {
3784 f2 = var2.vval.v_float;
3785 n2 = 0;
3786 }
3787 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003788 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003789 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003790 if (error)
3791 {
3792 clear_tv(rettv);
3793 clear_tv(&var2);
3794 return FAIL;
3795 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003796 if (rettv->v_type == VAR_FLOAT)
3797 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003798 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003799 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003800
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003801 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003802 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3803 {
3804 if (op == '+')
3805 f1 = f1 + f2;
3806 else
3807 f1 = f1 - f2;
3808 rettv->v_type = VAR_FLOAT;
3809 rettv->vval.v_float = f1;
3810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003812 {
3813 if (op == '+')
3814 n1 = n1 + n2;
3815 else
3816 n1 = n1 - n2;
3817 rettv->v_type = VAR_NUMBER;
3818 rettv->vval.v_number = n1;
3819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003821 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 }
3823 }
3824 return OK;
3825}
3826
3827/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003828 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 * * number multiplication
3830 * / number division
3831 * % number modulo
3832 *
3833 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003834 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 *
3836 * Return OK or FAIL.
3837 */
3838 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003839eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003840 char_u **arg,
3841 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003842 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003843 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003845 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846
3847 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003848 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003850 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 return FAIL;
3852
3853 /*
3854 * Repeat computing, until no '*', '/' or '%' is following.
3855 */
3856 for (;;)
3857 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003858 int evaluate;
3859 int getnext;
3860 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003861 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003862 int op;
3863 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003864 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003865 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003866
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003867 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003868 p = eval_next_non_blank(*arg, evalarg, &getnext);
3869 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003870 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003872
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003873 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003874 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003875 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003876 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003877 {
3878 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3879 {
mityu4ac198c2021-05-28 17:52:40 +02003880 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003881 clear_tv(rettv);
3882 return FAIL;
3883 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003884 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003887 f1 = 0;
3888 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003889 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003890 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003892 if (rettv->v_type == VAR_FLOAT)
3893 {
3894 f1 = rettv->vval.v_float;
3895 use_float = TRUE;
3896 n1 = 0;
3897 }
3898 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003899 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003900 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003901 if (error)
3902 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 }
3904 else
3905 n1 = 0;
3906
3907 /*
3908 * Get the second variable.
3909 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003910 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3911 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003912 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003913 clear_tv(rettv);
3914 return FAIL;
3915 }
3916 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003917 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 return FAIL;
3919
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003920 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003922 if (var2.v_type == VAR_FLOAT)
3923 {
3924 if (!use_float)
3925 {
3926 f1 = n1;
3927 use_float = TRUE;
3928 }
3929 f2 = var2.vval.v_float;
3930 n2 = 0;
3931 }
3932 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003933 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003934 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003935 clear_tv(&var2);
3936 if (error)
3937 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003938 if (use_float)
3939 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941
3942 /*
3943 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003944 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003946 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003948 if (op == '*')
3949 f1 = f1 * f2;
3950 else if (op == '/')
3951 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003952#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003953 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003954 if (f2 == 0.0)
3955 {
3956 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003957 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003958 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003959 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003960 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003961 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003962 }
3963 else
3964 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003965#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003966 // We rely on the floating point library to handle divide
3967 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003968 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003969#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003972 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003973 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003974 return FAIL;
3975 }
3976 rettv->v_type = VAR_FLOAT;
3977 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 }
3979 else
3980 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003981 int failed = FALSE;
3982
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003983 if (op == '*')
3984 n1 = n1 * n2;
3985 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003986 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003988 n1 = num_modulus(n1, n2, &failed);
3989 if (failed)
3990 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003991
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003992 rettv->v_type = VAR_NUMBER;
3993 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 }
3996 }
3997
3998 return OK;
3999}
4000
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004001/*
4002 * Handle a type cast before a base level expression.
4003 * "arg" must point to the first non-white of the expression.
4004 * "arg" is advanced to just after the recognized expression.
4005 * Return OK or FAIL.
4006 */
4007 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004008eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004009 char_u **arg,
4010 typval_T *rettv,
4011 evalarg_T *evalarg,
4012 int want_string) // after "." operator
4013{
4014 type_T *want_type = NULL;
4015 garray_T type_list; // list of pointers to allocated types
4016 int res;
4017 int evaluate = evalarg == NULL ? 0
4018 : (evalarg->eval_flags & EVAL_EVALUATE);
4019
4020 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02004021 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
4022 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004023 {
4024 ++*arg;
4025 ga_init2(&type_list, sizeof(type_T *), 10);
4026 want_type = parse_type(arg, &type_list, TRUE);
4027 if (want_type == NULL && (evaluate || **arg != '>'))
4028 {
4029 clear_type_list(&type_list);
4030 return FAIL;
4031 }
4032
4033 if (**arg != '>')
4034 {
4035 if (*skipwhite(*arg) == '>')
4036 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
4037 else
4038 emsg(_(e_missing_gt));
4039 clear_type_list(&type_list);
4040 return FAIL;
4041 }
4042 ++*arg;
4043 *arg = skipwhite_and_linebreak(*arg, evalarg);
4044 }
4045
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004046 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004047
4048 if (want_type != NULL && evaluate)
4049 {
4050 if (res == OK)
4051 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00004052 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
4053 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004054
Bram Moolenaar60dc8272021-07-29 22:48:54 +02004055 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004056 {
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02004057 if (want_type->tt_type == VAR_BOOL
4058 && actual->tt_type != VAR_BOOL
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004059 && (actual->tt_flags & TTFLAG_BOOL_OK))
4060 {
4061 int n = tv2bool(rettv);
4062
4063 // can use "0" and "1" for boolean in some places
4064 clear_tv(rettv);
4065 rettv->v_type = VAR_BOOL;
4066 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4067 }
4068 else
4069 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02004070 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004071
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004072 res = check_type(want_type, actual, TRUE, where);
4073 }
4074 }
4075 }
4076 clear_type_list(&type_list);
4077 }
4078
4079 return res;
4080}
4081
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004082 int
4083eval_leader(char_u **arg, int vim9)
4084{
4085 char_u *s = *arg;
4086 char_u *p = *arg;
4087
4088 while (*p == '!' || *p == '-' || *p == '+')
4089 {
4090 char_u *n = skipwhite(p + 1);
4091
4092 // ++, --, -+ and +- are not accepted in Vim9 script
4093 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
4094 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004095 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004096 return FAIL;
4097 }
4098 p = n;
4099 }
4100 *arg = p;
4101 return OK;
4102}
4103
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004105 * Check for a predefined value "true", "false" and "null.*".
4106 * Return OK when recognized.
4107 */
4108 int
4109handle_predefined(char_u *s, int len, typval_T *rettv)
4110{
4111 switch (len)
4112 {
4113 case 4: if (STRNCMP(s, "true", 4) == 0)
4114 {
4115 rettv->v_type = VAR_BOOL;
4116 rettv->vval.v_number = VVAL_TRUE;
4117 return OK;
4118 }
4119 if (STRNCMP(s, "null", 4) == 0)
4120 {
4121 rettv->v_type = VAR_SPECIAL;
4122 rettv->vval.v_number = VVAL_NULL;
4123 return OK;
4124 }
4125 break;
4126 case 5: if (STRNCMP(s, "false", 5) == 0)
4127 {
4128 rettv->v_type = VAR_BOOL;
4129 rettv->vval.v_number = VVAL_FALSE;
4130 return OK;
4131 }
4132 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004133 case 8: if (STRNCMP(s, "null_job", 8) == 0)
4134 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004135#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004136 rettv->v_type = VAR_JOB;
4137 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004138#else
4139 rettv->v_type = VAR_SPECIAL;
4140 rettv->vval.v_number = VVAL_NULL;
4141#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004142 return OK;
4143 }
4144 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004145 case 9:
4146 if (STRNCMP(s, "null_", 5) != 0)
4147 break;
4148 if (STRNCMP(s + 5, "list", 4) == 0)
4149 {
4150 rettv->v_type = VAR_LIST;
4151 rettv->vval.v_list = NULL;
4152 return OK;
4153 }
4154 if (STRNCMP(s + 5, "dict", 4) == 0)
4155 {
4156 rettv->v_type = VAR_DICT;
4157 rettv->vval.v_dict = NULL;
4158 return OK;
4159 }
4160 if (STRNCMP(s + 5, "blob", 4) == 0)
4161 {
4162 rettv->v_type = VAR_BLOB;
4163 rettv->vval.v_blob = NULL;
4164 return OK;
4165 }
4166 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004167 case 10: if (STRNCMP(s, "null_class", 10) == 0)
4168 {
4169 rettv->v_type = VAR_CLASS;
4170 rettv->vval.v_class = NULL;
4171 return OK;
4172 }
4173 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004174 case 11: if (STRNCMP(s, "null_string", 11) == 0)
4175 {
4176 rettv->v_type = VAR_STRING;
4177 rettv->vval.v_string = NULL;
4178 return OK;
4179 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004180 if (STRNCMP(s, "null_object", 11) == 0)
4181 {
4182 rettv->v_type = VAR_OBJECT;
4183 rettv->vval.v_object = NULL;
4184 return OK;
4185 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004186 break;
4187 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004188 if (STRNCMP(s, "null_channel", 12) == 0)
4189 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004190#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004191 rettv->v_type = VAR_CHANNEL;
4192 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004193#else
4194 rettv->v_type = VAR_SPECIAL;
4195 rettv->vval.v_number = VVAL_NULL;
4196#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004197 return OK;
4198 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004199 if (STRNCMP(s, "null_partial", 12) == 0)
4200 {
4201 rettv->v_type = VAR_PARTIAL;
4202 rettv->vval.v_partial = NULL;
4203 return OK;
4204 }
4205 break;
4206 case 13: if (STRNCMP(s, "null_function", 13) == 0)
4207 {
4208 rettv->v_type = VAR_FUNC;
4209 rettv->vval.v_string = NULL;
4210 return OK;
4211 }
4212 break;
4213 }
4214 return FAIL;
4215}
4216
4217/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 * Handle sixth level expression:
4219 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004220 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004221 * "string" string constant
4222 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 * &option-name option value
4224 * @r register contents
4225 * identifier variable value
4226 * function() function call
4227 * $VAR environment variable
4228 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004229 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004230 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004231 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004232 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 *
4234 * Also handle:
4235 * ! in front logical NOT
4236 * - in front unary minus
4237 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004238 * trailing [] subscript in String or List
4239 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004240 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 *
4242 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004243 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 *
4245 * Return OK or FAIL.
4246 */
4247 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004248eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004249 char_u **arg,
4250 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004251 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004252 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004254 int evaluate = evalarg != NULL
4255 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 int len;
4257 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004258 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 char_u *start_leader, *end_leader;
4260 int ret = OK;
4261 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004262 static int recurse = 0;
4263 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264
4265 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004266 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004267 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004269 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270
4271 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004272 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 */
4274 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004275 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004276 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 end_leader = *arg;
4278
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004279 if (**arg == '.' && (!isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004280 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004281 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004282 ++*arg;
4283 return FAIL;
4284 }
4285
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004286 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004287 // and crash. With MSVC the stack is smaller.
4288 if (recurse ==
4289#ifdef _MSC_VER
4290 300
4291#else
4292 1000
4293#endif
4294 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004295 {
4296 semsg(_(e_expression_too_recursive_str), *arg);
4297 return FAIL;
4298 }
4299 ++recurse;
4300
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 switch (**arg)
4302 {
4303 /*
4304 * Number constant.
4305 */
4306 case '0':
4307 case '1':
4308 case '2':
4309 case '3':
4310 case '4':
4311 case '5':
4312 case '6':
4313 case '7':
4314 case '8':
4315 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004316 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004317
4318 // Apply prefixed "-" and "+" now. Matters especially when
4319 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004320 if (ret == OK && evaluate && end_leader > start_leader
4321 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004322 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 break;
4324
4325 /*
4326 * String constant: "string".
4327 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004328 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 break;
4330
4331 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004332 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004334 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004335 break;
4336
4337 /*
4338 * List: [expr, expr]
4339 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004340 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 break;
4342
4343 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004344 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004345 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004346 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004347 {
4348 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4349 }
4350 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004351 {
4352 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004353 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004354 }
4355 else
4356 ret = NOTDONE;
4357 break;
4358
4359 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004360 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004361 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004362 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004363 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004364 ret = NOTDONE;
4365 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004366 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004367 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004368 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004369 break;
4370
4371 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004372 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004374 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 break;
4376
4377 /*
4378 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004379 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 */
LemonBoy2eaef102022-05-06 13:14:50 +01004381 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4382 ret = eval_interp_string(arg, rettv, evaluate);
4383 else
4384 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 break;
4386
4387 /*
4388 * Register contents: @r.
4389 */
4390 case '@': ++*arg;
4391 if (evaluate)
4392 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004393 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004394 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004395 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004396 emsg_invreg(**arg);
4397 else
4398 {
4399 rettv->v_type = VAR_STRING;
4400 rettv->vval.v_string = get_reg_contents(**arg,
4401 GREG_EXPR_SRC);
4402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 }
4404 if (**arg != NUL)
4405 ++*arg;
4406 break;
4407
4408 /*
4409 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004410 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004412 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004413 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004414 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004415 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004416 if (ret == OK && evaluate)
4417 {
4418 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4419
Bram Moolenaara9931532021-06-12 15:58:16 +02004420 // Compile it here to get the return type. The return
4421 // type is optional, when it's missing use t_unknown.
4422 // This is recognized in compile_return().
4423 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4424 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004425 if (compile_def_function(ufunc, FALSE,
4426 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004427 {
4428 clear_tv(rettv);
4429 ret = FAIL;
4430 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004431 }
4432 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004433 if (ret == NOTDONE)
4434 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004435 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004436 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004437
Bram Moolenaar9215f012020-06-27 21:18:00 +02004438 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004439 if (**arg == ')')
4440 ++*arg;
4441 else if (ret == OK)
4442 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004443 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004444 clear_tv(rettv);
4445 ret = FAIL;
4446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 }
4448 break;
4449
Bram Moolenaar8c711452005-01-14 21:53:12 +00004450 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 break;
4452 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004453
4454 if (ret == NOTDONE)
4455 {
4456 /*
4457 * Must be a variable or function name.
4458 * Can also be a curly-braces kind of name: {expr}.
4459 */
4460 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004461 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004462 if (alias != NULL)
4463 s = alias;
4464
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004465 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004466 ret = FAIL;
4467 else
4468 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004469 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4470
Bram Moolenaar4525a572022-02-13 11:57:33 +00004471 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004472 {
4473 emsg(_(e_cannot_use_underscore_here));
4474 ret = FAIL;
4475 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004476 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004477 && s[0] == 's' && s[1] == ':')
4478 {
4479 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4480 ret = FAIL;
4481 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004482 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004483 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004484 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004485 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004486 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004487 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004488 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004489 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004490 // get the value of "true", "false", etc. or a variable
4491 ret = FAIL;
4492 if (vim9script)
4493 ret = handle_predefined(s, len, rettv);
4494 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004495 {
4496 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004497 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004498 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004499 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004500 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004501 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004502 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004503 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004504 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004505 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004506 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004507 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004508 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004509 }
4510
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004511 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4512 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004513 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004514 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515
4516 /*
4517 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4518 */
4519 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004520 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004521
4522 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004523 return ret;
4524}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004525
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004526/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004527 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004528 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004529 * Adjusts "end_leaderp" until it is at "start_leader".
4530 */
4531 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004532eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004533 typval_T *rettv,
4534 int numeric_only,
4535 char_u *start_leader,
4536 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004537{
4538 char_u *end_leader = *end_leaderp;
4539 int ret = OK;
4540 int error = FALSE;
4541 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004542 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004543 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004544 float_T f = 0.0;
4545
4546 if (rettv->v_type == VAR_FLOAT)
4547 f = rettv->vval.v_float;
4548 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004549 {
4550 while (VIM_ISWHITE(end_leader[-1]))
4551 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004552 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004553 val = tv2bool(rettv);
4554 else
4555 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004556 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004557 if (error)
4558 {
4559 clear_tv(rettv);
4560 ret = FAIL;
4561 }
4562 else
4563 {
4564 while (end_leader > start_leader)
4565 {
4566 --end_leader;
4567 if (*end_leader == '!')
4568 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004569 if (numeric_only)
4570 {
4571 ++end_leader;
4572 break;
4573 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004574 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004575 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004576 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004577 {
4578 rettv->v_type = VAR_BOOL;
4579 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4580 }
4581 else
4582 f = !f;
4583 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004584 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004585 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004586 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004587 type = VAR_BOOL;
4588 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004589 }
4590 else if (*end_leader == '-')
4591 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004592 if (rettv->v_type == VAR_FLOAT)
4593 f = -f;
4594 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004595 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004596 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004597 type = VAR_NUMBER;
4598 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004599 }
4600 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004601 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004603 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004604 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004606 else
4607 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004608 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004609 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004610 rettv->v_type = type;
4611 else
4612 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004613 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004616 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 return ret;
4618}
4619
4620/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004621 * Call the function referred to in "rettv".
4622 */
4623 static int
4624call_func_rettv(
4625 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004626 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004627 typval_T *rettv,
4628 int evaluate,
4629 dict_T *selfdict,
4630 typval_T *basetv)
4631{
4632 partial_T *pt = NULL;
4633 funcexe_T funcexe;
4634 typval_T functv;
4635 char_u *s;
4636 int ret;
4637
4638 // need to copy the funcref so that we can clear rettv
4639 if (evaluate)
4640 {
4641 functv = *rettv;
4642 rettv->v_type = VAR_UNKNOWN;
4643
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004644 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004645 if (functv.v_type == VAR_PARTIAL)
4646 {
4647 pt = functv.vval.v_partial;
4648 s = partial_name(pt);
4649 }
4650 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004651 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004652 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004653 if (s == NULL || *s == NUL)
4654 {
4655 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004656 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004657 goto theend;
4658 }
4659 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004660 }
4661 else
4662 s = (char_u *)"";
4663
Bram Moolenaara80faa82020-04-12 19:37:17 +02004664 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004665 funcexe.fe_firstline = curwin->w_cursor.lnum;
4666 funcexe.fe_lastline = curwin->w_cursor.lnum;
4667 funcexe.fe_evaluate = evaluate;
4668 funcexe.fe_partial = pt;
4669 funcexe.fe_selfdict = selfdict;
4670 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004671 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004672
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004673theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004674 // Clear the funcref afterwards, so that deleting it while
4675 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004676 if (evaluate)
4677 clear_tv(&functv);
4678
4679 return ret;
4680}
4681
4682/*
4683 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004684 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004685 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4686 */
4687 static int
4688eval_lambda(
4689 char_u **arg,
4690 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004691 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004692 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004693{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004694 int evaluate = evalarg != NULL
4695 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004696 typval_T base = *rettv;
4697 int ret;
4698
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004699 rettv->v_type = VAR_UNKNOWN;
4700
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004701 if (**arg == '{')
4702 {
4703 // ->{lambda}()
4704 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4705 }
4706 else
4707 {
4708 // ->(lambda)()
4709 ++*arg;
4710 ret = eval1(arg, rettv, evalarg);
4711 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004712 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004713 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004714 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004715 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004716 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004717 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4718 && rettv->v_type != VAR_PARTIAL)
4719 {
4720 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4721 return FAIL;
4722 }
4723 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004724 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004725 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004726 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004727
4728 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004729 {
4730 if (verbose)
4731 {
4732 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004733 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004734 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004735 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004736 }
4737 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004738 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004739 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004740 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004741 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004742
4743 // Clear the funcref afterwards, so that deleting it while
4744 // evaluating the arguments is possible (see test55).
4745 if (evaluate)
4746 clear_tv(&base);
4747
4748 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004749}
4750
4751/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004752 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004753 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004754 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4755 */
4756 static int
4757eval_method(
4758 char_u **arg,
4759 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004760 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004761 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004762{
4763 char_u *name;
4764 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004765 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004766 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004767 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004768 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004769 int evaluate = evalarg != NULL
4770 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004771
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004772 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004773
Bram Moolenaarac92e252019-08-03 21:58:38 +02004774 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004775 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004776 if (alias != NULL)
4777 name = alias;
4778
4779 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004780 {
4781 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004782 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004783 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004784 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004785 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004786 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004787 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004788
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004789 // If there is no "(" immediately following, but there is further on,
4790 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4791 // Does not handle anything where "(" is part of the expression.
4792 *arg = skipwhite(*arg);
4793
4794 if (**arg != '(' && alias == NULL
4795 && (paren = vim_strchr(*arg, '(')) != NULL)
4796 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004797 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00004798
4799 // Truncate the name a the "(". Avoid trying to get another line
4800 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004801 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00004802 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
4803 if (evalarg != NULL)
4804 {
4805 getline = evalarg->eval_getline;
4806 evalarg->eval_getline = NULL;
4807 }
4808
4809 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004810 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004811 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004812 *arg = name + len;
4813 ret = FAIL;
4814 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004815 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004816 {
4817 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004818 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004819 }
Bram Moolenaar34820942022-12-19 20:28:38 +00004820
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004821 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00004822 if (getline != NULL)
4823 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004824 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004825
4826 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004827 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004828 *arg = skipwhite(*arg);
4829
4830 if (**arg != '(')
4831 {
4832 if (verbose)
4833 semsg(_(e_missing_parenthesis_str), name);
4834 ret = FAIL;
4835 }
4836 else if (VIM_ISWHITE((*arg)[-1]))
4837 {
4838 if (verbose)
4839 emsg(_(e_no_white_space_allowed_before_parenthesis));
4840 ret = FAIL;
4841 }
4842 else
4843 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004844 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004845 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004846 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004847
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004848 // Clear the funcref afterwards, so that deleting it while
4849 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004850 if (evaluate)
4851 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004852 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004853
Bram Moolenaarac92e252019-08-03 21:58:38 +02004854 return ret;
4855}
4856
4857/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004858 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4859 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004860 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4861 */
4862 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004863eval_index(
4864 char_u **arg,
4865 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004866 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004867 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004868{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004869 int evaluate = evalarg != NULL
4870 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004871 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004872 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004873 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004874 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004875 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004876 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004877
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004878 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4879 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004880
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004881 init_tv(&var1);
4882 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004883 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004884 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004885 /*
4886 * dict.name
4887 */
4888 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004889 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004890 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004891 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004892 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004893 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004894 }
4895 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004896 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004897 /*
4898 * something[idx]
4899 *
4900 * Get the (first) variable from inside the [].
4901 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004902 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004903 if (**arg == ':')
4904 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004905 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004906 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004907 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004908 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004909 semsg(_(e_white_space_required_before_and_after_str_at_str),
4910 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004911 clear_tv(&var1);
4912 return FAIL;
4913 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004914 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004915 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004916 int error = FALSE;
4917
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004918 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004919 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004920 && var1.v_type == VAR_FLOAT)
4921 {
4922 var1.vval.v_string = typval_tostring(&var1, TRUE);
4923 var1.v_type = VAR_STRING;
4924 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004925
Bram Moolenaar4525a572022-02-13 11:57:33 +00004926 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004927 tv_get_number_chk(&var1, &error);
4928 else
4929 error = tv_get_string_chk(&var1) == NULL;
4930 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004931 {
4932 // not a number or string
4933 clear_tv(&var1);
4934 return FAIL;
4935 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004936 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004937
4938 /*
4939 * Get the second variable from inside the [:].
4940 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004941 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004942 if (**arg == ':')
4943 {
4944 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004945 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004946 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004947 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004948 semsg(_(e_white_space_required_before_and_after_str_at_str),
4949 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004950 if (!empty1)
4951 clear_tv(&var1);
4952 return FAIL;
4953 }
4954 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004955 if (**arg == ']')
4956 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004957 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004958 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004959 if (!empty1)
4960 clear_tv(&var1);
4961 return FAIL;
4962 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004963 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004964 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004965 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004966 if (!empty1)
4967 clear_tv(&var1);
4968 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004969 return FAIL;
4970 }
4971 }
4972
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004973 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004974 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004975 if (**arg != ']')
4976 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004977 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004978 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004979 clear_tv(&var1);
4980 if (range)
4981 clear_tv(&var2);
4982 return FAIL;
4983 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004984 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004985 }
4986
4987 if (evaluate)
4988 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004989 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004990 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004991 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004992
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004993 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004994 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004995 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004996 clear_tv(&var2);
4997 return res;
4998 }
4999 return OK;
5000}
5001
5002/*
5003 * Check if "rettv" can have an [index] or [sli:ce]
5004 */
5005 int
5006check_can_index(typval_T *rettv, int evaluate, int verbose)
5007{
5008 switch (rettv->v_type)
5009 {
5010 case VAR_FUNC:
5011 case VAR_PARTIAL:
5012 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005013 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005014 return FAIL;
5015 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005016 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005017 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005018 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005019 case VAR_BOOL:
5020 case VAR_SPECIAL:
5021 case VAR_JOB:
5022 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005023 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005024 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005025 if (verbose)
5026 emsg(_(e_cannot_index_special_variable));
5027 return FAIL;
Ernie Raele75fde62023-12-21 17:18:54 +01005028 case VAR_CLASS:
5029 case VAR_TYPEALIAS:
5030 if (verbose)
5031 check_typval_is_value(rettv);
5032 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005033 case VAR_UNKNOWN:
5034 case VAR_ANY:
5035 case VAR_VOID:
5036 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005037 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005038 emsg(_(e_cannot_index_special_variable));
5039 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005040 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005041 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005042
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005043 case VAR_STRING:
5044 case VAR_LIST:
5045 case VAR_DICT:
5046 case VAR_BLOB:
5047 break;
5048 case VAR_NUMBER:
5049 if (in_vim9script())
5050 emsg(_(e_cannot_index_number));
5051 break;
5052 }
5053 return OK;
5054}
5055
5056/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01005057 * slice() function
5058 */
5059 void
5060f_slice(typval_T *argvars, typval_T *rettv)
5061{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005062 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005063 && ((argvars[0].v_type != VAR_STRING
5064 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005065 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005066 && check_for_list_arg(argvars, 0) == FAIL)
5067 || check_for_number_arg(argvars, 1) == FAIL
5068 || check_for_opt_number_arg(argvars, 2) == FAIL))
5069 return;
5070
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005071 if (check_can_index(argvars, TRUE, FALSE) != OK)
5072 return;
5073
5074 copy_tv(argvars, rettv);
5075 eval_index_inner(rettv, TRUE, argvars + 1,
5076 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
5077 TRUE, NULL, 0, FALSE);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005078}
5079
5080/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005081 * Apply index or range to "rettv".
5082 * "var1" is the first index, NULL for [:expr].
5083 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01005084 * "exclusive" is TRUE for slice(): second index is exclusive, use character
5085 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005086 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
5087 */
5088 int
5089eval_index_inner(
5090 typval_T *rettv,
5091 int is_range,
5092 typval_T *var1,
5093 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005094 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005095 char_u *key,
5096 int keylen,
5097 int verbose)
5098{
Bram Moolenaar6601b622021-01-13 21:47:15 +01005099 varnumber_T n1, n2 = 0;
5100 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005101
5102 n1 = 0;
5103 if (var1 != NULL && rettv->v_type != VAR_DICT)
5104 n1 = tv_get_number(var1);
5105
5106 if (is_range)
5107 {
5108 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005109 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005110 if (verbose)
5111 emsg(_(e_cannot_slice_dictionary));
5112 return FAIL;
5113 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01005114 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005115 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005116 else
5117 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005118 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005119
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005120 switch (rettv->v_type)
5121 {
5122 case VAR_UNKNOWN:
5123 case VAR_ANY:
5124 case VAR_VOID:
5125 case VAR_FUNC:
5126 case VAR_PARTIAL:
5127 case VAR_FLOAT:
5128 case VAR_BOOL:
5129 case VAR_SPECIAL:
5130 case VAR_JOB:
5131 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005132 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005133 case VAR_CLASS:
5134 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02005135 case VAR_TYPEALIAS:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005136 break; // not evaluating, skipping over subscript
5137
5138 case VAR_NUMBER:
5139 case VAR_STRING:
5140 {
5141 char_u *s = tv_get_string(rettv);
5142
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005143 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005144 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005145 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005146 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005147 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005148 else
5149 s = char_from_string(s, n1);
5150 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005151 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005152 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005153 // The resulting variable is a substring. If the indexes
5154 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005155 if (n1 < 0)
5156 {
5157 n1 = len + n1;
5158 if (n1 < 0)
5159 n1 = 0;
5160 }
5161 if (n2 < 0)
5162 n2 = len + n2;
5163 else if (n2 >= len)
5164 n2 = len;
5165 if (n1 >= len || n2 < 0 || n1 > n2)
5166 s = NULL;
5167 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02005168 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005169 }
5170 else
5171 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005172 // The resulting variable is a string of a single
5173 // character. If the index is too big or negative the
5174 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005175 if (n1 >= len || n1 < 0)
5176 s = NULL;
5177 else
5178 s = vim_strnsave(s + n1, 1);
5179 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005180 clear_tv(rettv);
5181 rettv->v_type = VAR_STRING;
5182 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005183 }
5184 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005185
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005186 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005187 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
5188 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005189 break;
5190
5191 case VAR_LIST:
5192 if (var1 == NULL)
5193 n1 = 0;
5194 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005195 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005196 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005197 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005198 return FAIL;
5199 break;
5200
5201 case VAR_DICT:
5202 {
5203 dictitem_T *item;
5204 typval_T tmp;
5205
5206 if (key == NULL)
5207 {
5208 key = tv_get_string_chk(var1);
5209 if (key == NULL)
5210 return FAIL;
5211 }
5212
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005213 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005214
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005215 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005216 {
5217 if (verbose)
5218 {
5219 if (keylen > 0)
5220 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005221 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005222 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005223 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005224 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005225
5226 copy_tv(&item->di_tv, &tmp);
5227 clear_tv(rettv);
5228 *rettv = tmp;
5229 }
5230 break;
5231 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005232 return OK;
5233}
5234
5235/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005236 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005237 */
5238 char_u *
5239partial_name(partial_T *pt)
5240{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02005241 if (pt != NULL)
5242 {
5243 if (pt->pt_name != NULL)
5244 return pt->pt_name;
5245 if (pt->pt_func != NULL)
5246 return pt->pt_func->uf_name;
5247 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005248 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005249}
5250
Bram Moolenaarddecc252016-04-06 22:59:37 +02005251 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005252partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005253{
5254 int i;
5255
5256 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005257 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005258 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005259 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005260 if (pt->pt_name != NULL)
5261 {
5262 func_unref(pt->pt_name);
5263 vim_free(pt->pt_name);
5264 }
5265 else
5266 func_ptr_unref(pt->pt_func);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005267 object_unref(pt->pt_obj);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005268
Bram Moolenaar54656012021-06-09 20:50:46 +02005269 // "out_up" is no longer used, decrement refcount on partial that owns it.
5270 partial_unref(pt->pt_outer.out_up_partial);
5271
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005272 // Using pt_outer from another partial.
5273 partial_unref(pt->pt_outer_partial);
5274
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005275 // Decrease the reference count for the context of a closure. If down
5276 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005277 if (pt->pt_funcstack != NULL)
5278 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005279 --pt->pt_funcstack->fs_refcount;
5280 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005281 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005282 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005283 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5284 if (pt->pt_loopvars[i] != NULL)
5285 {
5286 --pt->pt_loopvars[i]->lvs_refcount;
5287 loopvars_check_refcount(pt->pt_loopvars[i]);
5288 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005289
Bram Moolenaarddecc252016-04-06 22:59:37 +02005290 vim_free(pt);
5291}
5292
5293/*
5294 * Unreference a closure: decrement the reference count and free it when it
5295 * becomes zero.
5296 */
5297 void
5298partial_unref(partial_T *pt)
5299{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005300 if (pt == NULL)
5301 return;
5302
5303 int done = FALSE;
5304
5305 if (--pt->pt_refcount <= 0)
5306 partial_free(pt);
5307
5308 // If the reference count goes down to one, the funcstack may be the
5309 // only reference and can be freed if no other partials reference it.
5310 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005311 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005312 // careful: if the funcstack is freed it may contain this partial
5313 // and it gets freed as well
5314 if (pt->pt_funcstack != NULL)
5315 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005316
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005317 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005318 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005319 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005320
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005321 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5322 if (pt->pt_loopvars[depth] != NULL
5323 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005324 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005325 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005326 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005327}
5328
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005329/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005330 * Return the next (unique) copy ID.
5331 * Used for serializing nested structures.
5332 */
5333 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005334get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005335{
5336 current_copyID += COPYID_INC;
5337 return current_copyID;
5338}
5339
5340/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005341 * Garbage collection for lists and dictionaries.
5342 *
5343 * We use reference counts to be able to free most items right away when they
5344 * are no longer used. But for composite items it's possible that it becomes
5345 * unused while the reference count is > 0: When there is a recursive
5346 * reference. Example:
5347 * :let l = [1, 2, 3]
5348 * :let d = {9: l}
5349 * :let l[1] = d
5350 *
5351 * Since this is quite unusual we handle this with garbage collection: every
5352 * once in a while find out which lists and dicts are not referenced from any
5353 * variable.
5354 *
5355 * Here is a good reference text about garbage collection (refers to Python
5356 * but it applies to all reference-counting mechanisms):
5357 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005358 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005359
5360/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005361 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005362 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005363 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005364 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005365 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005366garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005367{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005368 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005369 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005370 buf_T *buf;
5371 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005372 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005373 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005374
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005375 if (!testing)
5376 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005377 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005378 want_garbage_collect = FALSE;
5379 may_garbage_collect = FALSE;
5380 garbage_collect_at_exit = FALSE;
5381 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005382
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005383 // The execution stack can grow big, limit the size.
5384 if (exestack.ga_maxlen - exestack.ga_len > 500)
5385 {
5386 size_t new_len;
5387 char_u *pp;
5388 int n;
5389
5390 // Keep 150% of the current size, with a minimum of the growth size.
5391 n = exestack.ga_len / 2;
5392 if (n < exestack.ga_growsize)
5393 n = exestack.ga_growsize;
5394
5395 // Don't make it bigger though.
5396 if (exestack.ga_len + n < exestack.ga_maxlen)
5397 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005398 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005399 pp = vim_realloc(exestack.ga_data, new_len);
5400 if (pp == NULL)
5401 return FAIL;
5402 exestack.ga_maxlen = exestack.ga_len + n;
5403 exestack.ga_data = pp;
5404 }
5405 }
5406
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005407 // We advance by two because we add one for items referenced through
5408 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005409 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005410
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005411 /*
5412 * 1. Go through all accessible variables and mark all lists and dicts
5413 * with copyID.
5414 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005415
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005416 // Don't free variables in the previous_funccal list unless they are only
5417 // referenced through previous_funccal. This must be first, because if
5418 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005419 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005420
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005421 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005422 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005423
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005424 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005425 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005426 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5427 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005428
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005429 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005430 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005431 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5432 NULL, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005433 // window-local variables in autocmd windows
Bram Moolenaare76062c2022-11-28 18:51:43 +00005434 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005435 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00005436 abort = abort || set_ref_in_item(
5437 &aucmd_win[i].auc_win->w_winvar.di_tv, copyID, NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005438#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005439 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005440 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5441 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005442 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005443 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005444 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5445 NULL, NULL);
5446#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005447
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005448 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005449 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005450 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5451 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005452 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005453 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005454
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005455 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005456 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005457
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005458 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005459 abort = abort || set_ref_in_functions(copyID);
5460
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005461 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005462 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005463
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005464 // funcstacks keep variables for closures
5465 abort = abort || set_ref_in_funcstacks(copyID);
5466
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005467 // loopvars keep variables for loop blocks
5468 abort = abort || set_ref_in_loopvars(copyID);
5469
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005470 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005471 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005472
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005473 // callbacks in buffers
5474 abort = abort || set_ref_in_buffers(copyID);
5475
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005476 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5477 abort = abort || set_ref_in_insexpand_funcs(copyID);
5478
5479 // 'operatorfunc' callback
5480 abort = abort || set_ref_in_opfunc(copyID);
5481
5482 // 'tagfunc' callback
5483 abort = abort || set_ref_in_tagfunc(copyID);
5484
5485 // 'imactivatefunc' and 'imstatusfunc' callbacks
5486 abort = abort || set_ref_in_im_funcs(copyID);
5487
Bram Moolenaar1dced572012-04-05 16:54:08 +02005488#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005489 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005490#endif
5491
Bram Moolenaardb913952012-06-29 12:54:53 +02005492#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005493 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005494#endif
5495
5496#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005497 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005498#endif
5499
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005500#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005501 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005502 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005503#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005504#ifdef FEAT_NETBEANS_INTG
5505 abort = abort || set_ref_in_nb_channel(copyID);
5506#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005507
Bram Moolenaare3188e22016-05-31 21:13:04 +02005508#ifdef FEAT_TIMERS
5509 abort = abort || set_ref_in_timer(copyID);
5510#endif
5511
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005512#ifdef FEAT_QUICKFIX
5513 abort = abort || set_ref_in_quickfix(copyID);
5514#endif
5515
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005516#ifdef FEAT_TERMINAL
5517 abort = abort || set_ref_in_term(copyID);
5518#endif
5519
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005520#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005521 abort = abort || set_ref_in_popups(copyID);
5522#endif
5523
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005524 abort = abort || set_ref_in_classes(copyID);
5525
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005526 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005527 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005528 /*
5529 * 2. Free lists and dictionaries that are not referenced.
5530 */
5531 did_free = free_unref_items(copyID);
5532
5533 /*
5534 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005535 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005536 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005537 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005538 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005539 else if (p_verbose > 0)
5540 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005541 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005542 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005543
5544 return did_free;
5545}
5546
5547/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005548 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005549 */
5550 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005551free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005552{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005553 int did_free = FALSE;
5554
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005555 // Let all "free" functions know that we are here. This means no
5556 // dictionaries, lists, channels or jobs are to be freed, because we will
5557 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005558 in_free_unref_items = TRUE;
5559
5560 /*
5561 * PASS 1: free the contents of the items. We don't free the items
5562 * themselves yet, so that it is possible to decrement refcount counters
5563 */
5564
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005565 // Go through the list of dicts and free items without this copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005566 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005567
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005568 // Go through the list of lists and free items without this copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005569 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005570
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005571 // Go through the list of objects and free items without this copyID.
5572 did_free |= object_free_nonref(copyID);
5573
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005574 // Go through the list of classes and free items without this copyID.
5575 did_free |= class_free_nonref(copyID);
5576
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005577#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005578 // Go through the list of jobs and free items without the copyID. This
5579 // must happen before doing channels, because jobs refer to channels, but
5580 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005581 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5582
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005583 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005584 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5585#endif
5586
5587 /*
5588 * PASS 2: free the items themselves.
5589 */
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005590 object_free_items(copyID);
Bram Moolenaarcd524592016-07-17 14:57:05 +02005591 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005592 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005593
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005594#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005595 // Go through the list of jobs and free items without the copyID. This
5596 // must happen before doing channels, because jobs refer to channels, but
5597 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005598 free_unused_jobs(copyID, COPYID_MASK);
5599
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005600 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005601 free_unused_channels(copyID, COPYID_MASK);
5602#endif
5603
5604 in_free_unref_items = FALSE;
5605
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005606 return did_free;
5607}
5608
5609/*
5610 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005611 * "list_stack" is used to add lists to be marked. Can be NULL.
5612 *
5613 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005614 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005615 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005616set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005617{
5618 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005619 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005620 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005621 hashtab_T *cur_ht;
5622 ht_stack_T *ht_stack = NULL;
5623 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005624
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005625 cur_ht = ht;
5626 for (;;)
5627 {
5628 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005629 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005630 // Mark each item in the hashtab. If the item contains a hashtab
5631 // it is added to ht_stack, if it contains a list it is added to
5632 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005633 todo = (int)cur_ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00005634 FOR_ALL_HASHTAB_ITEMS(cur_ht, hi, todo)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005635 if (!HASHITEM_EMPTY(hi))
5636 {
5637 --todo;
5638 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5639 &ht_stack, list_stack);
5640 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005641 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005642
5643 if (ht_stack == NULL)
5644 break;
5645
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005646 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005647 cur_ht = ht_stack->ht;
5648 tempitem = ht_stack;
5649 ht_stack = ht_stack->prev;
5650 free(tempitem);
5651 }
5652
5653 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005654}
5655
Dominique Pelle748b3082022-01-08 12:41:16 +00005656#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5657 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005658/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005659 * Mark a dict and its items with "copyID".
5660 * Returns TRUE if setting references failed somehow.
5661 */
5662 int
5663set_ref_in_dict(dict_T *d, int copyID)
5664{
5665 if (d != NULL && d->dv_copyID != copyID)
5666 {
5667 d->dv_copyID = copyID;
5668 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5669 }
5670 return FALSE;
5671}
Dominique Pelle748b3082022-01-08 12:41:16 +00005672#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005673
5674/*
5675 * Mark a list and its items with "copyID".
5676 * Returns TRUE if setting references failed somehow.
5677 */
5678 int
5679set_ref_in_list(list_T *ll, int copyID)
5680{
5681 if (ll != NULL && ll->lv_copyID != copyID)
5682 {
5683 ll->lv_copyID = copyID;
5684 return set_ref_in_list_items(ll, copyID, NULL);
5685 }
5686 return FALSE;
5687}
5688
5689/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005690 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005691 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5692 *
5693 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005694 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005695 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005696set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005697{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005698 listitem_T *li;
5699 int abort = FALSE;
5700 list_T *cur_l;
5701 list_stack_T *list_stack = NULL;
5702 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005703
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005704 cur_l = l;
5705 for (;;)
5706 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005707 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005708 // Mark each item in the list. If the item contains a hashtab
5709 // it is added to ht_stack, if it contains a list it is added to
5710 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005711 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5712 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5713 ht_stack, &list_stack);
5714 if (list_stack == NULL)
5715 break;
5716
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005717 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005718 cur_l = list_stack->list;
5719 tempitem = list_stack;
5720 list_stack = list_stack->prev;
5721 free(tempitem);
5722 }
5723
5724 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005725}
5726
5727/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005728 * Mark the partial in callback 'cb' with "copyID".
5729 */
5730 int
5731set_ref_in_callback(callback_T *cb, int copyID)
5732{
5733 typval_T tv;
5734
5735 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5736 return FALSE;
5737
5738 tv.v_type = VAR_PARTIAL;
5739 tv.vval.v_partial = cb->cb_partial;
5740 return set_ref_in_item(&tv, copyID, NULL, NULL);
5741}
5742
5743/*
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005744 * Mark the dict "dd" with "copyID".
5745 * Also see set_ref_in_item().
5746 */
5747 static int
5748set_ref_in_item_dict(
5749 dict_T *dd,
5750 int copyID,
5751 ht_stack_T **ht_stack,
5752 list_stack_T **list_stack)
5753{
5754 if (dd == NULL || dd->dv_copyID == copyID)
5755 return FALSE;
5756
5757 // Didn't see this dict yet.
5758 dd->dv_copyID = copyID;
5759 if (ht_stack == NULL)
5760 return set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5761
5762 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5763 if (newitem == NULL)
5764 return TRUE;
5765
5766 newitem->ht = &dd->dv_hashtab;
5767 newitem->prev = *ht_stack;
5768 *ht_stack = newitem;
5769
5770 return FALSE;
5771}
5772
5773/*
5774 * Mark the list "ll" with "copyID".
5775 * Also see set_ref_in_item().
5776 */
5777 static int
5778set_ref_in_item_list(
5779 list_T *ll,
5780 int copyID,
5781 ht_stack_T **ht_stack,
5782 list_stack_T **list_stack)
5783{
5784 if (ll == NULL || ll->lv_copyID == copyID)
5785 return FALSE;
5786
5787 // Didn't see this list yet.
5788 ll->lv_copyID = copyID;
5789 if (list_stack == NULL)
5790 return set_ref_in_list_items(ll, copyID, ht_stack);
5791
5792 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5793 if (newitem == NULL)
5794 return TRUE;
5795
5796 newitem->list = ll;
5797 newitem->prev = *list_stack;
5798 *list_stack = newitem;
5799
5800 return FALSE;
5801}
5802
5803/*
5804 * Mark the partial "pt" with "copyID".
5805 * Also see set_ref_in_item().
5806 */
5807 static int
5808set_ref_in_item_partial(
5809 partial_T *pt,
5810 int copyID,
5811 ht_stack_T **ht_stack,
5812 list_stack_T **list_stack)
5813{
5814 if (pt == NULL || pt->pt_copyID == copyID)
5815 return FALSE;
5816
5817 // Didn't see this partial yet.
5818 pt->pt_copyID = copyID;
5819
5820 int abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
5821
5822 if (pt->pt_dict != NULL)
5823 {
5824 typval_T dtv;
5825
5826 dtv.v_type = VAR_DICT;
5827 dtv.vval.v_dict = pt->pt_dict;
5828 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5829 }
5830
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005831 if (pt->pt_obj != NULL)
5832 {
5833 typval_T objtv;
5834
5835 objtv.v_type = VAR_OBJECT;
5836 objtv.vval.v_object = pt->pt_obj;
5837 set_ref_in_item(&objtv, copyID, ht_stack, list_stack);
5838 }
5839
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005840 for (int i = 0; i < pt->pt_argc; ++i)
5841 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5842 ht_stack, list_stack);
5843 // pt_funcstack is handled in set_ref_in_funcstacks()
5844 // pt_loopvars is handled in set_ref_in_loopvars()
5845
5846 return abort;
5847}
5848
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005849#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005850/*
5851 * Mark the job "pt" with "copyID".
5852 * Also see set_ref_in_item().
5853 */
5854 static int
5855set_ref_in_item_job(
5856 job_T *job,
5857 int copyID,
5858 ht_stack_T **ht_stack,
5859 list_stack_T **list_stack)
5860{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005861 typval_T dtv;
5862
5863 if (job == NULL || job->jv_copyID == copyID)
5864 return FALSE;
5865
5866 job->jv_copyID = copyID;
5867 if (job->jv_channel != NULL)
5868 {
5869 dtv.v_type = VAR_CHANNEL;
5870 dtv.vval.v_channel = job->jv_channel;
5871 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5872 }
5873 if (job->jv_exit_cb.cb_partial != NULL)
5874 {
5875 dtv.v_type = VAR_PARTIAL;
5876 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
5877 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5878 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005879
5880 return FALSE;
5881}
5882
5883/*
5884 * Mark the channel "ch" with "copyID".
5885 * Also see set_ref_in_item().
5886 */
5887 static int
5888set_ref_in_item_channel(
5889 channel_T *ch,
5890 int copyID,
5891 ht_stack_T **ht_stack,
5892 list_stack_T **list_stack)
5893{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005894 typval_T dtv;
5895
5896 if (ch == NULL || ch->ch_copyID == copyID)
5897 return FALSE;
5898
5899 ch->ch_copyID = copyID;
5900 for (ch_part_T part = PART_SOCK; part < PART_COUNT; ++part)
5901 {
5902 for (jsonq_T *jq = ch->ch_part[part].ch_json_head.jq_next;
5903 jq != NULL; jq = jq->jq_next)
5904 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5905 for (cbq_T *cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5906 cq = cq->cq_next)
5907 if (cq->cq_callback.cb_partial != NULL)
5908 {
5909 dtv.v_type = VAR_PARTIAL;
5910 dtv.vval.v_partial = cq->cq_callback.cb_partial;
5911 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5912 }
5913 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
5914 {
5915 dtv.v_type = VAR_PARTIAL;
5916 dtv.vval.v_partial = ch->ch_part[part].ch_callback.cb_partial;
5917 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5918 }
5919 }
5920 if (ch->ch_callback.cb_partial != NULL)
5921 {
5922 dtv.v_type = VAR_PARTIAL;
5923 dtv.vval.v_partial = ch->ch_callback.cb_partial;
5924 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5925 }
5926 if (ch->ch_close_cb.cb_partial != NULL)
5927 {
5928 dtv.v_type = VAR_PARTIAL;
5929 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
5930 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5931 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005932
5933 return FALSE;
5934}
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005935#endif
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005936
5937/*
5938 * Mark the class "cl" with "copyID".
5939 * Also see set_ref_in_item().
5940 */
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005941 int
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005942set_ref_in_item_class(
5943 class_T *cl,
5944 int copyID,
5945 ht_stack_T **ht_stack,
5946 list_stack_T **list_stack)
5947{
5948 int abort = FALSE;
5949
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005950 if (cl == NULL || cl->class_copyID == copyID)
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005951 return FALSE;
5952
5953 cl->class_copyID = copyID;
Yegappan Lakshmanan544be0d2023-09-04 22:14:28 +02005954 if (cl->class_members_tv != NULL)
5955 {
5956 // The "class_members_tv" table is allocated only for regular classes
5957 // and not for interfaces.
5958 for (int i = 0; !abort && i < cl->class_class_member_count; ++i)
5959 abort = abort || set_ref_in_item(
5960 &cl->class_members_tv[i],
5961 copyID, ht_stack, list_stack);
5962 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005963
5964 for (int i = 0; !abort && i < cl->class_class_function_count; ++i)
5965 abort = abort || set_ref_in_func(NULL,
5966 cl->class_class_functions[i], copyID);
5967
5968 for (int i = 0; !abort && i < cl->class_obj_method_count; ++i)
5969 abort = abort || set_ref_in_func(NULL,
5970 cl->class_obj_methods[i], copyID);
5971
5972 return abort;
5973}
5974
5975/*
5976 * Mark the object "cl" with "copyID".
5977 * Also see set_ref_in_item().
5978 */
5979 static int
5980set_ref_in_item_object(
5981 object_T *obj,
5982 int copyID,
5983 ht_stack_T **ht_stack,
5984 list_stack_T **list_stack)
5985{
5986 int abort = FALSE;
5987
5988 if (obj == NULL || obj->obj_copyID == copyID)
5989 return FALSE;
5990
5991 obj->obj_copyID = copyID;
5992
5993 // The typval_T array is right after the object_T.
5994 typval_T *mtv = (typval_T *)(obj + 1);
5995 for (int i = 0; !abort
5996 && i < obj->obj_class->class_obj_member_count; ++i)
5997 abort = abort || set_ref_in_item(mtv + i, copyID,
5998 ht_stack, list_stack);
5999
6000 return abort;
6001}
6002
6003/*
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006004 * Mark all lists, dicts and other container types referenced through typval
6005 * "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006006 * "list_stack" is used to add lists to be marked. Can be NULL.
6007 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6008 *
6009 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006010 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006011 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006012set_ref_in_item(
6013 typval_T *tv,
6014 int copyID,
6015 ht_stack_T **ht_stack,
6016 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006017{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006018 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006019
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006020 switch (tv->v_type)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006021 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006022 case VAR_DICT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006023 return set_ref_in_item_dict(tv->vval.v_dict, copyID,
6024 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006025
6026 case VAR_LIST:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006027 return set_ref_in_item_list(tv->vval.v_list, copyID,
6028 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006029
6030 case VAR_FUNC:
6031 {
6032 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
6033 break;
6034 }
6035
6036 case VAR_PARTIAL:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006037 return set_ref_in_item_partial(tv->vval.v_partial, copyID,
6038 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006039
6040 case VAR_JOB:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006041#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006042 return set_ref_in_item_job(tv->vval.v_job, copyID,
6043 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006044#else
6045 break;
6046#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006047
6048 case VAR_CHANNEL:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006049#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006050 return set_ref_in_item_channel(tv->vval.v_channel, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006051 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006052#else
6053 break;
6054#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006055
6056 case VAR_CLASS:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006057 return set_ref_in_item_class(tv->vval.v_class, copyID,
6058 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006059
6060 case VAR_OBJECT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006061 return set_ref_in_item_object(tv->vval.v_object, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006062 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006063
6064 case VAR_UNKNOWN:
6065 case VAR_ANY:
6066 case VAR_VOID:
6067 case VAR_BOOL:
6068 case VAR_SPECIAL:
6069 case VAR_NUMBER:
6070 case VAR_FLOAT:
6071 case VAR_STRING:
6072 case VAR_BLOB:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006073 case VAR_TYPEALIAS:
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006074 case VAR_INSTR:
6075 // Types that do not contain any other item
6076 break;
6077 }
6078
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006079 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006080}
6081
Bram Moolenaar8c711452005-01-14 21:53:12 +00006082/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006083 * Return a string with the string representation of a variable.
6084 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006085 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006086 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006087 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006088 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02006089 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006090 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6091 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006092 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006093 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006094 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006095echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006096 typval_T *tv,
6097 char_u **tofree,
6098 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006099 int copyID,
6100 int echo_style,
6101 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006102 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006103{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006104 static int recurse = 0;
6105 char_u *r = NULL;
6106
Bram Moolenaar33570922005-01-25 22:26:29 +00006107 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006108 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006109 if (!did_echo_string_emsg)
6110 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006111 // Only give this message once for a recursive call to avoid
6112 // flooding the user with errors. And stop iterating over lists
6113 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02006114 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00006115 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006116 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006117 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006118 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006119 }
6120 ++recurse;
6121
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006122 switch (tv->v_type)
6123 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006124 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006125 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006126 {
6127 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006128 r = tv->vval.v_string;
6129 if (r == NULL)
6130 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006131 }
6132 else
6133 {
6134 *tofree = string_quote(tv->vval.v_string, FALSE);
6135 r = *tofree;
6136 }
6137 break;
6138
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006139 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006140 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01006141 char_u buf[MAX_FUNC_NAME_LEN];
6142
6143 if (echo_style)
6144 {
LemonBoya5d35902022-04-29 21:15:02 +01006145 r = tv->vval.v_string == NULL ? (char_u *)"function()"
6146 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01006147 buf, MAX_FUNC_NAME_LEN);
6148 if (r == buf)
6149 {
6150 r = vim_strsave(buf);
6151 *tofree = r;
6152 }
6153 else
6154 *tofree = NULL;
6155 }
6156 else
6157 {
6158 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
6159 : make_ufunc_name_readable(
6160 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
6161 TRUE);
6162 r = *tofree;
6163 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006164 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006165 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006166
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006167 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006168 {
6169 partial_T *pt = tv->vval.v_partial;
6170 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006171 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006172 garray_T ga;
6173 int i;
6174 char_u *tf;
6175
6176 ga_init2(&ga, 1, 100);
6177 ga_concat(&ga, (char_u *)"function(");
6178 if (fname != NULL)
6179 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006180 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00006181 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006182 && vim_isupper(fname[1]))
6183 {
6184 ga_concat(&ga, (char_u *)"'g:");
6185 ga_concat(&ga, fname + 1);
6186 }
6187 else
6188 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006189 vim_free(fname);
6190 }
6191 if (pt != NULL && pt->pt_argc > 0)
6192 {
6193 ga_concat(&ga, (char_u *)", [");
6194 for (i = 0; i < pt->pt_argc; ++i)
6195 {
6196 if (i > 0)
6197 ga_concat(&ga, (char_u *)", ");
6198 ga_concat(&ga,
6199 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6200 vim_free(tf);
6201 }
6202 ga_concat(&ga, (char_u *)"]");
6203 }
6204 if (pt != NULL && pt->pt_dict != NULL)
6205 {
6206 typval_T dtv;
6207
6208 ga_concat(&ga, (char_u *)", ");
6209 dtv.v_type = VAR_DICT;
6210 dtv.vval.v_dict = pt->pt_dict;
6211 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6212 vim_free(tf);
6213 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00006214 // terminate with ')' and a NUL
6215 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006216
6217 *tofree = ga.ga_data;
6218 r = *tofree;
6219 break;
6220 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006221
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006222 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006223 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006224 break;
6225
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006226 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006227 if (tv->vval.v_list == NULL)
6228 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02006229 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006230 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02006231 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006232 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006233 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6234 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006235 {
6236 *tofree = NULL;
6237 r = (char_u *)"[...]";
6238 }
6239 else
6240 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006241 int old_copyID = tv->vval.v_list->lv_copyID;
6242
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006243 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006244 *tofree = list2string(tv, copyID, restore_copyID);
6245 if (restore_copyID)
6246 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006247 r = *tofree;
6248 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006249 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006250
Bram Moolenaar8c711452005-01-14 21:53:12 +00006251 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006252 if (tv->vval.v_dict == NULL)
6253 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006254 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006255 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006256 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006257 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006258 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6259 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006260 {
6261 *tofree = NULL;
6262 r = (char_u *)"{...}";
6263 }
6264 else
6265 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006266 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006267
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006268 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006269 *tofree = dict2string(tv, copyID, restore_copyID);
6270 if (restore_copyID)
6271 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006272 r = *tofree;
6273 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006274 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006275
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006276 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006277 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006278 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006279 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006280 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006281 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006282 break;
6283
Bram Moolenaar835dc632016-02-07 14:27:38 +01006284 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006285 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006286#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00006287 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02006288 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
6289 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006290 if (composite_val)
6291 {
6292 *tofree = string_quote(r, FALSE);
6293 r = *tofree;
6294 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006295#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006296 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006297
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006298 case VAR_INSTR:
6299 *tofree = NULL;
6300 r = (char_u *)"instructions";
6301 break;
6302
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006303 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006304 {
6305 class_T *cl = tv->vval.v_class;
6306 size_t len = 6 + (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
6307 r = *tofree = alloc(len);
6308 vim_snprintf((char *)r, len, "class %s",
6309 cl == NULL ? "[unknown]" : (char *)cl->class_name);
6310 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006311 break;
6312
6313 case VAR_OBJECT:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006314 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006315 garray_T ga;
6316 ga_init2(&ga, 1, 50);
6317 ga_concat(&ga, (char_u *)"object of ");
6318 object_T *obj = tv->vval.v_object;
6319 class_T *cl = obj == NULL ? NULL : obj->obj_class;
6320 ga_concat(&ga, cl == NULL ? (char_u *)"[unknown]"
6321 : cl->class_name);
6322 if (cl != NULL)
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006323 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006324 ga_concat(&ga, (char_u *)" {");
6325 for (int i = 0; i < cl->class_obj_member_count; ++i)
6326 {
6327 if (i > 0)
6328 ga_concat(&ga, (char_u *)", ");
Bram Moolenaard505d172022-12-18 21:42:55 +00006329 ocmember_T *m = &cl->class_obj_members[i];
6330 ga_concat(&ga, m->ocm_name);
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006331 ga_concat(&ga, (char_u *)": ");
6332 char_u *tf = NULL;
6333 ga_concat(&ga, echo_string_core(
6334 (typval_T *)(obj + 1) + i,
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006335 &tf, numbuf, copyID, echo_style,
6336 restore_copyID, composite_val));
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006337 vim_free(tf);
6338 }
6339 ga_concat(&ga, (char_u *)"}");
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006340 }
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006341
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006342 *tofree = r = ga.ga_data;
6343 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006344 break;
6345
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006346 case VAR_FLOAT:
6347 *tofree = NULL;
6348 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6349 r = numbuf;
6350 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006351
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006352 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006353 case VAR_SPECIAL:
6354 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006355 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006356 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006357
6358 case VAR_TYPEALIAS:
6359 *tofree = vim_strsave(tv->vval.v_typealias->ta_name);
6360 r = *tofree;
6361 if (r == NULL)
6362 r = (char_u *)"";
6363 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006364 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006365
Bram Moolenaar8502c702014-06-17 12:51:16 +02006366 if (--recurse == 0)
6367 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006368 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006369}
6370
6371/*
6372 * Return a string with the string representation of a variable.
6373 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6374 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006375 * Does not put quotes around strings, as ":echo" displays values.
6376 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6377 * May return NULL.
6378 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006379 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006380echo_string(
6381 typval_T *tv,
6382 char_u **tofree,
6383 char_u *numbuf,
6384 int copyID)
6385{
6386 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6387}
6388
6389/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006390 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6391 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006392 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006393 */
6394 int
6395buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6396{
6397 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006398 char_u *t;
6399 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006400
6401 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6402 return -1;
6403
6404 if (lnum > buf->b_ml.ml_line_count)
6405 lnum = buf->b_ml.ml_line_count;
6406
6407 str = ml_get_buf(buf, lnum, FALSE);
6408 if (str == NULL)
6409 return -1;
6410
6411 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006412 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006413
Bram Moolenaar91458462021-01-13 20:08:38 +01006414 // count the number of characters
6415 t = str;
6416 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6417 t += mb_ptr2len(t);
6418
6419 // In insert mode, when the cursor is at the end of a non-empty line,
6420 // byteidx points to the NUL character immediately past the end of the
6421 // string. In this case, add one to the character count.
6422 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6423 count++;
6424
6425 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006426}
6427
6428/*
6429 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006430 * byte index. Works only for loaded buffers. Returns -1 on failure.
6431 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006432 */
6433 int
6434buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6435{
6436 char_u *str;
6437 char_u *t;
6438
6439 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6440 return -1;
6441
6442 if (lnum > buf->b_ml.ml_line_count)
6443 lnum = buf->b_ml.ml_line_count;
6444
6445 str = ml_get_buf(buf, lnum, FALSE);
6446 if (str == NULL)
6447 return -1;
6448
6449 // Convert the character offset to a byte offset
6450 t = str;
6451 while (*t != NUL && --charidx > 0)
6452 t += mb_ptr2len(t);
6453
Bram Moolenaar91458462021-01-13 20:08:38 +01006454 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006455}
6456
6457/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006459 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006461 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006462var2fpos(
6463 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006464 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006465 int *fnum, // set to fnum for '0, 'A, etc.
6466 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006468 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006470 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006472 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006473 if (varp->v_type == VAR_LIST)
6474 {
6475 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006476 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006477 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006478 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006479
6480 l = varp->vval.v_list;
6481 if (l == NULL)
6482 return NULL;
6483
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006484 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006485 pos.lnum = list_find_nr(l, 0L, &error);
6486 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006487 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006488 if (charcol)
6489 len = (long)mb_charlen(ml_get(pos.lnum));
6490 else
6491 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006492
Bram Moolenaarec65d772020-08-20 22:29:12 +02006493 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006494 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006495 li = list_find(l, 1L);
6496 if (li != NULL && li->li_tv.v_type == VAR_STRING
6497 && li->li_tv.vval.v_string != NULL
6498 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006499 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006500 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006501 }
6502 else
6503 {
6504 pos.col = list_find_nr(l, 1L, &error);
6505 if (error)
6506 return NULL;
6507 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006508
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006509 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006510 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006511 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006512 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006513
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006514 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006515 pos.coladd = list_find_nr(l, 2L, &error);
6516 if (error)
6517 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006518
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006519 return &pos;
6520 }
6521
Bram Moolenaarc5809432021-03-27 21:23:30 +01006522 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6523 return NULL;
6524
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006525 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006526 if (name == NULL)
6527 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006528
6529 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006530 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006531 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006532 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006533 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006534 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006535 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006536 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006537 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006538 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006539 pos = VIsual;
6540 else
6541 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006542 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006543 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006544 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006546 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006547 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6549 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006550 pos = *pp;
6551 }
6552 if (pos.lnum != 0)
6553 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006554 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006555 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6556 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006558
Bram Moolenaara5525202006-03-02 22:52:09 +00006559 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006560
Bram Moolenaar477933c2007-07-17 14:32:23 +00006561 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006562 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006563 // the "w_valid" flags are not reset when moving the cursor, but they
6564 // do matter for update_topline() and validate_botline().
6565 check_cursor_moved(curwin);
6566
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006567 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006568 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006569 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006570 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006571 // In silent Ex mode topline is zero, but that's not a valid line
6572 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006573 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006574 return &pos;
6575 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006576 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006577 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006578 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006579 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006580 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006581 return &pos;
6582 }
6583 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006584 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006586 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 {
6588 pos.lnum = curbuf->b_ml.ml_line_count;
6589 pos.col = 0;
6590 }
6591 else
6592 {
6593 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006594 if (charcol)
6595 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6596 else
6597 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 }
6599 return &pos;
6600 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006601 if (in_vim9script())
6602 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 return NULL;
6604}
6605
6606/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006607 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006608 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006609 * Note that the column is passed on as-is, the caller may want to decrement
6610 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006611 * If "charcol" is TRUE use the column as the character index instead of the
6612 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006613 * Return FAIL when conversion is not possible, doesn't check the position for
6614 * validity.
6615 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006616 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006617list2fpos(
6618 typval_T *arg,
6619 pos_T *posp,
6620 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006621 colnr_T *curswantp,
6622 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006623{
6624 list_T *l = arg->vval.v_list;
6625 long i = 0;
6626 long n;
6627
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006628 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6629 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006630 if (arg->v_type != VAR_LIST
6631 || l == NULL
6632 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006633 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006634 return FAIL;
6635
6636 if (fnump != NULL)
6637 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006638 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006639 if (n < 0)
6640 return FAIL;
6641 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006642 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006643 *fnump = n;
6644 }
6645
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006646 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006647 if (n < 0)
6648 return FAIL;
6649 posp->lnum = n;
6650
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006651 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006652 if (n < 0)
6653 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006654 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006655 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006656 if (charcol)
6657 {
6658 buf_T *buf;
6659
6660 // Get the text for the specified line in a loaded buffer
6661 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6662 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6663 return FAIL;
6664
Bram Moolenaar79f23442022-10-10 12:42:57 +01006665 n = buf_charidx_to_byteidx(buf,
6666 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006667 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006668 posp->col = n;
6669
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006670 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006671 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006672 posp->coladd = 0;
6673 else
6674 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006675
Bram Moolenaar493c1782014-05-28 14:34:46 +02006676 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006677 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006678
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006679 return OK;
6680}
6681
6682/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 * Get the length of an environment variable name.
6684 * Advance "arg" to the first character after the name.
6685 * Return 0 for error.
6686 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006687 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006688get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689{
6690 char_u *p;
6691 int len;
6692
6693 for (p = *arg; vim_isIDc(*p); ++p)
6694 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006695 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 return 0;
6697
6698 len = (int)(p - *arg);
6699 *arg = p;
6700 return len;
6701}
6702
6703/*
6704 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006705 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 * Return 0 if something is wrong.
6707 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006708 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006709get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710{
6711 char_u *p;
6712 int len;
6713
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006714 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006716 {
6717 if (*p == ':')
6718 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006719 // "s:" is start of "s:var", but "n:" is not and can be used in
6720 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006721 len = (int)(p - *arg);
6722 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6723 || len > 1)
6724 break;
6725 }
6726 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006727 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 return 0;
6729
6730 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006731 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732
6733 return len;
6734}
6735
6736/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006737 * Get the length of the name of a variable or function.
6738 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006740 * Return -1 if curly braces expansion failed.
6741 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 * If the name contains 'magic' {}'s, expand them and return the
6743 * expanded name in an allocated string via 'alias' - caller must free.
6744 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006745 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006746get_name_len(
6747 char_u **arg,
6748 char_u **alias,
6749 int evaluate,
6750 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751{
6752 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 char_u *p;
6754 char_u *expr_start;
6755 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006757 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758
6759 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6760 && (*arg)[2] == (int)KE_SNR)
6761 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006762 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 *arg += 3;
6764 return get_id_len(arg) + 3;
6765 }
6766 len = eval_fname_script(*arg);
6767 if (len > 0)
6768 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006769 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 *arg += len;
6771 }
6772
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006774 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006776 p = find_name_end(*arg, &expr_start, &expr_end,
6777 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 if (expr_start != NULL)
6779 {
6780 char_u *temp_string;
6781
6782 if (!evaluate)
6783 {
6784 len += (int)(p - *arg);
6785 *arg = skipwhite(p);
6786 return len;
6787 }
6788
6789 /*
6790 * Include any <SID> etc in the expanded string:
6791 * Thus the -len here.
6792 */
6793 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6794 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006795 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 *alias = temp_string;
6797 *arg = skipwhite(p);
6798 return (int)STRLEN(temp_string);
6799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800
6801 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006802 // Only give an error when there is something, otherwise it will be
6803 // reported at a higher level.
6804 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006805 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806
6807 return len;
6808}
6809
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006810/*
6811 * Find the end of a variable or function name, taking care of magic braces.
6812 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6813 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006814 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006815 * Return a pointer to just after the name. Equal to "arg" if there is no
6816 * valid name.
6817 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006818 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006819find_name_end(
6820 char_u *arg,
6821 char_u **expr_start,
6822 char_u **expr_end,
6823 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006825 int mb_nest = 0;
6826 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006828 int len;
zeertzjqa93d9cd2023-05-02 16:25:47 +01006829 int allow_curly = (flags & FNE_ALLOW_CURLY) || !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006831 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006833 *expr_start = NULL;
6834 *expr_end = NULL;
6835 }
6836
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006837 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006838 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006839 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006840 return arg;
6841
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006842 for (p = arg; *p != NUL
6843 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006844 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006845 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006846 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006847 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006848 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006849 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006850 if (*p == '\'')
6851 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006852 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006853 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006854 ;
6855 if (*p == NUL)
6856 break;
6857 }
6858 else if (*p == '"')
6859 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006860 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006861 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006862 if (*p == '\\' && p[1] != NUL)
6863 ++p;
6864 if (*p == NUL)
6865 break;
6866 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006867 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6868 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006869 // "s:" is start of "s:var", but "n:" is not and can be used in
6870 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006871 len = (int)(p - arg);
6872 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006873 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006874 break;
6875 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006876
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006877 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006879 if (*p == '[')
6880 ++br_nest;
6881 else if (*p == ']')
6882 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006884
zeertzjqa93d9cd2023-05-02 16:25:47 +01006885 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006887 if (*p == '{')
6888 {
6889 mb_nest++;
6890 if (expr_start != NULL && *expr_start == NULL)
6891 *expr_start = p;
6892 }
6893 else if (*p == '}')
6894 {
6895 mb_nest--;
6896 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6897 *expr_end = p;
6898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 }
6901
6902 return p;
6903}
6904
6905/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006906 * Expands out the 'magic' {}'s in a variable/function name.
6907 * Note that this can call itself recursively, to deal with
6908 * constructs like foo{bar}{baz}{bam}
6909 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6910 * "in_start" ^
6911 * "expr_start" ^
6912 * "expr_end" ^
6913 * "in_end" ^
6914 *
6915 * Returns a new allocated string, which the caller must free.
6916 * Returns NULL for failure.
6917 */
6918 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006919make_expanded_name(
6920 char_u *in_start,
6921 char_u *expr_start,
6922 char_u *expr_end,
6923 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006924{
6925 char_u c1;
6926 char_u *retval = NULL;
6927 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006928
6929 if (expr_end == NULL || in_end == NULL)
6930 return NULL;
6931 *expr_start = NUL;
6932 *expr_end = NUL;
6933 c1 = *in_end;
6934 *in_end = NUL;
6935
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006936 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006937 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006938 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006939 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6940 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006941 if (retval != NULL)
6942 {
6943 STRCPY(retval, in_start);
6944 STRCAT(retval, temp_result);
6945 STRCAT(retval, expr_end + 1);
6946 }
6947 }
6948 vim_free(temp_result);
6949
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006950 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006951 *expr_start = '{';
6952 *expr_end = '}';
6953
6954 if (retval != NULL)
6955 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006956 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006957 if (expr_start != NULL)
6958 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006959 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006960 temp_result = make_expanded_name(retval, expr_start,
6961 expr_end, temp_result);
6962 vim_free(retval);
6963 retval = temp_result;
6964 }
6965 }
6966
6967 return retval;
6968}
6969
6970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006972 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006974 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006975eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006977 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006978}
6979
6980/*
6981 * Return TRUE if character "c" can be used as the first character in a
6982 * variable or function name (excluding '{' and '}').
6983 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006984 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006985eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006986{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006987 return ASCII_ISALPHA(c) || c == '_';
6988}
6989
6990/*
6991 * Return TRUE if character "c" can be used as the first character of a
6992 * dictionary key.
6993 */
6994 int
6995eval_isdictc(int c)
6996{
6997 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998}
6999
7000/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02007001 * Handle:
7002 * - expr[expr], expr[expr:expr] subscript
7003 * - ".name" lookup
7004 * - function call with Funcref variable: func(expr)
7005 * - method call: var->method()
7006 *
7007 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007008 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007009 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007010 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007011handle_subscript(
7012 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007013 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007014 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007015 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02007016 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007017{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007018 int evaluate = evalarg != NULL
7019 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007020 int ret = OK;
7021 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007022 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007023 int getnext;
7024 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007025
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007026 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007027 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007028 // When at the end of the line and ".name" or "->{" or "->X" follows in
7029 // the next line then consume the line break.
7030 p = eval_next_non_blank(*arg, evalarg, &getnext);
7031 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00007032 && ((*p == '.'
7033 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
7034 || rettv->v_type == VAR_CLASS
7035 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02007036 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
7037 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
7038 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007039 {
Bram Moolenaare442d592022-05-05 12:20:28 +01007040 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02007041 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007042 check_white = FALSE;
7043 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007044
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007045 if (rettv->v_type == VAR_ANY)
7046 {
7047 char_u *exp_name;
7048 int cc;
7049 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01007050 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007051 type_T *type;
7052
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007053 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00007054 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007055 if (**arg != '.')
7056 {
7057 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007058 semsg(_(e_expected_dot_after_name_str),
7059 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007060 ret = FAIL;
7061 break;
7062 }
7063 ++*arg;
7064 if (IS_WHITE_OR_NUL(**arg))
7065 {
7066 if (verbose)
7067 emsg(_(e_no_white_space_allowed_after_dot));
7068 ret = FAIL;
7069 break;
7070 }
7071
7072 // isolate the name
7073 exp_name = *arg;
7074 while (eval_isnamec(**arg))
7075 ++*arg;
7076 cc = **arg;
7077 **arg = NUL;
7078
7079 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01007080 evalarg == NULL ? NULL : evalarg->eval_cctx,
7081 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007082 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007083
7084 if (idx < 0 && ufunc == NULL)
7085 {
7086 ret = FAIL;
7087 break;
7088 }
7089 if (idx >= 0)
7090 {
7091 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
7092 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
7093
7094 copy_tv(sv->sv_tv, rettv);
7095 }
7096 else
7097 {
7098 rettv->v_type = VAR_FUNC;
7099 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
7100 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007101 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007102 }
7103
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007104 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7105 || rettv->v_type == VAR_PARTIAL))
7106 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007107 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02007108 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
7109 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007110
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007111 // Stop the expression evaluation when immediately aborting on
7112 // error, or when an interrupt occurred or an exception was thrown
7113 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007114 if (aborting())
7115 {
7116 if (ret == OK)
7117 clear_tv(rettv);
7118 ret = FAIL;
7119 }
7120 dict_unref(selfdict);
7121 selfdict = NULL;
7122 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02007123 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02007124 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02007125 if (in_vim9script())
7126 *arg = skipwhite(p + 2);
7127 else
7128 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00007129 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007130 {
zeertzjqc481ad32023-03-11 16:18:51 +00007131 emsg(_(e_no_white_space_allowed_before_parenthesis));
7132 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007133 }
zeertzjqc481ad32023-03-11 16:18:51 +00007134 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
7135 // expr->{lambda}() or expr->(lambda)()
7136 ret = eval_lambda(arg, rettv, evalarg, verbose);
7137 else
7138 // expr->name()
7139 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02007140 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007141 // "." is ".name" lookup when we found a dict or when evaluating and
7142 // scriptversion is at least 2, where string concatenation is "..".
7143 else if (**arg == '['
7144 || (**arg == '.' && (rettv->v_type == VAR_DICT
7145 || (!evaluate
7146 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02007147 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007148 {
7149 dict_unref(selfdict);
7150 if (rettv->v_type == VAR_DICT)
7151 {
7152 selfdict = rettv->vval.v_dict;
7153 if (selfdict != NULL)
7154 ++selfdict->dv_refcount;
7155 }
7156 else
7157 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007158 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007159 {
7160 clear_tv(rettv);
7161 ret = FAIL;
7162 }
7163 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007164 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
7165 || rettv->v_type == VAR_OBJECT))
7166 {
7167 // class member: SomeClass.varname
7168 // class method: SomeClass.SomeMethod()
7169 // class constructor: SomeClass.new()
7170 // object member: someObject.varname
7171 // object method: someObject.SomeMethod()
7172 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
7173 {
7174 clear_tv(rettv);
7175 ret = FAIL;
7176 }
7177 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007178 else
7179 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007180 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007181
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007182 // Turn "dict.Func" into a partial for "Func" bound to "dict".
7183 // Don't do this when "Func" is already a partial that was bound
7184 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02007185 if (selfdict != NULL
7186 && (rettv->v_type == VAR_FUNC
7187 || (rettv->v_type == VAR_PARTIAL
7188 && (rettv->vval.v_partial->pt_auto
7189 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007190 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007191
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007192 dict_unref(selfdict);
7193 return ret;
7194}
7195
7196/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007197 * Make a copy of an item.
7198 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00007199 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007200 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7201 * reference to an already copied list/dict can be used.
7202 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007203 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007204 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007205item_copy(
7206 typval_T *from,
7207 typval_T *to,
7208 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00007209 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007210 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007211{
7212 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007213 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007214
Bram Moolenaar33570922005-01-25 22:26:29 +00007215 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007216 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007217 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007218 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007219 }
7220 ++recurse;
7221
7222 switch (from->v_type)
7223 {
7224 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007225 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007226 case VAR_STRING:
7227 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007228 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01007229 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007230 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007231 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007232 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007233 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007234 case VAR_CLASS:
7235 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02007236 case VAR_TYPEALIAS:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007237 copy_tv(from, to);
7238 break;
7239 case VAR_LIST:
7240 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007241 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007242 if (from->vval.v_list == NULL)
7243 to->vval.v_list = NULL;
7244 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7245 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007246 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007247 to->vval.v_list = from->vval.v_list->lv_copylist;
7248 ++to->vval.v_list->lv_refcount;
7249 }
7250 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007251 to->vval.v_list = list_copy(from->vval.v_list,
7252 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007253 if (to->vval.v_list == NULL)
7254 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007255 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007256 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007257 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007258 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007259 case VAR_DICT:
7260 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007261 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007262 if (from->vval.v_dict == NULL)
7263 to->vval.v_dict = NULL;
7264 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7265 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007266 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007267 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7268 ++to->vval.v_dict->dv_refcount;
7269 }
7270 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007271 to->vval.v_dict = dict_copy(from->vval.v_dict,
7272 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007273 if (to->vval.v_dict == NULL)
7274 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007275 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007276 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007277 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007278 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007279 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007280 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007281 }
7282 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007283 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007284}
7285
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007286 void
7287echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7288{
7289 char_u *tofree;
7290 char_u numbuf[NUMBUFLEN];
7291 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7292
7293 if (*atstart)
7294 {
7295 *atstart = FALSE;
7296 // Call msg_start() after eval1(), evaluating the expression
7297 // may cause a message to appear.
7298 if (with_space)
7299 {
7300 // Mark the saved text as finishing the line, so that what
7301 // follows is displayed on a new line when scrolling back
7302 // at the more prompt.
7303 msg_sb_eol();
7304 msg_start();
7305 }
7306 }
7307 else if (with_space)
7308 msg_puts_attr(" ", echo_attr);
7309
7310 if (p != NULL)
7311 for ( ; *p != NUL && !got_int; ++p)
7312 {
7313 if (*p == '\n' || *p == '\r' || *p == TAB)
7314 {
7315 if (*p != TAB && *needclr)
7316 {
7317 // remove any text still there from the command
7318 msg_clr_eos();
7319 *needclr = FALSE;
7320 }
7321 msg_putchar_attr(*p, echo_attr);
7322 }
7323 else
7324 {
7325 if (has_mbyte)
7326 {
7327 int i = (*mb_ptr2len)(p);
7328
7329 (void)msg_outtrans_len_attr(p, i, echo_attr);
7330 p += i - 1;
7331 }
7332 else
7333 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7334 }
7335 }
7336 vim_free(tofree);
7337}
7338
Bram Moolenaare9a41262005-01-15 22:18:47 +00007339/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 * ":echo expr1 ..." print each argument separated with a space, add a
7341 * newline at the end.
7342 * ":echon expr1 ..." print each argument plain.
7343 */
7344 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007345ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346{
7347 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007348 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007349 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 int needclr = TRUE;
7351 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007352 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007353 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007354 evalarg_T evalarg;
7355
Bram Moolenaare707c882020-07-01 13:07:37 +02007356 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357
7358 if (eap->skip)
7359 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007360 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007362 // If eval1() causes an error message the text from the command may
7363 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007364 need_clr_eos = needclr;
7365
Bram Moolenaar68db9962021-05-09 23:19:22 +02007366 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007367 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 {
7369 /*
7370 * Report the invalid expression unless the expression evaluation
7371 * has been cancelled due to an aborting error, an interrupt, or an
7372 * exception.
7373 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007374 if (!aborting() && did_emsg == did_emsg_before
7375 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007376 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007377 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 break;
7379 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007380 need_clr_eos = FALSE;
7381
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007383 {
7384 if (rettv.v_type == VAR_VOID)
7385 {
7386 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7387 break;
7388 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007389 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007390 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007391
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007392 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 arg = skipwhite(arg);
7394 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007395 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007396 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397
7398 if (eap->skip)
7399 --emsg_skip;
7400 else
7401 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007402 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 if (needclr)
7404 msg_clr_eos();
7405 if (eap->cmdidx == CMD_echo)
7406 msg_end();
7407 }
7408}
7409
7410/*
7411 * ":echohl {name}".
7412 */
7413 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007414ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007416 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417}
7418
7419/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007420 * Returns the :echo attribute
7421 */
7422 int
7423get_echo_attr(void)
7424{
7425 return echo_attr;
7426}
7427
7428/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 * ":execute expr1 ..." execute the result of an expression.
7430 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007431 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007433 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 * Each gets spaces around each argument and a newline at the end for
7435 * echo commands
7436 */
7437 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007438ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439{
7440 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007441 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 int ret = OK;
7443 char_u *p;
7444 garray_T ga;
7445 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007446 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447
7448 ga_init2(&ga, 1, 80);
7449
7450 if (eap->skip)
7451 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007452 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007454 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007455 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457
7458 if (!eap->skip)
7459 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007460 char_u buf[NUMBUFLEN];
7461
7462 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007463 {
7464 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7465 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007466 semsg(_(e_using_invalid_value_as_string_str),
7467 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007468 p = NULL;
7469 }
7470 else
7471 p = tv_get_string_buf(&rettv, buf);
7472 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007473 else
7474 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007475 if (p == NULL)
7476 {
7477 clear_tv(&rettv);
7478 ret = FAIL;
7479 break;
7480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 len = (int)STRLEN(p);
7482 if (ga_grow(&ga, len + 2) == FAIL)
7483 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007484 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 ret = FAIL;
7486 break;
7487 }
7488 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 ga.ga_len += len;
7492 }
7493
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007494 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 arg = skipwhite(arg);
7496 }
7497
7498 if (ret != FAIL && ga.ga_data != NULL)
7499 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007500 // use the first line of continuation lines for messages
7501 SOURCING_LNUM = start_lnum;
7502
Bram Moolenaar37fef162022-08-29 18:16:32 +01007503 if (eap->cmdidx == CMD_echomsg
7504 || eap->cmdidx == CMD_echowindow
7505 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007506 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007507 // Mark the already saved text as finishing the line, so that what
7508 // follows is displayed on a new line when scrolling back at the
7509 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007510 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007511 }
7512
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007513 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007514 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007515 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007516 out_flush();
7517 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007518 else if (eap->cmdidx == CMD_echowindow)
7519 {
7520#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007521 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007522#endif
7523 msg_attr(ga.ga_data, echo_attr);
7524#ifdef HAS_MESSAGE_WINDOW
7525 end_echowindow();
7526#endif
7527 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007528 else if (eap->cmdidx == CMD_echoconsole)
7529 {
7530 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7531 ui_write((char_u *)"\r\n", 2, TRUE);
7532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 else if (eap->cmdidx == CMD_echoerr)
7534 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007535 int save_did_emsg = did_emsg;
7536
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007537 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007538 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 if (!force_abort)
7540 did_emsg = save_did_emsg;
7541 }
7542 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007543 {
7544 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7545
7546 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7547 sticky_cmdmod_flags = cmdmod.cmod_flags
7548 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 do_cmdline((char_u *)ga.ga_data,
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01007550 eap->ea_getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007551 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 }
7554
7555 ga_clear(&ga);
7556
7557 if (eap->skip)
7558 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007559 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560}
7561
7562/*
7563 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7564 * "arg" points to the "&" or '+' when called, to "option" when returning.
7565 * Returns NULL when no option name found. Otherwise pointer to the char
7566 * after the option name.
7567 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007568 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007569find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570{
7571 char_u *p = *arg;
7572
7573 ++p;
7574 if (*p == 'g' && p[1] == ':')
7575 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007576 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 p += 2;
7578 }
7579 else if (*p == 'l' && p[1] == ':')
7580 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007581 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 p += 2;
7583 }
7584 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007585 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586
7587 if (!ASCII_ISALPHA(*p))
7588 return NULL;
7589 *arg = p;
7590
7591 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007592 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 else
7594 while (ASCII_ISALPHA(*p))
7595 ++p;
7596 return p;
7597}
7598
7599/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007600 * Display script name where an item was last set.
7601 * Should only be invoked when 'verbose' is non-zero.
7602 */
7603 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007604last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007605{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007606 char_u *p;
7607
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007608 if (script_ctx.sc_sid == 0)
7609 return;
7610
7611 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7612 if (p == NULL)
7613 return;
7614
7615 verbose_enter();
7616 msg_puts(_("\n\tLast set from "));
7617 msg_puts((char *)p);
7618 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007619 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007620 msg_puts(_(line_msg));
7621 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007622 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007623 verbose_leave();
7624 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007625}
7626
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007627#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628
7629/*
7630 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007631 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 * "flags" can be "g" to do a global substitute.
7633 * Returns an allocated string, NULL for error.
7634 */
7635 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007636do_string_sub(
7637 char_u *str,
7638 char_u *pat,
7639 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007640 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007641 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642{
7643 int sublen;
7644 regmatch_T regmatch;
7645 int i;
7646 int do_all;
7647 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007648 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 garray_T ga;
7650 char_u *ret;
7651 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007652 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007654 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007656 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657
7658 ga_init2(&ga, 1, 200);
7659
7660 do_all = (flags[0] == 'g');
7661
7662 regmatch.rm_ic = p_ic;
7663 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7664 if (regmatch.regprog != NULL)
7665 {
7666 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007667 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7669 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007670 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007671 if (regmatch.startp[0] == regmatch.endp[0])
7672 {
7673 if (zero_width == regmatch.startp[0])
7674 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007675 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007676 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007677 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7678 (size_t)i);
7679 ga.ga_len += i;
7680 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007681 continue;
7682 }
7683 zero_width = regmatch.startp[0];
7684 }
7685
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 /*
7687 * Get some space for a temporary buffer to do the substitution
7688 * into. It will contain:
7689 * - The text up to where the match is.
7690 * - The substituted text.
7691 * - The text after the match.
7692 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007693 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007694 if (sublen <= 0)
7695 {
7696 ga_clear(&ga);
7697 break;
7698 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007699 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7701 {
7702 ga_clear(&ga);
7703 break;
7704 }
7705
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007706 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 i = (int)(regmatch.startp[0] - tail);
7708 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007709 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007710 (void)vim_regsub(&regmatch, sub, expr,
7711 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7712 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007714 tail = regmatch.endp[0];
7715 if (*tail == NUL)
7716 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 if (!do_all)
7718 break;
7719 }
7720
7721 if (ga.ga_data != NULL)
7722 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7723
Bram Moolenaar473de612013-06-08 18:19:48 +02007724 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 }
7726
7727 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7728 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007729 if (p_cpo == empty_option)
7730 p_cpo = save_cpo;
7731 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007732 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007733 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007734 // If it's still empty it was changed and restored, need to restore in
7735 // the complicated way.
7736 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007737 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007738 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740
7741 return ret;
7742}