blob: 93b840e61ee1bcca116e1dac6cab9244f98af9f6 [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);
Bram Moolenaard505d172022-12-18 21:42:55 +00001223 else if (v_type == VAR_BLOB
Bram Moolenaare65081d2021-06-27 15:04:05 +02001224 && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001225 r = rettv_blob_alloc(lp->ll_tv);
1226 if (r == FAIL)
1227 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001228
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001229 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001230 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001231 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001232 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001233 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001234 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001235
Bram Moolenaar4525a572022-02-13 11:57:33 +00001236 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001237 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001238 && lp->ll_tv == &v->di_tv
1239 && ht != NULL && ht == get_script_local_ht())
1240 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001241 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001242
1243 // Vim9 script local variable: get the type
1244 if (sv != NULL)
1245 lp->ll_valtype = sv->sv_type;
1246 }
1247
Bram Moolenaar8c711452005-01-14 21:53:12 +00001248 len = -1;
1249 if (*p == '.')
1250 {
1251 key = p + 1;
1252 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1253 ;
1254 if (len == 0)
1255 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001256 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001257 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001258 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001259 }
1260 p = key + len;
1261 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001262 else
1263 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001264 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001265 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001266 if (*p == ':')
1267 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001268 else
1269 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001270 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001271 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001272 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001273 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001274 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001275 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001276 clear_tv(&var1);
1277 return NULL;
1278 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001279 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001280 }
1281
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001282 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001283 if (*p == ':')
1284 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001285 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001286 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001287 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001288 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001289 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001290 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001291 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001292 if (rettv != NULL
1293 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001294 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001295 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001296 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001297 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001298 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001299 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001300 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001301 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001302 }
1303 p = skipwhite(p + 1);
1304 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001305 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001306 else
1307 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001308 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001309 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001310 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001311 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001312 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001313 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001314 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001315 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001316 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001317 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001318 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001319 clear_tv(&var2);
1320 return NULL;
1321 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001322 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001323 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001324 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001325 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001326 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001327
Bram Moolenaar8c711452005-01-14 21:53:12 +00001328 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001329 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001330 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001331 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001332 clear_tv(&var1);
1333 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001334 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001335 }
1336
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001337 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001338 ++p;
1339 }
1340
Bram Moolenaard505d172022-12-18 21:42:55 +00001341 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001342 {
1343 if (len == -1)
1344 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001345 // "[key]": get key from "var1"
1346 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001347 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001348 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001349 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001350 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001351 }
1352 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001353 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001354
1355 // a NULL dict is equivalent with an empty dict
1356 if (lp->ll_tv->vval.v_dict == NULL)
1357 {
1358 lp->ll_tv->vval.v_dict = dict_alloc();
1359 if (lp->ll_tv->vval.v_dict == NULL)
1360 {
1361 clear_tv(&var1);
1362 return NULL;
1363 }
1364 ++lp->ll_tv->vval.v_dict->dv_refcount;
1365 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001366 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001367
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001368 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001369
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001370 // When assigning to a scope dictionary check that a function and
1371 // variable name is valid (only variable name unless it is l: or
1372 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001373 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001374 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001375 int prevval;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001376
1377 if (len != -1)
1378 {
1379 prevval = key[len];
1380 key[len] = NUL;
1381 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001382 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001383 prevval = 0; // avoid compiler warning
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001384 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
zeertzjq91c75d12022-11-05 20:21:58 +00001385 && (rettv->v_type == VAR_FUNC
1386 || rettv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001387 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001388 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001389 if (len != -1)
1390 key[len] = prevval;
1391 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001392 {
1393 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001394 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001395 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001396 }
1397
Bram Moolenaar10c65862020-10-08 21:16:42 +02001398 if (lp->ll_valtype != NULL)
1399 // use the type of the member
1400 lp->ll_valtype = lp->ll_valtype->tt_member;
1401
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001402 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001403 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001404 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001405 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001406 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001407 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001408 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001409 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001410 return NULL;
1411 }
1412
Bram Moolenaar31b81602019-02-10 22:14:27 +01001413 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001414 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001415 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001416 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001417 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001418 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001419 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001420 }
1421 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001422 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001423 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001424 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001425 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001426 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001427 p = NULL;
1428 break;
1429 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001430 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001431 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001432 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1433 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001434 {
1435 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001436 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001437 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001438
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001439 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001440 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001441 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001442 else if (v_type == VAR_BLOB)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001443 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001444 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1445
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001446 /*
1447 * Get the number and item for the only or first index of the List.
1448 */
1449 if (empty1)
1450 lp->ll_n1 = 0;
1451 else
1452 // is number or string
1453 lp->ll_n1 = (long)tv_get_number(&var1);
1454 clear_tv(&var1);
1455
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001456 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001457 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001458 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001459 return NULL;
1460 }
1461 if (lp->ll_range && !lp->ll_empty2)
1462 {
1463 lp->ll_n2 = (long)tv_get_number(&var2);
1464 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001465 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1466 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001467 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001468 }
1469 lp->ll_blob = lp->ll_tv->vval.v_blob;
1470 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001471 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001472 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001473 else if (v_type == VAR_LIST)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001474 {
1475 /*
1476 * Get the number and item for the only or first index of the List.
1477 */
1478 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001479 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001480 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001481 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001482 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001483 clear_tv(&var1);
1484
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001485 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001486 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001487 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1488 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001489 if (lp->ll_li == NULL)
1490 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001491 clear_tv(&var2);
1492 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001493 }
1494
Bram Moolenaar10c65862020-10-08 21:16:42 +02001495 if (lp->ll_valtype != NULL)
1496 // use the type of the member
1497 lp->ll_valtype = lp->ll_valtype->tt_member;
1498
Bram Moolenaar8c711452005-01-14 21:53:12 +00001499 /*
1500 * May need to find the item or absolute index for the second
1501 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001502 * When no index given: "lp->ll_empty2" is TRUE.
1503 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001504 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001505 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001506 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001507 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001508 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001509 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001510 if (check_range_index_two(lp->ll_list,
1511 &lp->ll_n1, lp->ll_li,
1512 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001513 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001514 }
1515
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001516 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001517 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001518 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001519 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001520 class_T *cl = (v_type == VAR_OBJECT
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001521 && lp->ll_tv->vval.v_object != NULL)
1522 ? lp->ll_tv->vval.v_object->obj_class
1523 : lp->ll_tv->vval.v_class;
1524 // TODO: what if class is NULL?
1525 if (cl != NULL)
1526 {
1527 lp->ll_valtype = NULL;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001528
1529 if (flags & GLV_PREFER_FUNC)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001530 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001531 // First look for a function with this name.
1532 // round 1: class functions (skipped for an object)
1533 // round 2: object methods
1534 for (int round = v_type == VAR_OBJECT ? 2 : 1;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001535 round <= 2; ++round)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001536 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001537 int m_idx;
1538 ufunc_T *fp;
1539
1540 fp = method_lookup(cl,
1541 round == 1 ? VAR_CLASS : VAR_OBJECT,
1542 key, p - key, &m_idx);
1543 if (fp != NULL)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001544 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001545 lp->ll_ufunc = fp;
1546 lp->ll_valtype = fp->uf_func_type;
1547 break;
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001548 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001549 }
1550 }
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001551
1552 if (lp->ll_valtype == NULL)
1553 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001554 int m_idx;
1555 ocmember_T *om;
1556
1557 om = member_lookup(cl, v_type, key, p - key, &m_idx);
1558 if (om != NULL)
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001559 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001560 switch (om->ocm_access)
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001561 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001562 case VIM_ACCESS_PRIVATE:
1563 semsg(_(e_cannot_access_private_member_str),
1564 om->ocm_name);
1565 return NULL;
1566 case VIM_ACCESS_READ:
Ernie Rael98e68c02023-09-20 20:13:06 +02001567 // If [idx] or .key following, read only OK.
1568 if (*p == '[' || *p == '.')
1569 break;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001570 if ((flags & GLV_READ_ONLY) == 0)
1571 {
1572 semsg(_(e_member_is_not_writable_str),
1573 om->ocm_name);
1574 return NULL;
1575 }
1576 break;
1577 case VIM_ACCESS_ALL:
1578 break;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001579 }
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001580
1581 lp->ll_valtype = om->ocm_type;
1582
1583 if (v_type == VAR_OBJECT)
1584 lp->ll_tv = ((typval_T *)(
1585 lp->ll_tv->vval.v_object + 1)) + m_idx;
1586 else
1587 lp->ll_tv = &cl->class_members_tv[m_idx];
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001588 }
1589 }
1590
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001591 if (lp->ll_valtype == NULL)
1592 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001593 member_not_found_msg(cl, v_type, key, p - key);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001594 return NULL;
1595 }
1596 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001597 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001598 }
1599
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001600 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001601 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001602 return p;
1603}
1604
1605/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001606 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001607 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001608 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001609clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001610{
1611 vim_free(lp->ll_exp_name);
1612 vim_free(lp->ll_newkey);
1613}
1614
1615/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001616 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001617 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001618 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1619 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001620 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001621 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001622set_var_lval(
1623 lval_T *lp,
1624 char_u *endp,
1625 typval_T *rettv,
1626 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001627 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1628 char_u *op,
1629 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001630{
1631 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001632 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001633
1634 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001635 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001636 cc = *endp;
1637 *endp = NUL;
h-east2bd6a092023-05-19 19:01:17 +01001638 if (in_vim9script() && check_reserved_name(lp->ll_name, FALSE) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001639 return;
1640
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001641 if (lp->ll_blob != NULL)
1642 {
1643 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001644
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001645 if (op != NULL && *op != '=')
1646 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001647 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001648 return;
1649 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001650 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001651 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001652
1653 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1654 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001655 if (lp->ll_empty2)
1656 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1657
Bram Moolenaar68452172021-04-12 21:21:02 +02001658 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1659 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001660 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001661 }
1662 else
1663 {
1664 val = (int)tv_get_number_chk(rettv, &error);
1665 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001666 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001667 }
1668 }
1669 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001670 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001671 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001672
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001673 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1674 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001675 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001676 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001677 *endp = cc;
1678 return;
1679 }
1680
Bram Moolenaarff697e62019-02-12 22:28:33 +01001681 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001682 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001683 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001684 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001685 {
1686 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001687 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1688 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001689 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001690 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001691 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001692 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001693 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001694 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001695 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001696 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001697 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1698 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001699 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001700 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001701 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001702 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001703 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001704 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001705 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001706 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001707 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001708 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001709 else if (lp->ll_range)
1710 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001711 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1712 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001713 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001714 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001715 return;
1716 }
1717
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001718 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1719 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001720 }
1721 else
1722 {
1723 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001724 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001725 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001726 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1727 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001728 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001729 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001730 return;
1731 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001732
1733 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001734 && check_typval_arg_type(lp->ll_valtype, rettv,
1735 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001736 return;
1737
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001738 if (lp->ll_newkey != NULL)
1739 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001740 if (op != NULL && *op != '=')
1741 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001742 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001743 return;
1744 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001745 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1746 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001747 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001748
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001749 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001750 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001751 if (di == NULL)
1752 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001753 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1754 {
1755 vim_free(di);
1756 return;
1757 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001758 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001759 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001760 else if (op != NULL && *op != '=')
1761 {
1762 tv_op(lp->ll_tv, rettv, op);
1763 return;
1764 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001765 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001766 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001767
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001768 /*
1769 * Assign the value to the variable or list item.
1770 */
1771 if (copy)
1772 copy_tv(rettv, lp->ll_tv);
1773 else
1774 {
1775 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001776 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001777 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001778 }
1779 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001780}
1781
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001782/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001783 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1784 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001785 * Returns OK or FAIL.
1786 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001787 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001788tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001789{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001790 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001791 char_u numbuf[NUMBUFLEN];
1792 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001793 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001794
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001795 // Can't do anything with a Funcref or Dict on the right.
1796 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001797 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001798 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1799 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001800 {
1801 switch (tv1->v_type)
1802 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001803 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001804 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001806 case VAR_DICT:
1807 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001808 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001809 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001810 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001811 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001812 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001813 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001814 case VAR_CLASS:
1815 case VAR_OBJECT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001816 break;
1817
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001818 case VAR_BLOB:
1819 if (*op != '+' || tv2->v_type != VAR_BLOB)
1820 break;
1821 // BLOB += BLOB
1822 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1823 {
1824 blob_T *b1 = tv1->vval.v_blob;
1825 blob_T *b2 = tv2->vval.v_blob;
1826 int i, len = blob_len(b2);
1827 for (i = 0; i < len; i++)
1828 ga_append(&b1->bv_ga, blob_get(b2, i));
1829 }
1830 return OK;
1831
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001832 case VAR_LIST:
1833 if (*op != '+' || tv2->v_type != VAR_LIST)
1834 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001835 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001836 if (tv2->vval.v_list != NULL)
1837 {
1838 if (tv1->vval.v_list == NULL)
1839 {
1840 tv1->vval.v_list = tv2->vval.v_list;
1841 ++tv1->vval.v_list->lv_refcount;
1842 }
1843 else
1844 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1845 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001846 return OK;
1847
1848 case VAR_NUMBER:
1849 case VAR_STRING:
1850 if (tv2->v_type == VAR_LIST)
1851 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001852 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001853 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001854 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001855 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001856 if (tv2->v_type == VAR_FLOAT)
1857 {
1858 float_T f = n;
1859
Bram Moolenaarff697e62019-02-12 22:28:33 +01001860 if (*op == '%')
1861 break;
1862 switch (*op)
1863 {
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 case '/': f /= tv2->vval.v_float; break;
1868 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001869 clear_tv(tv1);
1870 tv1->v_type = VAR_FLOAT;
1871 tv1->vval.v_float = f;
1872 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001873 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001874 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001875 switch (*op)
1876 {
1877 case '+': n += tv_get_number(tv2); break;
1878 case '-': n -= tv_get_number(tv2); break;
1879 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001880 case '/': n = num_divide(n, tv_get_number(tv2),
1881 &failed); break;
1882 case '%': n = num_modulus(n, tv_get_number(tv2),
1883 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001884 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001885 clear_tv(tv1);
1886 tv1->v_type = VAR_NUMBER;
1887 tv1->vval.v_number = n;
1888 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001889 }
1890 else
1891 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001892 if (tv2->v_type == VAR_FLOAT)
1893 break;
1894
Bram Moolenaarff697e62019-02-12 22:28:33 +01001895 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001896 s = tv_get_string(tv1);
1897 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001898 clear_tv(tv1);
1899 tv1->v_type = VAR_STRING;
1900 tv1->vval.v_string = s;
1901 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001902 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001903
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001904 case VAR_FLOAT:
1905 {
1906 float_T f;
1907
Bram Moolenaarff697e62019-02-12 22:28:33 +01001908 if (*op == '%' || *op == '.'
1909 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001910 && tv2->v_type != VAR_NUMBER
1911 && tv2->v_type != VAR_STRING))
1912 break;
1913 if (tv2->v_type == VAR_FLOAT)
1914 f = tv2->vval.v_float;
1915 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001916 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001917 switch (*op)
1918 {
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 case '/': tv1->vval.v_float /= f; break;
1923 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001924 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001925 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001926 }
1927 }
1928
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001929 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001930 return FAIL;
1931}
1932
1933/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001934 * Evaluate the expression used in a ":for var in expr" command.
1935 * "arg" points to "var".
1936 * Set "*errp" to TRUE for an error, FALSE otherwise;
1937 * Return a pointer that holds the info. Null when there is an error.
1938 */
1939 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001940eval_for_line(
1941 char_u *arg,
1942 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001943 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001944 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001945{
Bram Moolenaar33570922005-01-25 22:26:29 +00001946 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001947 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001949 typval_T tv;
1950 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001951 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001952
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001953 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001954
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001955 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956 if (fi == NULL)
1957 return NULL;
1958
Bram Moolenaar404557e2021-07-05 21:41:48 +02001959 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1960 &fi->fi_semicolon, FALSE);
1961 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001962 return fi;
1963
Bram Moolenaar404557e2021-07-05 21:41:48 +02001964 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001965 if (expr[0] != 'i' || expr[1] != 'n'
1966 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001967 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001968 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1969 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1970 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001971 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001972 return fi;
1973 }
1974
1975 if (skip)
1976 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001977 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1978 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001979 {
1980 *errp = FALSE;
1981 if (!skip)
1982 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001983 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001984 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001985 l = tv.vval.v_list;
1986 if (l == NULL)
1987 {
1988 // a null list is like an empty list: do nothing
1989 clear_tv(&tv);
1990 }
1991 else
1992 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001994 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001995
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001996 // No need to increment the refcount, it's already set for
1997 // the list being used in "tv".
1998 fi->fi_list = l;
1999 list_add_watch(l, &fi->fi_lw);
2000 fi->fi_lw.lw_item = l->lv_first;
2001 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002002 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002003 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002004 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002005 fi->fi_bi = 0;
2006 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002007 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002008 typval_T btv;
2009
2010 // Make a copy, so that the iteration still works when the
2011 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002012 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002013 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002014 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002015 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002016 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002017 else if (tv.v_type == VAR_STRING)
2018 {
2019 fi->fi_byte_idx = 0;
2020 fi->fi_string = tv.vval.v_string;
2021 tv.vval.v_string = NULL;
2022 if (fi->fi_string == NULL)
2023 fi->fi_string = vim_strsave((char_u *)"");
2024 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002025 else
2026 {
Sean Dewar80d73952021-08-04 19:25:54 +02002027 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002028 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002029 }
2030 }
2031 }
2032 if (skip)
2033 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002034 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002035
2036 return fi;
2037}
2038
2039/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002040 * Used when looping over a :for line, skip the "in expr" part.
2041 */
2042 void
2043skip_for_lines(void *fi_void, evalarg_T *evalarg)
2044{
2045 forinfo_T *fi = (forinfo_T *)fi_void;
2046 int i;
2047
2048 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002049 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002050}
2051
2052/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002053 * Use the first item in a ":for" list. Advance to the next.
2054 * Assign the values to the variable (list). "arg" points to the first one.
2055 * Return TRUE when a valid item was found, FALSE when at end of list or
2056 * something wrong.
2057 */
2058 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002059next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002060{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002061 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002062 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002063 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002064 ? (ASSIGN_FINAL
2065 // first round: error if variable exists
2066 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002067 | ASSIGN_NO_MEMBER_TYPE
2068 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002069 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002070 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002071 int skip_assign = in_vim9script() && arg[0] == '_'
2072 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002073
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002074 if (fi->fi_blob != NULL)
2075 {
2076 typval_T tv;
2077
2078 if (fi->fi_bi >= blob_len(fi->fi_blob))
2079 return FALSE;
2080 tv.v_type = VAR_NUMBER;
2081 tv.v_lock = VAR_FIXED;
2082 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2083 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002084 if (skip_assign)
2085 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002086 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002087 fi->fi_varcount, flag, NULL) == OK;
2088 }
2089
2090 if (fi->fi_string != NULL)
2091 {
2092 typval_T tv;
2093 int len;
2094
2095 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2096 if (len == 0)
2097 return FALSE;
2098 tv.v_type = VAR_STRING;
2099 tv.v_lock = VAR_FIXED;
2100 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2101 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002102 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002103 if (skip_assign)
2104 result = TRUE;
2105 else
2106 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002107 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002108 vim_free(tv.vval.v_string);
2109 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002110 }
2111
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002112 item = fi->fi_lw.lw_item;
2113 if (item == NULL)
2114 result = FALSE;
2115 else
2116 {
2117 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002118 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002119 if (skip_assign)
2120 result = TRUE;
2121 else
2122 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002123 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002124 }
2125 return result;
2126}
2127
2128/*
2129 * Free the structure used to store info used by ":for".
2130 */
2131 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002132free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002133{
Bram Moolenaar33570922005-01-25 22:26:29 +00002134 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002135
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002136 if (fi == NULL)
2137 return;
2138 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002139 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002140 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002141 list_unref(fi->fi_list);
2142 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002143 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002144 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002145 else
2146 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002147 vim_free(fi);
2148}
2149
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002151set_context_for_expression(
2152 expand_T *xp,
2153 char_u *arg,
2154 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002156 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002158 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002160 if (cmdidx == CMD_let || cmdidx == CMD_var
2161 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002162 {
2163 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002164 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002165 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002166 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002167 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002168 {
2169 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002170 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002171 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002172 break;
2173 }
2174 return;
2175 }
2176 }
2177 else
2178 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2179 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 while ((xp->xp_pattern = vim_strpbrk(arg,
2181 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2182 {
2183 c = *xp->xp_pattern;
2184 if (c == '&')
2185 {
2186 c = xp->xp_pattern[1];
2187 if (c == '&')
2188 {
2189 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002190 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 }
2192 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002193 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002195 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2196 xp->xp_pattern += 2;
2197
2198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 }
2200 else if (c == '$')
2201 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002202 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 xp->xp_context = EXPAND_ENV_VARS;
2204 }
2205 else if (c == '=')
2206 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002207 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 xp->xp_context = EXPAND_EXPRESSION;
2209 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002210 else if (c == '#'
2211 && xp->xp_context == EXPAND_EXPRESSION)
2212 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002213 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002214 break;
2215 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002216 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 && xp->xp_context == EXPAND_FUNCTIONS
2218 && vim_strchr(xp->xp_pattern, '(') == NULL)
2219 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002220 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 break;
2222 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002223 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002225 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 {
2227 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2228 if (c == '\\' && xp->xp_pattern[1] != NUL)
2229 ++xp->xp_pattern;
2230 xp->xp_context = EXPAND_NOTHING;
2231 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002232 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002234 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2236 /* skip */ ;
2237 xp->xp_context = EXPAND_NOTHING;
2238 }
2239 else if (c == '|')
2240 {
2241 if (xp->xp_pattern[1] == '|')
2242 {
2243 ++xp->xp_pattern;
2244 xp->xp_context = EXPAND_EXPRESSION;
2245 }
2246 else
2247 xp->xp_context = EXPAND_COMMANDS;
2248 }
2249 else
2250 xp->xp_context = EXPAND_EXPRESSION;
2251 }
2252 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002253 // Doesn't look like something valid, expand as an expression
2254 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002255 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 arg = xp->xp_pattern;
2257 if (*arg != NUL)
2258 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2259 /* skip */ ;
2260 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002261
2262 // ":exe one two" completes "two"
2263 if ((cmdidx == CMD_execute
2264 || cmdidx == CMD_echo
2265 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002266 || cmdidx == CMD_echomsg
2267 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002268 && xp->xp_context == EXPAND_EXPRESSION)
2269 {
2270 for (;;)
2271 {
2272 char_u *n = skiptowhite(arg);
2273
2274 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2275 break;
2276 arg = skipwhite(n);
2277 }
2278 }
2279
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 xp->xp_pattern = arg;
2281}
2282
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002284 * Return TRUE if "pat" matches "text".
2285 * Does not use 'cpo' and always uses 'magic'.
2286 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002287 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002288pattern_match(char_u *pat, char_u *text, int ic)
2289{
2290 int matches = FALSE;
2291 char_u *save_cpo;
2292 regmatch_T regmatch;
2293
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002294 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002295 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002296 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002297 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2298 if (regmatch.regprog != NULL)
2299 {
2300 regmatch.rm_ic = ic;
2301 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2302 vim_regfree(regmatch.regprog);
2303 }
2304 p_cpo = save_cpo;
2305 return matches;
2306}
2307
2308/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002309 * Handle a name followed by "(". Both for just "name(arg)" and for
2310 * "expr->name(arg)".
2311 * Returns OK or FAIL.
2312 */
2313 static int
2314eval_func(
2315 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002316 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002317 char_u *name,
2318 int name_len,
2319 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002320 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002321 typval_T *basetv) // "expr" for "expr->name(arg)"
2322{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002323 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002324 char_u *s = name;
2325 int len = name_len;
2326 partial_T *partial;
2327 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002328 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002329 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002330
2331 if (!evaluate)
2332 check_vars(s, len);
2333
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002334 // If "s" is the name of a variable of type VAR_FUNC
2335 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002336 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002337 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002338
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002339 // Need to make a copy, in case evaluating the arguments makes
2340 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002341 s = vim_strsave(s);
zeertzjqb7f6f932023-04-13 22:12:50 +01002342 if (s == NULL || (evaluate && *s == NUL))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002343 ret = FAIL;
2344 else
2345 {
2346 funcexe_T funcexe;
2347
2348 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002349 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002350 funcexe.fe_firstline = curwin->w_cursor.lnum;
2351 funcexe.fe_lastline = curwin->w_cursor.lnum;
2352 funcexe.fe_evaluate = evaluate;
2353 funcexe.fe_partial = partial;
2354 funcexe.fe_basetv = basetv;
2355 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002356 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002357 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002358 }
2359 vim_free(s);
2360
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002361 // If evaluate is FALSE rettv->v_type was not set in
2362 // get_func_tv, but it's needed in handle_subscript() to parse
2363 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002364 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2365 {
2366 rettv->vval.v_string = NULL;
2367 rettv->v_type = VAR_FUNC;
2368 }
2369
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002370 // Stop the expression evaluation when immediately
2371 // aborting on error, or when an interrupt occurred or
2372 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002373 if (evaluate && aborting())
2374 {
2375 if (ret == OK)
2376 clear_tv(rettv);
2377 ret = FAIL;
2378 }
2379 return ret;
2380}
2381
2382/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002383 * After a NL, skip over empty lines and comment-only lines.
2384 */
2385 static char_u *
2386newline_skip_comments(char_u *arg)
2387{
2388 char_u *p = arg + 1;
2389
2390 for (;;)
2391 {
2392 p = skipwhite(p);
2393
2394 if (*p == NUL)
2395 break;
2396 if (vim9_comment_start(p))
2397 {
2398 char_u *nl = vim_strchr(p, NL);
2399
2400 if (nl == NULL)
2401 break;
2402 p = nl;
2403 }
2404 if (*p != NL)
2405 break;
2406 ++p; // skip another NL
2407 }
2408 return p;
2409}
2410
2411/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002412 * Get the next line source line without advancing. But do skip over comment
2413 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002414 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002415 */
2416 static char_u *
2417getline_peek_skip_comments(evalarg_T *evalarg)
2418{
2419 for (;;)
2420 {
2421 char_u *next = getline_peek(evalarg->eval_getline,
2422 evalarg->eval_cookie);
2423 char_u *p;
2424
2425 if (next == NULL)
2426 break;
2427 p = skipwhite(next);
2428 if (*p != NUL && !vim9_comment_start(p))
2429 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002430 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002431 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002432 }
2433 return NULL;
2434}
2435
2436/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002437 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2438 * comment) and there is a next line, return the next line (skipping blanks)
2439 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002440 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2441 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002442 * "arg" must point somewhere inside a line, not at the start.
2443 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002444 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002445eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2446{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002447 char_u *p = skipwhite(arg);
2448
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002449 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002450 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002451 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002452 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2453 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002454 && (*p == NUL || *p == NL
2455 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002456 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002457 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002458
Bram Moolenaare442d592022-05-05 12:20:28 +01002459 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002460 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002461 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002462 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002463 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002464 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002465
Bram Moolenaar9d489562020-07-30 20:08:50 +02002466 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002467 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002468 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002469 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002470 }
2471 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002472 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002473}
2474
2475/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002476 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002477 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002478 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002479 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002480eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002481{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002482 garray_T *gap = &evalarg->eval_ga;
2483 char_u *line;
2484
Bram Moolenaar39be4982022-05-06 21:51:50 +01002485 if (arg != NULL)
2486 {
2487 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002488 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002489 // Truncate before a trailing comment, so that concatenating the lines
2490 // won't turn the rest into a comment.
2491 if (*skipwhite(arg) == '#')
2492 *arg = NUL;
2493 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002494
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002495 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002496 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2497 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002498 else
2499 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002500 if (line == NULL)
2501 return NULL;
2502
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002503 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002504 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2505 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002506 char_u *p = skipwhite(line);
2507
2508 // Going to concatenate the lines after parsing. For an empty or
2509 // comment line use an empty string.
2510 if (*p == NUL || vim9_comment_start(p))
2511 {
2512 vim_free(line);
2513 line = vim_strsave((char_u *)"");
2514 }
2515
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002516 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2517 ++gap->ga_len;
2518 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002519 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002520 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002521 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002522 evalarg->eval_tofree = line;
2523 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002524
2525 // Advanced to the next line, "arg" no longer points into the previous
2526 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002527 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002528 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002529}
2530
Bram Moolenaare6b53242020-07-01 17:28:33 +02002531/*
2532 * Call eval_next_non_blank() and get the next line if needed.
2533 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002534 char_u *
2535skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2536{
2537 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002538 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002539
Bram Moolenaar962d7212020-07-04 14:15:00 +02002540 if (evalarg == NULL)
2541 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002542 eval_next_non_blank(p, evalarg, &getnext);
2543 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002544 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002545 return p;
2546}
2547
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002548/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002549 * The "eval" functions have an "evalarg" argument: When NULL or
2550 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
2551 * parsed but not executed. The functions may return OK, but the rettv will be
2552 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 */
2554
2555/*
2556 * Handle zero level expression.
2557 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002558 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002559 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002560 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 * Return OK or FAIL.
2562 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002563 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002564eval0(
2565 char_u *arg,
2566 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002567 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002568 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002570 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2571}
2572
2573/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002574 * If "arg" is a simple function call without arguments then call it and return
2575 * the result. Otherwise return NOTDONE.
2576 */
2577 int
2578may_call_simple_func(
2579 char_u *arg,
2580 typval_T *rettv)
2581{
2582 char_u *parens = (char_u *)strstr((char *)arg, "()");
2583 int r = NOTDONE;
2584
2585 // If the expression is "FuncName()" then we can skip a lot of overhead.
2586 if (parens != NULL && *skipwhite(parens + 2) == NUL)
2587 {
2588 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
2589
2590 if (to_name_end(p, TRUE) == parens)
2591 r = call_simple_func(arg, (int)(parens - arg), rettv);
2592 }
2593 return r;
2594}
2595
2596/*
2597 * Handle zero level expression with optimization for a simple function call.
2598 * Same arguments and return value as eval0().
2599 */
2600 int
2601eval0_simple_funccal(
2602 char_u *arg,
2603 typval_T *rettv,
2604 exarg_T *eap,
2605 evalarg_T *evalarg)
2606{
2607 int r = may_call_simple_func(arg, rettv);
2608
2609 if (r == NOTDONE)
2610 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
2611 return r;
2612}
2613
2614/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002615 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2616 * expression and don't check what comes after the expression.
2617 */
2618 int
2619eval0_retarg(
2620 char_u *arg,
2621 typval_T *rettv,
2622 exarg_T *eap,
2623 evalarg_T *evalarg,
2624 char_u **retarg)
2625{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 int ret;
2627 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002628 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002629 int did_emsg_before = did_emsg;
2630 int called_emsg_before = called_emsg;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002631 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002632 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633
2634 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002635 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002636
Bram Moolenaar79481362022-06-27 20:15:10 +01002637 if (ret != FAIL)
2638 {
2639 expr_end = p;
2640 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002641
Bram Moolenaar79481362022-06-27 20:15:10 +01002642 // In Vim9 script a command block is not split at NL characters for
2643 // commands using an expression argument. Skip over a '#' comment to
2644 // check for a following NL. Require white space before the '#'.
2645 if (in_vim9script() && p > expr_end && retarg == NULL)
2646 while (*p == '#')
2647 {
2648 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002649
Bram Moolenaar79481362022-06-27 20:15:10 +01002650 if (nl == NULL)
2651 break;
2652 p = skipwhite(nl + 1);
2653 if (eap != NULL && *p != NUL)
2654 eap->nextcmd = p;
2655 check_for_end = FALSE;
2656 }
2657
2658 if (check_for_end)
2659 end_error = !ends_excmd2(arg, p);
2660 }
2661
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002662 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 {
2664 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002665 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 /*
2667 * Report the invalid expression unless the expression evaluation has
2668 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002669 * exception, or we already gave a more specific error.
2670 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002672 if (!aborting()
2673 && did_emsg == did_emsg_before
2674 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002675 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002676 {
2677 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002678 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002679 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002680 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002681 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002682
zeertzjqf2588b62023-05-05 17:22:35 +01002683 if (eap != NULL && p != NULL)
2684 {
2685 // Some of the expression may not have been consumed.
2686 // Only execute a next command if it cannot be a "||" operator.
2687 // The next command may be "catch".
2688 char_u *nextcmd = check_nextcmd(p);
2689 if (nextcmd != NULL && *nextcmd != '|')
2690 eap->nextcmd = nextcmd;
2691 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002692 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002694
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002695 if (retarg != NULL)
2696 *retarg = p;
2697 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002698 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002699
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 return ret;
2701}
2702
2703/*
2704 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002705 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002706 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 *
2708 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002709 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002711 * Note: "rettv.v_lock" is not set.
2712 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 * Return OK or FAIL.
2714 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002715 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002716eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002718 char_u *p;
2719 int getnext;
2720
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002721 CLEAR_POINTER(rettv);
2722
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 /*
2724 * Get the first variable.
2725 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002726 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 return FAIL;
2728
Bram Moolenaar793648f2020-06-26 21:28:25 +02002729 p = eval_next_non_blank(*arg, evalarg, &getnext);
2730 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002732 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002733 int result;
2734 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002735 evalarg_T *evalarg_used = evalarg;
2736 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002737 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002738 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002739 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002740
2741 if (evalarg == NULL)
2742 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002743 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002744 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002745 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002746 orig_flags = evalarg_used->eval_flags;
2747 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002748
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002749 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002750 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002751 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002752 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002753 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002754 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002755 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002756 clear_tv(rettv);
2757 return FAIL;
2758 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002759 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002760 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002761
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002763 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002765 int error = FALSE;
2766
Bram Moolenaar13106602020-10-04 16:06:05 +02002767 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002768 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002769 else if (vim9script)
2770 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002771 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002773 if (error || !op_falsy || !result)
2774 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002775 if (error)
2776 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 }
2778
2779 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002780 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002782 if (op_falsy)
2783 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002784 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002785 {
mityu4ac198c2021-05-28 17:52:40 +02002786 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002787 clear_tv(rettv);
2788 return FAIL;
2789 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002790 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002791 evalarg_used->eval_flags = (op_falsy ? !result : result)
zeertzjqb7f6f932023-04-13 22:12:50 +01002792 ? orig_flags : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002793 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002794 {
2795 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002797 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002798 if (!op_falsy || !result)
2799 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002801 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002803 /*
2804 * Check for the ":".
2805 */
2806 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2807 if (*p != ':')
2808 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002809 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002810 if (evaluate && result)
2811 clear_tv(rettv);
2812 evalarg_used->eval_flags = orig_flags;
2813 return FAIL;
2814 }
2815 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002816 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002817 else
2818 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002819 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002820 {
2821 error_white_both(p, 1);
2822 clear_tv(rettv);
2823 evalarg_used->eval_flags = orig_flags;
2824 return FAIL;
2825 }
2826 *arg = p;
2827 }
2828
2829 /*
2830 * Get the third variable. Recursive!
2831 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002832 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002833 {
mityu4ac198c2021-05-28 17:52:40 +02002834 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002835 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002836 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002837 return FAIL;
2838 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002839 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2840 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01002841 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002842 if (eval1(arg, &var2, evalarg_used) == FAIL)
2843 {
2844 if (evaluate && result)
2845 clear_tv(rettv);
2846 evalarg_used->eval_flags = orig_flags;
2847 return FAIL;
2848 }
2849 if (evaluate && !result)
2850 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002852
2853 if (evalarg == NULL)
2854 clear_evalarg(&local_evalarg, NULL);
2855 else
2856 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 }
2858
2859 return OK;
2860}
2861
2862/*
2863 * Handle first level expression:
2864 * expr2 || expr2 || expr2 logical OR
2865 *
2866 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002867 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 *
2869 * Return OK or FAIL.
2870 */
2871 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002872eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002874 char_u *p;
2875 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876
2877 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002878 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002880 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 return FAIL;
2882
2883 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002884 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002886 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002887 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002889 evalarg_T *evalarg_used = evalarg;
2890 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002891 int evaluate;
2892 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002893 long result = FALSE;
2894 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002895 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002896 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002897
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002898 if (evalarg == NULL)
2899 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002900 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002901 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002902 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002903 orig_flags = evalarg_used->eval_flags;
2904 evaluate = orig_flags & EVAL_EVALUATE;
2905 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002906 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002907 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002908 result = tv_get_bool_chk(rettv, &error);
2909 else if (tv_get_number_chk(rettv, &error) != 0)
2910 result = TRUE;
2911 clear_tv(rettv);
2912 if (error)
2913 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 }
2915
2916 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002917 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002919 while (p[0] == '|' && p[1] == '|')
2920 {
2921 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002922 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002923 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002924 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002925 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002926 {
2927 error_white_both(p, 2);
2928 clear_tv(rettv);
2929 return FAIL;
2930 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002931 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002932 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002933
2934 /*
2935 * Get the second variable.
2936 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002937 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002938 {
mityu4ac198c2021-05-28 17:52:40 +02002939 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002940 clear_tv(rettv);
2941 return FAIL;
2942 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002943 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2944 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01002945 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002946 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002947 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002948
2949 /*
2950 * Compute the result.
2951 */
2952 if (evaluate && !result)
2953 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002954 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002955 result = tv_get_bool_chk(&var2, &error);
2956 else if (tv_get_number_chk(&var2, &error) != 0)
2957 result = TRUE;
2958 clear_tv(&var2);
2959 if (error)
2960 return FAIL;
2961 }
2962 if (evaluate)
2963 {
2964 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002965 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002966 rettv->v_type = VAR_BOOL;
2967 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002968 }
2969 else
2970 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002971 rettv->v_type = VAR_NUMBER;
2972 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002973 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002974 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002975
2976 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002978
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002979 if (evalarg == NULL)
2980 clear_evalarg(&local_evalarg, NULL);
2981 else
2982 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 }
2984
2985 return OK;
2986}
2987
2988/*
2989 * Handle second level expression:
2990 * expr3 && expr3 && expr3 logical AND
2991 *
2992 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002993 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 *
2995 * Return OK or FAIL.
2996 */
2997 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002998eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003000 char_u *p;
3001 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002
3003 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003004 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003006 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 return FAIL;
3008
3009 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003010 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003012 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003013 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003015 evalarg_T *evalarg_used = evalarg;
3016 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003017 int orig_flags;
3018 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003019 long result = TRUE;
3020 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003021 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003022 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003023
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003024 if (evalarg == NULL)
3025 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003026 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003027 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003028 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003029 orig_flags = evalarg_used->eval_flags;
3030 evaluate = orig_flags & EVAL_EVALUATE;
3031 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003032 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003033 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003034 result = tv_get_bool_chk(rettv, &error);
3035 else if (tv_get_number_chk(rettv, &error) == 0)
3036 result = FALSE;
3037 clear_tv(rettv);
3038 if (error)
3039 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 }
3041
3042 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003043 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003045 while (p[0] == '&' && p[1] == '&')
3046 {
3047 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003048 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003049 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003050 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003051 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003052 {
3053 error_white_both(p, 2);
3054 clear_tv(rettv);
3055 return FAIL;
3056 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003057 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003058 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003059
3060 /*
3061 * Get the second variable.
3062 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003063 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003064 {
mityu4ac198c2021-05-28 17:52:40 +02003065 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003066 clear_tv(rettv);
3067 return FAIL;
3068 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003069 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3070 evalarg_used->eval_flags = result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003071 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003072 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003073 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003074 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003075
3076 /*
3077 * Compute the result.
3078 */
3079 if (evaluate && result)
3080 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003081 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003082 result = tv_get_bool_chk(&var2, &error);
3083 else if (tv_get_number_chk(&var2, &error) == 0)
3084 result = FALSE;
3085 clear_tv(&var2);
3086 if (error)
3087 return FAIL;
3088 }
3089 if (evaluate)
3090 {
3091 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003092 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003093 rettv->v_type = VAR_BOOL;
3094 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003095 }
3096 else
3097 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003098 rettv->v_type = VAR_NUMBER;
3099 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003100 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003101 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003102
3103 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003105
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003106 if (evalarg == NULL)
3107 clear_evalarg(&local_evalarg, NULL);
3108 else
3109 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 }
3111
3112 return OK;
3113}
3114
3115/*
3116 * Handle third level expression:
3117 * var1 == var2
3118 * var1 =~ var2
3119 * var1 != var2
3120 * var1 !~ var2
3121 * var1 > var2
3122 * var1 >= var2
3123 * var1 < var2
3124 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003125 * var1 is var2
3126 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 *
3128 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003129 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 *
3131 * Return OK or FAIL.
3132 */
3133 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003134eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003137 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003138 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003140 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141
3142 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003143 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003145 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 return FAIL;
3147
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003148 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003149
Bram Moolenaar696ba232020-07-29 21:20:41 +02003150 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151
3152 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003153 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003155 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003157 typval_T var2;
3158 int ic;
3159 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003160 int evaluate = evalarg == NULL
3161 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003162 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003163
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003164 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003165 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003166 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003167 p = *arg;
3168 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003169 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3170 {
mityu4ac198c2021-05-28 17:52:40 +02003171 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003172 clear_tv(rettv);
3173 return FAIL;
3174 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003175
Bram Moolenaar696ba232020-07-29 21:20:41 +02003176 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3177 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003178 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003179 clear_tv(rettv);
3180 return FAIL;
3181 }
3182
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003183 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 if (p[len] == '?')
3185 {
3186 ic = TRUE;
3187 ++len;
3188 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003189 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 else if (p[len] == '#')
3191 {
3192 ic = FALSE;
3193 ++len;
3194 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003195 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003197 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198
3199 /*
3200 * Get the second variable.
3201 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003202 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3203 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003204 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003205 clear_tv(rettv);
3206 return FAIL;
3207 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003208 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003209 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003211 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 return FAIL;
3213 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003214 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003215 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003216 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003217
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003218 // use the line of the comparison for messages
3219 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003220 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003221 {
3222 ret = FAIL;
3223 clear_tv(rettv);
3224 }
3225 else
3226 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003227 clear_tv(&var2);
3228 return ret;
3229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 }
3231
3232 return OK;
3233}
3234
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003235/*
3236 * Make a copy of blob "tv1" and append blob "tv2".
3237 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 void
3239eval_addblob(typval_T *tv1, typval_T *tv2)
3240{
3241 blob_T *b1 = tv1->vval.v_blob;
3242 blob_T *b2 = tv2->vval.v_blob;
3243 blob_T *b = blob_alloc();
3244 int i;
3245
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003246 if (b == NULL)
3247 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003249 for (i = 0; i < blob_len(b1); i++)
3250 ga_append(&b->bv_ga, blob_get(b1, i));
3251 for (i = 0; i < blob_len(b2); i++)
3252 ga_append(&b->bv_ga, blob_get(b2, i));
3253
3254 clear_tv(tv1);
3255 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256}
3257
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003258/*
3259 * Make a copy of list "tv1" and append list "tv2".
3260 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 int
3262eval_addlist(typval_T *tv1, typval_T *tv2)
3263{
3264 typval_T var3;
3265
3266 // concatenate Lists
3267 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3268 {
3269 clear_tv(tv1);
3270 clear_tv(tv2);
3271 return FAIL;
3272 }
3273 clear_tv(tv1);
3274 *tv1 = var3;
3275 return OK;
3276}
3277
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003279 * Handle the bitwise left/right shift operator expression:
3280 * var1 << var2
3281 * var1 >> var2
3282 *
3283 * "arg" must point to the first non-white of the expression.
3284 * "arg" is advanced to just after the recognized expression.
3285 *
3286 * Return OK or FAIL.
3287 */
3288 static int
3289eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3290{
3291 /*
3292 * Get the first expression.
3293 */
3294 if (eval6(arg, rettv, evalarg) == FAIL)
3295 return FAIL;
3296
3297 /*
3298 * Repeat computing, until no '<<' or '>>' is following.
3299 */
3300 for (;;)
3301 {
3302 char_u *p;
3303 int getnext;
3304 exprtype_T type;
3305 int evaluate;
3306 typval_T var2;
3307 int vim9script;
3308
3309 p = eval_next_non_blank(*arg, evalarg, &getnext);
3310 if (p[0] == '<' && p[1] == '<')
3311 type = EXPR_LSHIFT;
3312 else if (p[0] == '>' && p[1] == '>')
3313 type = EXPR_RSHIFT;
3314 else
3315 return OK;
3316
3317 // Handle a bitwise left or right shift operator
3318 if (rettv->v_type != VAR_NUMBER)
3319 {
3320 // left operand should be a number
3321 emsg(_(e_bitshift_ops_must_be_number));
3322 clear_tv(rettv);
3323 return FAIL;
3324 }
3325
3326 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3327 vim9script = in_vim9script();
3328 if (getnext)
3329 {
3330 *arg = eval_next_line(*arg, evalarg);
3331 p = *arg;
3332 }
3333 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3334 {
3335 error_white_both(*arg, 2);
3336 clear_tv(rettv);
3337 return FAIL;
3338 }
3339
3340 /*
3341 * Get the second variable.
3342 */
3343 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3344 {
3345 error_white_both(p, 2);
3346 clear_tv(rettv);
3347 return FAIL;
3348 }
3349 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3350 if (eval6(arg, &var2, evalarg) == FAIL)
3351 {
3352 clear_tv(rettv);
3353 return FAIL;
3354 }
3355
3356 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3357 {
3358 // right operand should be a positive number
3359 if (var2.v_type != VAR_NUMBER)
3360 emsg(_(e_bitshift_ops_must_be_number));
3361 else
dundargocc57b5bc2022-11-02 13:30:51 +00003362 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003363 clear_tv(rettv);
3364 clear_tv(&var2);
3365 return FAIL;
3366 }
3367
3368 if (evaluate)
3369 {
3370 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3371 // shifting more bits than we have always results in zero
3372 rettv->vval.v_number = 0;
3373 else if (type == EXPR_LSHIFT)
3374 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003375 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003376 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003377 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003378 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003379 }
3380
3381 clear_tv(&var2);
3382 }
3383
3384 return OK;
3385}
3386
3387/*
3388 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003389 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003391 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003392 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 *
3394 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003395 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 *
3397 * Return OK or FAIL.
3398 */
3399 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003400eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003403 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003405 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 return FAIL;
3407
3408 /*
3409 * Repeat computing, until no '+', '-' or '.' is following.
3410 */
3411 for (;;)
3412 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003413 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003414 int getnext;
3415 char_u *p;
3416 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003417 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003418 int concat;
3419 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003420 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003421
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003422 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003423 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003424 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003425 p = eval_next_non_blank(*arg, evalarg, &getnext);
3426 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003427 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003428 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3429 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003431 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3432 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003433
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003434 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003435 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003436 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003437 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003438 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003439 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003440 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003441 {
mityu4ac198c2021-05-28 17:52:40 +02003442 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003443 clear_tv(rettv);
3444 return FAIL;
3445 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003446 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003447 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003448 if ((op != '+' || (rettv->v_type != VAR_LIST
3449 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003450 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003451 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003452 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003453 int error = FALSE;
3454
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003455 // For "list + ...", an illegal use of the first operand as
3456 // a number cannot be determined before evaluating the 2nd
3457 // operand: if this is also a list, all is ok.
3458 // For "something . ...", "something - ..." or "non-list + ...",
3459 // we know that the first operand needs to be a string or number
3460 // without evaluating the 2nd operand. So check before to avoid
3461 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003462 if (op != '.')
3463 tv_get_number_chk(rettv, &error);
3464 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003465 {
3466 clear_tv(rettv);
3467 return FAIL;
3468 }
3469 }
3470
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 /*
3472 * Get the second variable.
3473 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003474 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003475 {
mityu89dcb4d2021-05-28 13:50:17 +02003476 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003477 clear_tv(rettv);
3478 return FAIL;
3479 }
3480 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003481 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003483 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 return FAIL;
3485 }
3486
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003487 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 {
3489 /*
3490 * Compute the result.
3491 */
3492 if (op == '.')
3493 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003494 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3495 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003496 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003497
Bram Moolenaar2e086612020-08-25 22:37:48 +02003498 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003499 || var2.v_type == VAR_CHANNEL
3500 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003501 semsg(_(e_using_invalid_value_as_string_str),
3502 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003503 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003504 {
3505 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3506 var2.vval.v_float);
3507 s2 = buf2;
3508 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003509 else
3510 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003511 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003512 {
3513 clear_tv(rettv);
3514 clear_tv(&var2);
3515 return FAIL;
3516 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003517 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003518 clear_tv(rettv);
3519 rettv->v_type = VAR_STRING;
3520 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003522 else if (op == '+' && rettv->v_type == VAR_BLOB
3523 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003525 else if (op == '+' && rettv->v_type == VAR_LIST
3526 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003527 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003528 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003529 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 else
3532 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003533 int error = FALSE;
3534 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003535 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003536
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003537 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003538 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003539 f1 = rettv->vval.v_float;
3540 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003541 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003542 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003543 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003544 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003545 if (error)
3546 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003547 // This can only happen for "list + non-list" or
3548 // "blob + non-blob". For "non-list + ..." or
3549 // "something - ...", we returned before evaluating the
3550 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003551 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003552 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003553 return FAIL;
3554 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003555 if (var2.v_type == VAR_FLOAT)
3556 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003557 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003558 if (var2.v_type == VAR_FLOAT)
3559 {
3560 f2 = var2.vval.v_float;
3561 n2 = 0;
3562 }
3563 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003564 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003565 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003566 if (error)
3567 {
3568 clear_tv(rettv);
3569 clear_tv(&var2);
3570 return FAIL;
3571 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003572 if (rettv->v_type == VAR_FLOAT)
3573 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003574 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003575 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003576
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003577 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003578 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3579 {
3580 if (op == '+')
3581 f1 = f1 + f2;
3582 else
3583 f1 = f1 - f2;
3584 rettv->v_type = VAR_FLOAT;
3585 rettv->vval.v_float = f1;
3586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003588 {
3589 if (op == '+')
3590 n1 = n1 + n2;
3591 else
3592 n1 = n1 - n2;
3593 rettv->v_type = VAR_NUMBER;
3594 rettv->vval.v_number = n1;
3595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003597 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 }
3599 }
3600 return OK;
3601}
3602
3603/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003604 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 * * number multiplication
3606 * / number division
3607 * % number modulo
3608 *
3609 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003610 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 *
3612 * Return OK or FAIL.
3613 */
3614 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003615eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003616 char_u **arg,
3617 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003618 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003619 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003621 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622
3623 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003624 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003626 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 return FAIL;
3628
3629 /*
3630 * Repeat computing, until no '*', '/' or '%' is following.
3631 */
3632 for (;;)
3633 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003634 int evaluate;
3635 int getnext;
3636 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003637 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003638 int op;
3639 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003640 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003641 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003642
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003643 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003644 p = eval_next_non_blank(*arg, evalarg, &getnext);
3645 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003646 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003648
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003649 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003650 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003651 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003652 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003653 {
3654 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3655 {
mityu4ac198c2021-05-28 17:52:40 +02003656 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003657 clear_tv(rettv);
3658 return FAIL;
3659 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003660 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003663 f1 = 0;
3664 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003665 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003666 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003668 if (rettv->v_type == VAR_FLOAT)
3669 {
3670 f1 = rettv->vval.v_float;
3671 use_float = TRUE;
3672 n1 = 0;
3673 }
3674 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003675 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003676 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003677 if (error)
3678 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 }
3680 else
3681 n1 = 0;
3682
3683 /*
3684 * Get the second variable.
3685 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003686 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3687 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003688 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003689 clear_tv(rettv);
3690 return FAIL;
3691 }
3692 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003693 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 return FAIL;
3695
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003696 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003698 if (var2.v_type == VAR_FLOAT)
3699 {
3700 if (!use_float)
3701 {
3702 f1 = n1;
3703 use_float = TRUE;
3704 }
3705 f2 = var2.vval.v_float;
3706 n2 = 0;
3707 }
3708 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003709 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003710 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003711 clear_tv(&var2);
3712 if (error)
3713 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003714 if (use_float)
3715 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717
3718 /*
3719 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003720 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003722 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003724 if (op == '*')
3725 f1 = f1 * f2;
3726 else if (op == '/')
3727 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003728#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003729 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003730 if (f2 == 0.0)
3731 {
3732 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003733 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003734 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003735 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003736 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003737 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003738 }
3739 else
3740 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003741#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003742 // We rely on the floating point library to handle divide
3743 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003744 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003745#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003748 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003749 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003750 return FAIL;
3751 }
3752 rettv->v_type = VAR_FLOAT;
3753 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 }
3755 else
3756 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003757 int failed = FALSE;
3758
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003759 if (op == '*')
3760 n1 = n1 * n2;
3761 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003762 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003764 n1 = num_modulus(n1, n2, &failed);
3765 if (failed)
3766 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003767
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003768 rettv->v_type = VAR_NUMBER;
3769 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 }
3772 }
3773
3774 return OK;
3775}
3776
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003777/*
3778 * Handle a type cast before a base level expression.
3779 * "arg" must point to the first non-white of the expression.
3780 * "arg" is advanced to just after the recognized expression.
3781 * Return OK or FAIL.
3782 */
3783 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003784eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003785 char_u **arg,
3786 typval_T *rettv,
3787 evalarg_T *evalarg,
3788 int want_string) // after "." operator
3789{
3790 type_T *want_type = NULL;
3791 garray_T type_list; // list of pointers to allocated types
3792 int res;
3793 int evaluate = evalarg == NULL ? 0
3794 : (evalarg->eval_flags & EVAL_EVALUATE);
3795
3796 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003797 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3798 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003799 {
3800 ++*arg;
3801 ga_init2(&type_list, sizeof(type_T *), 10);
3802 want_type = parse_type(arg, &type_list, TRUE);
3803 if (want_type == NULL && (evaluate || **arg != '>'))
3804 {
3805 clear_type_list(&type_list);
3806 return FAIL;
3807 }
3808
3809 if (**arg != '>')
3810 {
3811 if (*skipwhite(*arg) == '>')
3812 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3813 else
3814 emsg(_(e_missing_gt));
3815 clear_type_list(&type_list);
3816 return FAIL;
3817 }
3818 ++*arg;
3819 *arg = skipwhite_and_linebreak(*arg, evalarg);
3820 }
3821
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003822 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003823
3824 if (want_type != NULL && evaluate)
3825 {
3826 if (res == OK)
3827 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003828 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3829 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003830
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003831 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003832 {
3833 if (want_type == &t_bool && actual != &t_bool
3834 && (actual->tt_flags & TTFLAG_BOOL_OK))
3835 {
3836 int n = tv2bool(rettv);
3837
3838 // can use "0" and "1" for boolean in some places
3839 clear_tv(rettv);
3840 rettv->v_type = VAR_BOOL;
3841 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3842 }
3843 else
3844 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003845 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003846
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003847 res = check_type(want_type, actual, TRUE, where);
3848 }
3849 }
3850 }
3851 clear_type_list(&type_list);
3852 }
3853
3854 return res;
3855}
3856
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003857 int
3858eval_leader(char_u **arg, int vim9)
3859{
3860 char_u *s = *arg;
3861 char_u *p = *arg;
3862
3863 while (*p == '!' || *p == '-' || *p == '+')
3864 {
3865 char_u *n = skipwhite(p + 1);
3866
3867 // ++, --, -+ and +- are not accepted in Vim9 script
3868 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3869 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003870 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003871 return FAIL;
3872 }
3873 p = n;
3874 }
3875 *arg = p;
3876 return OK;
3877}
3878
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003880 * Check for a predefined value "true", "false" and "null.*".
3881 * Return OK when recognized.
3882 */
3883 int
3884handle_predefined(char_u *s, int len, typval_T *rettv)
3885{
3886 switch (len)
3887 {
3888 case 4: if (STRNCMP(s, "true", 4) == 0)
3889 {
3890 rettv->v_type = VAR_BOOL;
3891 rettv->vval.v_number = VVAL_TRUE;
3892 return OK;
3893 }
3894 if (STRNCMP(s, "null", 4) == 0)
3895 {
3896 rettv->v_type = VAR_SPECIAL;
3897 rettv->vval.v_number = VVAL_NULL;
3898 return OK;
3899 }
3900 break;
3901 case 5: if (STRNCMP(s, "false", 5) == 0)
3902 {
3903 rettv->v_type = VAR_BOOL;
3904 rettv->vval.v_number = VVAL_FALSE;
3905 return OK;
3906 }
3907 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003908 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3909 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003910#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003911 rettv->v_type = VAR_JOB;
3912 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003913#else
3914 rettv->v_type = VAR_SPECIAL;
3915 rettv->vval.v_number = VVAL_NULL;
3916#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003917 return OK;
3918 }
3919 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003920 case 9:
3921 if (STRNCMP(s, "null_", 5) != 0)
3922 break;
3923 if (STRNCMP(s + 5, "list", 4) == 0)
3924 {
3925 rettv->v_type = VAR_LIST;
3926 rettv->vval.v_list = NULL;
3927 return OK;
3928 }
3929 if (STRNCMP(s + 5, "dict", 4) == 0)
3930 {
3931 rettv->v_type = VAR_DICT;
3932 rettv->vval.v_dict = NULL;
3933 return OK;
3934 }
3935 if (STRNCMP(s + 5, "blob", 4) == 0)
3936 {
3937 rettv->v_type = VAR_BLOB;
3938 rettv->vval.v_blob = NULL;
3939 return OK;
3940 }
3941 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003942 case 10: if (STRNCMP(s, "null_class", 10) == 0)
3943 {
3944 rettv->v_type = VAR_CLASS;
3945 rettv->vval.v_class = NULL;
3946 return OK;
3947 }
3948 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003949 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3950 {
3951 rettv->v_type = VAR_STRING;
3952 rettv->vval.v_string = NULL;
3953 return OK;
3954 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003955 if (STRNCMP(s, "null_object", 11) == 0)
3956 {
3957 rettv->v_type = VAR_OBJECT;
3958 rettv->vval.v_object = NULL;
3959 return OK;
3960 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003961 break;
3962 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003963 if (STRNCMP(s, "null_channel", 12) == 0)
3964 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003965#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003966 rettv->v_type = VAR_CHANNEL;
3967 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003968#else
3969 rettv->v_type = VAR_SPECIAL;
3970 rettv->vval.v_number = VVAL_NULL;
3971#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003972 return OK;
3973 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003974 if (STRNCMP(s, "null_partial", 12) == 0)
3975 {
3976 rettv->v_type = VAR_PARTIAL;
3977 rettv->vval.v_partial = NULL;
3978 return OK;
3979 }
3980 break;
3981 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3982 {
3983 rettv->v_type = VAR_FUNC;
3984 rettv->vval.v_string = NULL;
3985 return OK;
3986 }
3987 break;
3988 }
3989 return FAIL;
3990}
3991
3992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 * Handle sixth level expression:
3994 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003995 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003996 * "string" string constant
3997 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 * &option-name option value
3999 * @r register contents
4000 * identifier variable value
4001 * function() function call
4002 * $VAR environment variable
4003 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004004 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004005 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004006 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004007 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 *
4009 * Also handle:
4010 * ! in front logical NOT
4011 * - in front unary minus
4012 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004013 * trailing [] subscript in String or List
4014 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004015 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 *
4017 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004018 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 *
4020 * Return OK or FAIL.
4021 */
4022 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004023eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004024 char_u **arg,
4025 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004026 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004027 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004029 int evaluate = evalarg != NULL
4030 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 int len;
4032 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004033 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 char_u *start_leader, *end_leader;
4035 int ret = OK;
4036 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004037 static int recurse = 0;
4038 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039
4040 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004041 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004042 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004044 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045
4046 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004047 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 */
4049 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004050 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004051 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 end_leader = *arg;
4053
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004054 if (**arg == '.' && (!isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004055 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004056 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004057 ++*arg;
4058 return FAIL;
4059 }
4060
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004061 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004062 // and crash. With MSVC the stack is smaller.
4063 if (recurse ==
4064#ifdef _MSC_VER
4065 300
4066#else
4067 1000
4068#endif
4069 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004070 {
4071 semsg(_(e_expression_too_recursive_str), *arg);
4072 return FAIL;
4073 }
4074 ++recurse;
4075
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 switch (**arg)
4077 {
4078 /*
4079 * Number constant.
4080 */
4081 case '0':
4082 case '1':
4083 case '2':
4084 case '3':
4085 case '4':
4086 case '5':
4087 case '6':
4088 case '7':
4089 case '8':
4090 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004091 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004092
4093 // Apply prefixed "-" and "+" now. Matters especially when
4094 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004095 if (ret == OK && evaluate && end_leader > start_leader
4096 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004097 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 break;
4099
4100 /*
4101 * String constant: "string".
4102 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004103 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 break;
4105
4106 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004107 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004109 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004110 break;
4111
4112 /*
4113 * List: [expr, expr]
4114 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004115 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 break;
4117
4118 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004119 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004120 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004121 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004122 {
4123 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4124 }
4125 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004126 {
4127 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004128 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004129 }
4130 else
4131 ret = NOTDONE;
4132 break;
4133
4134 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004135 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004136 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004137 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004138 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004139 ret = NOTDONE;
4140 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004141 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004142 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004143 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004144 break;
4145
4146 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004147 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004149 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 break;
4151
4152 /*
4153 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004154 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 */
LemonBoy2eaef102022-05-06 13:14:50 +01004156 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4157 ret = eval_interp_string(arg, rettv, evaluate);
4158 else
4159 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 break;
4161
4162 /*
4163 * Register contents: @r.
4164 */
4165 case '@': ++*arg;
4166 if (evaluate)
4167 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004168 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004169 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004170 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004171 emsg_invreg(**arg);
4172 else
4173 {
4174 rettv->v_type = VAR_STRING;
4175 rettv->vval.v_string = get_reg_contents(**arg,
4176 GREG_EXPR_SRC);
4177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 }
4179 if (**arg != NUL)
4180 ++*arg;
4181 break;
4182
4183 /*
4184 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004185 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004187 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004188 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004189 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004190 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004191 if (ret == OK && evaluate)
4192 {
4193 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4194
Bram Moolenaara9931532021-06-12 15:58:16 +02004195 // Compile it here to get the return type. The return
4196 // type is optional, when it's missing use t_unknown.
4197 // This is recognized in compile_return().
4198 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4199 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004200 if (compile_def_function(ufunc, FALSE,
4201 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004202 {
4203 clear_tv(rettv);
4204 ret = FAIL;
4205 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004206 }
4207 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004208 if (ret == NOTDONE)
4209 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004210 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004211 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004212
Bram Moolenaar9215f012020-06-27 21:18:00 +02004213 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004214 if (**arg == ')')
4215 ++*arg;
4216 else if (ret == OK)
4217 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004218 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004219 clear_tv(rettv);
4220 ret = FAIL;
4221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 }
4223 break;
4224
Bram Moolenaar8c711452005-01-14 21:53:12 +00004225 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 break;
4227 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004228
4229 if (ret == NOTDONE)
4230 {
4231 /*
4232 * Must be a variable or function name.
4233 * Can also be a curly-braces kind of name: {expr}.
4234 */
4235 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004236 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004237 if (alias != NULL)
4238 s = alias;
4239
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004240 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004241 ret = FAIL;
4242 else
4243 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004244 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4245
Bram Moolenaar4525a572022-02-13 11:57:33 +00004246 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004247 {
4248 emsg(_(e_cannot_use_underscore_here));
4249 ret = FAIL;
4250 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004251 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004252 && s[0] == 's' && s[1] == ':')
4253 {
4254 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4255 ret = FAIL;
4256 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004257 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004258 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004259 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004260 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004261 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004262 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004263 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004264 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004265 // get the value of "true", "false", etc. or a variable
4266 ret = FAIL;
4267 if (vim9script)
4268 ret = handle_predefined(s, len, rettv);
4269 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004270 {
4271 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004272 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004273 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004274 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004275 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004276 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004277 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004278 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004279 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004280 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004281 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004282 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004283 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004284 }
4285
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004286 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4287 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004288 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004289 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290
4291 /*
4292 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4293 */
4294 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004295 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004296
4297 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004298 return ret;
4299}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004300
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004301/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004302 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004303 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004304 * Adjusts "end_leaderp" until it is at "start_leader".
4305 */
4306 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004307eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004308 typval_T *rettv,
4309 int numeric_only,
4310 char_u *start_leader,
4311 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004312{
4313 char_u *end_leader = *end_leaderp;
4314 int ret = OK;
4315 int error = FALSE;
4316 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004317 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004318 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004319 float_T f = 0.0;
4320
4321 if (rettv->v_type == VAR_FLOAT)
4322 f = rettv->vval.v_float;
4323 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004324 {
4325 while (VIM_ISWHITE(end_leader[-1]))
4326 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004327 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004328 val = tv2bool(rettv);
4329 else
4330 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004331 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004332 if (error)
4333 {
4334 clear_tv(rettv);
4335 ret = FAIL;
4336 }
4337 else
4338 {
4339 while (end_leader > start_leader)
4340 {
4341 --end_leader;
4342 if (*end_leader == '!')
4343 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004344 if (numeric_only)
4345 {
4346 ++end_leader;
4347 break;
4348 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004349 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004350 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004351 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004352 {
4353 rettv->v_type = VAR_BOOL;
4354 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4355 }
4356 else
4357 f = !f;
4358 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004359 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004360 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004361 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004362 type = VAR_BOOL;
4363 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004364 }
4365 else if (*end_leader == '-')
4366 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004367 if (rettv->v_type == VAR_FLOAT)
4368 f = -f;
4369 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004370 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004371 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004372 type = VAR_NUMBER;
4373 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004374 }
4375 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004376 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004378 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004379 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004381 else
4382 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004383 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004384 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004385 rettv->v_type = type;
4386 else
4387 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004388 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004391 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 return ret;
4393}
4394
4395/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004396 * Call the function referred to in "rettv".
4397 */
4398 static int
4399call_func_rettv(
4400 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004401 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004402 typval_T *rettv,
4403 int evaluate,
4404 dict_T *selfdict,
4405 typval_T *basetv)
4406{
4407 partial_T *pt = NULL;
4408 funcexe_T funcexe;
4409 typval_T functv;
4410 char_u *s;
4411 int ret;
4412
4413 // need to copy the funcref so that we can clear rettv
4414 if (evaluate)
4415 {
4416 functv = *rettv;
4417 rettv->v_type = VAR_UNKNOWN;
4418
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004419 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004420 if (functv.v_type == VAR_PARTIAL)
4421 {
4422 pt = functv.vval.v_partial;
4423 s = partial_name(pt);
4424 }
4425 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004426 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004427 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004428 if (s == NULL || *s == NUL)
4429 {
4430 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004431 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004432 goto theend;
4433 }
4434 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004435 }
4436 else
4437 s = (char_u *)"";
4438
Bram Moolenaara80faa82020-04-12 19:37:17 +02004439 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004440 funcexe.fe_firstline = curwin->w_cursor.lnum;
4441 funcexe.fe_lastline = curwin->w_cursor.lnum;
4442 funcexe.fe_evaluate = evaluate;
4443 funcexe.fe_partial = pt;
4444 funcexe.fe_selfdict = selfdict;
4445 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004446 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004447
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004448theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004449 // Clear the funcref afterwards, so that deleting it while
4450 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004451 if (evaluate)
4452 clear_tv(&functv);
4453
4454 return ret;
4455}
4456
4457/*
4458 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004459 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004460 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4461 */
4462 static int
4463eval_lambda(
4464 char_u **arg,
4465 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004466 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004467 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004468{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004469 int evaluate = evalarg != NULL
4470 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004471 typval_T base = *rettv;
4472 int ret;
4473
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004474 rettv->v_type = VAR_UNKNOWN;
4475
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004476 if (**arg == '{')
4477 {
4478 // ->{lambda}()
4479 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4480 }
4481 else
4482 {
4483 // ->(lambda)()
4484 ++*arg;
4485 ret = eval1(arg, rettv, evalarg);
4486 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004487 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004488 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004489 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004490 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004491 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004492 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4493 && rettv->v_type != VAR_PARTIAL)
4494 {
4495 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4496 return FAIL;
4497 }
4498 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004499 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004500 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004501 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004502
4503 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004504 {
4505 if (verbose)
4506 {
4507 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004508 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004509 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004510 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004511 }
4512 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004513 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004514 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004515 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004516 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004517
4518 // Clear the funcref afterwards, so that deleting it while
4519 // evaluating the arguments is possible (see test55).
4520 if (evaluate)
4521 clear_tv(&base);
4522
4523 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004524}
4525
4526/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004527 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004528 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004529 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4530 */
4531 static int
4532eval_method(
4533 char_u **arg,
4534 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004535 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004536 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004537{
4538 char_u *name;
4539 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004540 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004541 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004542 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004543 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004544 int evaluate = evalarg != NULL
4545 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004546
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004547 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004548
Bram Moolenaarac92e252019-08-03 21:58:38 +02004549 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004550 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004551 if (alias != NULL)
4552 name = alias;
4553
4554 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004555 {
4556 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004557 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004558 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004559 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004560 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004561 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004562 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004563
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004564 // If there is no "(" immediately following, but there is further on,
4565 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4566 // Does not handle anything where "(" is part of the expression.
4567 *arg = skipwhite(*arg);
4568
4569 if (**arg != '(' && alias == NULL
4570 && (paren = vim_strchr(*arg, '(')) != NULL)
4571 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004572 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00004573
4574 // Truncate the name a the "(". Avoid trying to get another line
4575 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004576 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00004577 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
4578 if (evalarg != NULL)
4579 {
4580 getline = evalarg->eval_getline;
4581 evalarg->eval_getline = NULL;
4582 }
4583
4584 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004585 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004586 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004587 *arg = name + len;
4588 ret = FAIL;
4589 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004590 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004591 {
4592 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004593 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004594 }
Bram Moolenaar34820942022-12-19 20:28:38 +00004595
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004596 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00004597 if (getline != NULL)
4598 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004599 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004600
4601 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004602 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004603 *arg = skipwhite(*arg);
4604
4605 if (**arg != '(')
4606 {
4607 if (verbose)
4608 semsg(_(e_missing_parenthesis_str), name);
4609 ret = FAIL;
4610 }
4611 else if (VIM_ISWHITE((*arg)[-1]))
4612 {
4613 if (verbose)
4614 emsg(_(e_no_white_space_allowed_before_parenthesis));
4615 ret = FAIL;
4616 }
4617 else
4618 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004619 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004620 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004621 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004622
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004623 // Clear the funcref afterwards, so that deleting it while
4624 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004625 if (evaluate)
4626 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004627 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004628
Bram Moolenaarac92e252019-08-03 21:58:38 +02004629 return ret;
4630}
4631
4632/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004633 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4634 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004635 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4636 */
4637 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004638eval_index(
4639 char_u **arg,
4640 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004641 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004642 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004643{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004644 int evaluate = evalarg != NULL
4645 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004646 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004647 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004648 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004649 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004650 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004651 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004652
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004653 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4654 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004655
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004656 init_tv(&var1);
4657 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004658 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004659 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004660 /*
4661 * dict.name
4662 */
4663 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004664 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004665 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004666 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004667 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004668 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004669 }
4670 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004671 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004672 /*
4673 * something[idx]
4674 *
4675 * Get the (first) variable from inside the [].
4676 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004677 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004678 if (**arg == ':')
4679 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004680 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004681 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004682 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004683 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004684 semsg(_(e_white_space_required_before_and_after_str_at_str),
4685 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004686 clear_tv(&var1);
4687 return FAIL;
4688 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004689 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004690 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004691 int error = FALSE;
4692
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004693 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004694 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004695 && var1.v_type == VAR_FLOAT)
4696 {
4697 var1.vval.v_string = typval_tostring(&var1, TRUE);
4698 var1.v_type = VAR_STRING;
4699 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004700
Bram Moolenaar4525a572022-02-13 11:57:33 +00004701 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004702 tv_get_number_chk(&var1, &error);
4703 else
4704 error = tv_get_string_chk(&var1) == NULL;
4705 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004706 {
4707 // not a number or string
4708 clear_tv(&var1);
4709 return FAIL;
4710 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004711 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004712
4713 /*
4714 * Get the second variable from inside the [:].
4715 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004716 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004717 if (**arg == ':')
4718 {
4719 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004720 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004721 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004722 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004723 semsg(_(e_white_space_required_before_and_after_str_at_str),
4724 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004725 if (!empty1)
4726 clear_tv(&var1);
4727 return FAIL;
4728 }
4729 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004730 if (**arg == ']')
4731 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004732 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004733 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004734 if (!empty1)
4735 clear_tv(&var1);
4736 return FAIL;
4737 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004738 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004739 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004740 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004741 if (!empty1)
4742 clear_tv(&var1);
4743 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004744 return FAIL;
4745 }
4746 }
4747
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004748 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004749 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004750 if (**arg != ']')
4751 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004752 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004753 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004754 clear_tv(&var1);
4755 if (range)
4756 clear_tv(&var2);
4757 return FAIL;
4758 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004759 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004760 }
4761
4762 if (evaluate)
4763 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004764 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004765 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004766 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004767
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004768 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004769 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004770 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004771 clear_tv(&var2);
4772 return res;
4773 }
4774 return OK;
4775}
4776
4777/*
4778 * Check if "rettv" can have an [index] or [sli:ce]
4779 */
4780 int
4781check_can_index(typval_T *rettv, int evaluate, int verbose)
4782{
4783 switch (rettv->v_type)
4784 {
4785 case VAR_FUNC:
4786 case VAR_PARTIAL:
4787 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004788 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004789 return FAIL;
4790 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004791 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004792 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004793 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004794 case VAR_BOOL:
4795 case VAR_SPECIAL:
4796 case VAR_JOB:
4797 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004798 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004799 case VAR_CLASS:
4800 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004801 if (verbose)
4802 emsg(_(e_cannot_index_special_variable));
4803 return FAIL;
4804 case VAR_UNKNOWN:
4805 case VAR_ANY:
4806 case VAR_VOID:
4807 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004808 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004809 emsg(_(e_cannot_index_special_variable));
4810 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004811 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004812 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004813
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004814 case VAR_STRING:
4815 case VAR_LIST:
4816 case VAR_DICT:
4817 case VAR_BLOB:
4818 break;
4819 case VAR_NUMBER:
4820 if (in_vim9script())
4821 emsg(_(e_cannot_index_number));
4822 break;
4823 }
4824 return OK;
4825}
4826
4827/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004828 * slice() function
4829 */
4830 void
4831f_slice(typval_T *argvars, typval_T *rettv)
4832{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004833 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004834 && ((argvars[0].v_type != VAR_STRING
4835 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004836 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004837 && check_for_list_arg(argvars, 0) == FAIL)
4838 || check_for_number_arg(argvars, 1) == FAIL
4839 || check_for_opt_number_arg(argvars, 2) == FAIL))
4840 return;
4841
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00004842 if (check_can_index(argvars, TRUE, FALSE) != OK)
4843 return;
4844
4845 copy_tv(argvars, rettv);
4846 eval_index_inner(rettv, TRUE, argvars + 1,
4847 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4848 TRUE, NULL, 0, FALSE);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004849}
4850
4851/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004852 * Apply index or range to "rettv".
4853 * "var1" is the first index, NULL for [:expr].
4854 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004855 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4856 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004857 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4858 */
4859 int
4860eval_index_inner(
4861 typval_T *rettv,
4862 int is_range,
4863 typval_T *var1,
4864 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004865 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004866 char_u *key,
4867 int keylen,
4868 int verbose)
4869{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004870 varnumber_T n1, n2 = 0;
4871 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004872
4873 n1 = 0;
4874 if (var1 != NULL && rettv->v_type != VAR_DICT)
4875 n1 = tv_get_number(var1);
4876
4877 if (is_range)
4878 {
4879 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004880 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004881 if (verbose)
4882 emsg(_(e_cannot_slice_dictionary));
4883 return FAIL;
4884 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004885 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004886 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004887 else
4888 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004889 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004890
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004891 switch (rettv->v_type)
4892 {
4893 case VAR_UNKNOWN:
4894 case VAR_ANY:
4895 case VAR_VOID:
4896 case VAR_FUNC:
4897 case VAR_PARTIAL:
4898 case VAR_FLOAT:
4899 case VAR_BOOL:
4900 case VAR_SPECIAL:
4901 case VAR_JOB:
4902 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004903 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004904 case VAR_CLASS:
4905 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004906 break; // not evaluating, skipping over subscript
4907
4908 case VAR_NUMBER:
4909 case VAR_STRING:
4910 {
4911 char_u *s = tv_get_string(rettv);
4912
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004913 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004914 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004915 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004916 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004917 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004918 else
4919 s = char_from_string(s, n1);
4920 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004921 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004922 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004923 // The resulting variable is a substring. If the indexes
4924 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004925 if (n1 < 0)
4926 {
4927 n1 = len + n1;
4928 if (n1 < 0)
4929 n1 = 0;
4930 }
4931 if (n2 < 0)
4932 n2 = len + n2;
4933 else if (n2 >= len)
4934 n2 = len;
4935 if (n1 >= len || n2 < 0 || n1 > n2)
4936 s = NULL;
4937 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004938 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004939 }
4940 else
4941 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004942 // The resulting variable is a string of a single
4943 // character. If the index is too big or negative the
4944 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004945 if (n1 >= len || n1 < 0)
4946 s = NULL;
4947 else
4948 s = vim_strnsave(s + n1, 1);
4949 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004950 clear_tv(rettv);
4951 rettv->v_type = VAR_STRING;
4952 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004953 }
4954 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004955
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004956 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004957 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4958 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004959 break;
4960
4961 case VAR_LIST:
4962 if (var1 == NULL)
4963 n1 = 0;
4964 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004965 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004966 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004967 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004968 return FAIL;
4969 break;
4970
4971 case VAR_DICT:
4972 {
4973 dictitem_T *item;
4974 typval_T tmp;
4975
4976 if (key == NULL)
4977 {
4978 key = tv_get_string_chk(var1);
4979 if (key == NULL)
4980 return FAIL;
4981 }
4982
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004983 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004984
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004985 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004986 {
4987 if (verbose)
4988 {
4989 if (keylen > 0)
4990 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004991 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004992 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004993 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004994 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004995
4996 copy_tv(&item->di_tv, &tmp);
4997 clear_tv(rettv);
4998 *rettv = tmp;
4999 }
5000 break;
5001 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005002 return OK;
5003}
5004
5005/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005006 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005007 */
5008 char_u *
5009partial_name(partial_T *pt)
5010{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02005011 if (pt != NULL)
5012 {
5013 if (pt->pt_name != NULL)
5014 return pt->pt_name;
5015 if (pt->pt_func != NULL)
5016 return pt->pt_func->uf_name;
5017 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005018 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005019}
5020
Bram Moolenaarddecc252016-04-06 22:59:37 +02005021 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005022partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005023{
5024 int i;
5025
5026 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005027 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005028 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005029 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005030 if (pt->pt_name != NULL)
5031 {
5032 func_unref(pt->pt_name);
5033 vim_free(pt->pt_name);
5034 }
5035 else
5036 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005037
Bram Moolenaar54656012021-06-09 20:50:46 +02005038 // "out_up" is no longer used, decrement refcount on partial that owns it.
5039 partial_unref(pt->pt_outer.out_up_partial);
5040
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005041 // Using pt_outer from another partial.
5042 partial_unref(pt->pt_outer_partial);
5043
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005044 // Decrease the reference count for the context of a closure. If down
5045 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005046 if (pt->pt_funcstack != NULL)
5047 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005048 --pt->pt_funcstack->fs_refcount;
5049 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005050 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005051 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005052 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5053 if (pt->pt_loopvars[i] != NULL)
5054 {
5055 --pt->pt_loopvars[i]->lvs_refcount;
5056 loopvars_check_refcount(pt->pt_loopvars[i]);
5057 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005058
Bram Moolenaarddecc252016-04-06 22:59:37 +02005059 vim_free(pt);
5060}
5061
5062/*
5063 * Unreference a closure: decrement the reference count and free it when it
5064 * becomes zero.
5065 */
5066 void
5067partial_unref(partial_T *pt)
5068{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005069 if (pt == NULL)
5070 return;
5071
5072 int done = FALSE;
5073
5074 if (--pt->pt_refcount <= 0)
5075 partial_free(pt);
5076
5077 // If the reference count goes down to one, the funcstack may be the
5078 // only reference and can be freed if no other partials reference it.
5079 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005080 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005081 // careful: if the funcstack is freed it may contain this partial
5082 // and it gets freed as well
5083 if (pt->pt_funcstack != NULL)
5084 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005085
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005086 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005087 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005088 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005089
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005090 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5091 if (pt->pt_loopvars[depth] != NULL
5092 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005093 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005094 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005095 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005096}
5097
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005098/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005099 * Return the next (unique) copy ID.
5100 * Used for serializing nested structures.
5101 */
5102 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005103get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005104{
5105 current_copyID += COPYID_INC;
5106 return current_copyID;
5107}
5108
5109/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005110 * Garbage collection for lists and dictionaries.
5111 *
5112 * We use reference counts to be able to free most items right away when they
5113 * are no longer used. But for composite items it's possible that it becomes
5114 * unused while the reference count is > 0: When there is a recursive
5115 * reference. Example:
5116 * :let l = [1, 2, 3]
5117 * :let d = {9: l}
5118 * :let l[1] = d
5119 *
5120 * Since this is quite unusual we handle this with garbage collection: every
5121 * once in a while find out which lists and dicts are not referenced from any
5122 * variable.
5123 *
5124 * Here is a good reference text about garbage collection (refers to Python
5125 * but it applies to all reference-counting mechanisms):
5126 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005127 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005128
5129/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005130 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005131 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005132 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005133 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005134 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005135garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005136{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005137 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005138 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005139 buf_T *buf;
5140 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005141 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005142 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005143
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005144 if (!testing)
5145 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005146 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005147 want_garbage_collect = FALSE;
5148 may_garbage_collect = FALSE;
5149 garbage_collect_at_exit = FALSE;
5150 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005151
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005152 // The execution stack can grow big, limit the size.
5153 if (exestack.ga_maxlen - exestack.ga_len > 500)
5154 {
5155 size_t new_len;
5156 char_u *pp;
5157 int n;
5158
5159 // Keep 150% of the current size, with a minimum of the growth size.
5160 n = exestack.ga_len / 2;
5161 if (n < exestack.ga_growsize)
5162 n = exestack.ga_growsize;
5163
5164 // Don't make it bigger though.
5165 if (exestack.ga_len + n < exestack.ga_maxlen)
5166 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005167 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005168 pp = vim_realloc(exestack.ga_data, new_len);
5169 if (pp == NULL)
5170 return FAIL;
5171 exestack.ga_maxlen = exestack.ga_len + n;
5172 exestack.ga_data = pp;
5173 }
5174 }
5175
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005176 // We advance by two because we add one for items referenced through
5177 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005178 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005179
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005180 /*
5181 * 1. Go through all accessible variables and mark all lists and dicts
5182 * with copyID.
5183 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005184
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005185 // Don't free variables in the previous_funccal list unless they are only
5186 // referenced through previous_funccal. This must be first, because if
5187 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005188 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005189
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005190 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005191 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005192
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005193 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005194 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005195 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5196 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005197
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005198 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005199 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005200 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5201 NULL, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005202 // window-local variables in autocmd windows
Bram Moolenaare76062c2022-11-28 18:51:43 +00005203 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005204 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00005205 abort = abort || set_ref_in_item(
5206 &aucmd_win[i].auc_win->w_winvar.di_tv, copyID, NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005207#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005208 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005209 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5210 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005211 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005212 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005213 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5214 NULL, NULL);
5215#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005216
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005217 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005218 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005219 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5220 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005221 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005222 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005223
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005224 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005225 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005226
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005227 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005228 abort = abort || set_ref_in_functions(copyID);
5229
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005230 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005231 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005232
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005233 // funcstacks keep variables for closures
5234 abort = abort || set_ref_in_funcstacks(copyID);
5235
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005236 // loopvars keep variables for loop blocks
5237 abort = abort || set_ref_in_loopvars(copyID);
5238
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005239 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005240 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005241
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005242 // callbacks in buffers
5243 abort = abort || set_ref_in_buffers(copyID);
5244
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005245 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5246 abort = abort || set_ref_in_insexpand_funcs(copyID);
5247
5248 // 'operatorfunc' callback
5249 abort = abort || set_ref_in_opfunc(copyID);
5250
5251 // 'tagfunc' callback
5252 abort = abort || set_ref_in_tagfunc(copyID);
5253
5254 // 'imactivatefunc' and 'imstatusfunc' callbacks
5255 abort = abort || set_ref_in_im_funcs(copyID);
5256
Bram Moolenaar1dced572012-04-05 16:54:08 +02005257#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005258 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005259#endif
5260
Bram Moolenaardb913952012-06-29 12:54:53 +02005261#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005262 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005263#endif
5264
5265#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005266 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005267#endif
5268
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005269#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005270 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005271 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005272#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005273#ifdef FEAT_NETBEANS_INTG
5274 abort = abort || set_ref_in_nb_channel(copyID);
5275#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005276
Bram Moolenaare3188e22016-05-31 21:13:04 +02005277#ifdef FEAT_TIMERS
5278 abort = abort || set_ref_in_timer(copyID);
5279#endif
5280
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005281#ifdef FEAT_QUICKFIX
5282 abort = abort || set_ref_in_quickfix(copyID);
5283#endif
5284
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005285#ifdef FEAT_TERMINAL
5286 abort = abort || set_ref_in_term(copyID);
5287#endif
5288
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005289#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005290 abort = abort || set_ref_in_popups(copyID);
5291#endif
5292
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005293 abort = abort || set_ref_in_classes(copyID);
5294
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005295 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005296 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005297 /*
5298 * 2. Free lists and dictionaries that are not referenced.
5299 */
5300 did_free = free_unref_items(copyID);
5301
5302 /*
5303 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005304 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005305 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005306 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005307 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005308 else if (p_verbose > 0)
5309 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005310 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005311 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005312
5313 return did_free;
5314}
5315
5316/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005317 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005318 */
5319 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005320free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005321{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005322 int did_free = FALSE;
5323
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005324 // Let all "free" functions know that we are here. This means no
5325 // dictionaries, lists, channels or jobs are to be freed, because we will
5326 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005327 in_free_unref_items = TRUE;
5328
5329 /*
5330 * PASS 1: free the contents of the items. We don't free the items
5331 * themselves yet, so that it is possible to decrement refcount counters
5332 */
5333
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005334 // Go through the list of dicts and free items without this copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005335 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005336
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005337 // Go through the list of lists and free items without this copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005338 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005339
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005340 // Go through the list of objects and free items without this copyID.
5341 did_free |= object_free_nonref(copyID);
5342
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005343 // Go through the list of classes and free items without this copyID.
5344 did_free |= class_free_nonref(copyID);
5345
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005346#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005347 // Go through the list of jobs and free items without the copyID. This
5348 // must happen before doing channels, because jobs refer to channels, but
5349 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005350 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5351
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005352 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005353 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5354#endif
5355
5356 /*
5357 * PASS 2: free the items themselves.
5358 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005359 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005360 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005361
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005362#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005363 // Go through the list of jobs and free items without the copyID. This
5364 // must happen before doing channels, because jobs refer to channels, but
5365 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005366 free_unused_jobs(copyID, COPYID_MASK);
5367
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005368 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005369 free_unused_channels(copyID, COPYID_MASK);
5370#endif
5371
5372 in_free_unref_items = FALSE;
5373
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005374 return did_free;
5375}
5376
5377/*
5378 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005379 * "list_stack" is used to add lists to be marked. Can be NULL.
5380 *
5381 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005382 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005383 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005384set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005385{
5386 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005387 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005388 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005389 hashtab_T *cur_ht;
5390 ht_stack_T *ht_stack = NULL;
5391 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005392
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005393 cur_ht = ht;
5394 for (;;)
5395 {
5396 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005397 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005398 // Mark each item in the hashtab. If the item contains a hashtab
5399 // it is added to ht_stack, if it contains a list it is added to
5400 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005401 todo = (int)cur_ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00005402 FOR_ALL_HASHTAB_ITEMS(cur_ht, hi, todo)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005403 if (!HASHITEM_EMPTY(hi))
5404 {
5405 --todo;
5406 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5407 &ht_stack, list_stack);
5408 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005409 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005410
5411 if (ht_stack == NULL)
5412 break;
5413
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005414 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005415 cur_ht = ht_stack->ht;
5416 tempitem = ht_stack;
5417 ht_stack = ht_stack->prev;
5418 free(tempitem);
5419 }
5420
5421 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005422}
5423
Dominique Pelle748b3082022-01-08 12:41:16 +00005424#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5425 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005426/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005427 * Mark a dict and its items with "copyID".
5428 * Returns TRUE if setting references failed somehow.
5429 */
5430 int
5431set_ref_in_dict(dict_T *d, int copyID)
5432{
5433 if (d != NULL && d->dv_copyID != copyID)
5434 {
5435 d->dv_copyID = copyID;
5436 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5437 }
5438 return FALSE;
5439}
Dominique Pelle748b3082022-01-08 12:41:16 +00005440#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005441
5442/*
5443 * Mark a list and its items with "copyID".
5444 * Returns TRUE if setting references failed somehow.
5445 */
5446 int
5447set_ref_in_list(list_T *ll, int copyID)
5448{
5449 if (ll != NULL && ll->lv_copyID != copyID)
5450 {
5451 ll->lv_copyID = copyID;
5452 return set_ref_in_list_items(ll, copyID, NULL);
5453 }
5454 return FALSE;
5455}
5456
5457/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005458 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005459 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5460 *
5461 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005462 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005463 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005464set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005465{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005466 listitem_T *li;
5467 int abort = FALSE;
5468 list_T *cur_l;
5469 list_stack_T *list_stack = NULL;
5470 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005471
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005472 cur_l = l;
5473 for (;;)
5474 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005475 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005476 // Mark each item in the list. If the item contains a hashtab
5477 // it is added to ht_stack, if it contains a list it is added to
5478 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005479 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5480 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5481 ht_stack, &list_stack);
5482 if (list_stack == NULL)
5483 break;
5484
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005485 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005486 cur_l = list_stack->list;
5487 tempitem = list_stack;
5488 list_stack = list_stack->prev;
5489 free(tempitem);
5490 }
5491
5492 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005493}
5494
5495/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005496 * Mark the partial in callback 'cb' with "copyID".
5497 */
5498 int
5499set_ref_in_callback(callback_T *cb, int copyID)
5500{
5501 typval_T tv;
5502
5503 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5504 return FALSE;
5505
5506 tv.v_type = VAR_PARTIAL;
5507 tv.vval.v_partial = cb->cb_partial;
5508 return set_ref_in_item(&tv, copyID, NULL, NULL);
5509}
5510
5511/*
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005512 * Mark the dict "dd" with "copyID".
5513 * Also see set_ref_in_item().
5514 */
5515 static int
5516set_ref_in_item_dict(
5517 dict_T *dd,
5518 int copyID,
5519 ht_stack_T **ht_stack,
5520 list_stack_T **list_stack)
5521{
5522 if (dd == NULL || dd->dv_copyID == copyID)
5523 return FALSE;
5524
5525 // Didn't see this dict yet.
5526 dd->dv_copyID = copyID;
5527 if (ht_stack == NULL)
5528 return set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5529
5530 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5531 if (newitem == NULL)
5532 return TRUE;
5533
5534 newitem->ht = &dd->dv_hashtab;
5535 newitem->prev = *ht_stack;
5536 *ht_stack = newitem;
5537
5538 return FALSE;
5539}
5540
5541/*
5542 * Mark the list "ll" with "copyID".
5543 * Also see set_ref_in_item().
5544 */
5545 static int
5546set_ref_in_item_list(
5547 list_T *ll,
5548 int copyID,
5549 ht_stack_T **ht_stack,
5550 list_stack_T **list_stack)
5551{
5552 if (ll == NULL || ll->lv_copyID == copyID)
5553 return FALSE;
5554
5555 // Didn't see this list yet.
5556 ll->lv_copyID = copyID;
5557 if (list_stack == NULL)
5558 return set_ref_in_list_items(ll, copyID, ht_stack);
5559
5560 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5561 if (newitem == NULL)
5562 return TRUE;
5563
5564 newitem->list = ll;
5565 newitem->prev = *list_stack;
5566 *list_stack = newitem;
5567
5568 return FALSE;
5569}
5570
5571/*
5572 * Mark the partial "pt" with "copyID".
5573 * Also see set_ref_in_item().
5574 */
5575 static int
5576set_ref_in_item_partial(
5577 partial_T *pt,
5578 int copyID,
5579 ht_stack_T **ht_stack,
5580 list_stack_T **list_stack)
5581{
5582 if (pt == NULL || pt->pt_copyID == copyID)
5583 return FALSE;
5584
5585 // Didn't see this partial yet.
5586 pt->pt_copyID = copyID;
5587
5588 int abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
5589
5590 if (pt->pt_dict != NULL)
5591 {
5592 typval_T dtv;
5593
5594 dtv.v_type = VAR_DICT;
5595 dtv.vval.v_dict = pt->pt_dict;
5596 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5597 }
5598
5599 for (int i = 0; i < pt->pt_argc; ++i)
5600 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5601 ht_stack, list_stack);
5602 // pt_funcstack is handled in set_ref_in_funcstacks()
5603 // pt_loopvars is handled in set_ref_in_loopvars()
5604
5605 return abort;
5606}
5607
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005608#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005609/*
5610 * Mark the job "pt" with "copyID".
5611 * Also see set_ref_in_item().
5612 */
5613 static int
5614set_ref_in_item_job(
5615 job_T *job,
5616 int copyID,
5617 ht_stack_T **ht_stack,
5618 list_stack_T **list_stack)
5619{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005620 typval_T dtv;
5621
5622 if (job == NULL || job->jv_copyID == copyID)
5623 return FALSE;
5624
5625 job->jv_copyID = copyID;
5626 if (job->jv_channel != NULL)
5627 {
5628 dtv.v_type = VAR_CHANNEL;
5629 dtv.vval.v_channel = job->jv_channel;
5630 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5631 }
5632 if (job->jv_exit_cb.cb_partial != NULL)
5633 {
5634 dtv.v_type = VAR_PARTIAL;
5635 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
5636 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5637 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005638
5639 return FALSE;
5640}
5641
5642/*
5643 * Mark the channel "ch" with "copyID".
5644 * Also see set_ref_in_item().
5645 */
5646 static int
5647set_ref_in_item_channel(
5648 channel_T *ch,
5649 int copyID,
5650 ht_stack_T **ht_stack,
5651 list_stack_T **list_stack)
5652{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005653 typval_T dtv;
5654
5655 if (ch == NULL || ch->ch_copyID == copyID)
5656 return FALSE;
5657
5658 ch->ch_copyID = copyID;
5659 for (ch_part_T part = PART_SOCK; part < PART_COUNT; ++part)
5660 {
5661 for (jsonq_T *jq = ch->ch_part[part].ch_json_head.jq_next;
5662 jq != NULL; jq = jq->jq_next)
5663 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5664 for (cbq_T *cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5665 cq = cq->cq_next)
5666 if (cq->cq_callback.cb_partial != NULL)
5667 {
5668 dtv.v_type = VAR_PARTIAL;
5669 dtv.vval.v_partial = cq->cq_callback.cb_partial;
5670 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5671 }
5672 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
5673 {
5674 dtv.v_type = VAR_PARTIAL;
5675 dtv.vval.v_partial = ch->ch_part[part].ch_callback.cb_partial;
5676 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5677 }
5678 }
5679 if (ch->ch_callback.cb_partial != NULL)
5680 {
5681 dtv.v_type = VAR_PARTIAL;
5682 dtv.vval.v_partial = ch->ch_callback.cb_partial;
5683 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5684 }
5685 if (ch->ch_close_cb.cb_partial != NULL)
5686 {
5687 dtv.v_type = VAR_PARTIAL;
5688 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
5689 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5690 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005691
5692 return FALSE;
5693}
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005694#endif
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005695
5696/*
5697 * Mark the class "cl" with "copyID".
5698 * Also see set_ref_in_item().
5699 */
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005700 int
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005701set_ref_in_item_class(
5702 class_T *cl,
5703 int copyID,
5704 ht_stack_T **ht_stack,
5705 list_stack_T **list_stack)
5706{
5707 int abort = FALSE;
5708
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005709 if (cl == NULL || cl->class_copyID == copyID)
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005710 return FALSE;
5711
5712 cl->class_copyID = copyID;
Yegappan Lakshmanan544be0d2023-09-04 22:14:28 +02005713 if (cl->class_members_tv != NULL)
5714 {
5715 // The "class_members_tv" table is allocated only for regular classes
5716 // and not for interfaces.
5717 for (int i = 0; !abort && i < cl->class_class_member_count; ++i)
5718 abort = abort || set_ref_in_item(
5719 &cl->class_members_tv[i],
5720 copyID, ht_stack, list_stack);
5721 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005722
5723 for (int i = 0; !abort && i < cl->class_class_function_count; ++i)
5724 abort = abort || set_ref_in_func(NULL,
5725 cl->class_class_functions[i], copyID);
5726
5727 for (int i = 0; !abort && i < cl->class_obj_method_count; ++i)
5728 abort = abort || set_ref_in_func(NULL,
5729 cl->class_obj_methods[i], copyID);
5730
5731 return abort;
5732}
5733
5734/*
5735 * Mark the object "cl" with "copyID".
5736 * Also see set_ref_in_item().
5737 */
5738 static int
5739set_ref_in_item_object(
5740 object_T *obj,
5741 int copyID,
5742 ht_stack_T **ht_stack,
5743 list_stack_T **list_stack)
5744{
5745 int abort = FALSE;
5746
5747 if (obj == NULL || obj->obj_copyID == copyID)
5748 return FALSE;
5749
5750 obj->obj_copyID = copyID;
5751
5752 // The typval_T array is right after the object_T.
5753 typval_T *mtv = (typval_T *)(obj + 1);
5754 for (int i = 0; !abort
5755 && i < obj->obj_class->class_obj_member_count; ++i)
5756 abort = abort || set_ref_in_item(mtv + i, copyID,
5757 ht_stack, list_stack);
5758
5759 return abort;
5760}
5761
5762/*
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005763 * Mark all lists, dicts and other container types referenced through typval
5764 * "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005765 * "list_stack" is used to add lists to be marked. Can be NULL.
5766 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5767 *
5768 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005769 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005770 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005771set_ref_in_item(
5772 typval_T *tv,
5773 int copyID,
5774 ht_stack_T **ht_stack,
5775 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005776{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005777 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005778
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005779 switch (tv->v_type)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005780 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005781 case VAR_DICT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005782 return set_ref_in_item_dict(tv->vval.v_dict, copyID,
5783 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005784
5785 case VAR_LIST:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005786 return set_ref_in_item_list(tv->vval.v_list, copyID,
5787 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005788
5789 case VAR_FUNC:
5790 {
5791 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
5792 break;
5793 }
5794
5795 case VAR_PARTIAL:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005796 return set_ref_in_item_partial(tv->vval.v_partial, copyID,
5797 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005798
5799 case VAR_JOB:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005800#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005801 return set_ref_in_item_job(tv->vval.v_job, copyID,
5802 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005803#else
5804 break;
5805#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005806
5807 case VAR_CHANNEL:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005808#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005809 return set_ref_in_item_channel(tv->vval.v_channel, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005810 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005811#else
5812 break;
5813#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005814
5815 case VAR_CLASS:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005816 return set_ref_in_item_class(tv->vval.v_class, copyID,
5817 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005818
5819 case VAR_OBJECT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005820 return set_ref_in_item_object(tv->vval.v_object, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005821 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005822
5823 case VAR_UNKNOWN:
5824 case VAR_ANY:
5825 case VAR_VOID:
5826 case VAR_BOOL:
5827 case VAR_SPECIAL:
5828 case VAR_NUMBER:
5829 case VAR_FLOAT:
5830 case VAR_STRING:
5831 case VAR_BLOB:
5832 case VAR_INSTR:
5833 // Types that do not contain any other item
5834 break;
5835 }
5836
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005837 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005838}
5839
Bram Moolenaar8c711452005-01-14 21:53:12 +00005840/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005841 * Return a string with the string representation of a variable.
5842 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005843 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005844 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005845 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005846 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005847 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005848 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5849 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005850 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005851 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005852 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005853echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005854 typval_T *tv,
5855 char_u **tofree,
5856 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005857 int copyID,
5858 int echo_style,
5859 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005860 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005861{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005862 static int recurse = 0;
5863 char_u *r = NULL;
5864
Bram Moolenaar33570922005-01-25 22:26:29 +00005865 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005866 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005867 if (!did_echo_string_emsg)
5868 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005869 // Only give this message once for a recursive call to avoid
5870 // flooding the user with errors. And stop iterating over lists
5871 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005872 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005873 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005874 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005875 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005876 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005877 }
5878 ++recurse;
5879
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005880 switch (tv->v_type)
5881 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005882 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005883 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005884 {
5885 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005886 r = tv->vval.v_string;
5887 if (r == NULL)
5888 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005889 }
5890 else
5891 {
5892 *tofree = string_quote(tv->vval.v_string, FALSE);
5893 r = *tofree;
5894 }
5895 break;
5896
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005897 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005898 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005899 char_u buf[MAX_FUNC_NAME_LEN];
5900
5901 if (echo_style)
5902 {
LemonBoya5d35902022-04-29 21:15:02 +01005903 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5904 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005905 buf, MAX_FUNC_NAME_LEN);
5906 if (r == buf)
5907 {
5908 r = vim_strsave(buf);
5909 *tofree = r;
5910 }
5911 else
5912 *tofree = NULL;
5913 }
5914 else
5915 {
5916 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5917 : make_ufunc_name_readable(
5918 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5919 TRUE);
5920 r = *tofree;
5921 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005922 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005923 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005924
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005925 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005926 {
5927 partial_T *pt = tv->vval.v_partial;
5928 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005929 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005930 garray_T ga;
5931 int i;
5932 char_u *tf;
5933
5934 ga_init2(&ga, 1, 100);
5935 ga_concat(&ga, (char_u *)"function(");
5936 if (fname != NULL)
5937 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005938 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005939 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005940 && vim_isupper(fname[1]))
5941 {
5942 ga_concat(&ga, (char_u *)"'g:");
5943 ga_concat(&ga, fname + 1);
5944 }
5945 else
5946 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005947 vim_free(fname);
5948 }
5949 if (pt != NULL && pt->pt_argc > 0)
5950 {
5951 ga_concat(&ga, (char_u *)", [");
5952 for (i = 0; i < pt->pt_argc; ++i)
5953 {
5954 if (i > 0)
5955 ga_concat(&ga, (char_u *)", ");
5956 ga_concat(&ga,
5957 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5958 vim_free(tf);
5959 }
5960 ga_concat(&ga, (char_u *)"]");
5961 }
5962 if (pt != NULL && pt->pt_dict != NULL)
5963 {
5964 typval_T dtv;
5965
5966 ga_concat(&ga, (char_u *)", ");
5967 dtv.v_type = VAR_DICT;
5968 dtv.vval.v_dict = pt->pt_dict;
5969 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5970 vim_free(tf);
5971 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005972 // terminate with ')' and a NUL
5973 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005974
5975 *tofree = ga.ga_data;
5976 r = *tofree;
5977 break;
5978 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005979
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005980 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005981 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005982 break;
5983
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005985 if (tv->vval.v_list == NULL)
5986 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005987 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005988 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005989 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005990 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005991 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5992 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005993 {
5994 *tofree = NULL;
5995 r = (char_u *)"[...]";
5996 }
5997 else
5998 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005999 int old_copyID = tv->vval.v_list->lv_copyID;
6000
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006001 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006002 *tofree = list2string(tv, copyID, restore_copyID);
6003 if (restore_copyID)
6004 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006005 r = *tofree;
6006 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006007 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006008
Bram Moolenaar8c711452005-01-14 21:53:12 +00006009 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006010 if (tv->vval.v_dict == NULL)
6011 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006012 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006013 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006014 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006015 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006016 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6017 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006018 {
6019 *tofree = NULL;
6020 r = (char_u *)"{...}";
6021 }
6022 else
6023 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006024 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006025
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006026 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006027 *tofree = dict2string(tv, copyID, restore_copyID);
6028 if (restore_copyID)
6029 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006030 r = *tofree;
6031 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006032 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006033
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006034 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006035 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006036 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006037 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006038 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006039 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006040 break;
6041
Bram Moolenaar835dc632016-02-07 14:27:38 +01006042 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006043 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006044#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00006045 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02006046 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
6047 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006048 if (composite_val)
6049 {
6050 *tofree = string_quote(r, FALSE);
6051 r = *tofree;
6052 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006053#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006055
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006056 case VAR_INSTR:
6057 *tofree = NULL;
6058 r = (char_u *)"instructions";
6059 break;
6060
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006061 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006062 {
6063 class_T *cl = tv->vval.v_class;
6064 size_t len = 6 + (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
6065 r = *tofree = alloc(len);
6066 vim_snprintf((char *)r, len, "class %s",
6067 cl == NULL ? "[unknown]" : (char *)cl->class_name);
6068 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006069 break;
6070
6071 case VAR_OBJECT:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006072 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006073 garray_T ga;
6074 ga_init2(&ga, 1, 50);
6075 ga_concat(&ga, (char_u *)"object of ");
6076 object_T *obj = tv->vval.v_object;
6077 class_T *cl = obj == NULL ? NULL : obj->obj_class;
6078 ga_concat(&ga, cl == NULL ? (char_u *)"[unknown]"
6079 : cl->class_name);
6080 if (cl != NULL)
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006081 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006082 ga_concat(&ga, (char_u *)" {");
6083 for (int i = 0; i < cl->class_obj_member_count; ++i)
6084 {
6085 if (i > 0)
6086 ga_concat(&ga, (char_u *)", ");
Bram Moolenaard505d172022-12-18 21:42:55 +00006087 ocmember_T *m = &cl->class_obj_members[i];
6088 ga_concat(&ga, m->ocm_name);
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006089 ga_concat(&ga, (char_u *)": ");
6090 char_u *tf = NULL;
6091 ga_concat(&ga, echo_string_core(
6092 (typval_T *)(obj + 1) + i,
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006093 &tf, numbuf, copyID, echo_style,
6094 restore_copyID, composite_val));
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006095 vim_free(tf);
6096 }
6097 ga_concat(&ga, (char_u *)"}");
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006098 }
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006099
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006100 *tofree = r = ga.ga_data;
6101 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006102 break;
6103
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006104 case VAR_FLOAT:
6105 *tofree = NULL;
6106 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6107 r = numbuf;
6108 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006109
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006110 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006111 case VAR_SPECIAL:
6112 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006113 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006114 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006115 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006116
Bram Moolenaar8502c702014-06-17 12:51:16 +02006117 if (--recurse == 0)
6118 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006119 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006120}
6121
6122/*
6123 * Return a string with the string representation of a variable.
6124 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6125 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006126 * Does not put quotes around strings, as ":echo" displays values.
6127 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6128 * May return NULL.
6129 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006130 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006131echo_string(
6132 typval_T *tv,
6133 char_u **tofree,
6134 char_u *numbuf,
6135 int copyID)
6136{
6137 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6138}
6139
6140/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006141 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6142 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006143 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006144 */
6145 int
6146buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6147{
6148 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006149 char_u *t;
6150 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006151
6152 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6153 return -1;
6154
6155 if (lnum > buf->b_ml.ml_line_count)
6156 lnum = buf->b_ml.ml_line_count;
6157
6158 str = ml_get_buf(buf, lnum, FALSE);
6159 if (str == NULL)
6160 return -1;
6161
6162 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006163 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006164
Bram Moolenaar91458462021-01-13 20:08:38 +01006165 // count the number of characters
6166 t = str;
6167 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6168 t += mb_ptr2len(t);
6169
6170 // In insert mode, when the cursor is at the end of a non-empty line,
6171 // byteidx points to the NUL character immediately past the end of the
6172 // string. In this case, add one to the character count.
6173 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6174 count++;
6175
6176 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006177}
6178
6179/*
6180 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006181 * byte index. Works only for loaded buffers. Returns -1 on failure.
6182 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006183 */
6184 int
6185buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6186{
6187 char_u *str;
6188 char_u *t;
6189
6190 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6191 return -1;
6192
6193 if (lnum > buf->b_ml.ml_line_count)
6194 lnum = buf->b_ml.ml_line_count;
6195
6196 str = ml_get_buf(buf, lnum, FALSE);
6197 if (str == NULL)
6198 return -1;
6199
6200 // Convert the character offset to a byte offset
6201 t = str;
6202 while (*t != NUL && --charidx > 0)
6203 t += mb_ptr2len(t);
6204
Bram Moolenaar91458462021-01-13 20:08:38 +01006205 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006206}
6207
6208/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006210 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006212 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006213var2fpos(
6214 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006215 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006216 int *fnum, // set to fnum for '0, 'A, etc.
6217 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006219 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006221 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006223 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006224 if (varp->v_type == VAR_LIST)
6225 {
6226 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006227 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006228 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006229 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006230
6231 l = varp->vval.v_list;
6232 if (l == NULL)
6233 return NULL;
6234
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006235 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006236 pos.lnum = list_find_nr(l, 0L, &error);
6237 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006238 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006239 if (charcol)
6240 len = (long)mb_charlen(ml_get(pos.lnum));
6241 else
6242 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006243
Bram Moolenaarec65d772020-08-20 22:29:12 +02006244 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006245 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006246 li = list_find(l, 1L);
6247 if (li != NULL && li->li_tv.v_type == VAR_STRING
6248 && li->li_tv.vval.v_string != NULL
6249 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006250 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006251 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006252 }
6253 else
6254 {
6255 pos.col = list_find_nr(l, 1L, &error);
6256 if (error)
6257 return NULL;
6258 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006259
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006260 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006261 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006262 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006263 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006264
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006265 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006266 pos.coladd = list_find_nr(l, 2L, &error);
6267 if (error)
6268 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006269
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006270 return &pos;
6271 }
6272
Bram Moolenaarc5809432021-03-27 21:23:30 +01006273 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6274 return NULL;
6275
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006276 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006277 if (name == NULL)
6278 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006279
6280 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006281 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006282 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006283 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006284 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006285 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006286 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006287 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006288 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006289 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006290 pos = VIsual;
6291 else
6292 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006293 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006294 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006295 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006297 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006298 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6300 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006301 pos = *pp;
6302 }
6303 if (pos.lnum != 0)
6304 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006305 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006306 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6307 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006309
Bram Moolenaara5525202006-03-02 22:52:09 +00006310 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006311
Bram Moolenaar477933c2007-07-17 14:32:23 +00006312 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006313 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006314 // the "w_valid" flags are not reset when moving the cursor, but they
6315 // do matter for update_topline() and validate_botline().
6316 check_cursor_moved(curwin);
6317
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006318 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006319 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006320 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006321 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006322 // In silent Ex mode topline is zero, but that's not a valid line
6323 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006324 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006325 return &pos;
6326 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006327 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006328 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006329 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006330 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006331 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006332 return &pos;
6333 }
6334 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006335 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006337 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 {
6339 pos.lnum = curbuf->b_ml.ml_line_count;
6340 pos.col = 0;
6341 }
6342 else
6343 {
6344 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006345 if (charcol)
6346 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6347 else
6348 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 }
6350 return &pos;
6351 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006352 if (in_vim9script())
6353 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 return NULL;
6355}
6356
6357/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006358 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006359 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006360 * Note that the column is passed on as-is, the caller may want to decrement
6361 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006362 * If "charcol" is TRUE use the column as the character index instead of the
6363 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006364 * Return FAIL when conversion is not possible, doesn't check the position for
6365 * validity.
6366 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006367 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006368list2fpos(
6369 typval_T *arg,
6370 pos_T *posp,
6371 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006372 colnr_T *curswantp,
6373 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006374{
6375 list_T *l = arg->vval.v_list;
6376 long i = 0;
6377 long n;
6378
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006379 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6380 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006381 if (arg->v_type != VAR_LIST
6382 || l == NULL
6383 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006384 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006385 return FAIL;
6386
6387 if (fnump != NULL)
6388 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006389 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006390 if (n < 0)
6391 return FAIL;
6392 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006393 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006394 *fnump = n;
6395 }
6396
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006397 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006398 if (n < 0)
6399 return FAIL;
6400 posp->lnum = n;
6401
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006402 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006403 if (n < 0)
6404 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006405 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006406 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006407 if (charcol)
6408 {
6409 buf_T *buf;
6410
6411 // Get the text for the specified line in a loaded buffer
6412 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6413 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6414 return FAIL;
6415
Bram Moolenaar79f23442022-10-10 12:42:57 +01006416 n = buf_charidx_to_byteidx(buf,
6417 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006418 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006419 posp->col = n;
6420
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006421 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006422 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006423 posp->coladd = 0;
6424 else
6425 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006426
Bram Moolenaar493c1782014-05-28 14:34:46 +02006427 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006428 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006429
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006430 return OK;
6431}
6432
6433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 * Get the length of an environment variable name.
6435 * Advance "arg" to the first character after the name.
6436 * Return 0 for error.
6437 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006438 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006439get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440{
6441 char_u *p;
6442 int len;
6443
6444 for (p = *arg; vim_isIDc(*p); ++p)
6445 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006446 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 return 0;
6448
6449 len = (int)(p - *arg);
6450 *arg = p;
6451 return len;
6452}
6453
6454/*
6455 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006456 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 * Return 0 if something is wrong.
6458 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006459 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006460get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461{
6462 char_u *p;
6463 int len;
6464
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006465 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006467 {
6468 if (*p == ':')
6469 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006470 // "s:" is start of "s:var", but "n:" is not and can be used in
6471 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006472 len = (int)(p - *arg);
6473 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6474 || len > 1)
6475 break;
6476 }
6477 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006478 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 return 0;
6480
6481 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006482 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483
6484 return len;
6485}
6486
6487/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006488 * Get the length of the name of a variable or function.
6489 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006491 * Return -1 if curly braces expansion failed.
6492 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 * If the name contains 'magic' {}'s, expand them and return the
6494 * expanded name in an allocated string via 'alias' - caller must free.
6495 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006496 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006497get_name_len(
6498 char_u **arg,
6499 char_u **alias,
6500 int evaluate,
6501 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502{
6503 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 char_u *p;
6505 char_u *expr_start;
6506 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006508 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509
6510 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6511 && (*arg)[2] == (int)KE_SNR)
6512 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006513 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 *arg += 3;
6515 return get_id_len(arg) + 3;
6516 }
6517 len = eval_fname_script(*arg);
6518 if (len > 0)
6519 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006520 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 *arg += len;
6522 }
6523
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006525 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006527 p = find_name_end(*arg, &expr_start, &expr_end,
6528 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 if (expr_start != NULL)
6530 {
6531 char_u *temp_string;
6532
6533 if (!evaluate)
6534 {
6535 len += (int)(p - *arg);
6536 *arg = skipwhite(p);
6537 return len;
6538 }
6539
6540 /*
6541 * Include any <SID> etc in the expanded string:
6542 * Thus the -len here.
6543 */
6544 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6545 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006546 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 *alias = temp_string;
6548 *arg = skipwhite(p);
6549 return (int)STRLEN(temp_string);
6550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551
6552 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006553 // Only give an error when there is something, otherwise it will be
6554 // reported at a higher level.
6555 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006556 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557
6558 return len;
6559}
6560
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006561/*
6562 * Find the end of a variable or function name, taking care of magic braces.
6563 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6564 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006565 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006566 * Return a pointer to just after the name. Equal to "arg" if there is no
6567 * valid name.
6568 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006569 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006570find_name_end(
6571 char_u *arg,
6572 char_u **expr_start,
6573 char_u **expr_end,
6574 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006576 int mb_nest = 0;
6577 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006579 int len;
zeertzjqa93d9cd2023-05-02 16:25:47 +01006580 int allow_curly = (flags & FNE_ALLOW_CURLY) || !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006582 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006584 *expr_start = NULL;
6585 *expr_end = NULL;
6586 }
6587
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006588 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006589 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006590 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006591 return arg;
6592
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006593 for (p = arg; *p != NUL
6594 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006595 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006596 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006597 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006598 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006599 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006600 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006601 if (*p == '\'')
6602 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006603 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006604 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006605 ;
6606 if (*p == NUL)
6607 break;
6608 }
6609 else if (*p == '"')
6610 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006611 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006612 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006613 if (*p == '\\' && p[1] != NUL)
6614 ++p;
6615 if (*p == NUL)
6616 break;
6617 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006618 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6619 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006620 // "s:" is start of "s:var", but "n:" is not and can be used in
6621 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006622 len = (int)(p - arg);
6623 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006624 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006625 break;
6626 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006627
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006628 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006630 if (*p == '[')
6631 ++br_nest;
6632 else if (*p == ']')
6633 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006635
zeertzjqa93d9cd2023-05-02 16:25:47 +01006636 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006638 if (*p == '{')
6639 {
6640 mb_nest++;
6641 if (expr_start != NULL && *expr_start == NULL)
6642 *expr_start = p;
6643 }
6644 else if (*p == '}')
6645 {
6646 mb_nest--;
6647 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6648 *expr_end = p;
6649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 }
6652
6653 return p;
6654}
6655
6656/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006657 * Expands out the 'magic' {}'s in a variable/function name.
6658 * Note that this can call itself recursively, to deal with
6659 * constructs like foo{bar}{baz}{bam}
6660 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6661 * "in_start" ^
6662 * "expr_start" ^
6663 * "expr_end" ^
6664 * "in_end" ^
6665 *
6666 * Returns a new allocated string, which the caller must free.
6667 * Returns NULL for failure.
6668 */
6669 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006670make_expanded_name(
6671 char_u *in_start,
6672 char_u *expr_start,
6673 char_u *expr_end,
6674 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006675{
6676 char_u c1;
6677 char_u *retval = NULL;
6678 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006679
6680 if (expr_end == NULL || in_end == NULL)
6681 return NULL;
6682 *expr_start = NUL;
6683 *expr_end = NUL;
6684 c1 = *in_end;
6685 *in_end = NUL;
6686
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006687 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006688 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006689 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006690 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6691 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006692 if (retval != NULL)
6693 {
6694 STRCPY(retval, in_start);
6695 STRCAT(retval, temp_result);
6696 STRCAT(retval, expr_end + 1);
6697 }
6698 }
6699 vim_free(temp_result);
6700
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006701 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006702 *expr_start = '{';
6703 *expr_end = '}';
6704
6705 if (retval != NULL)
6706 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006707 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006708 if (expr_start != NULL)
6709 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006710 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006711 temp_result = make_expanded_name(retval, expr_start,
6712 expr_end, temp_result);
6713 vim_free(retval);
6714 retval = temp_result;
6715 }
6716 }
6717
6718 return retval;
6719}
6720
6721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006723 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006725 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006726eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006728 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006729}
6730
6731/*
6732 * Return TRUE if character "c" can be used as the first character in a
6733 * variable or function name (excluding '{' and '}').
6734 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006735 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006736eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006737{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006738 return ASCII_ISALPHA(c) || c == '_';
6739}
6740
6741/*
6742 * Return TRUE if character "c" can be used as the first character of a
6743 * dictionary key.
6744 */
6745 int
6746eval_isdictc(int c)
6747{
6748 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749}
6750
6751/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006752 * Handle:
6753 * - expr[expr], expr[expr:expr] subscript
6754 * - ".name" lookup
6755 * - function call with Funcref variable: func(expr)
6756 * - method call: var->method()
6757 *
6758 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006759 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006760 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006761 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006762handle_subscript(
6763 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006764 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006765 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006766 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006767 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006768{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006769 int evaluate = evalarg != NULL
6770 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006771 int ret = OK;
6772 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006773 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006774 int getnext;
6775 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006776
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006777 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006778 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006779 // When at the end of the line and ".name" or "->{" or "->X" follows in
6780 // the next line then consume the line break.
6781 p = eval_next_non_blank(*arg, evalarg, &getnext);
6782 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00006783 && ((*p == '.'
6784 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
6785 || rettv->v_type == VAR_CLASS
6786 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02006787 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6788 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6789 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006790 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006791 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006792 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006793 check_white = FALSE;
6794 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006795
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006796 if (rettv->v_type == VAR_ANY)
6797 {
6798 char_u *exp_name;
6799 int cc;
6800 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006801 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006802 type_T *type;
6803
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006804 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006805 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006806 if (**arg != '.')
6807 {
6808 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006809 semsg(_(e_expected_dot_after_name_str),
6810 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006811 ret = FAIL;
6812 break;
6813 }
6814 ++*arg;
6815 if (IS_WHITE_OR_NUL(**arg))
6816 {
6817 if (verbose)
6818 emsg(_(e_no_white_space_allowed_after_dot));
6819 ret = FAIL;
6820 break;
6821 }
6822
6823 // isolate the name
6824 exp_name = *arg;
6825 while (eval_isnamec(**arg))
6826 ++*arg;
6827 cc = **arg;
6828 **arg = NUL;
6829
6830 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01006831 evalarg == NULL ? NULL : evalarg->eval_cctx,
6832 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006833 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006834
6835 if (idx < 0 && ufunc == NULL)
6836 {
6837 ret = FAIL;
6838 break;
6839 }
6840 if (idx >= 0)
6841 {
6842 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6843 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6844
6845 copy_tv(sv->sv_tv, rettv);
6846 }
6847 else
6848 {
6849 rettv->v_type = VAR_FUNC;
6850 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6851 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006852 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006853 }
6854
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006855 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6856 || rettv->v_type == VAR_PARTIAL))
6857 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006858 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006859 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6860 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006861
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006862 // Stop the expression evaluation when immediately aborting on
6863 // error, or when an interrupt occurred or an exception was thrown
6864 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006865 if (aborting())
6866 {
6867 if (ret == OK)
6868 clear_tv(rettv);
6869 ret = FAIL;
6870 }
6871 dict_unref(selfdict);
6872 selfdict = NULL;
6873 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006874 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006875 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006876 if (in_vim9script())
6877 *arg = skipwhite(p + 2);
6878 else
6879 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00006880 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006881 {
zeertzjqc481ad32023-03-11 16:18:51 +00006882 emsg(_(e_no_white_space_allowed_before_parenthesis));
6883 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006884 }
zeertzjqc481ad32023-03-11 16:18:51 +00006885 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
6886 // expr->{lambda}() or expr->(lambda)()
6887 ret = eval_lambda(arg, rettv, evalarg, verbose);
6888 else
6889 // expr->name()
6890 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02006891 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006892 // "." is ".name" lookup when we found a dict or when evaluating and
6893 // scriptversion is at least 2, where string concatenation is "..".
6894 else if (**arg == '['
6895 || (**arg == '.' && (rettv->v_type == VAR_DICT
6896 || (!evaluate
6897 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006898 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006899 {
6900 dict_unref(selfdict);
6901 if (rettv->v_type == VAR_DICT)
6902 {
6903 selfdict = rettv->vval.v_dict;
6904 if (selfdict != NULL)
6905 ++selfdict->dv_refcount;
6906 }
6907 else
6908 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006909 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006910 {
6911 clear_tv(rettv);
6912 ret = FAIL;
6913 }
6914 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006915 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
6916 || rettv->v_type == VAR_OBJECT))
6917 {
6918 // class member: SomeClass.varname
6919 // class method: SomeClass.SomeMethod()
6920 // class constructor: SomeClass.new()
6921 // object member: someObject.varname
6922 // object method: someObject.SomeMethod()
6923 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
6924 {
6925 clear_tv(rettv);
6926 ret = FAIL;
6927 }
6928 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006929 else
6930 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006931 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006932
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006933 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6934 // Don't do this when "Func" is already a partial that was bound
6935 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006936 if (selfdict != NULL
6937 && (rettv->v_type == VAR_FUNC
6938 || (rettv->v_type == VAR_PARTIAL
6939 && (rettv->vval.v_partial->pt_auto
6940 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006941 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006942
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006943 dict_unref(selfdict);
6944 return ret;
6945}
6946
6947/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006948 * Make a copy of an item.
6949 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006950 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006951 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6952 * reference to an already copied list/dict can be used.
6953 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006954 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006955 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006956item_copy(
6957 typval_T *from,
6958 typval_T *to,
6959 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006960 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006961 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006962{
6963 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006964 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006965
Bram Moolenaar33570922005-01-25 22:26:29 +00006966 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006967 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006968 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006969 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006970 }
6971 ++recurse;
6972
6973 switch (from->v_type)
6974 {
6975 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006976 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006977 case VAR_STRING:
6978 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006979 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006980 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006981 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006982 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006983 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006984 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006985 case VAR_CLASS:
6986 case VAR_OBJECT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006987 copy_tv(from, to);
6988 break;
6989 case VAR_LIST:
6990 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006991 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006992 if (from->vval.v_list == NULL)
6993 to->vval.v_list = NULL;
6994 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6995 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006996 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006997 to->vval.v_list = from->vval.v_list->lv_copylist;
6998 ++to->vval.v_list->lv_refcount;
6999 }
7000 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007001 to->vval.v_list = list_copy(from->vval.v_list,
7002 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007003 if (to->vval.v_list == NULL)
7004 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007005 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007006 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007007 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007008 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007009 case VAR_DICT:
7010 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007011 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007012 if (from->vval.v_dict == NULL)
7013 to->vval.v_dict = NULL;
7014 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7015 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007016 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007017 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7018 ++to->vval.v_dict->dv_refcount;
7019 }
7020 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007021 to->vval.v_dict = dict_copy(from->vval.v_dict,
7022 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007023 if (to->vval.v_dict == NULL)
7024 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007025 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007026 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007027 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007028 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007029 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007030 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007031 }
7032 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007033 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007034}
7035
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007036 void
7037echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7038{
7039 char_u *tofree;
7040 char_u numbuf[NUMBUFLEN];
7041 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7042
7043 if (*atstart)
7044 {
7045 *atstart = FALSE;
7046 // Call msg_start() after eval1(), evaluating the expression
7047 // may cause a message to appear.
7048 if (with_space)
7049 {
7050 // Mark the saved text as finishing the line, so that what
7051 // follows is displayed on a new line when scrolling back
7052 // at the more prompt.
7053 msg_sb_eol();
7054 msg_start();
7055 }
7056 }
7057 else if (with_space)
7058 msg_puts_attr(" ", echo_attr);
7059
7060 if (p != NULL)
7061 for ( ; *p != NUL && !got_int; ++p)
7062 {
7063 if (*p == '\n' || *p == '\r' || *p == TAB)
7064 {
7065 if (*p != TAB && *needclr)
7066 {
7067 // remove any text still there from the command
7068 msg_clr_eos();
7069 *needclr = FALSE;
7070 }
7071 msg_putchar_attr(*p, echo_attr);
7072 }
7073 else
7074 {
7075 if (has_mbyte)
7076 {
7077 int i = (*mb_ptr2len)(p);
7078
7079 (void)msg_outtrans_len_attr(p, i, echo_attr);
7080 p += i - 1;
7081 }
7082 else
7083 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7084 }
7085 }
7086 vim_free(tofree);
7087}
7088
Bram Moolenaare9a41262005-01-15 22:18:47 +00007089/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 * ":echo expr1 ..." print each argument separated with a space, add a
7091 * newline at the end.
7092 * ":echon expr1 ..." print each argument plain.
7093 */
7094 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007095ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096{
7097 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007098 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007099 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 int needclr = TRUE;
7101 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007102 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007103 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007104 evalarg_T evalarg;
7105
Bram Moolenaare707c882020-07-01 13:07:37 +02007106 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107
7108 if (eap->skip)
7109 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007110 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007112 // If eval1() causes an error message the text from the command may
7113 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007114 need_clr_eos = needclr;
7115
Bram Moolenaar68db9962021-05-09 23:19:22 +02007116 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007117 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 {
7119 /*
7120 * Report the invalid expression unless the expression evaluation
7121 * has been cancelled due to an aborting error, an interrupt, or an
7122 * exception.
7123 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007124 if (!aborting() && did_emsg == did_emsg_before
7125 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007126 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007127 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 break;
7129 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007130 need_clr_eos = FALSE;
7131
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007133 {
7134 if (rettv.v_type == VAR_VOID)
7135 {
7136 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7137 break;
7138 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007139 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007140 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007141
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007142 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 arg = skipwhite(arg);
7144 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007145 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007146 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147
7148 if (eap->skip)
7149 --emsg_skip;
7150 else
7151 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007152 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 if (needclr)
7154 msg_clr_eos();
7155 if (eap->cmdidx == CMD_echo)
7156 msg_end();
7157 }
7158}
7159
7160/*
7161 * ":echohl {name}".
7162 */
7163 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007164ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007166 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167}
7168
7169/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007170 * Returns the :echo attribute
7171 */
7172 int
7173get_echo_attr(void)
7174{
7175 return echo_attr;
7176}
7177
7178/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 * ":execute expr1 ..." execute the result of an expression.
7180 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007181 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007183 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 * Each gets spaces around each argument and a newline at the end for
7185 * echo commands
7186 */
7187 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007188ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189{
7190 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007191 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 int ret = OK;
7193 char_u *p;
7194 garray_T ga;
7195 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007196 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197
7198 ga_init2(&ga, 1, 80);
7199
7200 if (eap->skip)
7201 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007202 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007204 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007205 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207
7208 if (!eap->skip)
7209 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007210 char_u buf[NUMBUFLEN];
7211
7212 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007213 {
7214 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7215 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007216 semsg(_(e_using_invalid_value_as_string_str),
7217 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007218 p = NULL;
7219 }
7220 else
7221 p = tv_get_string_buf(&rettv, buf);
7222 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007223 else
7224 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007225 if (p == NULL)
7226 {
7227 clear_tv(&rettv);
7228 ret = FAIL;
7229 break;
7230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 len = (int)STRLEN(p);
7232 if (ga_grow(&ga, len + 2) == FAIL)
7233 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007234 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 ret = FAIL;
7236 break;
7237 }
7238 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 ga.ga_len += len;
7242 }
7243
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007244 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 arg = skipwhite(arg);
7246 }
7247
7248 if (ret != FAIL && ga.ga_data != NULL)
7249 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007250 // use the first line of continuation lines for messages
7251 SOURCING_LNUM = start_lnum;
7252
Bram Moolenaar37fef162022-08-29 18:16:32 +01007253 if (eap->cmdidx == CMD_echomsg
7254 || eap->cmdidx == CMD_echowindow
7255 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007256 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007257 // Mark the already saved text as finishing the line, so that what
7258 // follows is displayed on a new line when scrolling back at the
7259 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007260 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007261 }
7262
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007263 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007264 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007265 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007266 out_flush();
7267 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007268 else if (eap->cmdidx == CMD_echowindow)
7269 {
7270#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007271 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007272#endif
7273 msg_attr(ga.ga_data, echo_attr);
7274#ifdef HAS_MESSAGE_WINDOW
7275 end_echowindow();
7276#endif
7277 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007278 else if (eap->cmdidx == CMD_echoconsole)
7279 {
7280 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7281 ui_write((char_u *)"\r\n", 2, TRUE);
7282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 else if (eap->cmdidx == CMD_echoerr)
7284 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007285 int save_did_emsg = did_emsg;
7286
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007287 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007288 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 if (!force_abort)
7290 did_emsg = save_did_emsg;
7291 }
7292 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007293 {
7294 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7295
7296 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7297 sticky_cmdmod_flags = cmdmod.cmod_flags
7298 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 do_cmdline((char_u *)ga.ga_data,
7300 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007301 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 }
7304
7305 ga_clear(&ga);
7306
7307 if (eap->skip)
7308 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007309 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310}
7311
7312/*
7313 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7314 * "arg" points to the "&" or '+' when called, to "option" when returning.
7315 * Returns NULL when no option name found. Otherwise pointer to the char
7316 * after the option name.
7317 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007318 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007319find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320{
7321 char_u *p = *arg;
7322
7323 ++p;
7324 if (*p == 'g' && p[1] == ':')
7325 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007326 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 p += 2;
7328 }
7329 else if (*p == 'l' && p[1] == ':')
7330 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007331 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 p += 2;
7333 }
7334 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007335 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336
7337 if (!ASCII_ISALPHA(*p))
7338 return NULL;
7339 *arg = p;
7340
7341 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007342 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 else
7344 while (ASCII_ISALPHA(*p))
7345 ++p;
7346 return p;
7347}
7348
7349/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007350 * Display script name where an item was last set.
7351 * Should only be invoked when 'verbose' is non-zero.
7352 */
7353 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007354last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007355{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007356 char_u *p;
7357
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007358 if (script_ctx.sc_sid == 0)
7359 return;
7360
7361 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7362 if (p == NULL)
7363 return;
7364
7365 verbose_enter();
7366 msg_puts(_("\n\tLast set from "));
7367 msg_puts((char *)p);
7368 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007369 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007370 msg_puts(_(line_msg));
7371 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007372 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007373 verbose_leave();
7374 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007375}
7376
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007377#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378
7379/*
7380 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007381 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 * "flags" can be "g" to do a global substitute.
7383 * Returns an allocated string, NULL for error.
7384 */
7385 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007386do_string_sub(
7387 char_u *str,
7388 char_u *pat,
7389 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007390 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007391 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392{
7393 int sublen;
7394 regmatch_T regmatch;
7395 int i;
7396 int do_all;
7397 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007398 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 garray_T ga;
7400 char_u *ret;
7401 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007402 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007404 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007406 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407
7408 ga_init2(&ga, 1, 200);
7409
7410 do_all = (flags[0] == 'g');
7411
7412 regmatch.rm_ic = p_ic;
7413 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7414 if (regmatch.regprog != NULL)
7415 {
7416 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007417 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7419 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007420 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007421 if (regmatch.startp[0] == regmatch.endp[0])
7422 {
7423 if (zero_width == regmatch.startp[0])
7424 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007425 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007426 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007427 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7428 (size_t)i);
7429 ga.ga_len += i;
7430 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007431 continue;
7432 }
7433 zero_width = regmatch.startp[0];
7434 }
7435
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 /*
7437 * Get some space for a temporary buffer to do the substitution
7438 * into. It will contain:
7439 * - The text up to where the match is.
7440 * - The substituted text.
7441 * - The text after the match.
7442 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007443 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007444 if (sublen <= 0)
7445 {
7446 ga_clear(&ga);
7447 break;
7448 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007449 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7451 {
7452 ga_clear(&ga);
7453 break;
7454 }
7455
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007456 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 i = (int)(regmatch.startp[0] - tail);
7458 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007459 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007460 (void)vim_regsub(&regmatch, sub, expr,
7461 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7462 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007464 tail = regmatch.endp[0];
7465 if (*tail == NUL)
7466 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 if (!do_all)
7468 break;
7469 }
7470
7471 if (ga.ga_data != NULL)
7472 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7473
Bram Moolenaar473de612013-06-08 18:19:48 +02007474 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 }
7476
7477 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7478 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007479 if (p_cpo == empty_option)
7480 p_cpo = save_cpo;
7481 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007482 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007483 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007484 // If it's still empty it was changed and restored, need to restore in
7485 // the complicated way.
7486 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007487 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007488 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490
7491 return ret;
7492}