blob: 5edda682062f7f32f2341902937d660d52a2c764 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020032static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
33static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
34static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
35static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010036static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020037static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010038static int eval8(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
39static int eval9(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
40static int eval9_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000041
Bram Moolenaar48e697e2016-01-23 22:17:30 +010042static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020043static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020044
Bram Moolenaare21c1582019-03-02 11:57:09 +010045/*
46 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010047 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010048 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020049 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010050num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010051{
52 varnumber_T result;
53
Bram Moolenaar99880f92021-01-20 21:23:14 +010054 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010055 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010056 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010057 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010058 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010059 if (failed != NULL)
60 *failed = TRUE;
61 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010062 if (n1 == 0)
63 result = VARNUM_MIN; // similar to NaN
64 else if (n1 < 0)
65 result = -VARNUM_MAX;
66 else
67 result = VARNUM_MAX;
68 }
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +010069 else if (n1 == VARNUM_MIN && n2 == -1)
70 {
71 // specific case: trying to do VARNUM_MIN / -1 results in a positive
72 // number that doesn't fit in varnumber_T and causes an FPE
73 result = VARNUM_MAX;
74 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010075 else
76 result = n1 / n2;
77
78 return result;
79}
80
81/*
82 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010083 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010084 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020085 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010086num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010087{
Bram Moolenaar99880f92021-01-20 21:23:14 +010088 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010089 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010090 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010091 if (failed != NULL)
92 *failed = TRUE;
93 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010094 return (n2 == 0) ? 0 : (n1 % n2);
95}
96
Bram Moolenaar33570922005-01-25 22:26:29 +000097/*
98 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +000099 */
100 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100101eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000102{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200103 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200104 func_init();
Bram Moolenaara7043832005-01-21 11:56:39 +0000105}
106
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000107#if defined(EXITFREE) || defined(PROTO)
108 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100109eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000110{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200111 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100112 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200113 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000114
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200115 // autoloaded script names
116 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000117
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200118 // unreferenced lists and dicts
119 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200120
121 // functions not garbage collected
122 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000123}
124#endif
125
Bram Moolenaare6b53242020-07-01 17:28:33 +0200126 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200127fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
128{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100129 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200130 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000131
132 if (eap == NULL)
133 return;
134
135 evalarg->eval_cstack = eap->cstack;
136 if (sourcing_a_script(eap) || eap->getline == get_list_line)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200137 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000138 evalarg->eval_getline = eap->getline;
139 evalarg->eval_cookie = eap->cookie;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200140 }
141}
142
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143/*
144 * Top level evaluation function, returning a boolean.
145 * Sets "error" to TRUE if there was an error.
146 * Return TRUE or FALSE.
147 */
148 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100149eval_to_bool(
150 char_u *arg,
151 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200152 exarg_T *eap,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100153 int skip, // only parse, don't execute
154 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155{
Bram Moolenaar33570922005-01-25 22:26:29 +0000156 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200157 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200158 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100159 int r;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200160
Bram Moolenaar37c83712020-06-30 21:18:36 +0200161 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162
163 if (skip)
164 ++emsg_skip;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100165 if (use_simple_function)
166 r = eval0_simple_funccal(arg, &tv, eap, &evalarg);
167 else
168 r = eval0(arg, &tv, eap, &evalarg);
169 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 else
172 {
173 *error = FALSE;
174 if (!skip)
175 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200176 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200177 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200178 else
179 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000180 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 }
182 }
183 if (skip)
184 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200185 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200187 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188}
189
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100190/*
191 * Call eval1() and give an error message if not done at a lower level.
192 */
193 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200194eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100195{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100196 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100197 int ret;
198 int did_emsg_before = did_emsg;
199 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200200 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100201
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200202 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
203
204 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100205 if (ret == FAIL)
206 {
207 // Report the invalid expression unless the expression evaluation has
208 // been cancelled due to an aborting error, an interrupt, or an
209 // exception, or we already gave a more specific error.
210 // Also check called_emsg for when using assert_fails().
211 if (!aborting() && did_emsg == did_emsg_before
212 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200213 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100214 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200215 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100216 return ret;
217}
218
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200220 * Return whether a typval is a valid expression to pass to eval_expr_typval()
221 * or eval_expr_to_bool(). An empty string returns FALSE;
222 */
223 int
224eval_expr_valid_arg(typval_T *tv)
225{
226 return tv->v_type != VAR_UNKNOWN
227 && (tv->v_type != VAR_STRING
228 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
229}
230
231/*
Bram Moolenaar82418262022-09-28 16:16:15 +0100232 * When calling eval_expr_typval() many times we only need one funccall_T.
233 * Returns NULL when no funccall_T is to be used.
234 * When returning non-NULL remove_funccal() must be called later.
235 */
236 funccall_T *
237eval_expr_get_funccal(typval_T *expr, typval_T *rettv)
238{
239 if (expr->v_type != VAR_PARTIAL)
240 return NULL;
241
242 partial_T *partial = expr->vval.v_partial;
243 if (partial == NULL)
244 return NULL;
245 if (partial->pt_func == NULL
246 || partial->pt_func->uf_def_status == UF_NOT_COMPILED)
247 return NULL;
248
249 return create_funccal(partial->pt_func, rettv);
250}
251
252/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253 * Evaluate an expression, which can be a function, partial or string.
254 * Pass arguments "argv[argc]".
zeertzjqad0c4422023-08-17 22:15:47 +0200255 * If "want_func" is TRUE treat a string as a function name, not an expression.
Bram Moolenaar82418262022-09-28 16:16:15 +0100256 * "fc_arg" is from eval_expr_get_funccal() or NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100257 * Return the result in "rettv" and OK or FAIL.
258 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200259 int
Bram Moolenaar82418262022-09-28 16:16:15 +0100260eval_expr_typval(
261 typval_T *expr,
zeertzjqad0c4422023-08-17 22:15:47 +0200262 int want_func,
Bram Moolenaar82418262022-09-28 16:16:15 +0100263 typval_T *argv,
264 int argc,
265 funccall_T *fc_arg,
266 typval_T *rettv)
Bram Moolenaar48570482017-10-30 21:48:41 +0100267{
268 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100269 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200270 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100271
zeertzjqad0c4422023-08-17 22:15:47 +0200272 if (expr->v_type == VAR_PARTIAL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100273 {
274 partial_T *partial = expr->vval.v_partial;
275
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200276 if (partial == NULL)
277 return FAIL;
278
Bram Moolenaar822ba242020-05-24 23:00:18 +0200279 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200280 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100281 {
Bram Moolenaar82418262022-09-28 16:16:15 +0100282 funccall_T *fc = fc_arg != NULL ? fc_arg
283 : create_funccal(partial->pt_func, rettv);
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100284 int r;
285
286 if (fc == NULL)
287 return FAIL;
288
Bram Moolenaar69082912022-09-22 21:35:19 +0100289 // Shortcut to call a compiled function with minimal overhead.
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100290 r = call_def_function(partial->pt_func, argc, argv,
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000291 DEF_USE_PT_ARGV, partial, NULL, fc, rettv);
Bram Moolenaar82418262022-09-28 16:16:15 +0100292 if (fc_arg == NULL)
293 remove_funccal();
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100294 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100295 return FAIL;
296 }
297 else
298 {
299 s = partial_name(partial);
300 if (s == NULL || *s == NUL)
301 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200302 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000303 funcexe.fe_evaluate = TRUE;
304 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
306 return FAIL;
307 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100308 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200309 else if (expr->v_type == VAR_INSTR)
310 {
311 return exe_typval_instr(expr, rettv);
312 }
zeertzjqad0c4422023-08-17 22:15:47 +0200313 else if (expr->v_type == VAR_FUNC || want_func)
314 {
315 s = expr->v_type == VAR_FUNC
316 ? expr->vval.v_string
317 : tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
318 if (s == NULL || *s == NUL)
319 return FAIL;
320 CLEAR_FIELD(funcexe);
321 funcexe.fe_evaluate = TRUE;
322 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
323 return FAIL;
324 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100325 else
326 {
Bram Moolenaarfc4c4482022-01-26 21:17:04 +0000327 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
Bram Moolenaar48570482017-10-30 21:48:41 +0100328 if (s == NULL)
329 return FAIL;
330 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200331 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100332 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200333 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100334 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200335 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200336 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100337 return FAIL;
338 }
339 }
340 return OK;
341}
342
343/*
344 * Like eval_to_bool() but using a typval_T instead of a string.
345 * Works for string, funcref and partial.
346 */
347 int
348eval_expr_to_bool(typval_T *expr, int *error)
349{
350 typval_T rettv;
351 int res;
352
zeertzjqad0c4422023-08-17 22:15:47 +0200353 if (eval_expr_typval(expr, FALSE, NULL, 0, NULL, &rettv) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100354 {
355 *error = TRUE;
356 return FALSE;
357 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200358 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100359 clear_tv(&rettv);
360 return res;
361}
362
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363/*
364 * Top level evaluation function, returning a string. If "skip" is TRUE,
365 * only parsing to "nextcmd" is done, without reporting errors. Return
366 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
367 */
368 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100369eval_to_string_skip(
370 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200371 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100372 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373{
Bram Moolenaar33570922005-01-25 22:26:29 +0000374 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200376 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377
Bram Moolenaar37c83712020-06-30 21:18:36 +0200378 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 if (skip)
380 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200381 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 retval = NULL;
383 else
384 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100385 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000386 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 }
388 if (skip)
389 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200390 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391
392 return retval;
393}
394
395/*
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100396 * Initialize "evalarg" for use.
397 */
398 void
399init_evalarg(evalarg_T *evalarg)
400{
401 CLEAR_POINTER(evalarg);
402 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
403}
404
405/*
406 * If "evalarg->eval_tofree" is not NULL free it later.
407 * Caller is expected to overwrite "evalarg->eval_tofree" next.
408 */
409 static void
410free_eval_tofree_later(evalarg_T *evalarg)
411{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000412 if (evalarg->eval_tofree == NULL)
413 return;
414
415 if (ga_grow(&evalarg->eval_tofree_ga, 1) == OK)
416 ((char_u **)evalarg->eval_tofree_ga.ga_data)
417 [evalarg->eval_tofree_ga.ga_len++]
418 = evalarg->eval_tofree;
419 else
420 vim_free(evalarg->eval_tofree);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100421}
422
423/*
424 * After using "evalarg" filled from "eap": free the memory.
425 */
426 void
427clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
428{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000429 if (evalarg == NULL)
430 return;
431
432 garray_T *etga = &evalarg->eval_tofree_ga;
433
434 if (evalarg->eval_tofree != NULL || evalarg->eval_using_cmdline)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100435 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000436 if (eap != NULL)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100437 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000438 // We may need to keep the original command line, e.g. for
439 // ":let" it has the variable names. But we may also need
440 // the new one, "nextcmd" points into it. Keep both.
441 vim_free(eap->cmdline_tofree);
442 eap->cmdline_tofree = *eap->cmdlinep;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100443
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000444 if (evalarg->eval_using_cmdline && etga->ga_len > 0)
445 {
446 // "nextcmd" points into the last line in eval_tofree_ga,
447 // need to keep it around.
448 --etga->ga_len;
449 *eap->cmdlinep = ((char_u **)etga->ga_data)[etga->ga_len];
450 vim_free(evalarg->eval_tofree);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100451 }
452 else
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000453 *eap->cmdlinep = evalarg->eval_tofree;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100454 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000455 else
456 vim_free(evalarg->eval_tofree);
457 evalarg->eval_tofree = NULL;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100458 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000459
460 ga_clear_strings(etga);
461 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100462}
463
464/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000465 * Skip over an expression at "*pp".
466 * Return FAIL for an error, OK otherwise.
467 */
468 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200469skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000470{
Bram Moolenaar33570922005-01-25 22:26:29 +0000471 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000472
473 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200474 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000475}
476
477/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200478 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200479 * If in Vim9 script and line breaks are encountered, the lines are
480 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200481 * "arg" is advanced to just after the expression.
482 * "start" is set to the start of the expression, "end" to just after the end.
483 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200484 * Return FAIL for an error, OK otherwise.
485 */
486 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200487skip_expr_concatenate(
488 char_u **arg,
489 char_u **start,
490 char_u **end,
491 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200492{
493 typval_T rettv;
494 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200495 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200496 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200497 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200498 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200499 int evaluate = evalarg == NULL
500 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200501
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200502 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200503 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200504 {
505 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200506 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200507 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200508 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200509 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200510 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200511 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200512
513 // Don't evaluate the expression.
514 if (evalarg != NULL)
515 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200516 *arg = skipwhite(*arg);
517 res = eval1(arg, &rettv, evalarg);
518 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200519 if (evalarg != NULL)
520 evalarg->eval_flags = save_flags;
521
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200522 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200523 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200524 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200525 if (evalarg->eval_ga.ga_len == 1)
526 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200527 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200528 ga_clear(gap);
529 gap->ga_itemsize = 0;
530 }
531 else
532 {
533 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200534 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200535
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200536 // Line breaks encountered, concatenate all the lines.
537 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200538 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200539
540 // free the lines only when using getsourceline()
541 if (evalarg->eval_cookie != NULL)
542 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200543 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200544 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200545 // Do not free the last line, "arg" points into it, free it
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100546 // later. Also free "eval_tofree" later if needed.
547 free_eval_tofree_later(evalarg);
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200548 evalarg->eval_tofree =
549 ((char_u **)gap->ga_data)[gap->ga_len - 1];
550 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200551 ga_clear_strings(gap);
552 }
553 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200554 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200555 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200556
557 // free lines that were explicitly marked for freeing
558 ga_clear_strings(freegap);
559 }
560
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200561 gap->ga_itemsize = 0;
562 if (p == NULL)
563 return FAIL;
564 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200565 vim_free(evalarg->eval_tofree_lambda);
566 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200567 // Compute "end" relative to the end.
568 *end = *start + STRLEN(*start) - endoff;
569 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200570 }
571
572 return res;
573}
574
575/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100576 * Convert "tv" to a string.
zeertzjq19dfa272023-06-15 10:56:41 +0100577 * When "convert" is TRUE convert a List into a sequence of lines.
Bram Moolenaar883cf972021-01-15 18:04:43 +0100578 * Returns an allocated string (NULL when out of memory).
579 */
580 char_u *
581typval2string(typval_T *tv, int convert)
582{
583 garray_T ga;
584 char_u *retval;
Bram Moolenaar883cf972021-01-15 18:04:43 +0100585
586 if (convert && tv->v_type == VAR_LIST)
587 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000588 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100589 if (tv->vval.v_list != NULL)
590 {
591 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
592 if (tv->vval.v_list->lv_len > 0)
593 ga_append(&ga, NL);
594 }
595 ga_append(&ga, NUL);
596 retval = (char_u *)ga.ga_data;
597 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100598 else
599 retval = vim_strsave(tv_get_string(tv));
600 return retval;
601}
602
603/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200604 * Top level evaluation function, returning a string. Does not handle line
605 * breaks.
zeertzjq19dfa272023-06-15 10:56:41 +0100606 * When "convert" is TRUE convert a List into a sequence of lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 * Return pointer to allocated memory, or NULL for failure.
608 */
609 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100610eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100611 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100612 int convert,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100613 exarg_T *eap,
614 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615{
Bram Moolenaar33570922005-01-25 22:26:29 +0000616 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100618 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100619 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100621 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100622 if (use_simple_function)
623 r = eval0_simple_funccal(arg, &tv, NULL, &evalarg);
624 else
625 r = eval0(arg, &tv, NULL, &evalarg);
626 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 retval = NULL;
628 else
629 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100630 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000631 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100633 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634
635 return retval;
636}
637
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100638 char_u *
639eval_to_string(
640 char_u *arg,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100641 int convert,
642 int use_simple_function)
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100643{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100644 return eval_to_string_eap(arg, convert, NULL, use_simple_function);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100645}
646
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000648 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100649 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200650 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 */
652 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100653eval_to_string_safe(
654 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000655 int use_sandbox,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100656 int keep_script_version,
657 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658{
659 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200660 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200661 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200662 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663
Bram Moolenaar9530b582022-01-22 13:39:08 +0000664 if (!keep_script_version)
665 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200666 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000667 if (use_sandbox)
668 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100669 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200670 may_garbage_collect = FALSE;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100671 retval = eval_to_string(arg, FALSE, use_simple_function);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000672 if (use_sandbox)
673 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100674 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200675 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200676 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200677 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 return retval;
679}
680
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681/*
682 * Top level evaluation function, returning a number.
683 * Evaluates "expr" silently.
684 * Returns -1 for an error.
685 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200686 varnumber_T
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100687eval_to_number(char_u *expr, int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688{
Bram Moolenaar33570922005-01-25 22:26:29 +0000689 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200690 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000691 char_u *p = skipwhite(expr);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100692 int r = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693
694 ++emsg_off;
695
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100696 if (use_simple_function)
697 r = may_call_simple_func(expr, &rettv);
698 if (r == NOTDONE)
699 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
700 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 retval = -1;
702 else
703 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100704 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000705 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 }
707 --emsg_off;
708
709 return retval;
710}
711
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000712/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000713 * Top level evaluation function.
714 * Returns an allocated typval_T with the result.
715 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000716 */
717 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200718eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000719{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100720 return eval_expr_ext(arg, eap, FALSE);
721}
722
723 typval_T *
724eval_expr_ext(char_u *arg, exarg_T *eap, int use_simple_function)
725{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000726 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200727 evalarg_T evalarg;
728
729 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000730
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200731 tv = ALLOC_ONE(typval_T);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100732 if (tv != NULL)
733 {
734 int r = NOTDONE;
735
736 if (use_simple_function)
737 r = eval0_simple_funccal(arg, tv, eap, &evalarg);
738 if (r == NOTDONE)
739 r = eval0(arg, tv, eap, &evalarg);
740
741 if (r == FAIL)
742 VIM_CLEAR(tv);
743 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000744
Bram Moolenaar37c83712020-06-30 21:18:36 +0200745 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000746 return tv;
747}
748
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000750 * "*arg" points to what can be a function name in the form of "import.Name" or
751 * "Funcref". Return the name of the function. Set "tofree" to something that
752 * was allocated.
753 * If "verbose" is FALSE no errors are given.
754 * Return NULL for any failure.
755 */
756 static char_u *
757deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000758 char_u **arg,
759 char_u **tofree,
760 evalarg_T *evalarg,
761 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000762{
763 typval_T ref;
764 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100765 int save_flags = 0;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000766
767 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100768 if (evalarg != NULL)
769 {
770 // need to evaluate this to get an import, like in "a.Func"
771 save_flags = evalarg->eval_flags;
772 evalarg->eval_flags |= EVAL_EVALUATE;
773 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100774 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100775 {
776 dictitem_T *v;
777
778 // If <SID>VarName was used it would not be found, try another way.
779 v = find_var_also_in_script(name, NULL, FALSE);
780 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100781 {
782 name = NULL;
783 goto theend;
784 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100785 copy_tv(&v->di_tv, &ref);
786 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000787 if (*skipwhite(*arg) != NUL)
788 {
789 if (verbose)
790 semsg(_(e_trailing_characters_str), *arg);
791 name = NULL;
792 }
793 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
794 {
795 name = ref.vval.v_string;
796 ref.vval.v_string = NULL;
797 *tofree = name;
798 }
799 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
800 {
801 if (ref.vval.v_partial->pt_argc > 0
802 || ref.vval.v_partial->pt_dict != NULL)
803 {
804 if (verbose)
805 emsg(_(e_cannot_use_partial_here));
806 name = NULL;
807 }
808 else
809 {
810 name = vim_strsave(partial_name(ref.vval.v_partial));
811 *tofree = name;
812 }
813 }
814 else
815 {
816 if (verbose)
817 semsg(_(e_not_callable_type_str), name);
818 name = NULL;
819 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100820
821theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000822 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100823 if (evalarg != NULL)
824 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000825 return name;
826}
827
828/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100829 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200830 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
831 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000832 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200834 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100835call_vim_function(
836 char_u *func,
837 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200838 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200839 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000841 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200842 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000843 char_u *arg;
844 char_u *name;
845 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000846 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100848 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200849 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000850 funcexe.fe_firstline = curwin->w_cursor.lnum;
851 funcexe.fe_lastline = curwin->w_cursor.lnum;
852 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000853
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000854 // The name might be "import.Func" or "Funcref". We don't know, we need to
855 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000856 // autoload script has errors. Guess that when there is a dot in the name
857 // showing errors is the right choice.
858 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000859 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000860 if (ignore_errors)
861 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000862 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000863 if (ignore_errors)
864 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000865 if (name == NULL)
866 name = func;
867
868 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
869
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000870 if (ret == FAIL)
871 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000872 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000873
874 return ret;
875}
876
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100877/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100878 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000879 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
880 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000881 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000882 */
883 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100884call_func_retstr(
885 char_u *func,
886 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200887 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000888{
889 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000890 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000891
Bram Moolenaarded27a12018-08-01 19:06:03 +0200892 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000893 return NULL;
894
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100895 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000896 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 return retval;
898}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000899
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000900/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100901 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000902 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000903 * Returns NULL when there is something wrong.
Bram Moolenaar55e93662022-09-10 13:52:26 +0100904 * Gives an error when the returned value is not a list.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000905 */
906 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100907call_func_retlist(
908 char_u *func,
909 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200910 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000911{
912 typval_T rettv;
913
Bram Moolenaarded27a12018-08-01 19:06:03 +0200914 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000915 return NULL;
916
917 if (rettv.v_type != VAR_LIST)
918 {
Bram Moolenaar55e93662022-09-10 13:52:26 +0100919 semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
920 vartype_name(rettv.v_type));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000921 clear_tv(&rettv);
922 return NULL;
923 }
924
925 return rettv.vval.v_list;
926}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927
Bram Moolenaare70dd112022-01-21 16:31:11 +0000928#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100930 * Evaluate "arg", which is 'foldexpr'.
931 * Note: caller must set "curwin" to match "arg".
932 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
933 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 */
935 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000936eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000938 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000939 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200940 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000942 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000943 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100944 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100946 arg = skipwhite(wp->w_p_fde);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000947 current_sctx = wp->w_p_script_ctx[WV_FDE];
948
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000950 if (use_sandbox)
951 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100952 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 *cp = NUL;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100954
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100955 // Evaluate the expression. If the expression is "FuncName()" call the
956 // function directly.
957 if (eval0_simple_funccal(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 retval = 0;
959 else
960 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100961 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000962 if (tv.v_type == VAR_NUMBER)
963 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000964 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 retval = 0;
966 else
967 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100968 // If the result is a string, check if there is a non-digit before
969 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000970 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 if (!VIM_ISDIGIT(*s) && *s != '-')
972 *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
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000989 * Get an lval: variable, Dict item or List item that can be assigned a value
990 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
991 * "name.key", "name.key[expr]" etc.
992 * Indexing only works if "name" is an existing List or Dictionary.
993 * "name" points to the start of the name.
994 * If "rettv" is not NULL it points to the value to be assigned.
995 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
996 * wrong; must end in space or cmd separator.
997 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100998 * flags:
999 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001000 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001001 * GLV_NO_AUTOLOAD: do not use script autoloading
1002 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001003 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001004 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001005 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001006 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001007 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001008get_lval(
1009 char_u *name,
1010 typval_T *rettv,
1011 lval_T *lp,
1012 int unlet,
1013 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001014 int flags, // GLV_ values
1015 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001016{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001017 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001018 char_u *expr_start, *expr_end;
1019 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001020 dictitem_T *v;
1021 typval_T var1;
1022 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001023 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001024 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001025 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01001026 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001027 int quiet = flags & GLV_QUIET;
Ernie Rael86eda072023-09-05 07:33:48 +02001028 int writing = 0;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001029 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001030
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001031 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02001032 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001033
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001034 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001035 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001036 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001037 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02001038 lp->ll_name_end = find_name_end(name, NULL, NULL,
1039 FNE_INCL_BR | fne_flags);
1040 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001041 }
1042
Bram Moolenaara749a422022-02-12 19:52:25 +00001043 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001044 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00001045 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
1046 {
1047 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
1048 return NULL;
1049 }
1050
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001051 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001052 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001054 if (expr_start != NULL)
1055 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001056 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01001057 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001058 && *p != '[' && *p != '.')
1059 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001060 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001061 return NULL;
1062 }
1063
1064 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1065 if (lp->ll_exp_name == NULL)
1066 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001067 // Report an invalid expression in braces, unless the
1068 // expression evaluation has been cancelled due to an
1069 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001070 if (!aborting() && !quiet)
1071 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001072 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001073 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001074 return NULL;
1075 }
1076 }
1077 lp->ll_name = lp->ll_exp_name;
1078 }
1079 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001080 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001081 lp->ll_name = name;
1082
Bram Moolenaar4525a572022-02-13 11:57:33 +00001083 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001085 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001086 // However, "g:[key]" is indexing a dictionary.
1087 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001088 {
1089 --p;
1090 lp->ll_name_end = p;
1091 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001092 if (*skipwhite(p) == ':')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001093 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001094 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001095
Bram Moolenaar9510d222022-09-11 15:14:05 +01001096 if (is_scoped_variable(name))
1097 {
1098 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1099 return NULL;
1100 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001101 if (VIM_ISWHITE(*p))
1102 {
1103 semsg(_(e_no_white_space_allowed_before_colon_str), p);
1104 return NULL;
1105 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001106 if (tp == p + 1 && !quiet)
1107 {
1108 semsg(_(e_white_space_required_after_str_str), ":", p);
1109 return NULL;
1110 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001111 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001112 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001113 semsg(_(e_using_type_not_in_script_context_str), p);
1114 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001115 }
1116
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001117 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001118 lp->ll_type = parse_type(&tp,
1119 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1120 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001121 if (lp->ll_type == NULL && !quiet)
1122 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001123 lp->ll_name_end = tp;
1124 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001125 }
1126 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001127 if (lp->ll_name == NULL)
1128 return p;
1129
Bram Moolenaar62aec932022-01-29 21:45:34 +00001130 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001131 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001132 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001133
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001134 if (import != NULL)
1135 {
1136 ufunc_T *ufunc;
1137 type_T *type;
1138
Bram Moolenaar753885b2022-08-24 16:30:36 +01001139 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001140 lp->ll_sid = import->imp_sid;
1141 lp->ll_name = skipwhite(p + 1);
1142 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1143 lp->ll_name_end = p;
1144
1145 // check the item is exported
1146 cc = *p;
1147 *p = NUL;
1148 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001149 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001150 {
1151 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001152 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001153 }
1154 *p = cc;
1155 }
1156 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001157
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001158 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001159 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001160 return p;
1161
Bram Moolenaar4525a572022-02-13 11:57:33 +00001162 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001163 {
1164 // using local variable
1165 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001166 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001167 }
1168 else
1169 {
1170 cc = *p;
1171 *p = NUL;
1172 // When we would write to the variable pass &ht and prevent autoload.
1173 writing = !(flags & GLV_READ_ONLY);
1174 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001175 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001176 if (v == NULL && !quiet)
1177 semsg(_(e_undefined_variable_str), lp->ll_name);
1178 *p = cc;
1179 if (v == NULL)
1180 return NULL;
1181 lp->ll_tv = &v->di_tv;
1182 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001183
Bram Moolenaar4525a572022-02-13 11:57:33 +00001184 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001185 {
1186 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001187 semsg(_(e_variable_already_declared_str), lp->ll_name);
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001188 return NULL;
1189 }
1190
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001191 /*
1192 * Loop until no more [idx] or .key is following.
1193 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001194 var1.v_type = VAR_UNKNOWN;
1195 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001196 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001197 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001198 vartype_T v_type = lp->ll_tv->v_type;
1199
1200 if (*p == '.' && v_type != VAR_DICT
1201 && v_type != VAR_OBJECT
1202 && v_type != VAR_CLASS)
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001203 {
1204 if (!quiet)
1205 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1206 return NULL;
1207 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001208 if (v_type != VAR_LIST
1209 && v_type != VAR_DICT
1210 && v_type != VAR_BLOB
1211 && v_type != VAR_OBJECT
1212 && v_type != VAR_CLASS)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001213 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001214 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001215 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001216 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001217 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001218
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001219 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001220 int r = OK;
Bram Moolenaard505d172022-12-18 21:42:55 +00001221 if (v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001222 r = rettv_list_alloc(lp->ll_tv);
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +02001223 else if (v_type == VAR_BLOB && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001224 r = rettv_blob_alloc(lp->ll_tv);
1225 if (r == FAIL)
1226 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001227
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001228 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001229 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001230 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001231 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001232 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001233 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001234
Bram Moolenaar4525a572022-02-13 11:57:33 +00001235 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001236 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001237 && lp->ll_tv == &v->di_tv
1238 && ht != NULL && ht == get_script_local_ht())
1239 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001240 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001241
1242 // Vim9 script local variable: get the type
1243 if (sv != NULL)
1244 lp->ll_valtype = sv->sv_type;
1245 }
1246
Bram Moolenaar8c711452005-01-14 21:53:12 +00001247 len = -1;
1248 if (*p == '.')
1249 {
1250 key = p + 1;
1251 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1252 ;
1253 if (len == 0)
1254 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001255 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001256 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001257 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001258 }
1259 p = key + len;
1260 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001261 else
1262 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001263 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001264 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001265 if (*p == ':')
1266 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001267 else
1268 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001269 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001270 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001271 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001272 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001273 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001274 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001275 clear_tv(&var1);
1276 return NULL;
1277 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001278 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001279 }
1280
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001281 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001282 if (*p == ':')
1283 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001284 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001285 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001286 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001287 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001288 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001289 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001290 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001291 if (rettv != NULL
1292 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001293 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001294 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001295 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001296 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001297 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001298 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001299 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001300 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001301 }
1302 p = skipwhite(p + 1);
1303 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001304 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001305 else
1306 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001307 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001308 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001309 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001310 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001311 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001312 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001313 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001314 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001315 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001316 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001317 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001318 clear_tv(&var2);
1319 return NULL;
1320 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001321 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001322 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001323 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001324 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001325 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001326
Bram Moolenaar8c711452005-01-14 21:53:12 +00001327 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001328 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001329 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001330 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001331 clear_tv(&var1);
1332 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001333 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001334 }
1335
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001336 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001337 ++p;
1338 }
1339
Bram Moolenaard505d172022-12-18 21:42:55 +00001340 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001341 {
1342 if (len == -1)
1343 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001344 // "[key]": get key from "var1"
1345 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001346 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001347 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001348 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001349 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001350 }
1351 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001352 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001353
1354 // a NULL dict is equivalent with an empty dict
1355 if (lp->ll_tv->vval.v_dict == NULL)
1356 {
1357 lp->ll_tv->vval.v_dict = dict_alloc();
1358 if (lp->ll_tv->vval.v_dict == NULL)
1359 {
1360 clear_tv(&var1);
1361 return NULL;
1362 }
1363 ++lp->ll_tv->vval.v_dict->dv_refcount;
1364 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001365 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001366
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001367 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001368
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001369 // When assigning to a scope dictionary check that a function and
1370 // variable name is valid (only variable name unless it is l: or
1371 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001372 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001373 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001374 int prevval;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001375
1376 if (len != -1)
1377 {
1378 prevval = key[len];
1379 key[len] = NUL;
1380 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001381 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001382 prevval = 0; // avoid compiler warning
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001383 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
zeertzjq91c75d12022-11-05 20:21:58 +00001384 && (rettv->v_type == VAR_FUNC
1385 || rettv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001386 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001387 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001388 if (len != -1)
1389 key[len] = prevval;
1390 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001391 {
1392 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001393 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001394 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001395 }
1396
Bram Moolenaar10c65862020-10-08 21:16:42 +02001397 if (lp->ll_valtype != NULL)
1398 // use the type of the member
1399 lp->ll_valtype = lp->ll_valtype->tt_member;
1400
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001401 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001402 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001403 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001404 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001405 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001406 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001407 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001408 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001409 return NULL;
1410 }
1411
Bram Moolenaar31b81602019-02-10 22:14:27 +01001412 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001413 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001414 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001415 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001416 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001417 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001418 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001419 }
1420 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001421 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001422 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001423 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001424 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001425 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001426 p = NULL;
1427 break;
1428 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001429 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001430 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001431 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1432 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001433 {
1434 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001435 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001436 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001437
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001438 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001439 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001440 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001441 else if (v_type == VAR_BLOB)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001442 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001443 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1444
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001445 /*
1446 * Get the number and item for the only or first index of the List.
1447 */
1448 if (empty1)
1449 lp->ll_n1 = 0;
1450 else
1451 // is number or string
1452 lp->ll_n1 = (long)tv_get_number(&var1);
1453 clear_tv(&var1);
1454
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001455 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001456 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001457 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001458 return NULL;
1459 }
1460 if (lp->ll_range && !lp->ll_empty2)
1461 {
1462 lp->ll_n2 = (long)tv_get_number(&var2);
1463 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001464 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1465 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001466 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001467 }
1468 lp->ll_blob = lp->ll_tv->vval.v_blob;
1469 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001470 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001471 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001472 else if (v_type == VAR_LIST)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001473 {
1474 /*
1475 * Get the number and item for the only or first index of the List.
1476 */
1477 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001478 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001479 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001480 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001481 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001482 clear_tv(&var1);
1483
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001484 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001485 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001486 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1487 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001488 if (lp->ll_li == NULL)
1489 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001490 clear_tv(&var2);
1491 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001492 }
1493
Bram Moolenaar10c65862020-10-08 21:16:42 +02001494 if (lp->ll_valtype != NULL)
1495 // use the type of the member
1496 lp->ll_valtype = lp->ll_valtype->tt_member;
1497
Bram Moolenaar8c711452005-01-14 21:53:12 +00001498 /*
1499 * May need to find the item or absolute index for the second
1500 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001501 * When no index given: "lp->ll_empty2" is TRUE.
1502 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001503 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001504 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001505 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001506 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001507 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001508 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001509 if (check_range_index_two(lp->ll_list,
1510 &lp->ll_n1, lp->ll_li,
1511 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001512 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001513 }
1514
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001515 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001516 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001517 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001518 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001519 class_T *cl = (v_type == VAR_OBJECT
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001520 && lp->ll_tv->vval.v_object != NULL)
1521 ? lp->ll_tv->vval.v_object->obj_class
1522 : lp->ll_tv->vval.v_class;
1523 // TODO: what if class is NULL?
1524 if (cl != NULL)
1525 {
1526 lp->ll_valtype = NULL;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001527
1528 if (flags & GLV_PREFER_FUNC)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001529 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001530 // First look for a function with this name.
1531 // round 1: class functions (skipped for an object)
1532 // round 2: object methods
1533 for (int round = v_type == VAR_OBJECT ? 2 : 1;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001534 round <= 2; ++round)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001535 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001536 int m_idx;
1537 ufunc_T *fp;
1538
1539 fp = method_lookup(cl,
1540 round == 1 ? VAR_CLASS : VAR_OBJECT,
1541 key, p - key, &m_idx);
1542 if (fp != NULL)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001543 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001544 lp->ll_ufunc = fp;
1545 lp->ll_valtype = fp->uf_func_type;
1546 break;
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001547 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001548 }
1549 }
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001550
1551 if (lp->ll_valtype == NULL)
1552 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001553 int m_idx;
1554 ocmember_T *om;
1555
1556 om = member_lookup(cl, v_type, key, p - key, &m_idx);
1557 if (om != NULL)
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001558 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001559 switch (om->ocm_access)
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001560 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001561 case VIM_ACCESS_PRIVATE:
RestorerZ7fe8f432023-09-24 23:21:24 +02001562 semsg(_(e_cannot_access_private_variable_str),
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001563 om->ocm_name);
1564 return NULL;
1565 case VIM_ACCESS_READ:
Ernie Rael98e68c02023-09-20 20:13:06 +02001566 // If [idx] or .key following, read only OK.
1567 if (*p == '[' || *p == '.')
1568 break;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001569 if ((flags & GLV_READ_ONLY) == 0)
1570 {
RestorerZ7fe8f432023-09-24 23:21:24 +02001571 semsg(_(e_variable_is_not_writable_str),
Ernie Rael696270b2023-09-21 16:42:28 +02001572 om->ocm_name, cl->class_name);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001573 return NULL;
1574 }
1575 break;
1576 case VIM_ACCESS_ALL:
1577 break;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001578 }
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001579
1580 lp->ll_valtype = om->ocm_type;
1581
1582 if (v_type == VAR_OBJECT)
1583 lp->ll_tv = ((typval_T *)(
1584 lp->ll_tv->vval.v_object + 1)) + m_idx;
1585 else
1586 lp->ll_tv = &cl->class_members_tv[m_idx];
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001587 }
1588 }
1589
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001590 if (lp->ll_valtype == NULL)
1591 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001592 member_not_found_msg(cl, v_type, key, p - key);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001593 return NULL;
1594 }
1595 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001596 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001597 }
1598
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001599 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001601 return p;
1602}
1603
1604/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001605 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001606 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001607 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001608clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001609{
1610 vim_free(lp->ll_exp_name);
1611 vim_free(lp->ll_newkey);
1612}
1613
1614/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001615 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001616 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001617 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1618 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001619 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001620 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001621set_var_lval(
1622 lval_T *lp,
1623 char_u *endp,
1624 typval_T *rettv,
1625 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001626 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1627 char_u *op,
1628 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001629{
1630 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001631 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001632
1633 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001634 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001635 cc = *endp;
1636 *endp = NUL;
h-east2bd6a092023-05-19 19:01:17 +01001637 if (in_vim9script() && check_reserved_name(lp->ll_name, FALSE) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001638 return;
1639
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001640 if (lp->ll_blob != NULL)
1641 {
1642 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001643
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001644 if (op != NULL && *op != '=')
1645 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001646 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001647 return;
1648 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001649 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001650 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001651
1652 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1653 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001654 if (lp->ll_empty2)
1655 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1656
Bram Moolenaar68452172021-04-12 21:21:02 +02001657 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1658 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001659 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001660 }
1661 else
1662 {
1663 val = (int)tv_get_number_chk(rettv, &error);
1664 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001665 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001666 }
1667 }
1668 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001669 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001670 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001671
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001672 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1673 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001674 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001675 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001676 *endp = cc;
1677 return;
1678 }
1679
Bram Moolenaarff697e62019-02-12 22:28:33 +01001680 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001681 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001682 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001683 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001684 {
1685 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001686 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1687 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001688 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001689 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001690 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001691 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001692 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001693 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001694 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001695 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001696 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1697 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001698 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001699 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001700 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001701 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001702 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001703 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001704 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001705 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001706 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001707 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001708 else if (lp->ll_range)
1709 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001710 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1711 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001712 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001713 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001714 return;
1715 }
1716
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001717 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1718 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001719 }
1720 else
1721 {
1722 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001723 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001724 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001725 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1726 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001727 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001728 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001729 return;
1730 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001731
1732 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001733 && check_typval_arg_type(lp->ll_valtype, rettv,
1734 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001735 return;
1736
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001737 if (lp->ll_newkey != NULL)
1738 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001739 if (op != NULL && *op != '=')
1740 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001741 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001742 return;
1743 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001744 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1745 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001746 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001747
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001748 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001749 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001750 if (di == NULL)
1751 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001752 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1753 {
1754 vim_free(di);
1755 return;
1756 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001757 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001758 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001759 else if (op != NULL && *op != '=')
1760 {
1761 tv_op(lp->ll_tv, rettv, op);
1762 return;
1763 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001764 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001765 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001766
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001767 /*
1768 * Assign the value to the variable or list item.
1769 */
1770 if (copy)
1771 copy_tv(rettv, lp->ll_tv);
1772 else
1773 {
1774 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001775 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001776 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001777 }
1778 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001779}
1780
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001781/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001782 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1783 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001784 * Returns OK or FAIL.
1785 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001786 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001787tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001788{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001789 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001790 char_u numbuf[NUMBUFLEN];
1791 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001792 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001793
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001794 // Can't do anything with a Funcref or Dict on the right.
1795 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001796 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001797 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1798 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001799 {
1800 switch (tv1->v_type)
1801 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001802 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001803 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001804 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001805 case VAR_DICT:
1806 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001807 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001808 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001809 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001810 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001811 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001812 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001813 case VAR_CLASS:
1814 case VAR_OBJECT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001815 break;
1816
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001817 case VAR_BLOB:
1818 if (*op != '+' || tv2->v_type != VAR_BLOB)
1819 break;
1820 // BLOB += BLOB
1821 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1822 {
1823 blob_T *b1 = tv1->vval.v_blob;
1824 blob_T *b2 = tv2->vval.v_blob;
1825 int i, len = blob_len(b2);
1826 for (i = 0; i < len; i++)
1827 ga_append(&b1->bv_ga, blob_get(b2, i));
1828 }
1829 return OK;
1830
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001831 case VAR_LIST:
1832 if (*op != '+' || tv2->v_type != VAR_LIST)
1833 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001834 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001835 if (tv2->vval.v_list != NULL)
1836 {
1837 if (tv1->vval.v_list == NULL)
1838 {
1839 tv1->vval.v_list = tv2->vval.v_list;
1840 ++tv1->vval.v_list->lv_refcount;
1841 }
1842 else
1843 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1844 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001845 return OK;
1846
1847 case VAR_NUMBER:
1848 case VAR_STRING:
1849 if (tv2->v_type == VAR_LIST)
1850 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001851 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001852 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001853 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001854 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001855 if (tv2->v_type == VAR_FLOAT)
1856 {
1857 float_T f = n;
1858
Bram Moolenaarff697e62019-02-12 22:28:33 +01001859 if (*op == '%')
1860 break;
1861 switch (*op)
1862 {
1863 case '+': f += tv2->vval.v_float; break;
1864 case '-': f -= tv2->vval.v_float; break;
1865 case '*': f *= tv2->vval.v_float; break;
1866 case '/': f /= tv2->vval.v_float; break;
1867 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001868 clear_tv(tv1);
1869 tv1->v_type = VAR_FLOAT;
1870 tv1->vval.v_float = f;
1871 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001872 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001873 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001874 switch (*op)
1875 {
1876 case '+': n += tv_get_number(tv2); break;
1877 case '-': n -= tv_get_number(tv2); break;
1878 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001879 case '/': n = num_divide(n, tv_get_number(tv2),
1880 &failed); break;
1881 case '%': n = num_modulus(n, tv_get_number(tv2),
1882 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001883 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001884 clear_tv(tv1);
1885 tv1->v_type = VAR_NUMBER;
1886 tv1->vval.v_number = n;
1887 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001888 }
1889 else
1890 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001891 if (tv2->v_type == VAR_FLOAT)
1892 break;
1893
Bram Moolenaarff697e62019-02-12 22:28:33 +01001894 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001895 s = tv_get_string(tv1);
1896 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001897 clear_tv(tv1);
1898 tv1->v_type = VAR_STRING;
1899 tv1->vval.v_string = s;
1900 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001901 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001902
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001903 case VAR_FLOAT:
1904 {
1905 float_T f;
1906
Bram Moolenaarff697e62019-02-12 22:28:33 +01001907 if (*op == '%' || *op == '.'
1908 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001909 && tv2->v_type != VAR_NUMBER
1910 && tv2->v_type != VAR_STRING))
1911 break;
1912 if (tv2->v_type == VAR_FLOAT)
1913 f = tv2->vval.v_float;
1914 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001915 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001916 switch (*op)
1917 {
1918 case '+': tv1->vval.v_float += f; break;
1919 case '-': tv1->vval.v_float -= f; break;
1920 case '*': tv1->vval.v_float *= f; break;
1921 case '/': tv1->vval.v_float /= f; break;
1922 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001923 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001924 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001925 }
1926 }
1927
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001928 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001929 return FAIL;
1930}
1931
1932/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001933 * Evaluate the expression used in a ":for var in expr" command.
1934 * "arg" points to "var".
1935 * Set "*errp" to TRUE for an error, FALSE otherwise;
1936 * Return a pointer that holds the info. Null when there is an error.
1937 */
1938 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001939eval_for_line(
1940 char_u *arg,
1941 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001942 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001943 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001944{
Bram Moolenaar33570922005-01-25 22:26:29 +00001945 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001946 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001947 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001948 typval_T tv;
1949 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001950 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001952 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001953
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001954 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001955 if (fi == NULL)
1956 return NULL;
1957
Bram Moolenaar404557e2021-07-05 21:41:48 +02001958 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1959 &fi->fi_semicolon, FALSE);
1960 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001961 return fi;
1962
Bram Moolenaar404557e2021-07-05 21:41:48 +02001963 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001964 if (expr[0] != 'i' || expr[1] != 'n'
1965 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001967 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1968 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1969 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001970 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001971 return fi;
1972 }
1973
1974 if (skip)
1975 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001976 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1977 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001978 {
1979 *errp = FALSE;
1980 if (!skip)
1981 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001982 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001983 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001984 l = tv.vval.v_list;
1985 if (l == NULL)
1986 {
1987 // a null list is like an empty list: do nothing
1988 clear_tv(&tv);
1989 }
1990 else
1991 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001992 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001993 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001994
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001995 // No need to increment the refcount, it's already set for
1996 // the list being used in "tv".
1997 fi->fi_list = l;
1998 list_add_watch(l, &fi->fi_lw);
1999 fi->fi_lw.lw_item = l->lv_first;
2000 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002001 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002002 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002003 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002004 fi->fi_bi = 0;
2005 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002006 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002007 typval_T btv;
2008
2009 // Make a copy, so that the iteration still works when the
2010 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002011 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002012 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002013 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002014 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002015 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002016 else if (tv.v_type == VAR_STRING)
2017 {
2018 fi->fi_byte_idx = 0;
2019 fi->fi_string = tv.vval.v_string;
2020 tv.vval.v_string = NULL;
2021 if (fi->fi_string == NULL)
2022 fi->fi_string = vim_strsave((char_u *)"");
2023 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002024 else
2025 {
Sean Dewar80d73952021-08-04 19:25:54 +02002026 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002027 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002028 }
2029 }
2030 }
2031 if (skip)
2032 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002033 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002034
2035 return fi;
2036}
2037
2038/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002039 * Used when looping over a :for line, skip the "in expr" part.
2040 */
2041 void
2042skip_for_lines(void *fi_void, evalarg_T *evalarg)
2043{
2044 forinfo_T *fi = (forinfo_T *)fi_void;
2045 int i;
2046
2047 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002048 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002049}
2050
2051/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002052 * Use the first item in a ":for" list. Advance to the next.
2053 * Assign the values to the variable (list). "arg" points to the first one.
2054 * Return TRUE when a valid item was found, FALSE when at end of list or
2055 * something wrong.
2056 */
2057 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002058next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002059{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002060 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002061 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002062 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002063 ? (ASSIGN_FINAL
2064 // first round: error if variable exists
2065 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002066 | ASSIGN_NO_MEMBER_TYPE
2067 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002068 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002069 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002070 int skip_assign = in_vim9script() && arg[0] == '_'
2071 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002072
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002073 if (fi->fi_blob != NULL)
2074 {
2075 typval_T tv;
2076
2077 if (fi->fi_bi >= blob_len(fi->fi_blob))
2078 return FALSE;
2079 tv.v_type = VAR_NUMBER;
2080 tv.v_lock = VAR_FIXED;
2081 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2082 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002083 if (skip_assign)
2084 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002085 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002086 fi->fi_varcount, flag, NULL) == OK;
2087 }
2088
2089 if (fi->fi_string != NULL)
2090 {
2091 typval_T tv;
2092 int len;
2093
2094 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2095 if (len == 0)
2096 return FALSE;
2097 tv.v_type = VAR_STRING;
2098 tv.v_lock = VAR_FIXED;
2099 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2100 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002101 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002102 if (skip_assign)
2103 result = TRUE;
2104 else
2105 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002106 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002107 vim_free(tv.vval.v_string);
2108 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002109 }
2110
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002111 item = fi->fi_lw.lw_item;
2112 if (item == NULL)
2113 result = FALSE;
2114 else
2115 {
2116 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002117 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002118 if (skip_assign)
2119 result = TRUE;
2120 else
2121 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002122 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002123 }
2124 return result;
2125}
2126
2127/*
2128 * Free the structure used to store info used by ":for".
2129 */
2130 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002131free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002132{
Bram Moolenaar33570922005-01-25 22:26:29 +00002133 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002134
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002135 if (fi == NULL)
2136 return;
2137 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002138 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002139 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002140 list_unref(fi->fi_list);
2141 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002142 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002143 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002144 else
2145 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002146 vim_free(fi);
2147}
2148
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002150set_context_for_expression(
2151 expand_T *xp,
2152 char_u *arg,
2153 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002155 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002157 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002159 if (cmdidx == CMD_let || cmdidx == CMD_var
2160 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002161 {
2162 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002163 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002164 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002165 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002166 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002167 {
2168 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002169 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002170 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002171 break;
2172 }
2173 return;
2174 }
2175 }
2176 else
2177 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2178 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 while ((xp->xp_pattern = vim_strpbrk(arg,
2180 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2181 {
2182 c = *xp->xp_pattern;
2183 if (c == '&')
2184 {
2185 c = xp->xp_pattern[1];
2186 if (c == '&')
2187 {
2188 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002189 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 }
2191 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002192 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002194 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2195 xp->xp_pattern += 2;
2196
2197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 }
2199 else if (c == '$')
2200 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002201 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 xp->xp_context = EXPAND_ENV_VARS;
2203 }
2204 else if (c == '=')
2205 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002206 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 xp->xp_context = EXPAND_EXPRESSION;
2208 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002209 else if (c == '#'
2210 && xp->xp_context == EXPAND_EXPRESSION)
2211 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002212 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002213 break;
2214 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002215 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 && xp->xp_context == EXPAND_FUNCTIONS
2217 && vim_strchr(xp->xp_pattern, '(') == NULL)
2218 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002219 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 break;
2221 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002222 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002224 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 {
2226 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2227 if (c == '\\' && xp->xp_pattern[1] != NUL)
2228 ++xp->xp_pattern;
2229 xp->xp_context = EXPAND_NOTHING;
2230 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002231 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002233 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2235 /* skip */ ;
2236 xp->xp_context = EXPAND_NOTHING;
2237 }
2238 else if (c == '|')
2239 {
2240 if (xp->xp_pattern[1] == '|')
2241 {
2242 ++xp->xp_pattern;
2243 xp->xp_context = EXPAND_EXPRESSION;
2244 }
2245 else
2246 xp->xp_context = EXPAND_COMMANDS;
2247 }
2248 else
2249 xp->xp_context = EXPAND_EXPRESSION;
2250 }
2251 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002252 // Doesn't look like something valid, expand as an expression
2253 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002254 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 arg = xp->xp_pattern;
2256 if (*arg != NUL)
2257 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2258 /* skip */ ;
2259 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002260
2261 // ":exe one two" completes "two"
2262 if ((cmdidx == CMD_execute
2263 || cmdidx == CMD_echo
2264 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002265 || cmdidx == CMD_echomsg
2266 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002267 && xp->xp_context == EXPAND_EXPRESSION)
2268 {
2269 for (;;)
2270 {
2271 char_u *n = skiptowhite(arg);
2272
2273 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2274 break;
2275 arg = skipwhite(n);
2276 }
2277 }
2278
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 xp->xp_pattern = arg;
2280}
2281
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002283 * Return TRUE if "pat" matches "text".
2284 * Does not use 'cpo' and always uses 'magic'.
2285 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002286 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002287pattern_match(char_u *pat, char_u *text, int ic)
2288{
2289 int matches = FALSE;
2290 char_u *save_cpo;
2291 regmatch_T regmatch;
2292
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002293 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002294 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002295 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002296 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2297 if (regmatch.regprog != NULL)
2298 {
2299 regmatch.rm_ic = ic;
2300 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2301 vim_regfree(regmatch.regprog);
2302 }
2303 p_cpo = save_cpo;
2304 return matches;
2305}
2306
2307/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002308 * Handle a name followed by "(". Both for just "name(arg)" and for
2309 * "expr->name(arg)".
2310 * Returns OK or FAIL.
2311 */
2312 static int
2313eval_func(
2314 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002315 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002316 char_u *name,
2317 int name_len,
2318 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002319 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002320 typval_T *basetv) // "expr" for "expr->name(arg)"
2321{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002322 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002323 char_u *s = name;
2324 int len = name_len;
2325 partial_T *partial;
2326 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002327 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002328 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002329
2330 if (!evaluate)
2331 check_vars(s, len);
2332
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002333 // If "s" is the name of a variable of type VAR_FUNC
2334 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002335 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002336 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002337
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002338 // Need to make a copy, in case evaluating the arguments makes
2339 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002340 s = vim_strsave(s);
zeertzjqb7f6f932023-04-13 22:12:50 +01002341 if (s == NULL || (evaluate && *s == NUL))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002342 ret = FAIL;
2343 else
2344 {
2345 funcexe_T funcexe;
2346
2347 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002348 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002349 funcexe.fe_firstline = curwin->w_cursor.lnum;
2350 funcexe.fe_lastline = curwin->w_cursor.lnum;
2351 funcexe.fe_evaluate = evaluate;
2352 funcexe.fe_partial = partial;
2353 funcexe.fe_basetv = basetv;
2354 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002355 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002356 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002357 }
2358 vim_free(s);
2359
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002360 // If evaluate is FALSE rettv->v_type was not set in
2361 // get_func_tv, but it's needed in handle_subscript() to parse
2362 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002363 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2364 {
2365 rettv->vval.v_string = NULL;
2366 rettv->v_type = VAR_FUNC;
2367 }
2368
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002369 // Stop the expression evaluation when immediately
2370 // aborting on error, or when an interrupt occurred or
2371 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002372 if (evaluate && aborting())
2373 {
2374 if (ret == OK)
2375 clear_tv(rettv);
2376 ret = FAIL;
2377 }
2378 return ret;
2379}
2380
2381/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002382 * After a NL, skip over empty lines and comment-only lines.
2383 */
2384 static char_u *
2385newline_skip_comments(char_u *arg)
2386{
2387 char_u *p = arg + 1;
2388
2389 for (;;)
2390 {
2391 p = skipwhite(p);
2392
2393 if (*p == NUL)
2394 break;
2395 if (vim9_comment_start(p))
2396 {
2397 char_u *nl = vim_strchr(p, NL);
2398
2399 if (nl == NULL)
2400 break;
2401 p = nl;
2402 }
2403 if (*p != NL)
2404 break;
2405 ++p; // skip another NL
2406 }
2407 return p;
2408}
2409
2410/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002411 * Get the next line source line without advancing. But do skip over comment
2412 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002413 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002414 */
2415 static char_u *
2416getline_peek_skip_comments(evalarg_T *evalarg)
2417{
2418 for (;;)
2419 {
2420 char_u *next = getline_peek(evalarg->eval_getline,
2421 evalarg->eval_cookie);
2422 char_u *p;
2423
2424 if (next == NULL)
2425 break;
2426 p = skipwhite(next);
2427 if (*p != NUL && !vim9_comment_start(p))
2428 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002429 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002430 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002431 }
2432 return NULL;
2433}
2434
2435/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002436 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2437 * comment) and there is a next line, return the next line (skipping blanks)
2438 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002439 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2440 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002441 * "arg" must point somewhere inside a line, not at the start.
2442 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002443 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002444eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2445{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002446 char_u *p = skipwhite(arg);
2447
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002448 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002449 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002450 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002451 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2452 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002453 && (*p == NUL || *p == NL
2454 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002455 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002456 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002457
Bram Moolenaare442d592022-05-05 12:20:28 +01002458 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002459 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002460 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002461 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002462 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002463 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002464
Bram Moolenaar9d489562020-07-30 20:08:50 +02002465 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002466 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002467 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002468 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002469 }
2470 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002471 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002472}
2473
2474/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002475 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002476 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002477 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002478 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002479eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002480{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002481 garray_T *gap = &evalarg->eval_ga;
2482 char_u *line;
2483
Bram Moolenaar39be4982022-05-06 21:51:50 +01002484 if (arg != NULL)
2485 {
2486 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002487 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002488 // Truncate before a trailing comment, so that concatenating the lines
2489 // won't turn the rest into a comment.
2490 if (*skipwhite(arg) == '#')
2491 *arg = NUL;
2492 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002493
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002494 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002495 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2496 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002497 else
2498 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002499 if (line == NULL)
2500 return NULL;
2501
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002502 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002503 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2504 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002505 char_u *p = skipwhite(line);
2506
2507 // Going to concatenate the lines after parsing. For an empty or
2508 // comment line use an empty string.
2509 if (*p == NUL || vim9_comment_start(p))
2510 {
2511 vim_free(line);
2512 line = vim_strsave((char_u *)"");
2513 }
2514
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002515 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2516 ++gap->ga_len;
2517 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002518 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002519 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002520 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002521 evalarg->eval_tofree = line;
2522 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002523
2524 // Advanced to the next line, "arg" no longer points into the previous
2525 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002526 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002527 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002528}
2529
Bram Moolenaare6b53242020-07-01 17:28:33 +02002530/*
2531 * Call eval_next_non_blank() and get the next line if needed.
2532 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002533 char_u *
2534skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2535{
2536 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002537 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002538
Bram Moolenaar962d7212020-07-04 14:15:00 +02002539 if (evalarg == NULL)
2540 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002541 eval_next_non_blank(p, evalarg, &getnext);
2542 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002543 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002544 return p;
2545}
2546
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002547/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002548 * The "eval" functions have an "evalarg" argument: When NULL or
2549 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
2550 * parsed but not executed. The functions may return OK, but the rettv will be
2551 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 */
2553
2554/*
2555 * Handle zero level expression.
2556 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002557 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002558 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002559 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 * Return OK or FAIL.
2561 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002562 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002563eval0(
2564 char_u *arg,
2565 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002566 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002567 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002569 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2570}
2571
2572/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002573 * If "arg" is a simple function call without arguments then call it and return
2574 * the result. Otherwise return NOTDONE.
2575 */
2576 int
2577may_call_simple_func(
2578 char_u *arg,
2579 typval_T *rettv)
2580{
2581 char_u *parens = (char_u *)strstr((char *)arg, "()");
2582 int r = NOTDONE;
2583
2584 // If the expression is "FuncName()" then we can skip a lot of overhead.
2585 if (parens != NULL && *skipwhite(parens + 2) == NUL)
2586 {
2587 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
2588
2589 if (to_name_end(p, TRUE) == parens)
2590 r = call_simple_func(arg, (int)(parens - arg), rettv);
2591 }
2592 return r;
2593}
2594
2595/*
2596 * Handle zero level expression with optimization for a simple function call.
2597 * Same arguments and return value as eval0().
2598 */
2599 int
2600eval0_simple_funccal(
2601 char_u *arg,
2602 typval_T *rettv,
2603 exarg_T *eap,
2604 evalarg_T *evalarg)
2605{
2606 int r = may_call_simple_func(arg, rettv);
2607
2608 if (r == NOTDONE)
2609 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
2610 return r;
2611}
2612
2613/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002614 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2615 * expression and don't check what comes after the expression.
2616 */
2617 int
2618eval0_retarg(
2619 char_u *arg,
2620 typval_T *rettv,
2621 exarg_T *eap,
2622 evalarg_T *evalarg,
2623 char_u **retarg)
2624{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 int ret;
2626 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002627 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002628 int did_emsg_before = did_emsg;
2629 int called_emsg_before = called_emsg;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002630 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002631 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632
2633 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002634 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002635
Bram Moolenaar79481362022-06-27 20:15:10 +01002636 if (ret != FAIL)
2637 {
2638 expr_end = p;
2639 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002640
Bram Moolenaar79481362022-06-27 20:15:10 +01002641 // In Vim9 script a command block is not split at NL characters for
2642 // commands using an expression argument. Skip over a '#' comment to
2643 // check for a following NL. Require white space before the '#'.
2644 if (in_vim9script() && p > expr_end && retarg == NULL)
2645 while (*p == '#')
2646 {
2647 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002648
Bram Moolenaar79481362022-06-27 20:15:10 +01002649 if (nl == NULL)
2650 break;
2651 p = skipwhite(nl + 1);
2652 if (eap != NULL && *p != NUL)
2653 eap->nextcmd = p;
2654 check_for_end = FALSE;
2655 }
2656
2657 if (check_for_end)
2658 end_error = !ends_excmd2(arg, p);
2659 }
2660
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002661 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 {
2663 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002664 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 /*
2666 * Report the invalid expression unless the expression evaluation has
2667 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002668 * exception, or we already gave a more specific error.
2669 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002671 if (!aborting()
2672 && did_emsg == did_emsg_before
2673 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002674 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002675 {
2676 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002677 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002678 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002679 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002680 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002681
zeertzjqf2588b62023-05-05 17:22:35 +01002682 if (eap != NULL && p != NULL)
2683 {
2684 // Some of the expression may not have been consumed.
2685 // Only execute a next command if it cannot be a "||" operator.
2686 // The next command may be "catch".
2687 char_u *nextcmd = check_nextcmd(p);
2688 if (nextcmd != NULL && *nextcmd != '|')
2689 eap->nextcmd = nextcmd;
2690 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002691 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002693
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002694 if (retarg != NULL)
2695 *retarg = p;
2696 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002697 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002698
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 return ret;
2700}
2701
2702/*
2703 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002704 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002705 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 *
2707 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002708 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002710 * Note: "rettv.v_lock" is not set.
2711 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 * Return OK or FAIL.
2713 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002714 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002715eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002717 char_u *p;
2718 int getnext;
2719
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002720 CLEAR_POINTER(rettv);
2721
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 /*
2723 * Get the first variable.
2724 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002725 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 return FAIL;
2727
Bram Moolenaar793648f2020-06-26 21:28:25 +02002728 p = eval_next_non_blank(*arg, evalarg, &getnext);
2729 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002731 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002732 int result;
2733 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002734 evalarg_T *evalarg_used = evalarg;
2735 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002736 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002737 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002738 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002739
2740 if (evalarg == NULL)
2741 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002742 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002743 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002744 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002745 orig_flags = evalarg_used->eval_flags;
2746 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002747
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002748 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002749 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002750 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002751 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002752 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002753 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002754 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002755 clear_tv(rettv);
2756 return FAIL;
2757 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002758 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002759 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002760
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002762 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002764 int error = FALSE;
2765
Bram Moolenaar13106602020-10-04 16:06:05 +02002766 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002767 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002768 else if (vim9script)
2769 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002770 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002772 if (error || !op_falsy || !result)
2773 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002774 if (error)
2775 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 }
2777
2778 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002779 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002781 if (op_falsy)
2782 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002783 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002784 {
mityu4ac198c2021-05-28 17:52:40 +02002785 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002786 clear_tv(rettv);
2787 return FAIL;
2788 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002789 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002790 evalarg_used->eval_flags = (op_falsy ? !result : result)
zeertzjqb7f6f932023-04-13 22:12:50 +01002791 ? orig_flags : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002792 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002793 {
2794 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002796 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002797 if (!op_falsy || !result)
2798 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002800 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002802 /*
2803 * Check for the ":".
2804 */
2805 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2806 if (*p != ':')
2807 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002808 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002809 if (evaluate && result)
2810 clear_tv(rettv);
2811 evalarg_used->eval_flags = orig_flags;
2812 return FAIL;
2813 }
2814 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002815 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002816 else
2817 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002818 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002819 {
2820 error_white_both(p, 1);
2821 clear_tv(rettv);
2822 evalarg_used->eval_flags = orig_flags;
2823 return FAIL;
2824 }
2825 *arg = p;
2826 }
2827
2828 /*
2829 * Get the third variable. Recursive!
2830 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002831 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002832 {
mityu4ac198c2021-05-28 17:52:40 +02002833 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002834 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002835 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002836 return FAIL;
2837 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002838 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2839 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01002840 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002841 if (eval1(arg, &var2, evalarg_used) == FAIL)
2842 {
2843 if (evaluate && result)
2844 clear_tv(rettv);
2845 evalarg_used->eval_flags = orig_flags;
2846 return FAIL;
2847 }
2848 if (evaluate && !result)
2849 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002851
2852 if (evalarg == NULL)
2853 clear_evalarg(&local_evalarg, NULL);
2854 else
2855 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 }
2857
2858 return OK;
2859}
2860
2861/*
2862 * Handle first level expression:
2863 * expr2 || expr2 || expr2 logical OR
2864 *
2865 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002866 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 *
2868 * Return OK or FAIL.
2869 */
2870 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002871eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002873 char_u *p;
2874 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875
2876 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002877 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002879 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 return FAIL;
2881
2882 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002883 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002885 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002886 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002888 evalarg_T *evalarg_used = evalarg;
2889 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002890 int evaluate;
2891 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002892 long result = FALSE;
2893 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002894 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002895 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002896
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002897 if (evalarg == NULL)
2898 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002899 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002900 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002901 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002902 orig_flags = evalarg_used->eval_flags;
2903 evaluate = orig_flags & EVAL_EVALUATE;
2904 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002905 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002906 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002907 result = tv_get_bool_chk(rettv, &error);
2908 else if (tv_get_number_chk(rettv, &error) != 0)
2909 result = TRUE;
2910 clear_tv(rettv);
2911 if (error)
2912 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 }
2914
2915 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002916 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002918 while (p[0] == '|' && p[1] == '|')
2919 {
2920 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002921 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002922 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002923 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002924 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002925 {
2926 error_white_both(p, 2);
2927 clear_tv(rettv);
2928 return FAIL;
2929 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002930 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002931 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002932
2933 /*
2934 * Get the second variable.
2935 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002936 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002937 {
mityu4ac198c2021-05-28 17:52:40 +02002938 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002939 clear_tv(rettv);
2940 return FAIL;
2941 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002942 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2943 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01002944 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002945 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002946 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002947
2948 /*
2949 * Compute the result.
2950 */
2951 if (evaluate && !result)
2952 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002953 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002954 result = tv_get_bool_chk(&var2, &error);
2955 else if (tv_get_number_chk(&var2, &error) != 0)
2956 result = TRUE;
2957 clear_tv(&var2);
2958 if (error)
2959 return FAIL;
2960 }
2961 if (evaluate)
2962 {
2963 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002964 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002965 rettv->v_type = VAR_BOOL;
2966 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002967 }
2968 else
2969 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002970 rettv->v_type = VAR_NUMBER;
2971 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002972 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002973 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002974
2975 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002977
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002978 if (evalarg == NULL)
2979 clear_evalarg(&local_evalarg, NULL);
2980 else
2981 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 }
2983
2984 return OK;
2985}
2986
2987/*
2988 * Handle second level expression:
2989 * expr3 && expr3 && expr3 logical AND
2990 *
2991 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002992 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 *
2994 * Return OK or FAIL.
2995 */
2996 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002997eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002999 char_u *p;
3000 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001
3002 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003003 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003005 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 return FAIL;
3007
3008 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003009 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003011 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003012 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003014 evalarg_T *evalarg_used = evalarg;
3015 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003016 int orig_flags;
3017 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003018 long result = TRUE;
3019 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003020 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003021 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003022
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003023 if (evalarg == NULL)
3024 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003025 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003026 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003027 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003028 orig_flags = evalarg_used->eval_flags;
3029 evaluate = orig_flags & EVAL_EVALUATE;
3030 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003031 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003032 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003033 result = tv_get_bool_chk(rettv, &error);
3034 else if (tv_get_number_chk(rettv, &error) == 0)
3035 result = FALSE;
3036 clear_tv(rettv);
3037 if (error)
3038 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 }
3040
3041 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003042 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003044 while (p[0] == '&' && p[1] == '&')
3045 {
3046 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003047 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003048 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003049 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003050 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003051 {
3052 error_white_both(p, 2);
3053 clear_tv(rettv);
3054 return FAIL;
3055 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003056 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003057 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003058
3059 /*
3060 * Get the second variable.
3061 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003062 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003063 {
mityu4ac198c2021-05-28 17:52:40 +02003064 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003065 clear_tv(rettv);
3066 return FAIL;
3067 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003068 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3069 evalarg_used->eval_flags = result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003070 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003071 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003072 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003073 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003074
3075 /*
3076 * Compute the result.
3077 */
3078 if (evaluate && result)
3079 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003080 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003081 result = tv_get_bool_chk(&var2, &error);
3082 else if (tv_get_number_chk(&var2, &error) == 0)
3083 result = FALSE;
3084 clear_tv(&var2);
3085 if (error)
3086 return FAIL;
3087 }
3088 if (evaluate)
3089 {
3090 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003091 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003092 rettv->v_type = VAR_BOOL;
3093 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003094 }
3095 else
3096 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003097 rettv->v_type = VAR_NUMBER;
3098 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003099 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003100 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003101
3102 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003104
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003105 if (evalarg == NULL)
3106 clear_evalarg(&local_evalarg, NULL);
3107 else
3108 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 }
3110
3111 return OK;
3112}
3113
3114/*
3115 * Handle third level expression:
3116 * var1 == var2
3117 * var1 =~ var2
3118 * var1 != var2
3119 * var1 !~ var2
3120 * var1 > var2
3121 * var1 >= var2
3122 * var1 < var2
3123 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003124 * var1 is var2
3125 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 *
3127 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003128 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 *
3130 * Return OK or FAIL.
3131 */
3132 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003133eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003136 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003137 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003139 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140
3141 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003142 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003144 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 return FAIL;
3146
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003147 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003148
Bram Moolenaar696ba232020-07-29 21:20:41 +02003149 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150
3151 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003152 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003154 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003156 typval_T var2;
3157 int ic;
3158 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003159 int evaluate = evalarg == NULL
3160 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003161 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003162
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003163 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003164 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003165 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003166 p = *arg;
3167 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003168 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3169 {
mityu4ac198c2021-05-28 17:52:40 +02003170 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003171 clear_tv(rettv);
3172 return FAIL;
3173 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003174
Bram Moolenaar696ba232020-07-29 21:20:41 +02003175 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3176 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003177 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003178 clear_tv(rettv);
3179 return FAIL;
3180 }
3181
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003182 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 if (p[len] == '?')
3184 {
3185 ic = TRUE;
3186 ++len;
3187 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003188 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 else if (p[len] == '#')
3190 {
3191 ic = FALSE;
3192 ++len;
3193 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003194 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003196 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197
3198 /*
3199 * Get the second variable.
3200 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003201 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3202 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003203 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003204 clear_tv(rettv);
3205 return FAIL;
3206 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003207 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003208 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003210 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 return FAIL;
3212 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003213 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003214 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003215 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003216
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003217 // use the line of the comparison for messages
3218 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003219 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003220 {
3221 ret = FAIL;
3222 clear_tv(rettv);
3223 }
3224 else
3225 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003226 clear_tv(&var2);
3227 return ret;
3228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 }
3230
3231 return OK;
3232}
3233
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003234/*
3235 * Make a copy of blob "tv1" and append blob "tv2".
3236 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237 void
3238eval_addblob(typval_T *tv1, typval_T *tv2)
3239{
3240 blob_T *b1 = tv1->vval.v_blob;
3241 blob_T *b2 = tv2->vval.v_blob;
3242 blob_T *b = blob_alloc();
3243 int i;
3244
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003245 if (b == NULL)
3246 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003248 for (i = 0; i < blob_len(b1); i++)
3249 ga_append(&b->bv_ga, blob_get(b1, i));
3250 for (i = 0; i < blob_len(b2); i++)
3251 ga_append(&b->bv_ga, blob_get(b2, i));
3252
3253 clear_tv(tv1);
3254 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255}
3256
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003257/*
3258 * Make a copy of list "tv1" and append list "tv2".
3259 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003260 int
3261eval_addlist(typval_T *tv1, typval_T *tv2)
3262{
3263 typval_T var3;
3264
3265 // concatenate Lists
3266 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3267 {
3268 clear_tv(tv1);
3269 clear_tv(tv2);
3270 return FAIL;
3271 }
3272 clear_tv(tv1);
3273 *tv1 = var3;
3274 return OK;
3275}
3276
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003278 * Handle the bitwise left/right shift operator expression:
3279 * var1 << var2
3280 * var1 >> var2
3281 *
3282 * "arg" must point to the first non-white of the expression.
3283 * "arg" is advanced to just after the recognized expression.
3284 *
3285 * Return OK or FAIL.
3286 */
3287 static int
3288eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3289{
3290 /*
3291 * Get the first expression.
3292 */
3293 if (eval6(arg, rettv, evalarg) == FAIL)
3294 return FAIL;
3295
3296 /*
3297 * Repeat computing, until no '<<' or '>>' is following.
3298 */
3299 for (;;)
3300 {
3301 char_u *p;
3302 int getnext;
3303 exprtype_T type;
3304 int evaluate;
3305 typval_T var2;
3306 int vim9script;
3307
3308 p = eval_next_non_blank(*arg, evalarg, &getnext);
3309 if (p[0] == '<' && p[1] == '<')
3310 type = EXPR_LSHIFT;
3311 else if (p[0] == '>' && p[1] == '>')
3312 type = EXPR_RSHIFT;
3313 else
3314 return OK;
3315
3316 // Handle a bitwise left or right shift operator
3317 if (rettv->v_type != VAR_NUMBER)
3318 {
3319 // left operand should be a number
3320 emsg(_(e_bitshift_ops_must_be_number));
3321 clear_tv(rettv);
3322 return FAIL;
3323 }
3324
3325 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3326 vim9script = in_vim9script();
3327 if (getnext)
3328 {
3329 *arg = eval_next_line(*arg, evalarg);
3330 p = *arg;
3331 }
3332 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3333 {
3334 error_white_both(*arg, 2);
3335 clear_tv(rettv);
3336 return FAIL;
3337 }
3338
3339 /*
3340 * Get the second variable.
3341 */
3342 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3343 {
3344 error_white_both(p, 2);
3345 clear_tv(rettv);
3346 return FAIL;
3347 }
3348 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3349 if (eval6(arg, &var2, evalarg) == FAIL)
3350 {
3351 clear_tv(rettv);
3352 return FAIL;
3353 }
3354
3355 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3356 {
3357 // right operand should be a positive number
3358 if (var2.v_type != VAR_NUMBER)
3359 emsg(_(e_bitshift_ops_must_be_number));
3360 else
dundargocc57b5bc2022-11-02 13:30:51 +00003361 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003362 clear_tv(rettv);
3363 clear_tv(&var2);
3364 return FAIL;
3365 }
3366
3367 if (evaluate)
3368 {
3369 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3370 // shifting more bits than we have always results in zero
3371 rettv->vval.v_number = 0;
3372 else if (type == EXPR_LSHIFT)
3373 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003374 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003375 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003376 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003377 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003378 }
3379
3380 clear_tv(&var2);
3381 }
3382
3383 return OK;
3384}
3385
3386/*
3387 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003388 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003390 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003391 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 *
3393 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003394 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 *
3396 * Return OK or FAIL.
3397 */
3398 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003399eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003402 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003404 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 return FAIL;
3406
3407 /*
3408 * Repeat computing, until no '+', '-' or '.' is following.
3409 */
3410 for (;;)
3411 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003412 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003413 int getnext;
3414 char_u *p;
3415 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003416 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003417 int concat;
3418 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003419 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003420
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003421 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003422 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003423 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003424 p = eval_next_non_blank(*arg, evalarg, &getnext);
3425 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003426 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003427 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3428 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003430 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3431 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003432
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003433 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003434 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003435 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003436 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003437 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003438 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003439 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003440 {
mityu4ac198c2021-05-28 17:52:40 +02003441 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003442 clear_tv(rettv);
3443 return FAIL;
3444 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003445 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003446 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003447 if ((op != '+' || (rettv->v_type != VAR_LIST
3448 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003449 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003450 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003451 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003452 int error = FALSE;
3453
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003454 // For "list + ...", an illegal use of the first operand as
3455 // a number cannot be determined before evaluating the 2nd
3456 // operand: if this is also a list, all is ok.
3457 // For "something . ...", "something - ..." or "non-list + ...",
3458 // we know that the first operand needs to be a string or number
3459 // without evaluating the 2nd operand. So check before to avoid
3460 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003461 if (op != '.')
3462 tv_get_number_chk(rettv, &error);
3463 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003464 {
3465 clear_tv(rettv);
3466 return FAIL;
3467 }
3468 }
3469
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 /*
3471 * Get the second variable.
3472 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003473 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003474 {
mityu89dcb4d2021-05-28 13:50:17 +02003475 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003476 clear_tv(rettv);
3477 return FAIL;
3478 }
3479 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003480 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003482 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 return FAIL;
3484 }
3485
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003486 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 {
3488 /*
3489 * Compute the result.
3490 */
3491 if (op == '.')
3492 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003493 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3494 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003495 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003496
Bram Moolenaar2e086612020-08-25 22:37:48 +02003497 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003498 || var2.v_type == VAR_CHANNEL
3499 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003500 semsg(_(e_using_invalid_value_as_string_str),
3501 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003502 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003503 {
3504 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3505 var2.vval.v_float);
3506 s2 = buf2;
3507 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003508 else
3509 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003510 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003511 {
3512 clear_tv(rettv);
3513 clear_tv(&var2);
3514 return FAIL;
3515 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003516 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003517 clear_tv(rettv);
3518 rettv->v_type = VAR_STRING;
3519 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003521 else if (op == '+' && rettv->v_type == VAR_BLOB
3522 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003524 else if (op == '+' && rettv->v_type == VAR_LIST
3525 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003526 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003527 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003528 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 else
3531 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003532 int error = FALSE;
3533 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003534 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003535
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003536 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003537 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003538 f1 = rettv->vval.v_float;
3539 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003540 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003541 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003542 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003543 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003544 if (error)
3545 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003546 // This can only happen for "list + non-list" or
3547 // "blob + non-blob". For "non-list + ..." or
3548 // "something - ...", we returned before evaluating the
3549 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003550 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003551 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003552 return FAIL;
3553 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003554 if (var2.v_type == VAR_FLOAT)
3555 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003556 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003557 if (var2.v_type == VAR_FLOAT)
3558 {
3559 f2 = var2.vval.v_float;
3560 n2 = 0;
3561 }
3562 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003563 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003564 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003565 if (error)
3566 {
3567 clear_tv(rettv);
3568 clear_tv(&var2);
3569 return FAIL;
3570 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003571 if (rettv->v_type == VAR_FLOAT)
3572 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003573 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003574 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003575
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003576 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003577 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3578 {
3579 if (op == '+')
3580 f1 = f1 + f2;
3581 else
3582 f1 = f1 - f2;
3583 rettv->v_type = VAR_FLOAT;
3584 rettv->vval.v_float = f1;
3585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003587 {
3588 if (op == '+')
3589 n1 = n1 + n2;
3590 else
3591 n1 = n1 - n2;
3592 rettv->v_type = VAR_NUMBER;
3593 rettv->vval.v_number = n1;
3594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003596 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 }
3598 }
3599 return OK;
3600}
3601
3602/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003603 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 * * number multiplication
3605 * / number division
3606 * % number modulo
3607 *
3608 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003609 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 *
3611 * Return OK or FAIL.
3612 */
3613 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003614eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003615 char_u **arg,
3616 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003617 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003618 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003620 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621
3622 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003623 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003625 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 return FAIL;
3627
3628 /*
3629 * Repeat computing, until no '*', '/' or '%' is following.
3630 */
3631 for (;;)
3632 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003633 int evaluate;
3634 int getnext;
3635 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003636 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003637 int op;
3638 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003639 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003640 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003641
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003642 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003643 p = eval_next_non_blank(*arg, evalarg, &getnext);
3644 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003645 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003647
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003648 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003649 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003650 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003651 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003652 {
3653 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3654 {
mityu4ac198c2021-05-28 17:52:40 +02003655 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003656 clear_tv(rettv);
3657 return FAIL;
3658 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003659 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003662 f1 = 0;
3663 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003664 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003665 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003667 if (rettv->v_type == VAR_FLOAT)
3668 {
3669 f1 = rettv->vval.v_float;
3670 use_float = TRUE;
3671 n1 = 0;
3672 }
3673 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003674 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003675 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003676 if (error)
3677 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 }
3679 else
3680 n1 = 0;
3681
3682 /*
3683 * Get the second variable.
3684 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003685 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3686 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003687 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003688 clear_tv(rettv);
3689 return FAIL;
3690 }
3691 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003692 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 return FAIL;
3694
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003695 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003697 if (var2.v_type == VAR_FLOAT)
3698 {
3699 if (!use_float)
3700 {
3701 f1 = n1;
3702 use_float = TRUE;
3703 }
3704 f2 = var2.vval.v_float;
3705 n2 = 0;
3706 }
3707 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003708 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003709 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003710 clear_tv(&var2);
3711 if (error)
3712 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003713 if (use_float)
3714 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716
3717 /*
3718 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003719 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003721 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003723 if (op == '*')
3724 f1 = f1 * f2;
3725 else if (op == '/')
3726 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003727#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003728 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003729 if (f2 == 0.0)
3730 {
3731 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003732 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003733 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003734 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003735 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003736 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003737 }
3738 else
3739 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003740#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003741 // We rely on the floating point library to handle divide
3742 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003743 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003744#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003747 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003748 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003749 return FAIL;
3750 }
3751 rettv->v_type = VAR_FLOAT;
3752 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 }
3754 else
3755 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003756 int failed = FALSE;
3757
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003758 if (op == '*')
3759 n1 = n1 * n2;
3760 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003761 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003763 n1 = num_modulus(n1, n2, &failed);
3764 if (failed)
3765 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003766
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003767 rettv->v_type = VAR_NUMBER;
3768 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 }
3771 }
3772
3773 return OK;
3774}
3775
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003776/*
3777 * Handle a type cast before a base level expression.
3778 * "arg" must point to the first non-white of the expression.
3779 * "arg" is advanced to just after the recognized expression.
3780 * Return OK or FAIL.
3781 */
3782 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003783eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003784 char_u **arg,
3785 typval_T *rettv,
3786 evalarg_T *evalarg,
3787 int want_string) // after "." operator
3788{
3789 type_T *want_type = NULL;
3790 garray_T type_list; // list of pointers to allocated types
3791 int res;
3792 int evaluate = evalarg == NULL ? 0
3793 : (evalarg->eval_flags & EVAL_EVALUATE);
3794
3795 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003796 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3797 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003798 {
3799 ++*arg;
3800 ga_init2(&type_list, sizeof(type_T *), 10);
3801 want_type = parse_type(arg, &type_list, TRUE);
3802 if (want_type == NULL && (evaluate || **arg != '>'))
3803 {
3804 clear_type_list(&type_list);
3805 return FAIL;
3806 }
3807
3808 if (**arg != '>')
3809 {
3810 if (*skipwhite(*arg) == '>')
3811 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3812 else
3813 emsg(_(e_missing_gt));
3814 clear_type_list(&type_list);
3815 return FAIL;
3816 }
3817 ++*arg;
3818 *arg = skipwhite_and_linebreak(*arg, evalarg);
3819 }
3820
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003821 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003822
3823 if (want_type != NULL && evaluate)
3824 {
3825 if (res == OK)
3826 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003827 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3828 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003829
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003830 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003831 {
3832 if (want_type == &t_bool && actual != &t_bool
3833 && (actual->tt_flags & TTFLAG_BOOL_OK))
3834 {
3835 int n = tv2bool(rettv);
3836
3837 // can use "0" and "1" for boolean in some places
3838 clear_tv(rettv);
3839 rettv->v_type = VAR_BOOL;
3840 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3841 }
3842 else
3843 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003844 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003845
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003846 res = check_type(want_type, actual, TRUE, where);
3847 }
3848 }
3849 }
3850 clear_type_list(&type_list);
3851 }
3852
3853 return res;
3854}
3855
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003856 int
3857eval_leader(char_u **arg, int vim9)
3858{
3859 char_u *s = *arg;
3860 char_u *p = *arg;
3861
3862 while (*p == '!' || *p == '-' || *p == '+')
3863 {
3864 char_u *n = skipwhite(p + 1);
3865
3866 // ++, --, -+ and +- are not accepted in Vim9 script
3867 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3868 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003869 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003870 return FAIL;
3871 }
3872 p = n;
3873 }
3874 *arg = p;
3875 return OK;
3876}
3877
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003879 * Check for a predefined value "true", "false" and "null.*".
3880 * Return OK when recognized.
3881 */
3882 int
3883handle_predefined(char_u *s, int len, typval_T *rettv)
3884{
3885 switch (len)
3886 {
3887 case 4: if (STRNCMP(s, "true", 4) == 0)
3888 {
3889 rettv->v_type = VAR_BOOL;
3890 rettv->vval.v_number = VVAL_TRUE;
3891 return OK;
3892 }
3893 if (STRNCMP(s, "null", 4) == 0)
3894 {
3895 rettv->v_type = VAR_SPECIAL;
3896 rettv->vval.v_number = VVAL_NULL;
3897 return OK;
3898 }
3899 break;
3900 case 5: if (STRNCMP(s, "false", 5) == 0)
3901 {
3902 rettv->v_type = VAR_BOOL;
3903 rettv->vval.v_number = VVAL_FALSE;
3904 return OK;
3905 }
3906 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003907 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3908 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003909#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003910 rettv->v_type = VAR_JOB;
3911 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003912#else
3913 rettv->v_type = VAR_SPECIAL;
3914 rettv->vval.v_number = VVAL_NULL;
3915#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003916 return OK;
3917 }
3918 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003919 case 9:
3920 if (STRNCMP(s, "null_", 5) != 0)
3921 break;
3922 if (STRNCMP(s + 5, "list", 4) == 0)
3923 {
3924 rettv->v_type = VAR_LIST;
3925 rettv->vval.v_list = NULL;
3926 return OK;
3927 }
3928 if (STRNCMP(s + 5, "dict", 4) == 0)
3929 {
3930 rettv->v_type = VAR_DICT;
3931 rettv->vval.v_dict = NULL;
3932 return OK;
3933 }
3934 if (STRNCMP(s + 5, "blob", 4) == 0)
3935 {
3936 rettv->v_type = VAR_BLOB;
3937 rettv->vval.v_blob = NULL;
3938 return OK;
3939 }
3940 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003941 case 10: if (STRNCMP(s, "null_class", 10) == 0)
3942 {
3943 rettv->v_type = VAR_CLASS;
3944 rettv->vval.v_class = NULL;
3945 return OK;
3946 }
3947 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003948 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3949 {
3950 rettv->v_type = VAR_STRING;
3951 rettv->vval.v_string = NULL;
3952 return OK;
3953 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003954 if (STRNCMP(s, "null_object", 11) == 0)
3955 {
3956 rettv->v_type = VAR_OBJECT;
3957 rettv->vval.v_object = NULL;
3958 return OK;
3959 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003960 break;
3961 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003962 if (STRNCMP(s, "null_channel", 12) == 0)
3963 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003964#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003965 rettv->v_type = VAR_CHANNEL;
3966 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003967#else
3968 rettv->v_type = VAR_SPECIAL;
3969 rettv->vval.v_number = VVAL_NULL;
3970#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003971 return OK;
3972 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003973 if (STRNCMP(s, "null_partial", 12) == 0)
3974 {
3975 rettv->v_type = VAR_PARTIAL;
3976 rettv->vval.v_partial = NULL;
3977 return OK;
3978 }
3979 break;
3980 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3981 {
3982 rettv->v_type = VAR_FUNC;
3983 rettv->vval.v_string = NULL;
3984 return OK;
3985 }
3986 break;
3987 }
3988 return FAIL;
3989}
3990
3991/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 * Handle sixth level expression:
3993 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003994 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003995 * "string" string constant
3996 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 * &option-name option value
3998 * @r register contents
3999 * identifier variable value
4000 * function() function call
4001 * $VAR environment variable
4002 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004003 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004005 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004006 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 *
4008 * Also handle:
4009 * ! in front logical NOT
4010 * - in front unary minus
4011 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004012 * trailing [] subscript in String or List
4013 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004014 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 *
4016 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004017 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 *
4019 * Return OK or FAIL.
4020 */
4021 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004022eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004023 char_u **arg,
4024 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004025 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004028 int evaluate = evalarg != NULL
4029 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 int len;
4031 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004032 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 char_u *start_leader, *end_leader;
4034 int ret = OK;
4035 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004036 static int recurse = 0;
4037 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038
4039 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004040 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004041 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004043 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044
4045 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004046 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 */
4048 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004049 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004050 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 end_leader = *arg;
4052
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004053 if (**arg == '.' && (!isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004054 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004055 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004056 ++*arg;
4057 return FAIL;
4058 }
4059
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004060 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004061 // and crash. With MSVC the stack is smaller.
4062 if (recurse ==
4063#ifdef _MSC_VER
4064 300
4065#else
4066 1000
4067#endif
4068 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004069 {
4070 semsg(_(e_expression_too_recursive_str), *arg);
4071 return FAIL;
4072 }
4073 ++recurse;
4074
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 switch (**arg)
4076 {
4077 /*
4078 * Number constant.
4079 */
4080 case '0':
4081 case '1':
4082 case '2':
4083 case '3':
4084 case '4':
4085 case '5':
4086 case '6':
4087 case '7':
4088 case '8':
4089 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004090 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004091
4092 // Apply prefixed "-" and "+" now. Matters especially when
4093 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004094 if (ret == OK && evaluate && end_leader > start_leader
4095 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004096 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 break;
4098
4099 /*
4100 * String constant: "string".
4101 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004102 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 break;
4104
4105 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004106 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004108 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004109 break;
4110
4111 /*
4112 * List: [expr, expr]
4113 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004114 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 break;
4116
4117 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004118 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004119 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004120 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004121 {
4122 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4123 }
4124 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004125 {
4126 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004127 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004128 }
4129 else
4130 ret = NOTDONE;
4131 break;
4132
4133 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004134 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004135 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004136 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004137 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004138 ret = NOTDONE;
4139 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004140 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004141 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004142 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004143 break;
4144
4145 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004146 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004148 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 break;
4150
4151 /*
4152 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004153 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 */
LemonBoy2eaef102022-05-06 13:14:50 +01004155 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4156 ret = eval_interp_string(arg, rettv, evaluate);
4157 else
4158 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 break;
4160
4161 /*
4162 * Register contents: @r.
4163 */
4164 case '@': ++*arg;
4165 if (evaluate)
4166 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004167 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004168 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004169 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004170 emsg_invreg(**arg);
4171 else
4172 {
4173 rettv->v_type = VAR_STRING;
4174 rettv->vval.v_string = get_reg_contents(**arg,
4175 GREG_EXPR_SRC);
4176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 }
4178 if (**arg != NUL)
4179 ++*arg;
4180 break;
4181
4182 /*
4183 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004184 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004186 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004187 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004188 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004189 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004190 if (ret == OK && evaluate)
4191 {
4192 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4193
Bram Moolenaara9931532021-06-12 15:58:16 +02004194 // Compile it here to get the return type. The return
4195 // type is optional, when it's missing use t_unknown.
4196 // This is recognized in compile_return().
4197 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4198 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004199 if (compile_def_function(ufunc, FALSE,
4200 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004201 {
4202 clear_tv(rettv);
4203 ret = FAIL;
4204 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004205 }
4206 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004207 if (ret == NOTDONE)
4208 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004209 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004210 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004211
Bram Moolenaar9215f012020-06-27 21:18:00 +02004212 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004213 if (**arg == ')')
4214 ++*arg;
4215 else if (ret == OK)
4216 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004217 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004218 clear_tv(rettv);
4219 ret = FAIL;
4220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 }
4222 break;
4223
Bram Moolenaar8c711452005-01-14 21:53:12 +00004224 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 break;
4226 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004227
4228 if (ret == NOTDONE)
4229 {
4230 /*
4231 * Must be a variable or function name.
4232 * Can also be a curly-braces kind of name: {expr}.
4233 */
4234 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004235 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004236 if (alias != NULL)
4237 s = alias;
4238
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004239 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004240 ret = FAIL;
4241 else
4242 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004243 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4244
Bram Moolenaar4525a572022-02-13 11:57:33 +00004245 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004246 {
4247 emsg(_(e_cannot_use_underscore_here));
4248 ret = FAIL;
4249 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004250 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004251 && s[0] == 's' && s[1] == ':')
4252 {
4253 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4254 ret = FAIL;
4255 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004256 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004257 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004258 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004259 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004260 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004261 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004262 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004263 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004264 // get the value of "true", "false", etc. or a variable
4265 ret = FAIL;
4266 if (vim9script)
4267 ret = handle_predefined(s, len, rettv);
4268 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004269 {
4270 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004271 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004272 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004273 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004274 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004275 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004276 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004277 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004278 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004279 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004280 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004281 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004282 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004283 }
4284
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004285 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4286 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004287 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004288 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289
4290 /*
4291 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4292 */
4293 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004294 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004295
4296 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004297 return ret;
4298}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004299
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004300/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004301 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004302 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004303 * Adjusts "end_leaderp" until it is at "start_leader".
4304 */
4305 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004306eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004307 typval_T *rettv,
4308 int numeric_only,
4309 char_u *start_leader,
4310 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004311{
4312 char_u *end_leader = *end_leaderp;
4313 int ret = OK;
4314 int error = FALSE;
4315 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004316 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004317 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004318 float_T f = 0.0;
4319
4320 if (rettv->v_type == VAR_FLOAT)
4321 f = rettv->vval.v_float;
4322 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004323 {
4324 while (VIM_ISWHITE(end_leader[-1]))
4325 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004326 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004327 val = tv2bool(rettv);
4328 else
4329 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004330 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004331 if (error)
4332 {
4333 clear_tv(rettv);
4334 ret = FAIL;
4335 }
4336 else
4337 {
4338 while (end_leader > start_leader)
4339 {
4340 --end_leader;
4341 if (*end_leader == '!')
4342 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004343 if (numeric_only)
4344 {
4345 ++end_leader;
4346 break;
4347 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004348 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004349 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004350 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004351 {
4352 rettv->v_type = VAR_BOOL;
4353 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4354 }
4355 else
4356 f = !f;
4357 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004358 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004359 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004360 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004361 type = VAR_BOOL;
4362 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004363 }
4364 else if (*end_leader == '-')
4365 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004366 if (rettv->v_type == VAR_FLOAT)
4367 f = -f;
4368 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004369 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004370 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004371 type = VAR_NUMBER;
4372 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004373 }
4374 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004375 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004377 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004378 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004380 else
4381 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004382 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004383 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004384 rettv->v_type = type;
4385 else
4386 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004387 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004390 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 return ret;
4392}
4393
4394/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004395 * Call the function referred to in "rettv".
4396 */
4397 static int
4398call_func_rettv(
4399 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004400 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004401 typval_T *rettv,
4402 int evaluate,
4403 dict_T *selfdict,
4404 typval_T *basetv)
4405{
4406 partial_T *pt = NULL;
4407 funcexe_T funcexe;
4408 typval_T functv;
4409 char_u *s;
4410 int ret;
4411
4412 // need to copy the funcref so that we can clear rettv
4413 if (evaluate)
4414 {
4415 functv = *rettv;
4416 rettv->v_type = VAR_UNKNOWN;
4417
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004418 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004419 if (functv.v_type == VAR_PARTIAL)
4420 {
4421 pt = functv.vval.v_partial;
4422 s = partial_name(pt);
4423 }
4424 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004425 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004426 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004427 if (s == NULL || *s == NUL)
4428 {
4429 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004430 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004431 goto theend;
4432 }
4433 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004434 }
4435 else
4436 s = (char_u *)"";
4437
Bram Moolenaara80faa82020-04-12 19:37:17 +02004438 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004439 funcexe.fe_firstline = curwin->w_cursor.lnum;
4440 funcexe.fe_lastline = curwin->w_cursor.lnum;
4441 funcexe.fe_evaluate = evaluate;
4442 funcexe.fe_partial = pt;
4443 funcexe.fe_selfdict = selfdict;
4444 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004445 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004446
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004447theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004448 // Clear the funcref afterwards, so that deleting it while
4449 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004450 if (evaluate)
4451 clear_tv(&functv);
4452
4453 return ret;
4454}
4455
4456/*
4457 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004458 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004459 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4460 */
4461 static int
4462eval_lambda(
4463 char_u **arg,
4464 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004465 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004466 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004467{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004468 int evaluate = evalarg != NULL
4469 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004470 typval_T base = *rettv;
4471 int ret;
4472
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004473 rettv->v_type = VAR_UNKNOWN;
4474
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004475 if (**arg == '{')
4476 {
4477 // ->{lambda}()
4478 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4479 }
4480 else
4481 {
4482 // ->(lambda)()
4483 ++*arg;
4484 ret = eval1(arg, rettv, evalarg);
4485 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004486 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004487 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004488 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004489 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004490 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004491 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4492 && rettv->v_type != VAR_PARTIAL)
4493 {
4494 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4495 return FAIL;
4496 }
4497 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004498 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004499 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004500 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004501
4502 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004503 {
4504 if (verbose)
4505 {
4506 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004507 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004508 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004509 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004510 }
4511 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004512 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004513 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004514 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004515 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004516
4517 // Clear the funcref afterwards, so that deleting it while
4518 // evaluating the arguments is possible (see test55).
4519 if (evaluate)
4520 clear_tv(&base);
4521
4522 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004523}
4524
4525/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004526 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004527 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004528 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4529 */
4530 static int
4531eval_method(
4532 char_u **arg,
4533 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004534 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004535 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004536{
4537 char_u *name;
4538 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004539 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004540 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004541 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004542 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004543 int evaluate = evalarg != NULL
4544 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004545
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004546 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004547
Bram Moolenaarac92e252019-08-03 21:58:38 +02004548 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004549 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004550 if (alias != NULL)
4551 name = alias;
4552
4553 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004554 {
4555 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004556 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004557 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004558 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004559 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004560 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004561 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004562
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004563 // If there is no "(" immediately following, but there is further on,
4564 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4565 // Does not handle anything where "(" is part of the expression.
4566 *arg = skipwhite(*arg);
4567
4568 if (**arg != '(' && alias == NULL
4569 && (paren = vim_strchr(*arg, '(')) != NULL)
4570 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004571 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00004572
4573 // Truncate the name a the "(". Avoid trying to get another line
4574 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004575 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00004576 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
4577 if (evalarg != NULL)
4578 {
4579 getline = evalarg->eval_getline;
4580 evalarg->eval_getline = NULL;
4581 }
4582
4583 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004584 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004585 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004586 *arg = name + len;
4587 ret = FAIL;
4588 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004589 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004590 {
4591 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004592 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004593 }
Bram Moolenaar34820942022-12-19 20:28:38 +00004594
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004595 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00004596 if (getline != NULL)
4597 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004598 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004599
4600 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004601 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004602 *arg = skipwhite(*arg);
4603
4604 if (**arg != '(')
4605 {
4606 if (verbose)
4607 semsg(_(e_missing_parenthesis_str), name);
4608 ret = FAIL;
4609 }
4610 else if (VIM_ISWHITE((*arg)[-1]))
4611 {
4612 if (verbose)
4613 emsg(_(e_no_white_space_allowed_before_parenthesis));
4614 ret = FAIL;
4615 }
4616 else
4617 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004618 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004619 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004620 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004621
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004622 // Clear the funcref afterwards, so that deleting it while
4623 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004624 if (evaluate)
4625 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004626 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004627
Bram Moolenaarac92e252019-08-03 21:58:38 +02004628 return ret;
4629}
4630
4631/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004632 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4633 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004634 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4635 */
4636 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004637eval_index(
4638 char_u **arg,
4639 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004640 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004641 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004642{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004643 int evaluate = evalarg != NULL
4644 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004645 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004646 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004647 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004648 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004649 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004650 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004651
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004652 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4653 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004654
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004655 init_tv(&var1);
4656 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004657 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004658 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004659 /*
4660 * dict.name
4661 */
4662 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004663 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004664 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004665 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004666 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004667 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004668 }
4669 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004670 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004671 /*
4672 * something[idx]
4673 *
4674 * Get the (first) variable from inside the [].
4675 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004676 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004677 if (**arg == ':')
4678 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004679 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004680 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004681 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004682 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004683 semsg(_(e_white_space_required_before_and_after_str_at_str),
4684 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004685 clear_tv(&var1);
4686 return FAIL;
4687 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004688 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004689 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004690 int error = FALSE;
4691
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004692 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004693 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004694 && var1.v_type == VAR_FLOAT)
4695 {
4696 var1.vval.v_string = typval_tostring(&var1, TRUE);
4697 var1.v_type = VAR_STRING;
4698 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004699
Bram Moolenaar4525a572022-02-13 11:57:33 +00004700 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004701 tv_get_number_chk(&var1, &error);
4702 else
4703 error = tv_get_string_chk(&var1) == NULL;
4704 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004705 {
4706 // not a number or string
4707 clear_tv(&var1);
4708 return FAIL;
4709 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004710 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004711
4712 /*
4713 * Get the second variable from inside the [:].
4714 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004715 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004716 if (**arg == ':')
4717 {
4718 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004719 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004720 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004721 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004722 semsg(_(e_white_space_required_before_and_after_str_at_str),
4723 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004724 if (!empty1)
4725 clear_tv(&var1);
4726 return FAIL;
4727 }
4728 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004729 if (**arg == ']')
4730 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004731 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004732 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004733 if (!empty1)
4734 clear_tv(&var1);
4735 return FAIL;
4736 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004737 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004738 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004739 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004740 if (!empty1)
4741 clear_tv(&var1);
4742 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004743 return FAIL;
4744 }
4745 }
4746
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004747 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004748 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004749 if (**arg != ']')
4750 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004751 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004752 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004753 clear_tv(&var1);
4754 if (range)
4755 clear_tv(&var2);
4756 return FAIL;
4757 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004758 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004759 }
4760
4761 if (evaluate)
4762 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004763 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004764 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004765 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004766
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004767 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004768 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004769 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004770 clear_tv(&var2);
4771 return res;
4772 }
4773 return OK;
4774}
4775
4776/*
4777 * Check if "rettv" can have an [index] or [sli:ce]
4778 */
4779 int
4780check_can_index(typval_T *rettv, int evaluate, int verbose)
4781{
4782 switch (rettv->v_type)
4783 {
4784 case VAR_FUNC:
4785 case VAR_PARTIAL:
4786 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004787 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004788 return FAIL;
4789 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004790 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004791 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004792 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004793 case VAR_BOOL:
4794 case VAR_SPECIAL:
4795 case VAR_JOB:
4796 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004797 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004798 case VAR_CLASS:
4799 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004800 if (verbose)
4801 emsg(_(e_cannot_index_special_variable));
4802 return FAIL;
4803 case VAR_UNKNOWN:
4804 case VAR_ANY:
4805 case VAR_VOID:
4806 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004807 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004808 emsg(_(e_cannot_index_special_variable));
4809 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004810 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004811 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004812
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004813 case VAR_STRING:
4814 case VAR_LIST:
4815 case VAR_DICT:
4816 case VAR_BLOB:
4817 break;
4818 case VAR_NUMBER:
4819 if (in_vim9script())
4820 emsg(_(e_cannot_index_number));
4821 break;
4822 }
4823 return OK;
4824}
4825
4826/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004827 * slice() function
4828 */
4829 void
4830f_slice(typval_T *argvars, typval_T *rettv)
4831{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004832 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004833 && ((argvars[0].v_type != VAR_STRING
4834 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004835 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004836 && check_for_list_arg(argvars, 0) == FAIL)
4837 || check_for_number_arg(argvars, 1) == FAIL
4838 || check_for_opt_number_arg(argvars, 2) == FAIL))
4839 return;
4840
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00004841 if (check_can_index(argvars, TRUE, FALSE) != OK)
4842 return;
4843
4844 copy_tv(argvars, rettv);
4845 eval_index_inner(rettv, TRUE, argvars + 1,
4846 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4847 TRUE, NULL, 0, FALSE);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004848}
4849
4850/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004851 * Apply index or range to "rettv".
4852 * "var1" is the first index, NULL for [:expr].
4853 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004854 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4855 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004856 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4857 */
4858 int
4859eval_index_inner(
4860 typval_T *rettv,
4861 int is_range,
4862 typval_T *var1,
4863 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004864 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004865 char_u *key,
4866 int keylen,
4867 int verbose)
4868{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004869 varnumber_T n1, n2 = 0;
4870 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004871
4872 n1 = 0;
4873 if (var1 != NULL && rettv->v_type != VAR_DICT)
4874 n1 = tv_get_number(var1);
4875
4876 if (is_range)
4877 {
4878 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004879 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004880 if (verbose)
4881 emsg(_(e_cannot_slice_dictionary));
4882 return FAIL;
4883 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004884 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004885 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004886 else
4887 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004888 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004889
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004890 switch (rettv->v_type)
4891 {
4892 case VAR_UNKNOWN:
4893 case VAR_ANY:
4894 case VAR_VOID:
4895 case VAR_FUNC:
4896 case VAR_PARTIAL:
4897 case VAR_FLOAT:
4898 case VAR_BOOL:
4899 case VAR_SPECIAL:
4900 case VAR_JOB:
4901 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004902 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004903 case VAR_CLASS:
4904 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004905 break; // not evaluating, skipping over subscript
4906
4907 case VAR_NUMBER:
4908 case VAR_STRING:
4909 {
4910 char_u *s = tv_get_string(rettv);
4911
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004912 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004913 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004914 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004915 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004916 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004917 else
4918 s = char_from_string(s, n1);
4919 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004920 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004921 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004922 // The resulting variable is a substring. If the indexes
4923 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004924 if (n1 < 0)
4925 {
4926 n1 = len + n1;
4927 if (n1 < 0)
4928 n1 = 0;
4929 }
4930 if (n2 < 0)
4931 n2 = len + n2;
4932 else if (n2 >= len)
4933 n2 = len;
4934 if (n1 >= len || n2 < 0 || n1 > n2)
4935 s = NULL;
4936 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004937 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004938 }
4939 else
4940 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004941 // The resulting variable is a string of a single
4942 // character. If the index is too big or negative the
4943 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004944 if (n1 >= len || n1 < 0)
4945 s = NULL;
4946 else
4947 s = vim_strnsave(s + n1, 1);
4948 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004949 clear_tv(rettv);
4950 rettv->v_type = VAR_STRING;
4951 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004952 }
4953 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004954
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004955 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004956 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4957 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004958 break;
4959
4960 case VAR_LIST:
4961 if (var1 == NULL)
4962 n1 = 0;
4963 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004964 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004965 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004966 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004967 return FAIL;
4968 break;
4969
4970 case VAR_DICT:
4971 {
4972 dictitem_T *item;
4973 typval_T tmp;
4974
4975 if (key == NULL)
4976 {
4977 key = tv_get_string_chk(var1);
4978 if (key == NULL)
4979 return FAIL;
4980 }
4981
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004982 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004983
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004984 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004985 {
4986 if (verbose)
4987 {
4988 if (keylen > 0)
4989 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004990 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004991 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004992 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004993 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004994
4995 copy_tv(&item->di_tv, &tmp);
4996 clear_tv(rettv);
4997 *rettv = tmp;
4998 }
4999 break;
5000 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005001 return OK;
5002}
5003
5004/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005005 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005006 */
5007 char_u *
5008partial_name(partial_T *pt)
5009{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02005010 if (pt != NULL)
5011 {
5012 if (pt->pt_name != NULL)
5013 return pt->pt_name;
5014 if (pt->pt_func != NULL)
5015 return pt->pt_func->uf_name;
5016 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005017 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005018}
5019
Bram Moolenaarddecc252016-04-06 22:59:37 +02005020 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005021partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005022{
5023 int i;
5024
5025 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005026 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005027 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005028 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005029 if (pt->pt_name != NULL)
5030 {
5031 func_unref(pt->pt_name);
5032 vim_free(pt->pt_name);
5033 }
5034 else
5035 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005036
Bram Moolenaar54656012021-06-09 20:50:46 +02005037 // "out_up" is no longer used, decrement refcount on partial that owns it.
5038 partial_unref(pt->pt_outer.out_up_partial);
5039
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005040 // Using pt_outer from another partial.
5041 partial_unref(pt->pt_outer_partial);
5042
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005043 // Decrease the reference count for the context of a closure. If down
5044 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005045 if (pt->pt_funcstack != NULL)
5046 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005047 --pt->pt_funcstack->fs_refcount;
5048 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005049 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005050 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005051 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5052 if (pt->pt_loopvars[i] != NULL)
5053 {
5054 --pt->pt_loopvars[i]->lvs_refcount;
5055 loopvars_check_refcount(pt->pt_loopvars[i]);
5056 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005057
Bram Moolenaarddecc252016-04-06 22:59:37 +02005058 vim_free(pt);
5059}
5060
5061/*
5062 * Unreference a closure: decrement the reference count and free it when it
5063 * becomes zero.
5064 */
5065 void
5066partial_unref(partial_T *pt)
5067{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005068 if (pt == NULL)
5069 return;
5070
5071 int done = FALSE;
5072
5073 if (--pt->pt_refcount <= 0)
5074 partial_free(pt);
5075
5076 // If the reference count goes down to one, the funcstack may be the
5077 // only reference and can be freed if no other partials reference it.
5078 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005079 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005080 // careful: if the funcstack is freed it may contain this partial
5081 // and it gets freed as well
5082 if (pt->pt_funcstack != NULL)
5083 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005084
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005085 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005086 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005087 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005088
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005089 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5090 if (pt->pt_loopvars[depth] != NULL
5091 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005092 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005093 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005094 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005095}
5096
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005097/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005098 * Return the next (unique) copy ID.
5099 * Used for serializing nested structures.
5100 */
5101 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005102get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005103{
5104 current_copyID += COPYID_INC;
5105 return current_copyID;
5106}
5107
5108/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005109 * Garbage collection for lists and dictionaries.
5110 *
5111 * We use reference counts to be able to free most items right away when they
5112 * are no longer used. But for composite items it's possible that it becomes
5113 * unused while the reference count is > 0: When there is a recursive
5114 * reference. Example:
5115 * :let l = [1, 2, 3]
5116 * :let d = {9: l}
5117 * :let l[1] = d
5118 *
5119 * Since this is quite unusual we handle this with garbage collection: every
5120 * once in a while find out which lists and dicts are not referenced from any
5121 * variable.
5122 *
5123 * Here is a good reference text about garbage collection (refers to Python
5124 * but it applies to all reference-counting mechanisms):
5125 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005126 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005127
5128/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005129 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005130 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005131 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005132 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005133 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005134garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005135{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005136 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005137 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005138 buf_T *buf;
5139 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005140 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005141 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005142
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005143 if (!testing)
5144 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005145 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005146 want_garbage_collect = FALSE;
5147 may_garbage_collect = FALSE;
5148 garbage_collect_at_exit = FALSE;
5149 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005150
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005151 // The execution stack can grow big, limit the size.
5152 if (exestack.ga_maxlen - exestack.ga_len > 500)
5153 {
5154 size_t new_len;
5155 char_u *pp;
5156 int n;
5157
5158 // Keep 150% of the current size, with a minimum of the growth size.
5159 n = exestack.ga_len / 2;
5160 if (n < exestack.ga_growsize)
5161 n = exestack.ga_growsize;
5162
5163 // Don't make it bigger though.
5164 if (exestack.ga_len + n < exestack.ga_maxlen)
5165 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005166 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005167 pp = vim_realloc(exestack.ga_data, new_len);
5168 if (pp == NULL)
5169 return FAIL;
5170 exestack.ga_maxlen = exestack.ga_len + n;
5171 exestack.ga_data = pp;
5172 }
5173 }
5174
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005175 // We advance by two because we add one for items referenced through
5176 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005177 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005178
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005179 /*
5180 * 1. Go through all accessible variables and mark all lists and dicts
5181 * with copyID.
5182 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005183
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005184 // Don't free variables in the previous_funccal list unless they are only
5185 // referenced through previous_funccal. This must be first, because if
5186 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005187 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005188
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005189 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005190 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005191
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005192 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005193 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005194 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5195 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005196
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005197 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005198 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005199 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5200 NULL, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005201 // window-local variables in autocmd windows
Bram Moolenaare76062c2022-11-28 18:51:43 +00005202 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005203 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00005204 abort = abort || set_ref_in_item(
5205 &aucmd_win[i].auc_win->w_winvar.di_tv, copyID, NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005206#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005207 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005208 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5209 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005210 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005211 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005212 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5213 NULL, NULL);
5214#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005215
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005216 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005217 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005218 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5219 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005220 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005221 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005222
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005223 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005224 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005225
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005226 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005227 abort = abort || set_ref_in_functions(copyID);
5228
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005229 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005230 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005231
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005232 // funcstacks keep variables for closures
5233 abort = abort || set_ref_in_funcstacks(copyID);
5234
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005235 // loopvars keep variables for loop blocks
5236 abort = abort || set_ref_in_loopvars(copyID);
5237
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005238 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005239 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005240
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005241 // callbacks in buffers
5242 abort = abort || set_ref_in_buffers(copyID);
5243
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005244 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5245 abort = abort || set_ref_in_insexpand_funcs(copyID);
5246
5247 // 'operatorfunc' callback
5248 abort = abort || set_ref_in_opfunc(copyID);
5249
5250 // 'tagfunc' callback
5251 abort = abort || set_ref_in_tagfunc(copyID);
5252
5253 // 'imactivatefunc' and 'imstatusfunc' callbacks
5254 abort = abort || set_ref_in_im_funcs(copyID);
5255
Bram Moolenaar1dced572012-04-05 16:54:08 +02005256#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005257 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005258#endif
5259
Bram Moolenaardb913952012-06-29 12:54:53 +02005260#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005261 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005262#endif
5263
5264#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005265 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005266#endif
5267
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005268#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005269 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005270 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005271#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005272#ifdef FEAT_NETBEANS_INTG
5273 abort = abort || set_ref_in_nb_channel(copyID);
5274#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005275
Bram Moolenaare3188e22016-05-31 21:13:04 +02005276#ifdef FEAT_TIMERS
5277 abort = abort || set_ref_in_timer(copyID);
5278#endif
5279
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005280#ifdef FEAT_QUICKFIX
5281 abort = abort || set_ref_in_quickfix(copyID);
5282#endif
5283
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005284#ifdef FEAT_TERMINAL
5285 abort = abort || set_ref_in_term(copyID);
5286#endif
5287
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005288#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005289 abort = abort || set_ref_in_popups(copyID);
5290#endif
5291
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005292 abort = abort || set_ref_in_classes(copyID);
5293
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005294 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005295 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005296 /*
5297 * 2. Free lists and dictionaries that are not referenced.
5298 */
5299 did_free = free_unref_items(copyID);
5300
5301 /*
5302 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005303 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005304 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005305 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005306 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005307 else if (p_verbose > 0)
5308 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005309 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005310 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005311
5312 return did_free;
5313}
5314
5315/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005316 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005317 */
5318 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005319free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005320{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005321 int did_free = FALSE;
5322
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005323 // Let all "free" functions know that we are here. This means no
5324 // dictionaries, lists, channels or jobs are to be freed, because we will
5325 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005326 in_free_unref_items = TRUE;
5327
5328 /*
5329 * PASS 1: free the contents of the items. We don't free the items
5330 * themselves yet, so that it is possible to decrement refcount counters
5331 */
5332
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005333 // Go through the list of dicts and free items without this copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005334 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005335
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005336 // Go through the list of lists and free items without this copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005337 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005338
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005339 // Go through the list of objects and free items without this copyID.
5340 did_free |= object_free_nonref(copyID);
5341
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005342 // Go through the list of classes and free items without this copyID.
5343 did_free |= class_free_nonref(copyID);
5344
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005345#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005346 // Go through the list of jobs and free items without the copyID. This
5347 // must happen before doing channels, because jobs refer to channels, but
5348 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005349 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5350
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005351 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005352 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5353#endif
5354
5355 /*
5356 * PASS 2: free the items themselves.
5357 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005358 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005359 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005360
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005361#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005362 // Go through the list of jobs and free items without the copyID. This
5363 // must happen before doing channels, because jobs refer to channels, but
5364 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005365 free_unused_jobs(copyID, COPYID_MASK);
5366
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005367 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005368 free_unused_channels(copyID, COPYID_MASK);
5369#endif
5370
5371 in_free_unref_items = FALSE;
5372
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005373 return did_free;
5374}
5375
5376/*
5377 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005378 * "list_stack" is used to add lists to be marked. Can be NULL.
5379 *
5380 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005381 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005382 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005383set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005384{
5385 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005386 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005387 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005388 hashtab_T *cur_ht;
5389 ht_stack_T *ht_stack = NULL;
5390 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005391
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005392 cur_ht = ht;
5393 for (;;)
5394 {
5395 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005396 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005397 // Mark each item in the hashtab. If the item contains a hashtab
5398 // it is added to ht_stack, if it contains a list it is added to
5399 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005400 todo = (int)cur_ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00005401 FOR_ALL_HASHTAB_ITEMS(cur_ht, hi, todo)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005402 if (!HASHITEM_EMPTY(hi))
5403 {
5404 --todo;
5405 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5406 &ht_stack, list_stack);
5407 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005408 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005409
5410 if (ht_stack == NULL)
5411 break;
5412
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005413 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005414 cur_ht = ht_stack->ht;
5415 tempitem = ht_stack;
5416 ht_stack = ht_stack->prev;
5417 free(tempitem);
5418 }
5419
5420 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005421}
5422
Dominique Pelle748b3082022-01-08 12:41:16 +00005423#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5424 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005425/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005426 * Mark a dict and its items with "copyID".
5427 * Returns TRUE if setting references failed somehow.
5428 */
5429 int
5430set_ref_in_dict(dict_T *d, int copyID)
5431{
5432 if (d != NULL && d->dv_copyID != copyID)
5433 {
5434 d->dv_copyID = copyID;
5435 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5436 }
5437 return FALSE;
5438}
Dominique Pelle748b3082022-01-08 12:41:16 +00005439#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005440
5441/*
5442 * Mark a list and its items with "copyID".
5443 * Returns TRUE if setting references failed somehow.
5444 */
5445 int
5446set_ref_in_list(list_T *ll, int copyID)
5447{
5448 if (ll != NULL && ll->lv_copyID != copyID)
5449 {
5450 ll->lv_copyID = copyID;
5451 return set_ref_in_list_items(ll, copyID, NULL);
5452 }
5453 return FALSE;
5454}
5455
5456/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005457 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005458 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5459 *
5460 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005461 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005462 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005463set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005464{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005465 listitem_T *li;
5466 int abort = FALSE;
5467 list_T *cur_l;
5468 list_stack_T *list_stack = NULL;
5469 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005470
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005471 cur_l = l;
5472 for (;;)
5473 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005474 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005475 // Mark each item in the list. If the item contains a hashtab
5476 // it is added to ht_stack, if it contains a list it is added to
5477 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005478 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5479 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5480 ht_stack, &list_stack);
5481 if (list_stack == NULL)
5482 break;
5483
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005484 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005485 cur_l = list_stack->list;
5486 tempitem = list_stack;
5487 list_stack = list_stack->prev;
5488 free(tempitem);
5489 }
5490
5491 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005492}
5493
5494/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005495 * Mark the partial in callback 'cb' with "copyID".
5496 */
5497 int
5498set_ref_in_callback(callback_T *cb, int copyID)
5499{
5500 typval_T tv;
5501
5502 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5503 return FALSE;
5504
5505 tv.v_type = VAR_PARTIAL;
5506 tv.vval.v_partial = cb->cb_partial;
5507 return set_ref_in_item(&tv, copyID, NULL, NULL);
5508}
5509
5510/*
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005511 * Mark the dict "dd" with "copyID".
5512 * Also see set_ref_in_item().
5513 */
5514 static int
5515set_ref_in_item_dict(
5516 dict_T *dd,
5517 int copyID,
5518 ht_stack_T **ht_stack,
5519 list_stack_T **list_stack)
5520{
5521 if (dd == NULL || dd->dv_copyID == copyID)
5522 return FALSE;
5523
5524 // Didn't see this dict yet.
5525 dd->dv_copyID = copyID;
5526 if (ht_stack == NULL)
5527 return set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5528
5529 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5530 if (newitem == NULL)
5531 return TRUE;
5532
5533 newitem->ht = &dd->dv_hashtab;
5534 newitem->prev = *ht_stack;
5535 *ht_stack = newitem;
5536
5537 return FALSE;
5538}
5539
5540/*
5541 * Mark the list "ll" with "copyID".
5542 * Also see set_ref_in_item().
5543 */
5544 static int
5545set_ref_in_item_list(
5546 list_T *ll,
5547 int copyID,
5548 ht_stack_T **ht_stack,
5549 list_stack_T **list_stack)
5550{
5551 if (ll == NULL || ll->lv_copyID == copyID)
5552 return FALSE;
5553
5554 // Didn't see this list yet.
5555 ll->lv_copyID = copyID;
5556 if (list_stack == NULL)
5557 return set_ref_in_list_items(ll, copyID, ht_stack);
5558
5559 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5560 if (newitem == NULL)
5561 return TRUE;
5562
5563 newitem->list = ll;
5564 newitem->prev = *list_stack;
5565 *list_stack = newitem;
5566
5567 return FALSE;
5568}
5569
5570/*
5571 * Mark the partial "pt" with "copyID".
5572 * Also see set_ref_in_item().
5573 */
5574 static int
5575set_ref_in_item_partial(
5576 partial_T *pt,
5577 int copyID,
5578 ht_stack_T **ht_stack,
5579 list_stack_T **list_stack)
5580{
5581 if (pt == NULL || pt->pt_copyID == copyID)
5582 return FALSE;
5583
5584 // Didn't see this partial yet.
5585 pt->pt_copyID = copyID;
5586
5587 int abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
5588
5589 if (pt->pt_dict != NULL)
5590 {
5591 typval_T dtv;
5592
5593 dtv.v_type = VAR_DICT;
5594 dtv.vval.v_dict = pt->pt_dict;
5595 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5596 }
5597
5598 for (int i = 0; i < pt->pt_argc; ++i)
5599 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5600 ht_stack, list_stack);
5601 // pt_funcstack is handled in set_ref_in_funcstacks()
5602 // pt_loopvars is handled in set_ref_in_loopvars()
5603
5604 return abort;
5605}
5606
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005607#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005608/*
5609 * Mark the job "pt" with "copyID".
5610 * Also see set_ref_in_item().
5611 */
5612 static int
5613set_ref_in_item_job(
5614 job_T *job,
5615 int copyID,
5616 ht_stack_T **ht_stack,
5617 list_stack_T **list_stack)
5618{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005619 typval_T dtv;
5620
5621 if (job == NULL || job->jv_copyID == copyID)
5622 return FALSE;
5623
5624 job->jv_copyID = copyID;
5625 if (job->jv_channel != NULL)
5626 {
5627 dtv.v_type = VAR_CHANNEL;
5628 dtv.vval.v_channel = job->jv_channel;
5629 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5630 }
5631 if (job->jv_exit_cb.cb_partial != NULL)
5632 {
5633 dtv.v_type = VAR_PARTIAL;
5634 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
5635 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5636 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005637
5638 return FALSE;
5639}
5640
5641/*
5642 * Mark the channel "ch" with "copyID".
5643 * Also see set_ref_in_item().
5644 */
5645 static int
5646set_ref_in_item_channel(
5647 channel_T *ch,
5648 int copyID,
5649 ht_stack_T **ht_stack,
5650 list_stack_T **list_stack)
5651{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005652 typval_T dtv;
5653
5654 if (ch == NULL || ch->ch_copyID == copyID)
5655 return FALSE;
5656
5657 ch->ch_copyID = copyID;
5658 for (ch_part_T part = PART_SOCK; part < PART_COUNT; ++part)
5659 {
5660 for (jsonq_T *jq = ch->ch_part[part].ch_json_head.jq_next;
5661 jq != NULL; jq = jq->jq_next)
5662 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5663 for (cbq_T *cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5664 cq = cq->cq_next)
5665 if (cq->cq_callback.cb_partial != NULL)
5666 {
5667 dtv.v_type = VAR_PARTIAL;
5668 dtv.vval.v_partial = cq->cq_callback.cb_partial;
5669 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5670 }
5671 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
5672 {
5673 dtv.v_type = VAR_PARTIAL;
5674 dtv.vval.v_partial = ch->ch_part[part].ch_callback.cb_partial;
5675 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5676 }
5677 }
5678 if (ch->ch_callback.cb_partial != NULL)
5679 {
5680 dtv.v_type = VAR_PARTIAL;
5681 dtv.vval.v_partial = ch->ch_callback.cb_partial;
5682 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5683 }
5684 if (ch->ch_close_cb.cb_partial != NULL)
5685 {
5686 dtv.v_type = VAR_PARTIAL;
5687 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
5688 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5689 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005690
5691 return FALSE;
5692}
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005693#endif
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005694
5695/*
5696 * Mark the class "cl" with "copyID".
5697 * Also see set_ref_in_item().
5698 */
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005699 int
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005700set_ref_in_item_class(
5701 class_T *cl,
5702 int copyID,
5703 ht_stack_T **ht_stack,
5704 list_stack_T **list_stack)
5705{
5706 int abort = FALSE;
5707
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005708 if (cl == NULL || cl->class_copyID == copyID)
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005709 return FALSE;
5710
5711 cl->class_copyID = copyID;
Yegappan Lakshmanan544be0d2023-09-04 22:14:28 +02005712 if (cl->class_members_tv != NULL)
5713 {
5714 // The "class_members_tv" table is allocated only for regular classes
5715 // and not for interfaces.
5716 for (int i = 0; !abort && i < cl->class_class_member_count; ++i)
5717 abort = abort || set_ref_in_item(
5718 &cl->class_members_tv[i],
5719 copyID, ht_stack, list_stack);
5720 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005721
5722 for (int i = 0; !abort && i < cl->class_class_function_count; ++i)
5723 abort = abort || set_ref_in_func(NULL,
5724 cl->class_class_functions[i], copyID);
5725
5726 for (int i = 0; !abort && i < cl->class_obj_method_count; ++i)
5727 abort = abort || set_ref_in_func(NULL,
5728 cl->class_obj_methods[i], copyID);
5729
5730 return abort;
5731}
5732
5733/*
5734 * Mark the object "cl" with "copyID".
5735 * Also see set_ref_in_item().
5736 */
5737 static int
5738set_ref_in_item_object(
5739 object_T *obj,
5740 int copyID,
5741 ht_stack_T **ht_stack,
5742 list_stack_T **list_stack)
5743{
5744 int abort = FALSE;
5745
5746 if (obj == NULL || obj->obj_copyID == copyID)
5747 return FALSE;
5748
5749 obj->obj_copyID = copyID;
5750
5751 // The typval_T array is right after the object_T.
5752 typval_T *mtv = (typval_T *)(obj + 1);
5753 for (int i = 0; !abort
5754 && i < obj->obj_class->class_obj_member_count; ++i)
5755 abort = abort || set_ref_in_item(mtv + i, copyID,
5756 ht_stack, list_stack);
5757
5758 return abort;
5759}
5760
5761/*
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005762 * Mark all lists, dicts and other container types referenced through typval
5763 * "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005764 * "list_stack" is used to add lists to be marked. Can be NULL.
5765 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5766 *
5767 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005768 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005769 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005770set_ref_in_item(
5771 typval_T *tv,
5772 int copyID,
5773 ht_stack_T **ht_stack,
5774 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005775{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005776 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005777
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005778 switch (tv->v_type)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005779 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005780 case VAR_DICT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005781 return set_ref_in_item_dict(tv->vval.v_dict, copyID,
5782 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005783
5784 case VAR_LIST:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005785 return set_ref_in_item_list(tv->vval.v_list, copyID,
5786 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005787
5788 case VAR_FUNC:
5789 {
5790 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
5791 break;
5792 }
5793
5794 case VAR_PARTIAL:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005795 return set_ref_in_item_partial(tv->vval.v_partial, copyID,
5796 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005797
5798 case VAR_JOB:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005799#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005800 return set_ref_in_item_job(tv->vval.v_job, copyID,
5801 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005802#else
5803 break;
5804#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005805
5806 case VAR_CHANNEL:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005807#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005808 return set_ref_in_item_channel(tv->vval.v_channel, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005809 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005810#else
5811 break;
5812#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005813
5814 case VAR_CLASS:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005815 return set_ref_in_item_class(tv->vval.v_class, copyID,
5816 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005817
5818 case VAR_OBJECT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005819 return set_ref_in_item_object(tv->vval.v_object, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005820 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005821
5822 case VAR_UNKNOWN:
5823 case VAR_ANY:
5824 case VAR_VOID:
5825 case VAR_BOOL:
5826 case VAR_SPECIAL:
5827 case VAR_NUMBER:
5828 case VAR_FLOAT:
5829 case VAR_STRING:
5830 case VAR_BLOB:
5831 case VAR_INSTR:
5832 // Types that do not contain any other item
5833 break;
5834 }
5835
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005836 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005837}
5838
Bram Moolenaar8c711452005-01-14 21:53:12 +00005839/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005840 * Return a string with the string representation of a variable.
5841 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005842 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005843 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005844 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005845 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005846 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005847 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5848 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005849 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005850 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005851 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005852echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005853 typval_T *tv,
5854 char_u **tofree,
5855 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005856 int copyID,
5857 int echo_style,
5858 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005859 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005860{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005861 static int recurse = 0;
5862 char_u *r = NULL;
5863
Bram Moolenaar33570922005-01-25 22:26:29 +00005864 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005865 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005866 if (!did_echo_string_emsg)
5867 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005868 // Only give this message once for a recursive call to avoid
5869 // flooding the user with errors. And stop iterating over lists
5870 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005871 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005872 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005873 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005874 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005875 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005876 }
5877 ++recurse;
5878
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005879 switch (tv->v_type)
5880 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005881 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005882 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005883 {
5884 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005885 r = tv->vval.v_string;
5886 if (r == NULL)
5887 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005888 }
5889 else
5890 {
5891 *tofree = string_quote(tv->vval.v_string, FALSE);
5892 r = *tofree;
5893 }
5894 break;
5895
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005896 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005897 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005898 char_u buf[MAX_FUNC_NAME_LEN];
5899
5900 if (echo_style)
5901 {
LemonBoya5d35902022-04-29 21:15:02 +01005902 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5903 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005904 buf, MAX_FUNC_NAME_LEN);
5905 if (r == buf)
5906 {
5907 r = vim_strsave(buf);
5908 *tofree = r;
5909 }
5910 else
5911 *tofree = NULL;
5912 }
5913 else
5914 {
5915 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5916 : make_ufunc_name_readable(
5917 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5918 TRUE);
5919 r = *tofree;
5920 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005921 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005922 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005923
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005924 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005925 {
5926 partial_T *pt = tv->vval.v_partial;
5927 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005928 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005929 garray_T ga;
5930 int i;
5931 char_u *tf;
5932
5933 ga_init2(&ga, 1, 100);
5934 ga_concat(&ga, (char_u *)"function(");
5935 if (fname != NULL)
5936 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005937 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005938 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005939 && vim_isupper(fname[1]))
5940 {
5941 ga_concat(&ga, (char_u *)"'g:");
5942 ga_concat(&ga, fname + 1);
5943 }
5944 else
5945 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005946 vim_free(fname);
5947 }
5948 if (pt != NULL && pt->pt_argc > 0)
5949 {
5950 ga_concat(&ga, (char_u *)", [");
5951 for (i = 0; i < pt->pt_argc; ++i)
5952 {
5953 if (i > 0)
5954 ga_concat(&ga, (char_u *)", ");
5955 ga_concat(&ga,
5956 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5957 vim_free(tf);
5958 }
5959 ga_concat(&ga, (char_u *)"]");
5960 }
5961 if (pt != NULL && pt->pt_dict != NULL)
5962 {
5963 typval_T dtv;
5964
5965 ga_concat(&ga, (char_u *)", ");
5966 dtv.v_type = VAR_DICT;
5967 dtv.vval.v_dict = pt->pt_dict;
5968 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5969 vim_free(tf);
5970 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005971 // terminate with ')' and a NUL
5972 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005973
5974 *tofree = ga.ga_data;
5975 r = *tofree;
5976 break;
5977 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005978
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005979 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005980 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005981 break;
5982
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005984 if (tv->vval.v_list == NULL)
5985 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005986 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005987 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005988 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005989 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005990 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5991 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005992 {
5993 *tofree = NULL;
5994 r = (char_u *)"[...]";
5995 }
5996 else
5997 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005998 int old_copyID = tv->vval.v_list->lv_copyID;
5999
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006000 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006001 *tofree = list2string(tv, copyID, restore_copyID);
6002 if (restore_copyID)
6003 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006004 r = *tofree;
6005 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006006 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006007
Bram Moolenaar8c711452005-01-14 21:53:12 +00006008 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006009 if (tv->vval.v_dict == NULL)
6010 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006011 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006012 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006013 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006014 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006015 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6016 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006017 {
6018 *tofree = NULL;
6019 r = (char_u *)"{...}";
6020 }
6021 else
6022 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006023 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006024
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006025 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006026 *tofree = dict2string(tv, copyID, restore_copyID);
6027 if (restore_copyID)
6028 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006029 r = *tofree;
6030 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006031 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006032
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006033 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006034 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006035 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006036 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006037 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006038 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006039 break;
6040
Bram Moolenaar835dc632016-02-07 14:27:38 +01006041 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006042 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006043#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00006044 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02006045 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
6046 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006047 if (composite_val)
6048 {
6049 *tofree = string_quote(r, FALSE);
6050 r = *tofree;
6051 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006052#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006053 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006054
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006055 case VAR_INSTR:
6056 *tofree = NULL;
6057 r = (char_u *)"instructions";
6058 break;
6059
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006060 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006061 {
6062 class_T *cl = tv->vval.v_class;
6063 size_t len = 6 + (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
6064 r = *tofree = alloc(len);
6065 vim_snprintf((char *)r, len, "class %s",
6066 cl == NULL ? "[unknown]" : (char *)cl->class_name);
6067 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006068 break;
6069
6070 case VAR_OBJECT:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006071 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006072 garray_T ga;
6073 ga_init2(&ga, 1, 50);
6074 ga_concat(&ga, (char_u *)"object of ");
6075 object_T *obj = tv->vval.v_object;
6076 class_T *cl = obj == NULL ? NULL : obj->obj_class;
6077 ga_concat(&ga, cl == NULL ? (char_u *)"[unknown]"
6078 : cl->class_name);
6079 if (cl != NULL)
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006080 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006081 ga_concat(&ga, (char_u *)" {");
6082 for (int i = 0; i < cl->class_obj_member_count; ++i)
6083 {
6084 if (i > 0)
6085 ga_concat(&ga, (char_u *)", ");
Bram Moolenaard505d172022-12-18 21:42:55 +00006086 ocmember_T *m = &cl->class_obj_members[i];
6087 ga_concat(&ga, m->ocm_name);
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006088 ga_concat(&ga, (char_u *)": ");
6089 char_u *tf = NULL;
6090 ga_concat(&ga, echo_string_core(
6091 (typval_T *)(obj + 1) + i,
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006092 &tf, numbuf, copyID, echo_style,
6093 restore_copyID, composite_val));
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006094 vim_free(tf);
6095 }
6096 ga_concat(&ga, (char_u *)"}");
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006097 }
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006098
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006099 *tofree = r = ga.ga_data;
6100 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006101 break;
6102
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006103 case VAR_FLOAT:
6104 *tofree = NULL;
6105 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6106 r = numbuf;
6107 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006108
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006109 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006110 case VAR_SPECIAL:
6111 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006112 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006113 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006114 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006115
Bram Moolenaar8502c702014-06-17 12:51:16 +02006116 if (--recurse == 0)
6117 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006118 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006119}
6120
6121/*
6122 * Return a string with the string representation of a variable.
6123 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6124 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006125 * Does not put quotes around strings, as ":echo" displays values.
6126 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6127 * May return NULL.
6128 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006129 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006130echo_string(
6131 typval_T *tv,
6132 char_u **tofree,
6133 char_u *numbuf,
6134 int copyID)
6135{
6136 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6137}
6138
6139/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006140 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6141 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006142 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006143 */
6144 int
6145buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6146{
6147 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006148 char_u *t;
6149 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006150
6151 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6152 return -1;
6153
6154 if (lnum > buf->b_ml.ml_line_count)
6155 lnum = buf->b_ml.ml_line_count;
6156
6157 str = ml_get_buf(buf, lnum, FALSE);
6158 if (str == NULL)
6159 return -1;
6160
6161 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006162 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006163
Bram Moolenaar91458462021-01-13 20:08:38 +01006164 // count the number of characters
6165 t = str;
6166 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6167 t += mb_ptr2len(t);
6168
6169 // In insert mode, when the cursor is at the end of a non-empty line,
6170 // byteidx points to the NUL character immediately past the end of the
6171 // string. In this case, add one to the character count.
6172 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6173 count++;
6174
6175 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006176}
6177
6178/*
6179 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006180 * byte index. Works only for loaded buffers. Returns -1 on failure.
6181 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006182 */
6183 int
6184buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6185{
6186 char_u *str;
6187 char_u *t;
6188
6189 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6190 return -1;
6191
6192 if (lnum > buf->b_ml.ml_line_count)
6193 lnum = buf->b_ml.ml_line_count;
6194
6195 str = ml_get_buf(buf, lnum, FALSE);
6196 if (str == NULL)
6197 return -1;
6198
6199 // Convert the character offset to a byte offset
6200 t = str;
6201 while (*t != NUL && --charidx > 0)
6202 t += mb_ptr2len(t);
6203
Bram Moolenaar91458462021-01-13 20:08:38 +01006204 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006205}
6206
6207/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006209 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006211 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006212var2fpos(
6213 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006214 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006215 int *fnum, // set to fnum for '0, 'A, etc.
6216 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006218 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006220 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006222 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006223 if (varp->v_type == VAR_LIST)
6224 {
6225 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006226 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006227 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006228 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006229
6230 l = varp->vval.v_list;
6231 if (l == NULL)
6232 return NULL;
6233
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006234 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006235 pos.lnum = list_find_nr(l, 0L, &error);
6236 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006237 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006238 if (charcol)
6239 len = (long)mb_charlen(ml_get(pos.lnum));
6240 else
6241 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006242
Bram Moolenaarec65d772020-08-20 22:29:12 +02006243 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006244 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006245 li = list_find(l, 1L);
6246 if (li != NULL && li->li_tv.v_type == VAR_STRING
6247 && li->li_tv.vval.v_string != NULL
6248 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006249 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006250 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006251 }
6252 else
6253 {
6254 pos.col = list_find_nr(l, 1L, &error);
6255 if (error)
6256 return NULL;
6257 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006258
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006259 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006260 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006261 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006262 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006263
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006264 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006265 pos.coladd = list_find_nr(l, 2L, &error);
6266 if (error)
6267 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006268
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006269 return &pos;
6270 }
6271
Bram Moolenaarc5809432021-03-27 21:23:30 +01006272 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6273 return NULL;
6274
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006275 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006276 if (name == NULL)
6277 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006278
6279 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006280 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006281 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006282 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006283 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006284 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006285 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006286 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006287 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006288 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006289 pos = VIsual;
6290 else
6291 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006292 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006293 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006294 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006296 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006297 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6299 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006300 pos = *pp;
6301 }
6302 if (pos.lnum != 0)
6303 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006304 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006305 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6306 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006308
Bram Moolenaara5525202006-03-02 22:52:09 +00006309 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006310
Bram Moolenaar477933c2007-07-17 14:32:23 +00006311 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006312 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006313 // the "w_valid" flags are not reset when moving the cursor, but they
6314 // do matter for update_topline() and validate_botline().
6315 check_cursor_moved(curwin);
6316
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006317 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006318 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006319 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006320 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006321 // In silent Ex mode topline is zero, but that's not a valid line
6322 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006323 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006324 return &pos;
6325 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006326 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006327 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006328 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006329 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006330 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006331 return &pos;
6332 }
6333 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006334 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006336 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 {
6338 pos.lnum = curbuf->b_ml.ml_line_count;
6339 pos.col = 0;
6340 }
6341 else
6342 {
6343 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006344 if (charcol)
6345 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6346 else
6347 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 }
6349 return &pos;
6350 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006351 if (in_vim9script())
6352 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 return NULL;
6354}
6355
6356/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006357 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006358 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006359 * Note that the column is passed on as-is, the caller may want to decrement
6360 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006361 * If "charcol" is TRUE use the column as the character index instead of the
6362 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006363 * Return FAIL when conversion is not possible, doesn't check the position for
6364 * validity.
6365 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006366 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006367list2fpos(
6368 typval_T *arg,
6369 pos_T *posp,
6370 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006371 colnr_T *curswantp,
6372 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006373{
6374 list_T *l = arg->vval.v_list;
6375 long i = 0;
6376 long n;
6377
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006378 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6379 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006380 if (arg->v_type != VAR_LIST
6381 || l == NULL
6382 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006383 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006384 return FAIL;
6385
6386 if (fnump != NULL)
6387 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006388 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006389 if (n < 0)
6390 return FAIL;
6391 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006392 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006393 *fnump = n;
6394 }
6395
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006396 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006397 if (n < 0)
6398 return FAIL;
6399 posp->lnum = n;
6400
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006401 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006402 if (n < 0)
6403 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006404 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006405 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006406 if (charcol)
6407 {
6408 buf_T *buf;
6409
6410 // Get the text for the specified line in a loaded buffer
6411 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6412 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6413 return FAIL;
6414
Bram Moolenaar79f23442022-10-10 12:42:57 +01006415 n = buf_charidx_to_byteidx(buf,
6416 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006417 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006418 posp->col = n;
6419
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006420 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006421 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006422 posp->coladd = 0;
6423 else
6424 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006425
Bram Moolenaar493c1782014-05-28 14:34:46 +02006426 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006427 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006428
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006429 return OK;
6430}
6431
6432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 * Get the length of an environment variable name.
6434 * Advance "arg" to the first character after the name.
6435 * Return 0 for error.
6436 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006437 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006438get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439{
6440 char_u *p;
6441 int len;
6442
6443 for (p = *arg; vim_isIDc(*p); ++p)
6444 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006445 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 return 0;
6447
6448 len = (int)(p - *arg);
6449 *arg = p;
6450 return len;
6451}
6452
6453/*
6454 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006455 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 * Return 0 if something is wrong.
6457 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006458 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006459get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460{
6461 char_u *p;
6462 int len;
6463
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006464 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006466 {
6467 if (*p == ':')
6468 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006469 // "s:" is start of "s:var", but "n:" is not and can be used in
6470 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006471 len = (int)(p - *arg);
6472 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6473 || len > 1)
6474 break;
6475 }
6476 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006477 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 return 0;
6479
6480 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006481 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482
6483 return len;
6484}
6485
6486/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006487 * Get the length of the name of a variable or function.
6488 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006490 * Return -1 if curly braces expansion failed.
6491 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 * If the name contains 'magic' {}'s, expand them and return the
6493 * expanded name in an allocated string via 'alias' - caller must free.
6494 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006495 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006496get_name_len(
6497 char_u **arg,
6498 char_u **alias,
6499 int evaluate,
6500 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501{
6502 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 char_u *p;
6504 char_u *expr_start;
6505 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006507 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508
6509 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6510 && (*arg)[2] == (int)KE_SNR)
6511 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006512 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 *arg += 3;
6514 return get_id_len(arg) + 3;
6515 }
6516 len = eval_fname_script(*arg);
6517 if (len > 0)
6518 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006519 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 *arg += len;
6521 }
6522
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006524 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006526 p = find_name_end(*arg, &expr_start, &expr_end,
6527 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 if (expr_start != NULL)
6529 {
6530 char_u *temp_string;
6531
6532 if (!evaluate)
6533 {
6534 len += (int)(p - *arg);
6535 *arg = skipwhite(p);
6536 return len;
6537 }
6538
6539 /*
6540 * Include any <SID> etc in the expanded string:
6541 * Thus the -len here.
6542 */
6543 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6544 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006545 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 *alias = temp_string;
6547 *arg = skipwhite(p);
6548 return (int)STRLEN(temp_string);
6549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550
6551 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006552 // Only give an error when there is something, otherwise it will be
6553 // reported at a higher level.
6554 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006555 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556
6557 return len;
6558}
6559
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006560/*
6561 * Find the end of a variable or function name, taking care of magic braces.
6562 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6563 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006564 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006565 * Return a pointer to just after the name. Equal to "arg" if there is no
6566 * valid name.
6567 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006568 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006569find_name_end(
6570 char_u *arg,
6571 char_u **expr_start,
6572 char_u **expr_end,
6573 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006575 int mb_nest = 0;
6576 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006578 int len;
zeertzjqa93d9cd2023-05-02 16:25:47 +01006579 int allow_curly = (flags & FNE_ALLOW_CURLY) || !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006581 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006583 *expr_start = NULL;
6584 *expr_end = NULL;
6585 }
6586
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006587 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006588 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006589 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006590 return arg;
6591
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006592 for (p = arg; *p != NUL
6593 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006594 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006595 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006596 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006597 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006598 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006599 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006600 if (*p == '\'')
6601 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006602 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006603 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006604 ;
6605 if (*p == NUL)
6606 break;
6607 }
6608 else if (*p == '"')
6609 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006610 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006611 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006612 if (*p == '\\' && p[1] != NUL)
6613 ++p;
6614 if (*p == NUL)
6615 break;
6616 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006617 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6618 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006619 // "s:" is start of "s:var", but "n:" is not and can be used in
6620 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006621 len = (int)(p - arg);
6622 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006623 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006624 break;
6625 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006626
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006627 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006629 if (*p == '[')
6630 ++br_nest;
6631 else if (*p == ']')
6632 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006634
zeertzjqa93d9cd2023-05-02 16:25:47 +01006635 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006637 if (*p == '{')
6638 {
6639 mb_nest++;
6640 if (expr_start != NULL && *expr_start == NULL)
6641 *expr_start = p;
6642 }
6643 else if (*p == '}')
6644 {
6645 mb_nest--;
6646 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6647 *expr_end = p;
6648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 }
6651
6652 return p;
6653}
6654
6655/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006656 * Expands out the 'magic' {}'s in a variable/function name.
6657 * Note that this can call itself recursively, to deal with
6658 * constructs like foo{bar}{baz}{bam}
6659 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6660 * "in_start" ^
6661 * "expr_start" ^
6662 * "expr_end" ^
6663 * "in_end" ^
6664 *
6665 * Returns a new allocated string, which the caller must free.
6666 * Returns NULL for failure.
6667 */
6668 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006669make_expanded_name(
6670 char_u *in_start,
6671 char_u *expr_start,
6672 char_u *expr_end,
6673 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006674{
6675 char_u c1;
6676 char_u *retval = NULL;
6677 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006678
6679 if (expr_end == NULL || in_end == NULL)
6680 return NULL;
6681 *expr_start = NUL;
6682 *expr_end = NUL;
6683 c1 = *in_end;
6684 *in_end = NUL;
6685
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006686 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006687 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006688 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006689 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6690 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006691 if (retval != NULL)
6692 {
6693 STRCPY(retval, in_start);
6694 STRCAT(retval, temp_result);
6695 STRCAT(retval, expr_end + 1);
6696 }
6697 }
6698 vim_free(temp_result);
6699
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006700 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006701 *expr_start = '{';
6702 *expr_end = '}';
6703
6704 if (retval != NULL)
6705 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006706 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006707 if (expr_start != NULL)
6708 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006709 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006710 temp_result = make_expanded_name(retval, expr_start,
6711 expr_end, temp_result);
6712 vim_free(retval);
6713 retval = temp_result;
6714 }
6715 }
6716
6717 return retval;
6718}
6719
6720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006722 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006724 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006725eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006727 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006728}
6729
6730/*
6731 * Return TRUE if character "c" can be used as the first character in a
6732 * variable or function name (excluding '{' and '}').
6733 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006734 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006735eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006736{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006737 return ASCII_ISALPHA(c) || c == '_';
6738}
6739
6740/*
6741 * Return TRUE if character "c" can be used as the first character of a
6742 * dictionary key.
6743 */
6744 int
6745eval_isdictc(int c)
6746{
6747 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748}
6749
6750/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006751 * Handle:
6752 * - expr[expr], expr[expr:expr] subscript
6753 * - ".name" lookup
6754 * - function call with Funcref variable: func(expr)
6755 * - method call: var->method()
6756 *
6757 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006758 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006759 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006760 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006761handle_subscript(
6762 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006763 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006764 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006765 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006766 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006767{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006768 int evaluate = evalarg != NULL
6769 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006770 int ret = OK;
6771 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006772 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006773 int getnext;
6774 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006775
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006776 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006777 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006778 // When at the end of the line and ".name" or "->{" or "->X" follows in
6779 // the next line then consume the line break.
6780 p = eval_next_non_blank(*arg, evalarg, &getnext);
6781 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00006782 && ((*p == '.'
6783 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
6784 || rettv->v_type == VAR_CLASS
6785 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02006786 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6787 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6788 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006789 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006790 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006791 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006792 check_white = FALSE;
6793 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006794
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006795 if (rettv->v_type == VAR_ANY)
6796 {
6797 char_u *exp_name;
6798 int cc;
6799 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006800 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006801 type_T *type;
6802
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006803 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006804 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006805 if (**arg != '.')
6806 {
6807 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006808 semsg(_(e_expected_dot_after_name_str),
6809 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006810 ret = FAIL;
6811 break;
6812 }
6813 ++*arg;
6814 if (IS_WHITE_OR_NUL(**arg))
6815 {
6816 if (verbose)
6817 emsg(_(e_no_white_space_allowed_after_dot));
6818 ret = FAIL;
6819 break;
6820 }
6821
6822 // isolate the name
6823 exp_name = *arg;
6824 while (eval_isnamec(**arg))
6825 ++*arg;
6826 cc = **arg;
6827 **arg = NUL;
6828
6829 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01006830 evalarg == NULL ? NULL : evalarg->eval_cctx,
6831 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006832 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006833
6834 if (idx < 0 && ufunc == NULL)
6835 {
6836 ret = FAIL;
6837 break;
6838 }
6839 if (idx >= 0)
6840 {
6841 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6842 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6843
6844 copy_tv(sv->sv_tv, rettv);
6845 }
6846 else
6847 {
6848 rettv->v_type = VAR_FUNC;
6849 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6850 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006851 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006852 }
6853
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006854 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6855 || rettv->v_type == VAR_PARTIAL))
6856 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006857 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006858 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6859 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006860
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006861 // Stop the expression evaluation when immediately aborting on
6862 // error, or when an interrupt occurred or an exception was thrown
6863 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006864 if (aborting())
6865 {
6866 if (ret == OK)
6867 clear_tv(rettv);
6868 ret = FAIL;
6869 }
6870 dict_unref(selfdict);
6871 selfdict = NULL;
6872 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006873 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006874 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006875 if (in_vim9script())
6876 *arg = skipwhite(p + 2);
6877 else
6878 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00006879 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006880 {
zeertzjqc481ad32023-03-11 16:18:51 +00006881 emsg(_(e_no_white_space_allowed_before_parenthesis));
6882 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006883 }
zeertzjqc481ad32023-03-11 16:18:51 +00006884 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
6885 // expr->{lambda}() or expr->(lambda)()
6886 ret = eval_lambda(arg, rettv, evalarg, verbose);
6887 else
6888 // expr->name()
6889 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02006890 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006891 // "." is ".name" lookup when we found a dict or when evaluating and
6892 // scriptversion is at least 2, where string concatenation is "..".
6893 else if (**arg == '['
6894 || (**arg == '.' && (rettv->v_type == VAR_DICT
6895 || (!evaluate
6896 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006897 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006898 {
6899 dict_unref(selfdict);
6900 if (rettv->v_type == VAR_DICT)
6901 {
6902 selfdict = rettv->vval.v_dict;
6903 if (selfdict != NULL)
6904 ++selfdict->dv_refcount;
6905 }
6906 else
6907 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006908 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006909 {
6910 clear_tv(rettv);
6911 ret = FAIL;
6912 }
6913 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006914 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
6915 || rettv->v_type == VAR_OBJECT))
6916 {
6917 // class member: SomeClass.varname
6918 // class method: SomeClass.SomeMethod()
6919 // class constructor: SomeClass.new()
6920 // object member: someObject.varname
6921 // object method: someObject.SomeMethod()
6922 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
6923 {
6924 clear_tv(rettv);
6925 ret = FAIL;
6926 }
6927 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006928 else
6929 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006930 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006931
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006932 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6933 // Don't do this when "Func" is already a partial that was bound
6934 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006935 if (selfdict != NULL
6936 && (rettv->v_type == VAR_FUNC
6937 || (rettv->v_type == VAR_PARTIAL
6938 && (rettv->vval.v_partial->pt_auto
6939 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006940 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006941
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006942 dict_unref(selfdict);
6943 return ret;
6944}
6945
6946/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006947 * Make a copy of an item.
6948 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006949 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006950 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6951 * reference to an already copied list/dict can be used.
6952 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006953 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006954 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006955item_copy(
6956 typval_T *from,
6957 typval_T *to,
6958 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006959 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006960 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006961{
6962 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006963 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006964
Bram Moolenaar33570922005-01-25 22:26:29 +00006965 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006966 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006967 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006968 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006969 }
6970 ++recurse;
6971
6972 switch (from->v_type)
6973 {
6974 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006975 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006976 case VAR_STRING:
6977 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006978 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006979 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006980 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006981 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006982 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006983 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006984 case VAR_CLASS:
6985 case VAR_OBJECT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006986 copy_tv(from, to);
6987 break;
6988 case VAR_LIST:
6989 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006990 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006991 if (from->vval.v_list == NULL)
6992 to->vval.v_list = NULL;
6993 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6994 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006995 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006996 to->vval.v_list = from->vval.v_list->lv_copylist;
6997 ++to->vval.v_list->lv_refcount;
6998 }
6999 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007000 to->vval.v_list = list_copy(from->vval.v_list,
7001 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007002 if (to->vval.v_list == NULL)
7003 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007004 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007005 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007006 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007007 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007008 case VAR_DICT:
7009 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007010 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007011 if (from->vval.v_dict == NULL)
7012 to->vval.v_dict = NULL;
7013 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7014 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007015 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007016 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7017 ++to->vval.v_dict->dv_refcount;
7018 }
7019 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007020 to->vval.v_dict = dict_copy(from->vval.v_dict,
7021 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007022 if (to->vval.v_dict == NULL)
7023 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007024 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007025 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007026 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007027 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007028 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007029 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007030 }
7031 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007032 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007033}
7034
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007035 void
7036echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7037{
7038 char_u *tofree;
7039 char_u numbuf[NUMBUFLEN];
7040 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7041
7042 if (*atstart)
7043 {
7044 *atstart = FALSE;
7045 // Call msg_start() after eval1(), evaluating the expression
7046 // may cause a message to appear.
7047 if (with_space)
7048 {
7049 // Mark the saved text as finishing the line, so that what
7050 // follows is displayed on a new line when scrolling back
7051 // at the more prompt.
7052 msg_sb_eol();
7053 msg_start();
7054 }
7055 }
7056 else if (with_space)
7057 msg_puts_attr(" ", echo_attr);
7058
7059 if (p != NULL)
7060 for ( ; *p != NUL && !got_int; ++p)
7061 {
7062 if (*p == '\n' || *p == '\r' || *p == TAB)
7063 {
7064 if (*p != TAB && *needclr)
7065 {
7066 // remove any text still there from the command
7067 msg_clr_eos();
7068 *needclr = FALSE;
7069 }
7070 msg_putchar_attr(*p, echo_attr);
7071 }
7072 else
7073 {
7074 if (has_mbyte)
7075 {
7076 int i = (*mb_ptr2len)(p);
7077
7078 (void)msg_outtrans_len_attr(p, i, echo_attr);
7079 p += i - 1;
7080 }
7081 else
7082 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7083 }
7084 }
7085 vim_free(tofree);
7086}
7087
Bram Moolenaare9a41262005-01-15 22:18:47 +00007088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 * ":echo expr1 ..." print each argument separated with a space, add a
7090 * newline at the end.
7091 * ":echon expr1 ..." print each argument plain.
7092 */
7093 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007094ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095{
7096 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007097 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007098 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 int needclr = TRUE;
7100 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007101 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007102 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007103 evalarg_T evalarg;
7104
Bram Moolenaare707c882020-07-01 13:07:37 +02007105 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106
7107 if (eap->skip)
7108 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007109 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007111 // If eval1() causes an error message the text from the command may
7112 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007113 need_clr_eos = needclr;
7114
Bram Moolenaar68db9962021-05-09 23:19:22 +02007115 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007116 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 {
7118 /*
7119 * Report the invalid expression unless the expression evaluation
7120 * has been cancelled due to an aborting error, an interrupt, or an
7121 * exception.
7122 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007123 if (!aborting() && did_emsg == did_emsg_before
7124 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007125 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007126 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 break;
7128 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007129 need_clr_eos = FALSE;
7130
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007132 {
7133 if (rettv.v_type == VAR_VOID)
7134 {
7135 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7136 break;
7137 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007138 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007139 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007140
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007141 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 arg = skipwhite(arg);
7143 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007144 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007145 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146
7147 if (eap->skip)
7148 --emsg_skip;
7149 else
7150 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007151 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 if (needclr)
7153 msg_clr_eos();
7154 if (eap->cmdidx == CMD_echo)
7155 msg_end();
7156 }
7157}
7158
7159/*
7160 * ":echohl {name}".
7161 */
7162 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007163ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007165 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166}
7167
7168/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007169 * Returns the :echo attribute
7170 */
7171 int
7172get_echo_attr(void)
7173{
7174 return echo_attr;
7175}
7176
7177/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 * ":execute expr1 ..." execute the result of an expression.
7179 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007180 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007182 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 * Each gets spaces around each argument and a newline at the end for
7184 * echo commands
7185 */
7186 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007187ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188{
7189 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007190 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 int ret = OK;
7192 char_u *p;
7193 garray_T ga;
7194 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007195 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196
7197 ga_init2(&ga, 1, 80);
7198
7199 if (eap->skip)
7200 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007201 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007203 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007204 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206
7207 if (!eap->skip)
7208 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007209 char_u buf[NUMBUFLEN];
7210
7211 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007212 {
7213 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7214 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007215 semsg(_(e_using_invalid_value_as_string_str),
7216 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007217 p = NULL;
7218 }
7219 else
7220 p = tv_get_string_buf(&rettv, buf);
7221 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007222 else
7223 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007224 if (p == NULL)
7225 {
7226 clear_tv(&rettv);
7227 ret = FAIL;
7228 break;
7229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 len = (int)STRLEN(p);
7231 if (ga_grow(&ga, len + 2) == FAIL)
7232 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007233 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 ret = FAIL;
7235 break;
7236 }
7237 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 ga.ga_len += len;
7241 }
7242
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007243 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 arg = skipwhite(arg);
7245 }
7246
7247 if (ret != FAIL && ga.ga_data != NULL)
7248 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007249 // use the first line of continuation lines for messages
7250 SOURCING_LNUM = start_lnum;
7251
Bram Moolenaar37fef162022-08-29 18:16:32 +01007252 if (eap->cmdidx == CMD_echomsg
7253 || eap->cmdidx == CMD_echowindow
7254 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007255 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007256 // Mark the already saved text as finishing the line, so that what
7257 // follows is displayed on a new line when scrolling back at the
7258 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007259 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007260 }
7261
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007262 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007263 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007264 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007265 out_flush();
7266 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007267 else if (eap->cmdidx == CMD_echowindow)
7268 {
7269#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007270 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007271#endif
7272 msg_attr(ga.ga_data, echo_attr);
7273#ifdef HAS_MESSAGE_WINDOW
7274 end_echowindow();
7275#endif
7276 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007277 else if (eap->cmdidx == CMD_echoconsole)
7278 {
7279 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7280 ui_write((char_u *)"\r\n", 2, TRUE);
7281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 else if (eap->cmdidx == CMD_echoerr)
7283 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007284 int save_did_emsg = did_emsg;
7285
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007286 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007287 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 if (!force_abort)
7289 did_emsg = save_did_emsg;
7290 }
7291 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007292 {
7293 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7294
7295 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7296 sticky_cmdmod_flags = cmdmod.cmod_flags
7297 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 do_cmdline((char_u *)ga.ga_data,
7299 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007300 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 }
7303
7304 ga_clear(&ga);
7305
7306 if (eap->skip)
7307 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007308 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309}
7310
7311/*
7312 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7313 * "arg" points to the "&" or '+' when called, to "option" when returning.
7314 * Returns NULL when no option name found. Otherwise pointer to the char
7315 * after the option name.
7316 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007317 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007318find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319{
7320 char_u *p = *arg;
7321
7322 ++p;
7323 if (*p == 'g' && p[1] == ':')
7324 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007325 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 p += 2;
7327 }
7328 else if (*p == 'l' && p[1] == ':')
7329 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007330 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 p += 2;
7332 }
7333 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007334 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335
7336 if (!ASCII_ISALPHA(*p))
7337 return NULL;
7338 *arg = p;
7339
7340 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007341 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 else
7343 while (ASCII_ISALPHA(*p))
7344 ++p;
7345 return p;
7346}
7347
7348/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007349 * Display script name where an item was last set.
7350 * Should only be invoked when 'verbose' is non-zero.
7351 */
7352 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007353last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007354{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007355 char_u *p;
7356
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007357 if (script_ctx.sc_sid == 0)
7358 return;
7359
7360 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7361 if (p == NULL)
7362 return;
7363
7364 verbose_enter();
7365 msg_puts(_("\n\tLast set from "));
7366 msg_puts((char *)p);
7367 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007368 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007369 msg_puts(_(line_msg));
7370 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007371 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007372 verbose_leave();
7373 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007374}
7375
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007376#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377
7378/*
7379 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007380 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 * "flags" can be "g" to do a global substitute.
7382 * Returns an allocated string, NULL for error.
7383 */
7384 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007385do_string_sub(
7386 char_u *str,
7387 char_u *pat,
7388 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007389 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007390 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391{
7392 int sublen;
7393 regmatch_T regmatch;
7394 int i;
7395 int do_all;
7396 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007397 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 garray_T ga;
7399 char_u *ret;
7400 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007401 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007403 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007405 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406
7407 ga_init2(&ga, 1, 200);
7408
7409 do_all = (flags[0] == 'g');
7410
7411 regmatch.rm_ic = p_ic;
7412 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7413 if (regmatch.regprog != NULL)
7414 {
7415 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007416 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7418 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007419 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007420 if (regmatch.startp[0] == regmatch.endp[0])
7421 {
7422 if (zero_width == regmatch.startp[0])
7423 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007424 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007425 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007426 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7427 (size_t)i);
7428 ga.ga_len += i;
7429 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007430 continue;
7431 }
7432 zero_width = regmatch.startp[0];
7433 }
7434
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 /*
7436 * Get some space for a temporary buffer to do the substitution
7437 * into. It will contain:
7438 * - The text up to where the match is.
7439 * - The substituted text.
7440 * - The text after the match.
7441 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007442 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007443 if (sublen <= 0)
7444 {
7445 ga_clear(&ga);
7446 break;
7447 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007448 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7450 {
7451 ga_clear(&ga);
7452 break;
7453 }
7454
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007455 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 i = (int)(regmatch.startp[0] - tail);
7457 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007458 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007459 (void)vim_regsub(&regmatch, sub, expr,
7460 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7461 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007463 tail = regmatch.endp[0];
7464 if (*tail == NUL)
7465 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 if (!do_all)
7467 break;
7468 }
7469
7470 if (ga.ga_data != NULL)
7471 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7472
Bram Moolenaar473de612013-06-08 18:19:48 +02007473 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 }
7475
7476 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7477 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007478 if (p_cpo == empty_option)
7479 p_cpo = save_cpo;
7480 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007481 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007482 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007483 // If it's still empty it was changed and restored, need to restore in
7484 // the complicated way.
7485 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007486 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007487 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489
7490 return ret;
7491}