blob: 4e1dbdeeaf35757d906829276aefeaee22c012c1 [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;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001028 int writing;
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 }
Ernie Rael18143d32023-09-04 22:30:41 +02001183 if (vim9script && writing && lp->ll_tv->v_type == VAR_CLASS
1184 && (lp->ll_tv->vval.v_class->class_flags & CLASS_INTERFACE) != 0)
1185 {
1186 if (!quiet)
1187 semsg(_(e_interface_static_direct_access_str),
1188 lp->ll_tv->vval.v_class->class_name, lp->ll_name);
1189 return NULL;
1190 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001191
Bram Moolenaar4525a572022-02-13 11:57:33 +00001192 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001193 {
1194 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001195 semsg(_(e_variable_already_declared_str), lp->ll_name);
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001196 return NULL;
1197 }
1198
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001199 /*
1200 * Loop until no more [idx] or .key is following.
1201 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001202 var1.v_type = VAR_UNKNOWN;
1203 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001204 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001205 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001206 vartype_T v_type = lp->ll_tv->v_type;
1207
1208 if (*p == '.' && v_type != VAR_DICT
1209 && v_type != VAR_OBJECT
1210 && v_type != VAR_CLASS)
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001211 {
1212 if (!quiet)
1213 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1214 return NULL;
1215 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001216 if (v_type != VAR_LIST
1217 && v_type != VAR_DICT
1218 && v_type != VAR_BLOB
1219 && v_type != VAR_OBJECT
1220 && v_type != VAR_CLASS)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001221 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001222 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001223 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001224 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001225 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001226
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001227 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001228 int r = OK;
Bram Moolenaard505d172022-12-18 21:42:55 +00001229 if (v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001230 r = rettv_list_alloc(lp->ll_tv);
Bram Moolenaard505d172022-12-18 21:42:55 +00001231 else if (v_type == VAR_BLOB
Bram Moolenaare65081d2021-06-27 15:04:05 +02001232 && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001233 r = rettv_blob_alloc(lp->ll_tv);
1234 if (r == FAIL)
1235 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001236
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001237 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001238 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001239 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001240 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001241 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001242 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001243
Bram Moolenaar4525a572022-02-13 11:57:33 +00001244 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001245 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001246 && lp->ll_tv == &v->di_tv
1247 && ht != NULL && ht == get_script_local_ht())
1248 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001249 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001250
1251 // Vim9 script local variable: get the type
1252 if (sv != NULL)
1253 lp->ll_valtype = sv->sv_type;
1254 }
1255
Bram Moolenaar8c711452005-01-14 21:53:12 +00001256 len = -1;
1257 if (*p == '.')
1258 {
1259 key = p + 1;
1260 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1261 ;
1262 if (len == 0)
1263 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001264 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001265 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001266 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001267 }
1268 p = key + len;
1269 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001270 else
1271 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001272 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001273 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001274 if (*p == ':')
1275 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001276 else
1277 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001278 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001279 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001280 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001281 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001282 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001283 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001284 clear_tv(&var1);
1285 return NULL;
1286 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001287 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001288 }
1289
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001290 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001291 if (*p == ':')
1292 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001293 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001294 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001295 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001296 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001297 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001298 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001299 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001300 if (rettv != NULL
1301 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001302 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001303 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001304 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001305 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001306 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001307 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001308 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001309 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001310 }
1311 p = skipwhite(p + 1);
1312 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001313 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001314 else
1315 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001316 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001317 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001318 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001319 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001320 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001321 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001322 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001323 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001324 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001325 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001326 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001327 clear_tv(&var2);
1328 return NULL;
1329 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001330 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001331 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001332 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001333 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001334 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001335
Bram Moolenaar8c711452005-01-14 21:53:12 +00001336 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001337 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001338 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001339 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001340 clear_tv(&var1);
1341 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001342 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001343 }
1344
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001345 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001346 ++p;
1347 }
1348
Bram Moolenaard505d172022-12-18 21:42:55 +00001349 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001350 {
1351 if (len == -1)
1352 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001353 // "[key]": get key from "var1"
1354 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001355 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001356 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001357 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001358 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001359 }
1360 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001361 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001362
1363 // a NULL dict is equivalent with an empty dict
1364 if (lp->ll_tv->vval.v_dict == NULL)
1365 {
1366 lp->ll_tv->vval.v_dict = dict_alloc();
1367 if (lp->ll_tv->vval.v_dict == NULL)
1368 {
1369 clear_tv(&var1);
1370 return NULL;
1371 }
1372 ++lp->ll_tv->vval.v_dict->dv_refcount;
1373 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001374 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001375
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001376 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001377
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001378 // When assigning to a scope dictionary check that a function and
1379 // variable name is valid (only variable name unless it is l: or
1380 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001381 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001382 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001383 int prevval;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001384
1385 if (len != -1)
1386 {
1387 prevval = key[len];
1388 key[len] = NUL;
1389 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001390 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001391 prevval = 0; // avoid compiler warning
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001392 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
zeertzjq91c75d12022-11-05 20:21:58 +00001393 && (rettv->v_type == VAR_FUNC
1394 || rettv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001395 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001396 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001397 if (len != -1)
1398 key[len] = prevval;
1399 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001400 {
1401 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001402 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001403 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001404 }
1405
Bram Moolenaar10c65862020-10-08 21:16:42 +02001406 if (lp->ll_valtype != NULL)
1407 // use the type of the member
1408 lp->ll_valtype = lp->ll_valtype->tt_member;
1409
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001410 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001411 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001412 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001413 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001414 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001415 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001416 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001417 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001418 return NULL;
1419 }
1420
Bram Moolenaar31b81602019-02-10 22:14:27 +01001421 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001422 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001423 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001424 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001425 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001426 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001427 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001428 }
1429 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001430 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001431 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001432 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001433 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001434 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001435 p = NULL;
1436 break;
1437 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001438 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001439 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001440 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1441 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001442 {
1443 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001444 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001445 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001446
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001447 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001448 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001449 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001450 else if (v_type == VAR_BLOB)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001451 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001452 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1453
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001454 /*
1455 * Get the number and item for the only or first index of the List.
1456 */
1457 if (empty1)
1458 lp->ll_n1 = 0;
1459 else
1460 // is number or string
1461 lp->ll_n1 = (long)tv_get_number(&var1);
1462 clear_tv(&var1);
1463
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001464 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001465 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001466 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001467 return NULL;
1468 }
1469 if (lp->ll_range && !lp->ll_empty2)
1470 {
1471 lp->ll_n2 = (long)tv_get_number(&var2);
1472 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001473 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1474 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001475 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001476 }
1477 lp->ll_blob = lp->ll_tv->vval.v_blob;
1478 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001479 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001480 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001481 else if (v_type == VAR_LIST)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001482 {
1483 /*
1484 * Get the number and item for the only or first index of the List.
1485 */
1486 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001487 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001488 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001489 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001490 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001491 clear_tv(&var1);
1492
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001493 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001494 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001495 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1496 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001497 if (lp->ll_li == NULL)
1498 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001499 clear_tv(&var2);
1500 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001501 }
1502
Bram Moolenaar10c65862020-10-08 21:16:42 +02001503 if (lp->ll_valtype != NULL)
1504 // use the type of the member
1505 lp->ll_valtype = lp->ll_valtype->tt_member;
1506
Bram Moolenaar8c711452005-01-14 21:53:12 +00001507 /*
1508 * May need to find the item or absolute index for the second
1509 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001510 * When no index given: "lp->ll_empty2" is TRUE.
1511 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001512 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001513 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001514 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001515 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001516 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001517 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001518 if (check_range_index_two(lp->ll_list,
1519 &lp->ll_n1, lp->ll_li,
1520 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001521 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001522 }
1523
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001524 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001525 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001526 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001527 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001528 class_T *cl = (v_type == VAR_OBJECT
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001529 && lp->ll_tv->vval.v_object != NULL)
1530 ? lp->ll_tv->vval.v_object->obj_class
1531 : lp->ll_tv->vval.v_class;
1532 // TODO: what if class is NULL?
1533 if (cl != NULL)
1534 {
1535 lp->ll_valtype = NULL;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001536
1537 if (flags & GLV_PREFER_FUNC)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001538 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001539 // First look for a function with this name.
1540 // round 1: class functions (skipped for an object)
1541 // round 2: object methods
1542 for (int round = v_type == VAR_OBJECT ? 2 : 1;
1543 round <= 2; ++round)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001544 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001545 int count = round == 1
1546 ? cl->class_class_function_count
1547 : cl->class_obj_method_count;
1548 ufunc_T **funcs = round == 1
1549 ? cl->class_class_functions
1550 : cl->class_obj_methods;
1551 for (int i = 0; i < count; ++i)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001552 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001553 ufunc_T *fp = funcs[i];
1554 char_u *ufname = (char_u *)fp->uf_name;
1555 if (STRNCMP(ufname, key, p - key) == 0
1556 && ufname[p - key] == NUL)
1557 {
1558 lp->ll_ufunc = fp;
1559 lp->ll_valtype = fp->uf_func_type;
1560 round = 3;
1561 break;
1562 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001563 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001564 }
1565 }
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001566
1567 if (lp->ll_valtype == NULL)
1568 {
1569 int count = v_type == VAR_OBJECT
1570 ? cl->class_obj_member_count
1571 : cl->class_class_member_count;
1572 ocmember_T *members = v_type == VAR_OBJECT
1573 ? cl->class_obj_members
1574 : cl->class_class_members;
1575 for (int i = 0; i < count; ++i)
1576 {
1577 ocmember_T *om = members + i;
1578 if (STRNCMP(om->ocm_name, key, p - key) == 0
1579 && om->ocm_name[p - key] == NUL)
1580 {
1581 switch (om->ocm_access)
1582 {
=?UTF-8?q?Ola=20S=C3=B6der?=d8742472023-03-05 13:12:32 +00001583 case VIM_ACCESS_PRIVATE:
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001584 semsg(_(e_cannot_access_private_member_str),
1585 om->ocm_name);
1586 return NULL;
=?UTF-8?q?Ola=20S=C3=B6der?=d8742472023-03-05 13:12:32 +00001587 case VIM_ACCESS_READ:
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001588 if ((flags & GLV_READ_ONLY) == 0)
1589 {
1590 semsg(_(e_member_is_not_writable_str),
1591 om->ocm_name);
1592 return NULL;
1593 }
1594 break;
=?UTF-8?q?Ola=20S=C3=B6der?=d8742472023-03-05 13:12:32 +00001595 case VIM_ACCESS_ALL:
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001596 break;
1597 }
1598
1599 lp->ll_valtype = om->ocm_type;
1600
1601 if (v_type == VAR_OBJECT)
1602 lp->ll_tv = ((typval_T *)(
1603 lp->ll_tv->vval.v_object + 1)) + i;
1604 else
1605 lp->ll_tv = &cl->class_members_tv[i];
1606 break;
1607 }
1608 }
1609 }
1610
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001611 if (lp->ll_valtype == NULL)
1612 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001613 if (v_type == VAR_OBJECT)
1614 semsg(_(e_object_member_not_found_str), key);
1615 else
1616 semsg(_(e_class_member_not_found_str), key);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001617 return NULL;
1618 }
1619 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001620 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001621 }
1622
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001623 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001624 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001625 return p;
1626}
1627
1628/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001629 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001630 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001631 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001632clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001633{
1634 vim_free(lp->ll_exp_name);
1635 vim_free(lp->ll_newkey);
1636}
1637
1638/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001639 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001640 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001641 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1642 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001643 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001644 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001645set_var_lval(
1646 lval_T *lp,
1647 char_u *endp,
1648 typval_T *rettv,
1649 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001650 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1651 char_u *op,
1652 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001653{
1654 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001655 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001656
1657 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001658 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001659 cc = *endp;
1660 *endp = NUL;
h-east2bd6a092023-05-19 19:01:17 +01001661 if (in_vim9script() && check_reserved_name(lp->ll_name, FALSE) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001662 return;
1663
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001664 if (lp->ll_blob != NULL)
1665 {
1666 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001667
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001668 if (op != NULL && *op != '=')
1669 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001670 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001671 return;
1672 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001673 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001674 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001675
1676 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1677 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001678 if (lp->ll_empty2)
1679 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1680
Bram Moolenaar68452172021-04-12 21:21:02 +02001681 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1682 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001683 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001684 }
1685 else
1686 {
1687 val = (int)tv_get_number_chk(rettv, &error);
1688 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001689 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001690 }
1691 }
1692 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001693 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001694 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001695
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001696 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1697 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001698 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001699 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001700 *endp = cc;
1701 return;
1702 }
1703
Bram Moolenaarff697e62019-02-12 22:28:33 +01001704 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001705 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001706 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001707 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001708 {
1709 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001710 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1711 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001712 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001713 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001714 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001715 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001716 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001717 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001718 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001719 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001720 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1721 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001722 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001723 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001724 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001725 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001726 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001727 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001728 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001729 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001730 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001731 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001732 else if (lp->ll_range)
1733 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001734 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1735 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001736 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001737 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001738 return;
1739 }
1740
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001741 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1742 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001743 }
1744 else
1745 {
1746 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001747 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001748 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001749 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1750 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001751 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001752 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001753 return;
1754 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001755
1756 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001757 && check_typval_arg_type(lp->ll_valtype, rettv,
1758 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001759 return;
1760
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001761 if (lp->ll_newkey != NULL)
1762 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001763 if (op != NULL && *op != '=')
1764 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001765 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001766 return;
1767 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001768 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1769 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001770 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001771
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001772 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001773 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001774 if (di == NULL)
1775 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001776 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1777 {
1778 vim_free(di);
1779 return;
1780 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001781 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001782 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001783 else if (op != NULL && *op != '=')
1784 {
1785 tv_op(lp->ll_tv, rettv, op);
1786 return;
1787 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001788 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001789 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001790
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001791 /*
1792 * Assign the value to the variable or list item.
1793 */
1794 if (copy)
1795 copy_tv(rettv, lp->ll_tv);
1796 else
1797 {
1798 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001799 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001800 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001801 }
1802 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001803}
1804
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001805/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001806 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1807 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001808 * Returns OK or FAIL.
1809 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001810 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001811tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001812{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001813 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001814 char_u numbuf[NUMBUFLEN];
1815 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001816 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001817
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001818 // Can't do anything with a Funcref or Dict on the right.
1819 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001820 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001821 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1822 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001823 {
1824 switch (tv1->v_type)
1825 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001826 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001827 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001829 case VAR_DICT:
1830 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001831 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001832 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001833 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001834 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001835 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001836 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001837 case VAR_CLASS:
1838 case VAR_OBJECT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001839 break;
1840
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001841 case VAR_BLOB:
1842 if (*op != '+' || tv2->v_type != VAR_BLOB)
1843 break;
1844 // BLOB += BLOB
1845 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1846 {
1847 blob_T *b1 = tv1->vval.v_blob;
1848 blob_T *b2 = tv2->vval.v_blob;
1849 int i, len = blob_len(b2);
1850 for (i = 0; i < len; i++)
1851 ga_append(&b1->bv_ga, blob_get(b2, i));
1852 }
1853 return OK;
1854
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001855 case VAR_LIST:
1856 if (*op != '+' || tv2->v_type != VAR_LIST)
1857 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001858 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001859 if (tv2->vval.v_list != NULL)
1860 {
1861 if (tv1->vval.v_list == NULL)
1862 {
1863 tv1->vval.v_list = tv2->vval.v_list;
1864 ++tv1->vval.v_list->lv_refcount;
1865 }
1866 else
1867 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1868 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001869 return OK;
1870
1871 case VAR_NUMBER:
1872 case VAR_STRING:
1873 if (tv2->v_type == VAR_LIST)
1874 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001875 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001876 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001877 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001878 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001879 if (tv2->v_type == VAR_FLOAT)
1880 {
1881 float_T f = n;
1882
Bram Moolenaarff697e62019-02-12 22:28:33 +01001883 if (*op == '%')
1884 break;
1885 switch (*op)
1886 {
1887 case '+': f += tv2->vval.v_float; break;
1888 case '-': f -= tv2->vval.v_float; break;
1889 case '*': f *= tv2->vval.v_float; break;
1890 case '/': f /= tv2->vval.v_float; break;
1891 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001892 clear_tv(tv1);
1893 tv1->v_type = VAR_FLOAT;
1894 tv1->vval.v_float = f;
1895 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001896 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001897 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001898 switch (*op)
1899 {
1900 case '+': n += tv_get_number(tv2); break;
1901 case '-': n -= tv_get_number(tv2); break;
1902 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001903 case '/': n = num_divide(n, tv_get_number(tv2),
1904 &failed); break;
1905 case '%': n = num_modulus(n, tv_get_number(tv2),
1906 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001907 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001908 clear_tv(tv1);
1909 tv1->v_type = VAR_NUMBER;
1910 tv1->vval.v_number = n;
1911 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001912 }
1913 else
1914 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001915 if (tv2->v_type == VAR_FLOAT)
1916 break;
1917
Bram Moolenaarff697e62019-02-12 22:28:33 +01001918 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001919 s = tv_get_string(tv1);
1920 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001921 clear_tv(tv1);
1922 tv1->v_type = VAR_STRING;
1923 tv1->vval.v_string = s;
1924 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001925 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001926
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001927 case VAR_FLOAT:
1928 {
1929 float_T f;
1930
Bram Moolenaarff697e62019-02-12 22:28:33 +01001931 if (*op == '%' || *op == '.'
1932 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001933 && tv2->v_type != VAR_NUMBER
1934 && tv2->v_type != VAR_STRING))
1935 break;
1936 if (tv2->v_type == VAR_FLOAT)
1937 f = tv2->vval.v_float;
1938 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001939 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001940 switch (*op)
1941 {
1942 case '+': tv1->vval.v_float += f; break;
1943 case '-': tv1->vval.v_float -= f; break;
1944 case '*': tv1->vval.v_float *= f; break;
1945 case '/': tv1->vval.v_float /= f; break;
1946 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001947 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001948 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001949 }
1950 }
1951
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001952 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001953 return FAIL;
1954}
1955
1956/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957 * Evaluate the expression used in a ":for var in expr" command.
1958 * "arg" points to "var".
1959 * Set "*errp" to TRUE for an error, FALSE otherwise;
1960 * Return a pointer that holds the info. Null when there is an error.
1961 */
1962 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001963eval_for_line(
1964 char_u *arg,
1965 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001966 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001967 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001968{
Bram Moolenaar33570922005-01-25 22:26:29 +00001969 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001970 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001971 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001972 typval_T tv;
1973 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001974 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001975
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001976 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001977
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001978 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001979 if (fi == NULL)
1980 return NULL;
1981
Bram Moolenaar404557e2021-07-05 21:41:48 +02001982 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1983 &fi->fi_semicolon, FALSE);
1984 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001985 return fi;
1986
Bram Moolenaar404557e2021-07-05 21:41:48 +02001987 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001988 if (expr[0] != 'i' || expr[1] != 'n'
1989 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001990 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001991 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1992 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1993 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001994 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001995 return fi;
1996 }
1997
1998 if (skip)
1999 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002000 expr = skipwhite_and_linebreak(expr + 2, evalarg);
2001 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002002 {
2003 *errp = FALSE;
2004 if (!skip)
2005 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002006 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002007 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002008 l = tv.vval.v_list;
2009 if (l == NULL)
2010 {
2011 // a null list is like an empty list: do nothing
2012 clear_tv(&tv);
2013 }
2014 else
2015 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002016 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002017 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002018
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002019 // No need to increment the refcount, it's already set for
2020 // the list being used in "tv".
2021 fi->fi_list = l;
2022 list_add_watch(l, &fi->fi_lw);
2023 fi->fi_lw.lw_item = l->lv_first;
2024 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002025 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002026 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002027 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002028 fi->fi_bi = 0;
2029 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002030 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002031 typval_T btv;
2032
2033 // Make a copy, so that the iteration still works when the
2034 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002036 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002037 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002038 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002039 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002040 else if (tv.v_type == VAR_STRING)
2041 {
2042 fi->fi_byte_idx = 0;
2043 fi->fi_string = tv.vval.v_string;
2044 tv.vval.v_string = NULL;
2045 if (fi->fi_string == NULL)
2046 fi->fi_string = vim_strsave((char_u *)"");
2047 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002048 else
2049 {
Sean Dewar80d73952021-08-04 19:25:54 +02002050 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002051 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002052 }
2053 }
2054 }
2055 if (skip)
2056 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002057 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002058
2059 return fi;
2060}
2061
2062/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002063 * Used when looping over a :for line, skip the "in expr" part.
2064 */
2065 void
2066skip_for_lines(void *fi_void, evalarg_T *evalarg)
2067{
2068 forinfo_T *fi = (forinfo_T *)fi_void;
2069 int i;
2070
2071 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002072 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002073}
2074
2075/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002076 * Use the first item in a ":for" list. Advance to the next.
2077 * Assign the values to the variable (list). "arg" points to the first one.
2078 * Return TRUE when a valid item was found, FALSE when at end of list or
2079 * something wrong.
2080 */
2081 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002082next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002083{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002084 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002085 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002086 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002087 ? (ASSIGN_FINAL
2088 // first round: error if variable exists
2089 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002090 | ASSIGN_NO_MEMBER_TYPE
2091 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002092 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002093 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002094 int skip_assign = in_vim9script() && arg[0] == '_'
2095 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002096
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002097 if (fi->fi_blob != NULL)
2098 {
2099 typval_T tv;
2100
2101 if (fi->fi_bi >= blob_len(fi->fi_blob))
2102 return FALSE;
2103 tv.v_type = VAR_NUMBER;
2104 tv.v_lock = VAR_FIXED;
2105 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2106 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002107 if (skip_assign)
2108 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002109 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002110 fi->fi_varcount, flag, NULL) == OK;
2111 }
2112
2113 if (fi->fi_string != NULL)
2114 {
2115 typval_T tv;
2116 int len;
2117
2118 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2119 if (len == 0)
2120 return FALSE;
2121 tv.v_type = VAR_STRING;
2122 tv.v_lock = VAR_FIXED;
2123 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2124 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002125 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002126 if (skip_assign)
2127 result = TRUE;
2128 else
2129 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002130 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002131 vim_free(tv.vval.v_string);
2132 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002133 }
2134
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002135 item = fi->fi_lw.lw_item;
2136 if (item == NULL)
2137 result = FALSE;
2138 else
2139 {
2140 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002141 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002142 if (skip_assign)
2143 result = TRUE;
2144 else
2145 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002146 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002147 }
2148 return result;
2149}
2150
2151/*
2152 * Free the structure used to store info used by ":for".
2153 */
2154 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002155free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002156{
Bram Moolenaar33570922005-01-25 22:26:29 +00002157 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002158
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002159 if (fi == NULL)
2160 return;
2161 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002162 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002163 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002164 list_unref(fi->fi_list);
2165 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002166 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002167 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002168 else
2169 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002170 vim_free(fi);
2171}
2172
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002174set_context_for_expression(
2175 expand_T *xp,
2176 char_u *arg,
2177 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002179 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002181 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002183 if (cmdidx == CMD_let || cmdidx == CMD_var
2184 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002185 {
2186 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002187 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002188 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002189 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002190 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002191 {
2192 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002193 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002194 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002195 break;
2196 }
2197 return;
2198 }
2199 }
2200 else
2201 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2202 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 while ((xp->xp_pattern = vim_strpbrk(arg,
2204 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2205 {
2206 c = *xp->xp_pattern;
2207 if (c == '&')
2208 {
2209 c = xp->xp_pattern[1];
2210 if (c == '&')
2211 {
2212 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002213 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 }
2215 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002216 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002218 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2219 xp->xp_pattern += 2;
2220
2221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 }
2223 else if (c == '$')
2224 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002225 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 xp->xp_context = EXPAND_ENV_VARS;
2227 }
2228 else if (c == '=')
2229 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002230 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 xp->xp_context = EXPAND_EXPRESSION;
2232 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002233 else if (c == '#'
2234 && xp->xp_context == EXPAND_EXPRESSION)
2235 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002236 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002237 break;
2238 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002239 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 && xp->xp_context == EXPAND_FUNCTIONS
2241 && vim_strchr(xp->xp_pattern, '(') == NULL)
2242 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002243 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 break;
2245 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002246 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002248 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 {
2250 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2251 if (c == '\\' && xp->xp_pattern[1] != NUL)
2252 ++xp->xp_pattern;
2253 xp->xp_context = EXPAND_NOTHING;
2254 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002255 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002257 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2259 /* skip */ ;
2260 xp->xp_context = EXPAND_NOTHING;
2261 }
2262 else if (c == '|')
2263 {
2264 if (xp->xp_pattern[1] == '|')
2265 {
2266 ++xp->xp_pattern;
2267 xp->xp_context = EXPAND_EXPRESSION;
2268 }
2269 else
2270 xp->xp_context = EXPAND_COMMANDS;
2271 }
2272 else
2273 xp->xp_context = EXPAND_EXPRESSION;
2274 }
2275 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002276 // Doesn't look like something valid, expand as an expression
2277 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002278 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 arg = xp->xp_pattern;
2280 if (*arg != NUL)
2281 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2282 /* skip */ ;
2283 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002284
2285 // ":exe one two" completes "two"
2286 if ((cmdidx == CMD_execute
2287 || cmdidx == CMD_echo
2288 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002289 || cmdidx == CMD_echomsg
2290 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002291 && xp->xp_context == EXPAND_EXPRESSION)
2292 {
2293 for (;;)
2294 {
2295 char_u *n = skiptowhite(arg);
2296
2297 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2298 break;
2299 arg = skipwhite(n);
2300 }
2301 }
2302
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 xp->xp_pattern = arg;
2304}
2305
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002307 * Return TRUE if "pat" matches "text".
2308 * Does not use 'cpo' and always uses 'magic'.
2309 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002310 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002311pattern_match(char_u *pat, char_u *text, int ic)
2312{
2313 int matches = FALSE;
2314 char_u *save_cpo;
2315 regmatch_T regmatch;
2316
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002317 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002318 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002319 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002320 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2321 if (regmatch.regprog != NULL)
2322 {
2323 regmatch.rm_ic = ic;
2324 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2325 vim_regfree(regmatch.regprog);
2326 }
2327 p_cpo = save_cpo;
2328 return matches;
2329}
2330
2331/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002332 * Handle a name followed by "(". Both for just "name(arg)" and for
2333 * "expr->name(arg)".
2334 * Returns OK or FAIL.
2335 */
2336 static int
2337eval_func(
2338 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002339 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002340 char_u *name,
2341 int name_len,
2342 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002343 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002344 typval_T *basetv) // "expr" for "expr->name(arg)"
2345{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002346 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002347 char_u *s = name;
2348 int len = name_len;
2349 partial_T *partial;
2350 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002351 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002352 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002353
2354 if (!evaluate)
2355 check_vars(s, len);
2356
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002357 // If "s" is the name of a variable of type VAR_FUNC
2358 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002359 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002360 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002361
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002362 // Need to make a copy, in case evaluating the arguments makes
2363 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002364 s = vim_strsave(s);
zeertzjqb7f6f932023-04-13 22:12:50 +01002365 if (s == NULL || (evaluate && *s == NUL))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002366 ret = FAIL;
2367 else
2368 {
2369 funcexe_T funcexe;
2370
2371 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002372 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002373 funcexe.fe_firstline = curwin->w_cursor.lnum;
2374 funcexe.fe_lastline = curwin->w_cursor.lnum;
2375 funcexe.fe_evaluate = evaluate;
2376 funcexe.fe_partial = partial;
2377 funcexe.fe_basetv = basetv;
2378 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002379 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002380 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002381 }
2382 vim_free(s);
2383
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002384 // If evaluate is FALSE rettv->v_type was not set in
2385 // get_func_tv, but it's needed in handle_subscript() to parse
2386 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002387 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2388 {
2389 rettv->vval.v_string = NULL;
2390 rettv->v_type = VAR_FUNC;
2391 }
2392
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002393 // Stop the expression evaluation when immediately
2394 // aborting on error, or when an interrupt occurred or
2395 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002396 if (evaluate && aborting())
2397 {
2398 if (ret == OK)
2399 clear_tv(rettv);
2400 ret = FAIL;
2401 }
2402 return ret;
2403}
2404
2405/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002406 * After a NL, skip over empty lines and comment-only lines.
2407 */
2408 static char_u *
2409newline_skip_comments(char_u *arg)
2410{
2411 char_u *p = arg + 1;
2412
2413 for (;;)
2414 {
2415 p = skipwhite(p);
2416
2417 if (*p == NUL)
2418 break;
2419 if (vim9_comment_start(p))
2420 {
2421 char_u *nl = vim_strchr(p, NL);
2422
2423 if (nl == NULL)
2424 break;
2425 p = nl;
2426 }
2427 if (*p != NL)
2428 break;
2429 ++p; // skip another NL
2430 }
2431 return p;
2432}
2433
2434/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002435 * Get the next line source line without advancing. But do skip over comment
2436 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002437 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002438 */
2439 static char_u *
2440getline_peek_skip_comments(evalarg_T *evalarg)
2441{
2442 for (;;)
2443 {
2444 char_u *next = getline_peek(evalarg->eval_getline,
2445 evalarg->eval_cookie);
2446 char_u *p;
2447
2448 if (next == NULL)
2449 break;
2450 p = skipwhite(next);
2451 if (*p != NUL && !vim9_comment_start(p))
2452 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002453 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002454 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002455 }
2456 return NULL;
2457}
2458
2459/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002460 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2461 * comment) and there is a next line, return the next line (skipping blanks)
2462 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002463 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2464 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002465 * "arg" must point somewhere inside a line, not at the start.
2466 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002467 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002468eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2469{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002470 char_u *p = skipwhite(arg);
2471
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002472 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002473 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002474 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002475 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2476 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002477 && (*p == NUL || *p == NL
2478 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002479 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002480 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002481
Bram Moolenaare442d592022-05-05 12:20:28 +01002482 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002483 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002484 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002485 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002486 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002487 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002488
Bram Moolenaar9d489562020-07-30 20:08:50 +02002489 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002490 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002491 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002492 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002493 }
2494 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002495 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002496}
2497
2498/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002499 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002500 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002501 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002502 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002503eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002504{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002505 garray_T *gap = &evalarg->eval_ga;
2506 char_u *line;
2507
Bram Moolenaar39be4982022-05-06 21:51:50 +01002508 if (arg != NULL)
2509 {
2510 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002511 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002512 // Truncate before a trailing comment, so that concatenating the lines
2513 // won't turn the rest into a comment.
2514 if (*skipwhite(arg) == '#')
2515 *arg = NUL;
2516 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002517
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002518 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002519 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2520 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002521 else
2522 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002523 if (line == NULL)
2524 return NULL;
2525
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002526 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002527 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2528 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002529 char_u *p = skipwhite(line);
2530
2531 // Going to concatenate the lines after parsing. For an empty or
2532 // comment line use an empty string.
2533 if (*p == NUL || vim9_comment_start(p))
2534 {
2535 vim_free(line);
2536 line = vim_strsave((char_u *)"");
2537 }
2538
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002539 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2540 ++gap->ga_len;
2541 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002542 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002543 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002544 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002545 evalarg->eval_tofree = line;
2546 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002547
2548 // Advanced to the next line, "arg" no longer points into the previous
2549 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002550 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002551 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002552}
2553
Bram Moolenaare6b53242020-07-01 17:28:33 +02002554/*
2555 * Call eval_next_non_blank() and get the next line if needed.
2556 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002557 char_u *
2558skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2559{
2560 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002561 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002562
Bram Moolenaar962d7212020-07-04 14:15:00 +02002563 if (evalarg == NULL)
2564 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002565 eval_next_non_blank(p, evalarg, &getnext);
2566 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002567 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002568 return p;
2569}
2570
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002571/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002572 * The "eval" functions have an "evalarg" argument: When NULL or
2573 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
2574 * parsed but not executed. The functions may return OK, but the rettv will be
2575 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 */
2577
2578/*
2579 * Handle zero level expression.
2580 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002581 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002582 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002583 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 * Return OK or FAIL.
2585 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002586 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002587eval0(
2588 char_u *arg,
2589 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002590 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002591 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002593 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2594}
2595
2596/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002597 * If "arg" is a simple function call without arguments then call it and return
2598 * the result. Otherwise return NOTDONE.
2599 */
2600 int
2601may_call_simple_func(
2602 char_u *arg,
2603 typval_T *rettv)
2604{
2605 char_u *parens = (char_u *)strstr((char *)arg, "()");
2606 int r = NOTDONE;
2607
2608 // If the expression is "FuncName()" then we can skip a lot of overhead.
2609 if (parens != NULL && *skipwhite(parens + 2) == NUL)
2610 {
2611 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
2612
2613 if (to_name_end(p, TRUE) == parens)
2614 r = call_simple_func(arg, (int)(parens - arg), rettv);
2615 }
2616 return r;
2617}
2618
2619/*
2620 * Handle zero level expression with optimization for a simple function call.
2621 * Same arguments and return value as eval0().
2622 */
2623 int
2624eval0_simple_funccal(
2625 char_u *arg,
2626 typval_T *rettv,
2627 exarg_T *eap,
2628 evalarg_T *evalarg)
2629{
2630 int r = may_call_simple_func(arg, rettv);
2631
2632 if (r == NOTDONE)
2633 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
2634 return r;
2635}
2636
2637/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002638 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2639 * expression and don't check what comes after the expression.
2640 */
2641 int
2642eval0_retarg(
2643 char_u *arg,
2644 typval_T *rettv,
2645 exarg_T *eap,
2646 evalarg_T *evalarg,
2647 char_u **retarg)
2648{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 int ret;
2650 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002651 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002652 int did_emsg_before = did_emsg;
2653 int called_emsg_before = called_emsg;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002654 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002655 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656
2657 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002658 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002659
Bram Moolenaar79481362022-06-27 20:15:10 +01002660 if (ret != FAIL)
2661 {
2662 expr_end = p;
2663 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002664
Bram Moolenaar79481362022-06-27 20:15:10 +01002665 // In Vim9 script a command block is not split at NL characters for
2666 // commands using an expression argument. Skip over a '#' comment to
2667 // check for a following NL. Require white space before the '#'.
2668 if (in_vim9script() && p > expr_end && retarg == NULL)
2669 while (*p == '#')
2670 {
2671 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002672
Bram Moolenaar79481362022-06-27 20:15:10 +01002673 if (nl == NULL)
2674 break;
2675 p = skipwhite(nl + 1);
2676 if (eap != NULL && *p != NUL)
2677 eap->nextcmd = p;
2678 check_for_end = FALSE;
2679 }
2680
2681 if (check_for_end)
2682 end_error = !ends_excmd2(arg, p);
2683 }
2684
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002685 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 {
2687 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002688 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 /*
2690 * Report the invalid expression unless the expression evaluation has
2691 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002692 * exception, or we already gave a more specific error.
2693 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002695 if (!aborting()
2696 && did_emsg == did_emsg_before
2697 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002698 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002699 {
2700 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002701 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002702 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002703 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002704 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002705
zeertzjqf2588b62023-05-05 17:22:35 +01002706 if (eap != NULL && p != NULL)
2707 {
2708 // Some of the expression may not have been consumed.
2709 // Only execute a next command if it cannot be a "||" operator.
2710 // The next command may be "catch".
2711 char_u *nextcmd = check_nextcmd(p);
2712 if (nextcmd != NULL && *nextcmd != '|')
2713 eap->nextcmd = nextcmd;
2714 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002715 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002717
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002718 if (retarg != NULL)
2719 *retarg = p;
2720 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002721 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002722
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 return ret;
2724}
2725
2726/*
2727 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002728 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002729 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 *
2731 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002732 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002734 * Note: "rettv.v_lock" is not set.
2735 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 * Return OK or FAIL.
2737 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002738 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002739eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002741 char_u *p;
2742 int getnext;
2743
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002744 CLEAR_POINTER(rettv);
2745
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 /*
2747 * Get the first variable.
2748 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002749 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 return FAIL;
2751
Bram Moolenaar793648f2020-06-26 21:28:25 +02002752 p = eval_next_non_blank(*arg, evalarg, &getnext);
2753 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002755 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002756 int result;
2757 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002758 evalarg_T *evalarg_used = evalarg;
2759 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002760 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002761 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002762 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002763
2764 if (evalarg == NULL)
2765 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002766 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002767 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002768 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002769 orig_flags = evalarg_used->eval_flags;
2770 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002771
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002772 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002773 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002774 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002775 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002776 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002777 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002778 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002779 clear_tv(rettv);
2780 return FAIL;
2781 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002782 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002783 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002784
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002786 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002788 int error = FALSE;
2789
Bram Moolenaar13106602020-10-04 16:06:05 +02002790 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002791 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002792 else if (vim9script)
2793 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002794 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002796 if (error || !op_falsy || !result)
2797 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002798 if (error)
2799 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 }
2801
2802 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002803 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002805 if (op_falsy)
2806 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002807 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002808 {
mityu4ac198c2021-05-28 17:52:40 +02002809 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002810 clear_tv(rettv);
2811 return FAIL;
2812 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002813 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002814 evalarg_used->eval_flags = (op_falsy ? !result : result)
zeertzjqb7f6f932023-04-13 22:12:50 +01002815 ? orig_flags : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002816 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002817 {
2818 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002820 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002821 if (!op_falsy || !result)
2822 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002824 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002826 /*
2827 * Check for the ":".
2828 */
2829 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2830 if (*p != ':')
2831 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002832 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002833 if (evaluate && result)
2834 clear_tv(rettv);
2835 evalarg_used->eval_flags = orig_flags;
2836 return FAIL;
2837 }
2838 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002839 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002840 else
2841 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002842 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002843 {
2844 error_white_both(p, 1);
2845 clear_tv(rettv);
2846 evalarg_used->eval_flags = orig_flags;
2847 return FAIL;
2848 }
2849 *arg = p;
2850 }
2851
2852 /*
2853 * Get the third variable. Recursive!
2854 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002855 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002856 {
mityu4ac198c2021-05-28 17:52:40 +02002857 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002858 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002859 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002860 return FAIL;
2861 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002862 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2863 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01002864 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002865 if (eval1(arg, &var2, evalarg_used) == FAIL)
2866 {
2867 if (evaluate && result)
2868 clear_tv(rettv);
2869 evalarg_used->eval_flags = orig_flags;
2870 return FAIL;
2871 }
2872 if (evaluate && !result)
2873 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002875
2876 if (evalarg == NULL)
2877 clear_evalarg(&local_evalarg, NULL);
2878 else
2879 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 }
2881
2882 return OK;
2883}
2884
2885/*
2886 * Handle first level expression:
2887 * expr2 || expr2 || expr2 logical OR
2888 *
2889 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002890 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 *
2892 * Return OK or FAIL.
2893 */
2894 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002895eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002897 char_u *p;
2898 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899
2900 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002901 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002903 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 return FAIL;
2905
2906 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002907 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002909 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002910 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002912 evalarg_T *evalarg_used = evalarg;
2913 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002914 int evaluate;
2915 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002916 long result = FALSE;
2917 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002918 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002919 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002920
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002921 if (evalarg == NULL)
2922 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002923 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002924 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002925 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002926 orig_flags = evalarg_used->eval_flags;
2927 evaluate = orig_flags & EVAL_EVALUATE;
2928 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002929 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002930 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002931 result = tv_get_bool_chk(rettv, &error);
2932 else if (tv_get_number_chk(rettv, &error) != 0)
2933 result = TRUE;
2934 clear_tv(rettv);
2935 if (error)
2936 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 }
2938
2939 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002940 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002942 while (p[0] == '|' && p[1] == '|')
2943 {
2944 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002945 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002946 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002947 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002948 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002949 {
2950 error_white_both(p, 2);
2951 clear_tv(rettv);
2952 return FAIL;
2953 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002954 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002955 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002956
2957 /*
2958 * Get the second variable.
2959 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002960 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002961 {
mityu4ac198c2021-05-28 17:52:40 +02002962 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002963 clear_tv(rettv);
2964 return FAIL;
2965 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002966 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2967 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01002968 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002969 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002970 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002971
2972 /*
2973 * Compute the result.
2974 */
2975 if (evaluate && !result)
2976 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002977 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002978 result = tv_get_bool_chk(&var2, &error);
2979 else if (tv_get_number_chk(&var2, &error) != 0)
2980 result = TRUE;
2981 clear_tv(&var2);
2982 if (error)
2983 return FAIL;
2984 }
2985 if (evaluate)
2986 {
2987 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002988 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002989 rettv->v_type = VAR_BOOL;
2990 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002991 }
2992 else
2993 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002994 rettv->v_type = VAR_NUMBER;
2995 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002996 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002997 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002998
2999 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003001
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003002 if (evalarg == NULL)
3003 clear_evalarg(&local_evalarg, NULL);
3004 else
3005 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 }
3007
3008 return OK;
3009}
3010
3011/*
3012 * Handle second level expression:
3013 * expr3 && expr3 && expr3 logical AND
3014 *
3015 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003016 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 *
3018 * Return OK or FAIL.
3019 */
3020 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003021eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003023 char_u *p;
3024 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025
3026 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003027 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003029 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 return FAIL;
3031
3032 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003033 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003035 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003036 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003038 evalarg_T *evalarg_used = evalarg;
3039 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003040 int orig_flags;
3041 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003042 long result = TRUE;
3043 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003044 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003045 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003046
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003047 if (evalarg == NULL)
3048 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003049 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003050 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003051 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003052 orig_flags = evalarg_used->eval_flags;
3053 evaluate = orig_flags & EVAL_EVALUATE;
3054 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003055 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003056 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003057 result = tv_get_bool_chk(rettv, &error);
3058 else if (tv_get_number_chk(rettv, &error) == 0)
3059 result = FALSE;
3060 clear_tv(rettv);
3061 if (error)
3062 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 }
3064
3065 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003066 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003068 while (p[0] == '&' && p[1] == '&')
3069 {
3070 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003071 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003072 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003073 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003074 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003075 {
3076 error_white_both(p, 2);
3077 clear_tv(rettv);
3078 return FAIL;
3079 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003080 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003081 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003082
3083 /*
3084 * Get the second variable.
3085 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003086 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003087 {
mityu4ac198c2021-05-28 17:52:40 +02003088 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003089 clear_tv(rettv);
3090 return FAIL;
3091 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003092 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3093 evalarg_used->eval_flags = result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003094 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003095 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003096 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003097 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003098
3099 /*
3100 * Compute the result.
3101 */
3102 if (evaluate && result)
3103 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003104 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003105 result = tv_get_bool_chk(&var2, &error);
3106 else if (tv_get_number_chk(&var2, &error) == 0)
3107 result = FALSE;
3108 clear_tv(&var2);
3109 if (error)
3110 return FAIL;
3111 }
3112 if (evaluate)
3113 {
3114 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003115 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003116 rettv->v_type = VAR_BOOL;
3117 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003118 }
3119 else
3120 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003121 rettv->v_type = VAR_NUMBER;
3122 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003123 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003124 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003125
3126 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003128
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003129 if (evalarg == NULL)
3130 clear_evalarg(&local_evalarg, NULL);
3131 else
3132 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 }
3134
3135 return OK;
3136}
3137
3138/*
3139 * Handle third level expression:
3140 * var1 == var2
3141 * var1 =~ var2
3142 * var1 != var2
3143 * var1 !~ var2
3144 * var1 > var2
3145 * var1 >= var2
3146 * var1 < var2
3147 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003148 * var1 is var2
3149 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 *
3151 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003152 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 *
3154 * Return OK or FAIL.
3155 */
3156 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003157eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003160 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003161 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003163 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164
3165 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003166 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003168 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 return FAIL;
3170
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003171 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003172
Bram Moolenaar696ba232020-07-29 21:20:41 +02003173 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174
3175 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003176 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003178 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003180 typval_T var2;
3181 int ic;
3182 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003183 int evaluate = evalarg == NULL
3184 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003185 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003186
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003187 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003188 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003189 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003190 p = *arg;
3191 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003192 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3193 {
mityu4ac198c2021-05-28 17:52:40 +02003194 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003195 clear_tv(rettv);
3196 return FAIL;
3197 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003198
Bram Moolenaar696ba232020-07-29 21:20:41 +02003199 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3200 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003201 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003202 clear_tv(rettv);
3203 return FAIL;
3204 }
3205
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003206 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 if (p[len] == '?')
3208 {
3209 ic = TRUE;
3210 ++len;
3211 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003212 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 else if (p[len] == '#')
3214 {
3215 ic = FALSE;
3216 ++len;
3217 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003218 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003220 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221
3222 /*
3223 * Get the second variable.
3224 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003225 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3226 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003227 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003228 clear_tv(rettv);
3229 return FAIL;
3230 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003231 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003232 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003234 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 return FAIL;
3236 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003237 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003238 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003239 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003240
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003241 // use the line of the comparison for messages
3242 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003243 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003244 {
3245 ret = FAIL;
3246 clear_tv(rettv);
3247 }
3248 else
3249 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003250 clear_tv(&var2);
3251 return ret;
3252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 }
3254
3255 return OK;
3256}
3257
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003258/*
3259 * Make a copy of blob "tv1" and append blob "tv2".
3260 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 void
3262eval_addblob(typval_T *tv1, typval_T *tv2)
3263{
3264 blob_T *b1 = tv1->vval.v_blob;
3265 blob_T *b2 = tv2->vval.v_blob;
3266 blob_T *b = blob_alloc();
3267 int i;
3268
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003269 if (b == NULL)
3270 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003272 for (i = 0; i < blob_len(b1); i++)
3273 ga_append(&b->bv_ga, blob_get(b1, i));
3274 for (i = 0; i < blob_len(b2); i++)
3275 ga_append(&b->bv_ga, blob_get(b2, i));
3276
3277 clear_tv(tv1);
3278 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003279}
3280
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003281/*
3282 * Make a copy of list "tv1" and append list "tv2".
3283 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284 int
3285eval_addlist(typval_T *tv1, typval_T *tv2)
3286{
3287 typval_T var3;
3288
3289 // concatenate Lists
3290 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3291 {
3292 clear_tv(tv1);
3293 clear_tv(tv2);
3294 return FAIL;
3295 }
3296 clear_tv(tv1);
3297 *tv1 = var3;
3298 return OK;
3299}
3300
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003302 * Handle the bitwise left/right shift operator expression:
3303 * var1 << var2
3304 * var1 >> var2
3305 *
3306 * "arg" must point to the first non-white of the expression.
3307 * "arg" is advanced to just after the recognized expression.
3308 *
3309 * Return OK or FAIL.
3310 */
3311 static int
3312eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3313{
3314 /*
3315 * Get the first expression.
3316 */
3317 if (eval6(arg, rettv, evalarg) == FAIL)
3318 return FAIL;
3319
3320 /*
3321 * Repeat computing, until no '<<' or '>>' is following.
3322 */
3323 for (;;)
3324 {
3325 char_u *p;
3326 int getnext;
3327 exprtype_T type;
3328 int evaluate;
3329 typval_T var2;
3330 int vim9script;
3331
3332 p = eval_next_non_blank(*arg, evalarg, &getnext);
3333 if (p[0] == '<' && p[1] == '<')
3334 type = EXPR_LSHIFT;
3335 else if (p[0] == '>' && p[1] == '>')
3336 type = EXPR_RSHIFT;
3337 else
3338 return OK;
3339
3340 // Handle a bitwise left or right shift operator
3341 if (rettv->v_type != VAR_NUMBER)
3342 {
3343 // left operand should be a number
3344 emsg(_(e_bitshift_ops_must_be_number));
3345 clear_tv(rettv);
3346 return FAIL;
3347 }
3348
3349 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3350 vim9script = in_vim9script();
3351 if (getnext)
3352 {
3353 *arg = eval_next_line(*arg, evalarg);
3354 p = *arg;
3355 }
3356 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3357 {
3358 error_white_both(*arg, 2);
3359 clear_tv(rettv);
3360 return FAIL;
3361 }
3362
3363 /*
3364 * Get the second variable.
3365 */
3366 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3367 {
3368 error_white_both(p, 2);
3369 clear_tv(rettv);
3370 return FAIL;
3371 }
3372 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3373 if (eval6(arg, &var2, evalarg) == FAIL)
3374 {
3375 clear_tv(rettv);
3376 return FAIL;
3377 }
3378
3379 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3380 {
3381 // right operand should be a positive number
3382 if (var2.v_type != VAR_NUMBER)
3383 emsg(_(e_bitshift_ops_must_be_number));
3384 else
dundargocc57b5bc2022-11-02 13:30:51 +00003385 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003386 clear_tv(rettv);
3387 clear_tv(&var2);
3388 return FAIL;
3389 }
3390
3391 if (evaluate)
3392 {
3393 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3394 // shifting more bits than we have always results in zero
3395 rettv->vval.v_number = 0;
3396 else if (type == EXPR_LSHIFT)
3397 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003398 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003399 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003400 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003401 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003402 }
3403
3404 clear_tv(&var2);
3405 }
3406
3407 return OK;
3408}
3409
3410/*
3411 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003412 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003414 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003415 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 *
3417 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003418 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 *
3420 * Return OK or FAIL.
3421 */
3422 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003423eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003426 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003428 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 return FAIL;
3430
3431 /*
3432 * Repeat computing, until no '+', '-' or '.' is following.
3433 */
3434 for (;;)
3435 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003436 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003437 int getnext;
3438 char_u *p;
3439 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003440 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003441 int concat;
3442 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003443 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003444
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003445 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003446 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003447 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003448 p = eval_next_non_blank(*arg, evalarg, &getnext);
3449 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003450 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003451 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3452 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003454 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3455 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003456
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003457 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003458 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003459 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003460 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003461 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003462 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003463 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003464 {
mityu4ac198c2021-05-28 17:52:40 +02003465 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003466 clear_tv(rettv);
3467 return FAIL;
3468 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003469 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003470 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003471 if ((op != '+' || (rettv->v_type != VAR_LIST
3472 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003473 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003474 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003475 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003476 int error = FALSE;
3477
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003478 // For "list + ...", an illegal use of the first operand as
3479 // a number cannot be determined before evaluating the 2nd
3480 // operand: if this is also a list, all is ok.
3481 // For "something . ...", "something - ..." or "non-list + ...",
3482 // we know that the first operand needs to be a string or number
3483 // without evaluating the 2nd operand. So check before to avoid
3484 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003485 if (op != '.')
3486 tv_get_number_chk(rettv, &error);
3487 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003488 {
3489 clear_tv(rettv);
3490 return FAIL;
3491 }
3492 }
3493
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 /*
3495 * Get the second variable.
3496 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003497 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003498 {
mityu89dcb4d2021-05-28 13:50:17 +02003499 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003500 clear_tv(rettv);
3501 return FAIL;
3502 }
3503 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003504 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003506 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 return FAIL;
3508 }
3509
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003510 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 {
3512 /*
3513 * Compute the result.
3514 */
3515 if (op == '.')
3516 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003517 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3518 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003519 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003520
Bram Moolenaar2e086612020-08-25 22:37:48 +02003521 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003522 || var2.v_type == VAR_CHANNEL
3523 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003524 semsg(_(e_using_invalid_value_as_string_str),
3525 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003526 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003527 {
3528 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3529 var2.vval.v_float);
3530 s2 = buf2;
3531 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003532 else
3533 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003534 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003535 {
3536 clear_tv(rettv);
3537 clear_tv(&var2);
3538 return FAIL;
3539 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003540 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003541 clear_tv(rettv);
3542 rettv->v_type = VAR_STRING;
3543 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003545 else if (op == '+' && rettv->v_type == VAR_BLOB
3546 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003547 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003548 else if (op == '+' && rettv->v_type == VAR_LIST
3549 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003550 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003551 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003552 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 else
3555 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003556 int error = FALSE;
3557 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003558 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003559
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003560 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003561 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003562 f1 = rettv->vval.v_float;
3563 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003564 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003565 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003566 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003567 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003568 if (error)
3569 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003570 // This can only happen for "list + non-list" or
3571 // "blob + non-blob". For "non-list + ..." or
3572 // "something - ...", we returned before evaluating the
3573 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003574 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003575 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003576 return FAIL;
3577 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003578 if (var2.v_type == VAR_FLOAT)
3579 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003580 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003581 if (var2.v_type == VAR_FLOAT)
3582 {
3583 f2 = var2.vval.v_float;
3584 n2 = 0;
3585 }
3586 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003587 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003588 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003589 if (error)
3590 {
3591 clear_tv(rettv);
3592 clear_tv(&var2);
3593 return FAIL;
3594 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003595 if (rettv->v_type == VAR_FLOAT)
3596 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003597 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003598 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003599
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003600 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003601 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3602 {
3603 if (op == '+')
3604 f1 = f1 + f2;
3605 else
3606 f1 = f1 - f2;
3607 rettv->v_type = VAR_FLOAT;
3608 rettv->vval.v_float = f1;
3609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003611 {
3612 if (op == '+')
3613 n1 = n1 + n2;
3614 else
3615 n1 = n1 - n2;
3616 rettv->v_type = VAR_NUMBER;
3617 rettv->vval.v_number = n1;
3618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003620 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 }
3622 }
3623 return OK;
3624}
3625
3626/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003627 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 * * number multiplication
3629 * / number division
3630 * % number modulo
3631 *
3632 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003633 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 *
3635 * Return OK or FAIL.
3636 */
3637 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003638eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003639 char_u **arg,
3640 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003641 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003642 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003644 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645
3646 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003647 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003649 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 return FAIL;
3651
3652 /*
3653 * Repeat computing, until no '*', '/' or '%' is following.
3654 */
3655 for (;;)
3656 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003657 int evaluate;
3658 int getnext;
3659 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003660 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003661 int op;
3662 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003663 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003664 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003665
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003666 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003667 p = eval_next_non_blank(*arg, evalarg, &getnext);
3668 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003669 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003671
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003672 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003673 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003674 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003675 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003676 {
3677 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3678 {
mityu4ac198c2021-05-28 17:52:40 +02003679 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003680 clear_tv(rettv);
3681 return FAIL;
3682 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003683 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003686 f1 = 0;
3687 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003688 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003689 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003691 if (rettv->v_type == VAR_FLOAT)
3692 {
3693 f1 = rettv->vval.v_float;
3694 use_float = TRUE;
3695 n1 = 0;
3696 }
3697 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003698 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003699 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003700 if (error)
3701 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 }
3703 else
3704 n1 = 0;
3705
3706 /*
3707 * Get the second variable.
3708 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003709 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3710 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003711 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003712 clear_tv(rettv);
3713 return FAIL;
3714 }
3715 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003716 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 return FAIL;
3718
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003719 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003721 if (var2.v_type == VAR_FLOAT)
3722 {
3723 if (!use_float)
3724 {
3725 f1 = n1;
3726 use_float = TRUE;
3727 }
3728 f2 = var2.vval.v_float;
3729 n2 = 0;
3730 }
3731 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003732 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003733 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003734 clear_tv(&var2);
3735 if (error)
3736 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003737 if (use_float)
3738 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740
3741 /*
3742 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003743 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003745 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003747 if (op == '*')
3748 f1 = f1 * f2;
3749 else if (op == '/')
3750 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003751#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003752 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003753 if (f2 == 0.0)
3754 {
3755 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003756 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003757 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003758 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003759 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003760 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003761 }
3762 else
3763 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003764#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003765 // We rely on the floating point library to handle divide
3766 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003767 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003768#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003771 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003772 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003773 return FAIL;
3774 }
3775 rettv->v_type = VAR_FLOAT;
3776 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 }
3778 else
3779 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003780 int failed = FALSE;
3781
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003782 if (op == '*')
3783 n1 = n1 * n2;
3784 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003785 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003787 n1 = num_modulus(n1, n2, &failed);
3788 if (failed)
3789 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003790
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003791 rettv->v_type = VAR_NUMBER;
3792 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 }
3795 }
3796
3797 return OK;
3798}
3799
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003800/*
3801 * Handle a type cast before a base level expression.
3802 * "arg" must point to the first non-white of the expression.
3803 * "arg" is advanced to just after the recognized expression.
3804 * Return OK or FAIL.
3805 */
3806 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003807eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003808 char_u **arg,
3809 typval_T *rettv,
3810 evalarg_T *evalarg,
3811 int want_string) // after "." operator
3812{
3813 type_T *want_type = NULL;
3814 garray_T type_list; // list of pointers to allocated types
3815 int res;
3816 int evaluate = evalarg == NULL ? 0
3817 : (evalarg->eval_flags & EVAL_EVALUATE);
3818
3819 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003820 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3821 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003822 {
3823 ++*arg;
3824 ga_init2(&type_list, sizeof(type_T *), 10);
3825 want_type = parse_type(arg, &type_list, TRUE);
3826 if (want_type == NULL && (evaluate || **arg != '>'))
3827 {
3828 clear_type_list(&type_list);
3829 return FAIL;
3830 }
3831
3832 if (**arg != '>')
3833 {
3834 if (*skipwhite(*arg) == '>')
3835 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3836 else
3837 emsg(_(e_missing_gt));
3838 clear_type_list(&type_list);
3839 return FAIL;
3840 }
3841 ++*arg;
3842 *arg = skipwhite_and_linebreak(*arg, evalarg);
3843 }
3844
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003845 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003846
3847 if (want_type != NULL && evaluate)
3848 {
3849 if (res == OK)
3850 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003851 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3852 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003853
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003854 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003855 {
3856 if (want_type == &t_bool && actual != &t_bool
3857 && (actual->tt_flags & TTFLAG_BOOL_OK))
3858 {
3859 int n = tv2bool(rettv);
3860
3861 // can use "0" and "1" for boolean in some places
3862 clear_tv(rettv);
3863 rettv->v_type = VAR_BOOL;
3864 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3865 }
3866 else
3867 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003868 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003869
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003870 res = check_type(want_type, actual, TRUE, where);
3871 }
3872 }
3873 }
3874 clear_type_list(&type_list);
3875 }
3876
3877 return res;
3878}
3879
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003880 int
3881eval_leader(char_u **arg, int vim9)
3882{
3883 char_u *s = *arg;
3884 char_u *p = *arg;
3885
3886 while (*p == '!' || *p == '-' || *p == '+')
3887 {
3888 char_u *n = skipwhite(p + 1);
3889
3890 // ++, --, -+ and +- are not accepted in Vim9 script
3891 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3892 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003893 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003894 return FAIL;
3895 }
3896 p = n;
3897 }
3898 *arg = p;
3899 return OK;
3900}
3901
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003903 * Check for a predefined value "true", "false" and "null.*".
3904 * Return OK when recognized.
3905 */
3906 int
3907handle_predefined(char_u *s, int len, typval_T *rettv)
3908{
3909 switch (len)
3910 {
3911 case 4: if (STRNCMP(s, "true", 4) == 0)
3912 {
3913 rettv->v_type = VAR_BOOL;
3914 rettv->vval.v_number = VVAL_TRUE;
3915 return OK;
3916 }
3917 if (STRNCMP(s, "null", 4) == 0)
3918 {
3919 rettv->v_type = VAR_SPECIAL;
3920 rettv->vval.v_number = VVAL_NULL;
3921 return OK;
3922 }
3923 break;
3924 case 5: if (STRNCMP(s, "false", 5) == 0)
3925 {
3926 rettv->v_type = VAR_BOOL;
3927 rettv->vval.v_number = VVAL_FALSE;
3928 return OK;
3929 }
3930 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003931 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3932 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003933#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003934 rettv->v_type = VAR_JOB;
3935 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003936#else
3937 rettv->v_type = VAR_SPECIAL;
3938 rettv->vval.v_number = VVAL_NULL;
3939#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003940 return OK;
3941 }
3942 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003943 case 9:
3944 if (STRNCMP(s, "null_", 5) != 0)
3945 break;
3946 if (STRNCMP(s + 5, "list", 4) == 0)
3947 {
3948 rettv->v_type = VAR_LIST;
3949 rettv->vval.v_list = NULL;
3950 return OK;
3951 }
3952 if (STRNCMP(s + 5, "dict", 4) == 0)
3953 {
3954 rettv->v_type = VAR_DICT;
3955 rettv->vval.v_dict = NULL;
3956 return OK;
3957 }
3958 if (STRNCMP(s + 5, "blob", 4) == 0)
3959 {
3960 rettv->v_type = VAR_BLOB;
3961 rettv->vval.v_blob = NULL;
3962 return OK;
3963 }
3964 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003965 case 10: if (STRNCMP(s, "null_class", 10) == 0)
3966 {
3967 rettv->v_type = VAR_CLASS;
3968 rettv->vval.v_class = NULL;
3969 return OK;
3970 }
3971 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003972 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3973 {
3974 rettv->v_type = VAR_STRING;
3975 rettv->vval.v_string = NULL;
3976 return OK;
3977 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003978 if (STRNCMP(s, "null_object", 11) == 0)
3979 {
3980 rettv->v_type = VAR_OBJECT;
3981 rettv->vval.v_object = NULL;
3982 return OK;
3983 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003984 break;
3985 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003986 if (STRNCMP(s, "null_channel", 12) == 0)
3987 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003988#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003989 rettv->v_type = VAR_CHANNEL;
3990 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003991#else
3992 rettv->v_type = VAR_SPECIAL;
3993 rettv->vval.v_number = VVAL_NULL;
3994#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003995 return OK;
3996 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003997 if (STRNCMP(s, "null_partial", 12) == 0)
3998 {
3999 rettv->v_type = VAR_PARTIAL;
4000 rettv->vval.v_partial = NULL;
4001 return OK;
4002 }
4003 break;
4004 case 13: if (STRNCMP(s, "null_function", 13) == 0)
4005 {
4006 rettv->v_type = VAR_FUNC;
4007 rettv->vval.v_string = NULL;
4008 return OK;
4009 }
4010 break;
4011 }
4012 return FAIL;
4013}
4014
4015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 * Handle sixth level expression:
4017 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004018 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004019 * "string" string constant
4020 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 * &option-name option value
4022 * @r register contents
4023 * identifier variable value
4024 * function() function call
4025 * $VAR environment variable
4026 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004027 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004029 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004030 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 *
4032 * Also handle:
4033 * ! in front logical NOT
4034 * - in front unary minus
4035 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004036 * trailing [] subscript in String or List
4037 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004038 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 *
4040 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004041 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 *
4043 * Return OK or FAIL.
4044 */
4045 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004046eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004047 char_u **arg,
4048 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004049 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004050 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004052 int evaluate = evalarg != NULL
4053 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 int len;
4055 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004056 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 char_u *start_leader, *end_leader;
4058 int ret = OK;
4059 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004060 static int recurse = 0;
4061 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062
4063 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004064 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004065 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004067 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068
4069 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004070 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 */
4072 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004073 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004074 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 end_leader = *arg;
4076
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004077 if (**arg == '.' && (!isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004078 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004079 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004080 ++*arg;
4081 return FAIL;
4082 }
4083
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004084 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004085 // and crash. With MSVC the stack is smaller.
4086 if (recurse ==
4087#ifdef _MSC_VER
4088 300
4089#else
4090 1000
4091#endif
4092 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004093 {
4094 semsg(_(e_expression_too_recursive_str), *arg);
4095 return FAIL;
4096 }
4097 ++recurse;
4098
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 switch (**arg)
4100 {
4101 /*
4102 * Number constant.
4103 */
4104 case '0':
4105 case '1':
4106 case '2':
4107 case '3':
4108 case '4':
4109 case '5':
4110 case '6':
4111 case '7':
4112 case '8':
4113 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004114 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004115
4116 // Apply prefixed "-" and "+" now. Matters especially when
4117 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004118 if (ret == OK && evaluate && end_leader > start_leader
4119 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004120 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 break;
4122
4123 /*
4124 * String constant: "string".
4125 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004126 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 break;
4128
4129 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004130 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004132 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004133 break;
4134
4135 /*
4136 * List: [expr, expr]
4137 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004138 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 break;
4140
4141 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004142 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004143 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004144 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004145 {
4146 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4147 }
4148 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004149 {
4150 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004151 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004152 }
4153 else
4154 ret = NOTDONE;
4155 break;
4156
4157 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004158 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004159 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004160 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004161 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004162 ret = NOTDONE;
4163 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004164 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004165 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004166 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004167 break;
4168
4169 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004170 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004172 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 break;
4174
4175 /*
4176 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004177 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 */
LemonBoy2eaef102022-05-06 13:14:50 +01004179 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4180 ret = eval_interp_string(arg, rettv, evaluate);
4181 else
4182 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 break;
4184
4185 /*
4186 * Register contents: @r.
4187 */
4188 case '@': ++*arg;
4189 if (evaluate)
4190 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004191 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004192 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004193 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004194 emsg_invreg(**arg);
4195 else
4196 {
4197 rettv->v_type = VAR_STRING;
4198 rettv->vval.v_string = get_reg_contents(**arg,
4199 GREG_EXPR_SRC);
4200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 }
4202 if (**arg != NUL)
4203 ++*arg;
4204 break;
4205
4206 /*
4207 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004208 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004210 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004211 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004212 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004213 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004214 if (ret == OK && evaluate)
4215 {
4216 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4217
Bram Moolenaara9931532021-06-12 15:58:16 +02004218 // Compile it here to get the return type. The return
4219 // type is optional, when it's missing use t_unknown.
4220 // This is recognized in compile_return().
4221 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4222 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004223 if (compile_def_function(ufunc, FALSE,
4224 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004225 {
4226 clear_tv(rettv);
4227 ret = FAIL;
4228 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004229 }
4230 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004231 if (ret == NOTDONE)
4232 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004233 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004234 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004235
Bram Moolenaar9215f012020-06-27 21:18:00 +02004236 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004237 if (**arg == ')')
4238 ++*arg;
4239 else if (ret == OK)
4240 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004241 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004242 clear_tv(rettv);
4243 ret = FAIL;
4244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 }
4246 break;
4247
Bram Moolenaar8c711452005-01-14 21:53:12 +00004248 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 break;
4250 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004251
4252 if (ret == NOTDONE)
4253 {
4254 /*
4255 * Must be a variable or function name.
4256 * Can also be a curly-braces kind of name: {expr}.
4257 */
4258 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004259 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004260 if (alias != NULL)
4261 s = alias;
4262
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004263 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004264 ret = FAIL;
4265 else
4266 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004267 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4268
Bram Moolenaar4525a572022-02-13 11:57:33 +00004269 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004270 {
4271 emsg(_(e_cannot_use_underscore_here));
4272 ret = FAIL;
4273 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004274 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004275 && s[0] == 's' && s[1] == ':')
4276 {
4277 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4278 ret = FAIL;
4279 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004280 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004281 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004282 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004283 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004284 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004285 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004286 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004287 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004288 // get the value of "true", "false", etc. or a variable
4289 ret = FAIL;
4290 if (vim9script)
4291 ret = handle_predefined(s, len, rettv);
4292 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004293 {
4294 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004295 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004296 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004297 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004298 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004299 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004300 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004301 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004302 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004303 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004304 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004305 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004306 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004307 }
4308
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004309 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4310 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004311 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004312 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313
4314 /*
4315 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4316 */
4317 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004318 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004319
4320 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004321 return ret;
4322}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004323
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004324/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004325 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004326 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004327 * Adjusts "end_leaderp" until it is at "start_leader".
4328 */
4329 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004330eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004331 typval_T *rettv,
4332 int numeric_only,
4333 char_u *start_leader,
4334 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004335{
4336 char_u *end_leader = *end_leaderp;
4337 int ret = OK;
4338 int error = FALSE;
4339 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004340 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004341 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004342 float_T f = 0.0;
4343
4344 if (rettv->v_type == VAR_FLOAT)
4345 f = rettv->vval.v_float;
4346 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004347 {
4348 while (VIM_ISWHITE(end_leader[-1]))
4349 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004350 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004351 val = tv2bool(rettv);
4352 else
4353 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004354 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004355 if (error)
4356 {
4357 clear_tv(rettv);
4358 ret = FAIL;
4359 }
4360 else
4361 {
4362 while (end_leader > start_leader)
4363 {
4364 --end_leader;
4365 if (*end_leader == '!')
4366 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004367 if (numeric_only)
4368 {
4369 ++end_leader;
4370 break;
4371 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004372 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004373 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004374 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004375 {
4376 rettv->v_type = VAR_BOOL;
4377 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4378 }
4379 else
4380 f = !f;
4381 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004382 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004383 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004384 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004385 type = VAR_BOOL;
4386 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004387 }
4388 else if (*end_leader == '-')
4389 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004390 if (rettv->v_type == VAR_FLOAT)
4391 f = -f;
4392 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004393 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004394 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004395 type = VAR_NUMBER;
4396 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004397 }
4398 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004399 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004401 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004402 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004404 else
4405 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004406 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004407 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004408 rettv->v_type = type;
4409 else
4410 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004411 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004414 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 return ret;
4416}
4417
4418/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004419 * Call the function referred to in "rettv".
4420 */
4421 static int
4422call_func_rettv(
4423 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004424 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004425 typval_T *rettv,
4426 int evaluate,
4427 dict_T *selfdict,
4428 typval_T *basetv)
4429{
4430 partial_T *pt = NULL;
4431 funcexe_T funcexe;
4432 typval_T functv;
4433 char_u *s;
4434 int ret;
4435
4436 // need to copy the funcref so that we can clear rettv
4437 if (evaluate)
4438 {
4439 functv = *rettv;
4440 rettv->v_type = VAR_UNKNOWN;
4441
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004442 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004443 if (functv.v_type == VAR_PARTIAL)
4444 {
4445 pt = functv.vval.v_partial;
4446 s = partial_name(pt);
4447 }
4448 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004449 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004450 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004451 if (s == NULL || *s == NUL)
4452 {
4453 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004454 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004455 goto theend;
4456 }
4457 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004458 }
4459 else
4460 s = (char_u *)"";
4461
Bram Moolenaara80faa82020-04-12 19:37:17 +02004462 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004463 funcexe.fe_firstline = curwin->w_cursor.lnum;
4464 funcexe.fe_lastline = curwin->w_cursor.lnum;
4465 funcexe.fe_evaluate = evaluate;
4466 funcexe.fe_partial = pt;
4467 funcexe.fe_selfdict = selfdict;
4468 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004469 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004470
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004471theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004472 // Clear the funcref afterwards, so that deleting it while
4473 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004474 if (evaluate)
4475 clear_tv(&functv);
4476
4477 return ret;
4478}
4479
4480/*
4481 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004482 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004483 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4484 */
4485 static int
4486eval_lambda(
4487 char_u **arg,
4488 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004489 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004490 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004491{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004492 int evaluate = evalarg != NULL
4493 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004494 typval_T base = *rettv;
4495 int ret;
4496
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004497 rettv->v_type = VAR_UNKNOWN;
4498
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004499 if (**arg == '{')
4500 {
4501 // ->{lambda}()
4502 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4503 }
4504 else
4505 {
4506 // ->(lambda)()
4507 ++*arg;
4508 ret = eval1(arg, rettv, evalarg);
4509 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004510 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004511 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004512 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004513 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004514 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004515 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4516 && rettv->v_type != VAR_PARTIAL)
4517 {
4518 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4519 return FAIL;
4520 }
4521 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004522 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004523 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004524 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004525
4526 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004527 {
4528 if (verbose)
4529 {
4530 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004531 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004532 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004533 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004534 }
4535 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004536 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004537 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004538 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004539 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004540
4541 // Clear the funcref afterwards, so that deleting it while
4542 // evaluating the arguments is possible (see test55).
4543 if (evaluate)
4544 clear_tv(&base);
4545
4546 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004547}
4548
4549/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004550 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004551 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004552 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4553 */
4554 static int
4555eval_method(
4556 char_u **arg,
4557 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004558 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004559 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004560{
4561 char_u *name;
4562 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004563 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004564 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004565 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004566 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004567 int evaluate = evalarg != NULL
4568 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004569
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004570 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004571
Bram Moolenaarac92e252019-08-03 21:58:38 +02004572 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004573 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004574 if (alias != NULL)
4575 name = alias;
4576
4577 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004578 {
4579 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004580 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004581 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004582 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004583 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004584 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004585 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004586
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004587 // If there is no "(" immediately following, but there is further on,
4588 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4589 // Does not handle anything where "(" is part of the expression.
4590 *arg = skipwhite(*arg);
4591
4592 if (**arg != '(' && alias == NULL
4593 && (paren = vim_strchr(*arg, '(')) != NULL)
4594 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004595 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00004596
4597 // Truncate the name a the "(". Avoid trying to get another line
4598 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004599 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00004600 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
4601 if (evalarg != NULL)
4602 {
4603 getline = evalarg->eval_getline;
4604 evalarg->eval_getline = NULL;
4605 }
4606
4607 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004608 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004609 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004610 *arg = name + len;
4611 ret = FAIL;
4612 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004613 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004614 {
4615 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004616 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004617 }
Bram Moolenaar34820942022-12-19 20:28:38 +00004618
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004619 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00004620 if (getline != NULL)
4621 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004622 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004623
4624 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004625 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004626 *arg = skipwhite(*arg);
4627
4628 if (**arg != '(')
4629 {
4630 if (verbose)
4631 semsg(_(e_missing_parenthesis_str), name);
4632 ret = FAIL;
4633 }
4634 else if (VIM_ISWHITE((*arg)[-1]))
4635 {
4636 if (verbose)
4637 emsg(_(e_no_white_space_allowed_before_parenthesis));
4638 ret = FAIL;
4639 }
4640 else
4641 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004642 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004643 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004644 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004645
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004646 // Clear the funcref afterwards, so that deleting it while
4647 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004648 if (evaluate)
4649 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004650 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004651
Bram Moolenaarac92e252019-08-03 21:58:38 +02004652 return ret;
4653}
4654
4655/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004656 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4657 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004658 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4659 */
4660 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004661eval_index(
4662 char_u **arg,
4663 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004664 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004665 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004666{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004667 int evaluate = evalarg != NULL
4668 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004669 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004670 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004671 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004672 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004673 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004674 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004675
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004676 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4677 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004678
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004679 init_tv(&var1);
4680 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004681 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004682 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004683 /*
4684 * dict.name
4685 */
4686 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004687 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004688 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004689 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004690 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004691 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004692 }
4693 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004694 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004695 /*
4696 * something[idx]
4697 *
4698 * Get the (first) variable from inside the [].
4699 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004700 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004701 if (**arg == ':')
4702 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004703 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004704 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004705 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004706 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004707 semsg(_(e_white_space_required_before_and_after_str_at_str),
4708 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004709 clear_tv(&var1);
4710 return FAIL;
4711 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004712 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004713 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004714 int error = FALSE;
4715
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004716 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004717 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004718 && var1.v_type == VAR_FLOAT)
4719 {
4720 var1.vval.v_string = typval_tostring(&var1, TRUE);
4721 var1.v_type = VAR_STRING;
4722 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004723
Bram Moolenaar4525a572022-02-13 11:57:33 +00004724 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004725 tv_get_number_chk(&var1, &error);
4726 else
4727 error = tv_get_string_chk(&var1) == NULL;
4728 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004729 {
4730 // not a number or string
4731 clear_tv(&var1);
4732 return FAIL;
4733 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004734 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004735
4736 /*
4737 * Get the second variable from inside the [:].
4738 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004739 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004740 if (**arg == ':')
4741 {
4742 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004743 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004744 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004745 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004746 semsg(_(e_white_space_required_before_and_after_str_at_str),
4747 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004748 if (!empty1)
4749 clear_tv(&var1);
4750 return FAIL;
4751 }
4752 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004753 if (**arg == ']')
4754 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004755 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004756 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004757 if (!empty1)
4758 clear_tv(&var1);
4759 return FAIL;
4760 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004761 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004762 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004763 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004764 if (!empty1)
4765 clear_tv(&var1);
4766 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004767 return FAIL;
4768 }
4769 }
4770
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004771 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004772 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004773 if (**arg != ']')
4774 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004775 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004776 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004777 clear_tv(&var1);
4778 if (range)
4779 clear_tv(&var2);
4780 return FAIL;
4781 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004782 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004783 }
4784
4785 if (evaluate)
4786 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004787 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004788 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004789 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004790
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004791 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004792 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004793 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004794 clear_tv(&var2);
4795 return res;
4796 }
4797 return OK;
4798}
4799
4800/*
4801 * Check if "rettv" can have an [index] or [sli:ce]
4802 */
4803 int
4804check_can_index(typval_T *rettv, int evaluate, int verbose)
4805{
4806 switch (rettv->v_type)
4807 {
4808 case VAR_FUNC:
4809 case VAR_PARTIAL:
4810 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004811 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004812 return FAIL;
4813 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004814 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004815 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004816 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004817 case VAR_BOOL:
4818 case VAR_SPECIAL:
4819 case VAR_JOB:
4820 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004821 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004822 case VAR_CLASS:
4823 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004824 if (verbose)
4825 emsg(_(e_cannot_index_special_variable));
4826 return FAIL;
4827 case VAR_UNKNOWN:
4828 case VAR_ANY:
4829 case VAR_VOID:
4830 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004831 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004832 emsg(_(e_cannot_index_special_variable));
4833 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004834 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004835 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004836
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004837 case VAR_STRING:
4838 case VAR_LIST:
4839 case VAR_DICT:
4840 case VAR_BLOB:
4841 break;
4842 case VAR_NUMBER:
4843 if (in_vim9script())
4844 emsg(_(e_cannot_index_number));
4845 break;
4846 }
4847 return OK;
4848}
4849
4850/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004851 * slice() function
4852 */
4853 void
4854f_slice(typval_T *argvars, typval_T *rettv)
4855{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004856 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004857 && ((argvars[0].v_type != VAR_STRING
4858 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004859 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004860 && check_for_list_arg(argvars, 0) == FAIL)
4861 || check_for_number_arg(argvars, 1) == FAIL
4862 || check_for_opt_number_arg(argvars, 2) == FAIL))
4863 return;
4864
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00004865 if (check_can_index(argvars, TRUE, FALSE) != OK)
4866 return;
4867
4868 copy_tv(argvars, rettv);
4869 eval_index_inner(rettv, TRUE, argvars + 1,
4870 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4871 TRUE, NULL, 0, FALSE);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004872}
4873
4874/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004875 * Apply index or range to "rettv".
4876 * "var1" is the first index, NULL for [:expr].
4877 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004878 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4879 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004880 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4881 */
4882 int
4883eval_index_inner(
4884 typval_T *rettv,
4885 int is_range,
4886 typval_T *var1,
4887 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004888 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004889 char_u *key,
4890 int keylen,
4891 int verbose)
4892{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004893 varnumber_T n1, n2 = 0;
4894 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004895
4896 n1 = 0;
4897 if (var1 != NULL && rettv->v_type != VAR_DICT)
4898 n1 = tv_get_number(var1);
4899
4900 if (is_range)
4901 {
4902 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004903 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004904 if (verbose)
4905 emsg(_(e_cannot_slice_dictionary));
4906 return FAIL;
4907 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004908 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004909 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004910 else
4911 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004912 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004913
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004914 switch (rettv->v_type)
4915 {
4916 case VAR_UNKNOWN:
4917 case VAR_ANY:
4918 case VAR_VOID:
4919 case VAR_FUNC:
4920 case VAR_PARTIAL:
4921 case VAR_FLOAT:
4922 case VAR_BOOL:
4923 case VAR_SPECIAL:
4924 case VAR_JOB:
4925 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004926 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004927 case VAR_CLASS:
4928 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004929 break; // not evaluating, skipping over subscript
4930
4931 case VAR_NUMBER:
4932 case VAR_STRING:
4933 {
4934 char_u *s = tv_get_string(rettv);
4935
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004936 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004937 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004938 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004939 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004940 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004941 else
4942 s = char_from_string(s, n1);
4943 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004944 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004945 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004946 // The resulting variable is a substring. If the indexes
4947 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004948 if (n1 < 0)
4949 {
4950 n1 = len + n1;
4951 if (n1 < 0)
4952 n1 = 0;
4953 }
4954 if (n2 < 0)
4955 n2 = len + n2;
4956 else if (n2 >= len)
4957 n2 = len;
4958 if (n1 >= len || n2 < 0 || n1 > n2)
4959 s = NULL;
4960 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004961 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004962 }
4963 else
4964 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004965 // The resulting variable is a string of a single
4966 // character. If the index is too big or negative the
4967 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004968 if (n1 >= len || n1 < 0)
4969 s = NULL;
4970 else
4971 s = vim_strnsave(s + n1, 1);
4972 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004973 clear_tv(rettv);
4974 rettv->v_type = VAR_STRING;
4975 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004976 }
4977 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004978
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004979 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004980 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4981 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004982 break;
4983
4984 case VAR_LIST:
4985 if (var1 == NULL)
4986 n1 = 0;
4987 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004988 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004989 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004990 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004991 return FAIL;
4992 break;
4993
4994 case VAR_DICT:
4995 {
4996 dictitem_T *item;
4997 typval_T tmp;
4998
4999 if (key == NULL)
5000 {
5001 key = tv_get_string_chk(var1);
5002 if (key == NULL)
5003 return FAIL;
5004 }
5005
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005006 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005007
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005008 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005009 {
5010 if (verbose)
5011 {
5012 if (keylen > 0)
5013 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005014 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005015 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005016 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005017 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005018
5019 copy_tv(&item->di_tv, &tmp);
5020 clear_tv(rettv);
5021 *rettv = tmp;
5022 }
5023 break;
5024 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005025 return OK;
5026}
5027
5028/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005029 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005030 */
5031 char_u *
5032partial_name(partial_T *pt)
5033{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02005034 if (pt != NULL)
5035 {
5036 if (pt->pt_name != NULL)
5037 return pt->pt_name;
5038 if (pt->pt_func != NULL)
5039 return pt->pt_func->uf_name;
5040 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005041 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005042}
5043
Bram Moolenaarddecc252016-04-06 22:59:37 +02005044 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005045partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005046{
5047 int i;
5048
5049 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005050 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005051 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005052 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005053 if (pt->pt_name != NULL)
5054 {
5055 func_unref(pt->pt_name);
5056 vim_free(pt->pt_name);
5057 }
5058 else
5059 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005060
Bram Moolenaar54656012021-06-09 20:50:46 +02005061 // "out_up" is no longer used, decrement refcount on partial that owns it.
5062 partial_unref(pt->pt_outer.out_up_partial);
5063
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005064 // Using pt_outer from another partial.
5065 partial_unref(pt->pt_outer_partial);
5066
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005067 // Decrease the reference count for the context of a closure. If down
5068 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005069 if (pt->pt_funcstack != NULL)
5070 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005071 --pt->pt_funcstack->fs_refcount;
5072 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005073 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005074 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005075 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5076 if (pt->pt_loopvars[i] != NULL)
5077 {
5078 --pt->pt_loopvars[i]->lvs_refcount;
5079 loopvars_check_refcount(pt->pt_loopvars[i]);
5080 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005081
Bram Moolenaarddecc252016-04-06 22:59:37 +02005082 vim_free(pt);
5083}
5084
5085/*
5086 * Unreference a closure: decrement the reference count and free it when it
5087 * becomes zero.
5088 */
5089 void
5090partial_unref(partial_T *pt)
5091{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005092 if (pt == NULL)
5093 return;
5094
5095 int done = FALSE;
5096
5097 if (--pt->pt_refcount <= 0)
5098 partial_free(pt);
5099
5100 // If the reference count goes down to one, the funcstack may be the
5101 // only reference and can be freed if no other partials reference it.
5102 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005103 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005104 // careful: if the funcstack is freed it may contain this partial
5105 // and it gets freed as well
5106 if (pt->pt_funcstack != NULL)
5107 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005108
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005109 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005110 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005111 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005112
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005113 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5114 if (pt->pt_loopvars[depth] != NULL
5115 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005116 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005117 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005118 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005119}
5120
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005121/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005122 * Return the next (unique) copy ID.
5123 * Used for serializing nested structures.
5124 */
5125 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005126get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005127{
5128 current_copyID += COPYID_INC;
5129 return current_copyID;
5130}
5131
5132/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005133 * Garbage collection for lists and dictionaries.
5134 *
5135 * We use reference counts to be able to free most items right away when they
5136 * are no longer used. But for composite items it's possible that it becomes
5137 * unused while the reference count is > 0: When there is a recursive
5138 * reference. Example:
5139 * :let l = [1, 2, 3]
5140 * :let d = {9: l}
5141 * :let l[1] = d
5142 *
5143 * Since this is quite unusual we handle this with garbage collection: every
5144 * once in a while find out which lists and dicts are not referenced from any
5145 * variable.
5146 *
5147 * Here is a good reference text about garbage collection (refers to Python
5148 * but it applies to all reference-counting mechanisms):
5149 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005150 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005151
5152/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005153 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005154 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005155 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005156 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005157 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005158garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005159{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005160 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005161 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005162 buf_T *buf;
5163 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005164 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005165 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005166
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005167 if (!testing)
5168 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005169 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005170 want_garbage_collect = FALSE;
5171 may_garbage_collect = FALSE;
5172 garbage_collect_at_exit = FALSE;
5173 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005174
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005175 // The execution stack can grow big, limit the size.
5176 if (exestack.ga_maxlen - exestack.ga_len > 500)
5177 {
5178 size_t new_len;
5179 char_u *pp;
5180 int n;
5181
5182 // Keep 150% of the current size, with a minimum of the growth size.
5183 n = exestack.ga_len / 2;
5184 if (n < exestack.ga_growsize)
5185 n = exestack.ga_growsize;
5186
5187 // Don't make it bigger though.
5188 if (exestack.ga_len + n < exestack.ga_maxlen)
5189 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005190 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005191 pp = vim_realloc(exestack.ga_data, new_len);
5192 if (pp == NULL)
5193 return FAIL;
5194 exestack.ga_maxlen = exestack.ga_len + n;
5195 exestack.ga_data = pp;
5196 }
5197 }
5198
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005199 // We advance by two because we add one for items referenced through
5200 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005201 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005202
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005203 /*
5204 * 1. Go through all accessible variables and mark all lists and dicts
5205 * with copyID.
5206 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005207
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005208 // Don't free variables in the previous_funccal list unless they are only
5209 // referenced through previous_funccal. This must be first, because if
5210 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005211 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005212
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005213 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005214 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005215
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005216 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005217 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005218 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5219 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005220
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005221 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005222 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005223 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5224 NULL, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005225 // window-local variables in autocmd windows
Bram Moolenaare76062c2022-11-28 18:51:43 +00005226 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005227 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00005228 abort = abort || set_ref_in_item(
5229 &aucmd_win[i].auc_win->w_winvar.di_tv, copyID, NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005230#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005231 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005232 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5233 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005234 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005235 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005236 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5237 NULL, NULL);
5238#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005239
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005240 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005241 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005242 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5243 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005244 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005245 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005246
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005247 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005248 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005249
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005250 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005251 abort = abort || set_ref_in_functions(copyID);
5252
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005253 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005254 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005255
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005256 // funcstacks keep variables for closures
5257 abort = abort || set_ref_in_funcstacks(copyID);
5258
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005259 // loopvars keep variables for loop blocks
5260 abort = abort || set_ref_in_loopvars(copyID);
5261
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005262 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005263 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005264
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005265 // callbacks in buffers
5266 abort = abort || set_ref_in_buffers(copyID);
5267
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005268 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5269 abort = abort || set_ref_in_insexpand_funcs(copyID);
5270
5271 // 'operatorfunc' callback
5272 abort = abort || set_ref_in_opfunc(copyID);
5273
5274 // 'tagfunc' callback
5275 abort = abort || set_ref_in_tagfunc(copyID);
5276
5277 // 'imactivatefunc' and 'imstatusfunc' callbacks
5278 abort = abort || set_ref_in_im_funcs(copyID);
5279
Bram Moolenaar1dced572012-04-05 16:54:08 +02005280#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005281 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005282#endif
5283
Bram Moolenaardb913952012-06-29 12:54:53 +02005284#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005285 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005286#endif
5287
5288#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005289 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005290#endif
5291
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005292#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005293 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005294 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005295#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005296#ifdef FEAT_NETBEANS_INTG
5297 abort = abort || set_ref_in_nb_channel(copyID);
5298#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005299
Bram Moolenaare3188e22016-05-31 21:13:04 +02005300#ifdef FEAT_TIMERS
5301 abort = abort || set_ref_in_timer(copyID);
5302#endif
5303
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005304#ifdef FEAT_QUICKFIX
5305 abort = abort || set_ref_in_quickfix(copyID);
5306#endif
5307
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005308#ifdef FEAT_TERMINAL
5309 abort = abort || set_ref_in_term(copyID);
5310#endif
5311
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005312#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005313 abort = abort || set_ref_in_popups(copyID);
5314#endif
5315
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005316 abort = abort || set_ref_in_classes(copyID);
5317
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005318 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005319 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005320 /*
5321 * 2. Free lists and dictionaries that are not referenced.
5322 */
5323 did_free = free_unref_items(copyID);
5324
5325 /*
5326 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005327 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005328 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005329 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005330 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005331 else if (p_verbose > 0)
5332 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005333 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005334 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005335
5336 return did_free;
5337}
5338
5339/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005340 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005341 */
5342 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005343free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005344{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005345 int did_free = FALSE;
5346
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005347 // Let all "free" functions know that we are here. This means no
5348 // dictionaries, lists, channels or jobs are to be freed, because we will
5349 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005350 in_free_unref_items = TRUE;
5351
5352 /*
5353 * PASS 1: free the contents of the items. We don't free the items
5354 * themselves yet, so that it is possible to decrement refcount counters
5355 */
5356
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005357 // Go through the list of dicts and free items without this copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005358 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005359
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005360 // Go through the list of lists and free items without this copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005361 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005362
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005363 // Go through the list of objects and free items without this copyID.
5364 did_free |= object_free_nonref(copyID);
5365
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005366 // Go through the list of classes and free items without this copyID.
5367 did_free |= class_free_nonref(copyID);
5368
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005369#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005370 // Go through the list of jobs and free items without the copyID. This
5371 // must happen before doing channels, because jobs refer to channels, but
5372 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005373 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5374
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005375 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005376 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5377#endif
5378
5379 /*
5380 * PASS 2: free the items themselves.
5381 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005382 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005383 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005384
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005385#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005386 // Go through the list of jobs and free items without the copyID. This
5387 // must happen before doing channels, because jobs refer to channels, but
5388 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005389 free_unused_jobs(copyID, COPYID_MASK);
5390
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005391 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005392 free_unused_channels(copyID, COPYID_MASK);
5393#endif
5394
5395 in_free_unref_items = FALSE;
5396
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005397 return did_free;
5398}
5399
5400/*
5401 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005402 * "list_stack" is used to add lists to be marked. Can be NULL.
5403 *
5404 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005405 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005406 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005407set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005408{
5409 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005410 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005411 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005412 hashtab_T *cur_ht;
5413 ht_stack_T *ht_stack = NULL;
5414 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005415
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005416 cur_ht = ht;
5417 for (;;)
5418 {
5419 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005420 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005421 // Mark each item in the hashtab. If the item contains a hashtab
5422 // it is added to ht_stack, if it contains a list it is added to
5423 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005424 todo = (int)cur_ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00005425 FOR_ALL_HASHTAB_ITEMS(cur_ht, hi, todo)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005426 if (!HASHITEM_EMPTY(hi))
5427 {
5428 --todo;
5429 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5430 &ht_stack, list_stack);
5431 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005432 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005433
5434 if (ht_stack == NULL)
5435 break;
5436
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005437 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005438 cur_ht = ht_stack->ht;
5439 tempitem = ht_stack;
5440 ht_stack = ht_stack->prev;
5441 free(tempitem);
5442 }
5443
5444 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005445}
5446
Dominique Pelle748b3082022-01-08 12:41:16 +00005447#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5448 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005449/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005450 * Mark a dict and its items with "copyID".
5451 * Returns TRUE if setting references failed somehow.
5452 */
5453 int
5454set_ref_in_dict(dict_T *d, int copyID)
5455{
5456 if (d != NULL && d->dv_copyID != copyID)
5457 {
5458 d->dv_copyID = copyID;
5459 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5460 }
5461 return FALSE;
5462}
Dominique Pelle748b3082022-01-08 12:41:16 +00005463#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005464
5465/*
5466 * Mark a list and its items with "copyID".
5467 * Returns TRUE if setting references failed somehow.
5468 */
5469 int
5470set_ref_in_list(list_T *ll, int copyID)
5471{
5472 if (ll != NULL && ll->lv_copyID != copyID)
5473 {
5474 ll->lv_copyID = copyID;
5475 return set_ref_in_list_items(ll, copyID, NULL);
5476 }
5477 return FALSE;
5478}
5479
5480/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005481 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005482 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5483 *
5484 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005485 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005486 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005487set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005488{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005489 listitem_T *li;
5490 int abort = FALSE;
5491 list_T *cur_l;
5492 list_stack_T *list_stack = NULL;
5493 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005494
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005495 cur_l = l;
5496 for (;;)
5497 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005498 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005499 // Mark each item in the list. If the item contains a hashtab
5500 // it is added to ht_stack, if it contains a list it is added to
5501 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005502 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5503 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5504 ht_stack, &list_stack);
5505 if (list_stack == NULL)
5506 break;
5507
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005508 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005509 cur_l = list_stack->list;
5510 tempitem = list_stack;
5511 list_stack = list_stack->prev;
5512 free(tempitem);
5513 }
5514
5515 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005516}
5517
5518/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005519 * Mark the partial in callback 'cb' with "copyID".
5520 */
5521 int
5522set_ref_in_callback(callback_T *cb, int copyID)
5523{
5524 typval_T tv;
5525
5526 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5527 return FALSE;
5528
5529 tv.v_type = VAR_PARTIAL;
5530 tv.vval.v_partial = cb->cb_partial;
5531 return set_ref_in_item(&tv, copyID, NULL, NULL);
5532}
5533
5534/*
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005535 * Mark the dict "dd" with "copyID".
5536 * Also see set_ref_in_item().
5537 */
5538 static int
5539set_ref_in_item_dict(
5540 dict_T *dd,
5541 int copyID,
5542 ht_stack_T **ht_stack,
5543 list_stack_T **list_stack)
5544{
5545 if (dd == NULL || dd->dv_copyID == copyID)
5546 return FALSE;
5547
5548 // Didn't see this dict yet.
5549 dd->dv_copyID = copyID;
5550 if (ht_stack == NULL)
5551 return set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5552
5553 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5554 if (newitem == NULL)
5555 return TRUE;
5556
5557 newitem->ht = &dd->dv_hashtab;
5558 newitem->prev = *ht_stack;
5559 *ht_stack = newitem;
5560
5561 return FALSE;
5562}
5563
5564/*
5565 * Mark the list "ll" with "copyID".
5566 * Also see set_ref_in_item().
5567 */
5568 static int
5569set_ref_in_item_list(
5570 list_T *ll,
5571 int copyID,
5572 ht_stack_T **ht_stack,
5573 list_stack_T **list_stack)
5574{
5575 if (ll == NULL || ll->lv_copyID == copyID)
5576 return FALSE;
5577
5578 // Didn't see this list yet.
5579 ll->lv_copyID = copyID;
5580 if (list_stack == NULL)
5581 return set_ref_in_list_items(ll, copyID, ht_stack);
5582
5583 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5584 if (newitem == NULL)
5585 return TRUE;
5586
5587 newitem->list = ll;
5588 newitem->prev = *list_stack;
5589 *list_stack = newitem;
5590
5591 return FALSE;
5592}
5593
5594/*
5595 * Mark the partial "pt" with "copyID".
5596 * Also see set_ref_in_item().
5597 */
5598 static int
5599set_ref_in_item_partial(
5600 partial_T *pt,
5601 int copyID,
5602 ht_stack_T **ht_stack,
5603 list_stack_T **list_stack)
5604{
5605 if (pt == NULL || pt->pt_copyID == copyID)
5606 return FALSE;
5607
5608 // Didn't see this partial yet.
5609 pt->pt_copyID = copyID;
5610
5611 int abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
5612
5613 if (pt->pt_dict != NULL)
5614 {
5615 typval_T dtv;
5616
5617 dtv.v_type = VAR_DICT;
5618 dtv.vval.v_dict = pt->pt_dict;
5619 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5620 }
5621
5622 for (int i = 0; i < pt->pt_argc; ++i)
5623 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5624 ht_stack, list_stack);
5625 // pt_funcstack is handled in set_ref_in_funcstacks()
5626 // pt_loopvars is handled in set_ref_in_loopvars()
5627
5628 return abort;
5629}
5630
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005631#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005632/*
5633 * Mark the job "pt" with "copyID".
5634 * Also see set_ref_in_item().
5635 */
5636 static int
5637set_ref_in_item_job(
5638 job_T *job,
5639 int copyID,
5640 ht_stack_T **ht_stack,
5641 list_stack_T **list_stack)
5642{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005643 typval_T dtv;
5644
5645 if (job == NULL || job->jv_copyID == copyID)
5646 return FALSE;
5647
5648 job->jv_copyID = copyID;
5649 if (job->jv_channel != NULL)
5650 {
5651 dtv.v_type = VAR_CHANNEL;
5652 dtv.vval.v_channel = job->jv_channel;
5653 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5654 }
5655 if (job->jv_exit_cb.cb_partial != NULL)
5656 {
5657 dtv.v_type = VAR_PARTIAL;
5658 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
5659 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5660 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005661
5662 return FALSE;
5663}
5664
5665/*
5666 * Mark the channel "ch" with "copyID".
5667 * Also see set_ref_in_item().
5668 */
5669 static int
5670set_ref_in_item_channel(
5671 channel_T *ch,
5672 int copyID,
5673 ht_stack_T **ht_stack,
5674 list_stack_T **list_stack)
5675{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005676 typval_T dtv;
5677
5678 if (ch == NULL || ch->ch_copyID == copyID)
5679 return FALSE;
5680
5681 ch->ch_copyID = copyID;
5682 for (ch_part_T part = PART_SOCK; part < PART_COUNT; ++part)
5683 {
5684 for (jsonq_T *jq = ch->ch_part[part].ch_json_head.jq_next;
5685 jq != NULL; jq = jq->jq_next)
5686 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5687 for (cbq_T *cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5688 cq = cq->cq_next)
5689 if (cq->cq_callback.cb_partial != NULL)
5690 {
5691 dtv.v_type = VAR_PARTIAL;
5692 dtv.vval.v_partial = cq->cq_callback.cb_partial;
5693 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5694 }
5695 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
5696 {
5697 dtv.v_type = VAR_PARTIAL;
5698 dtv.vval.v_partial = ch->ch_part[part].ch_callback.cb_partial;
5699 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5700 }
5701 }
5702 if (ch->ch_callback.cb_partial != NULL)
5703 {
5704 dtv.v_type = VAR_PARTIAL;
5705 dtv.vval.v_partial = ch->ch_callback.cb_partial;
5706 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5707 }
5708 if (ch->ch_close_cb.cb_partial != NULL)
5709 {
5710 dtv.v_type = VAR_PARTIAL;
5711 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
5712 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5713 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005714
5715 return FALSE;
5716}
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005717#endif
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005718
5719/*
5720 * Mark the class "cl" with "copyID".
5721 * Also see set_ref_in_item().
5722 */
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005723 int
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005724set_ref_in_item_class(
5725 class_T *cl,
5726 int copyID,
5727 ht_stack_T **ht_stack,
5728 list_stack_T **list_stack)
5729{
5730 int abort = FALSE;
5731
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005732 if (cl == NULL || cl->class_copyID == copyID)
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005733 return FALSE;
5734
5735 cl->class_copyID = copyID;
Yegappan Lakshmanan544be0d2023-09-04 22:14:28 +02005736 if (cl->class_members_tv != NULL)
5737 {
5738 // The "class_members_tv" table is allocated only for regular classes
5739 // and not for interfaces.
5740 for (int i = 0; !abort && i < cl->class_class_member_count; ++i)
5741 abort = abort || set_ref_in_item(
5742 &cl->class_members_tv[i],
5743 copyID, ht_stack, list_stack);
5744 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005745
5746 for (int i = 0; !abort && i < cl->class_class_function_count; ++i)
5747 abort = abort || set_ref_in_func(NULL,
5748 cl->class_class_functions[i], copyID);
5749
5750 for (int i = 0; !abort && i < cl->class_obj_method_count; ++i)
5751 abort = abort || set_ref_in_func(NULL,
5752 cl->class_obj_methods[i], copyID);
5753
5754 return abort;
5755}
5756
5757/*
5758 * Mark the object "cl" with "copyID".
5759 * Also see set_ref_in_item().
5760 */
5761 static int
5762set_ref_in_item_object(
5763 object_T *obj,
5764 int copyID,
5765 ht_stack_T **ht_stack,
5766 list_stack_T **list_stack)
5767{
5768 int abort = FALSE;
5769
5770 if (obj == NULL || obj->obj_copyID == copyID)
5771 return FALSE;
5772
5773 obj->obj_copyID = copyID;
5774
5775 // The typval_T array is right after the object_T.
5776 typval_T *mtv = (typval_T *)(obj + 1);
5777 for (int i = 0; !abort
5778 && i < obj->obj_class->class_obj_member_count; ++i)
5779 abort = abort || set_ref_in_item(mtv + i, copyID,
5780 ht_stack, list_stack);
5781
5782 return abort;
5783}
5784
5785/*
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005786 * Mark all lists, dicts and other container types referenced through typval
5787 * "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005788 * "list_stack" is used to add lists to be marked. Can be NULL.
5789 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5790 *
5791 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005792 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005793 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005794set_ref_in_item(
5795 typval_T *tv,
5796 int copyID,
5797 ht_stack_T **ht_stack,
5798 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005799{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005800 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005801
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005802 switch (tv->v_type)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005803 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005804 case VAR_DICT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005805 return set_ref_in_item_dict(tv->vval.v_dict, copyID,
5806 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005807
5808 case VAR_LIST:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005809 return set_ref_in_item_list(tv->vval.v_list, copyID,
5810 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005811
5812 case VAR_FUNC:
5813 {
5814 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
5815 break;
5816 }
5817
5818 case VAR_PARTIAL:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005819 return set_ref_in_item_partial(tv->vval.v_partial, copyID,
5820 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005821
5822 case VAR_JOB:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005823#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005824 return set_ref_in_item_job(tv->vval.v_job, copyID,
5825 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005826#else
5827 break;
5828#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005829
5830 case VAR_CHANNEL:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005831#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005832 return set_ref_in_item_channel(tv->vval.v_channel, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005833 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005834#else
5835 break;
5836#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005837
5838 case VAR_CLASS:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005839 return set_ref_in_item_class(tv->vval.v_class, copyID,
5840 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005841
5842 case VAR_OBJECT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005843 return set_ref_in_item_object(tv->vval.v_object, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005844 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005845
5846 case VAR_UNKNOWN:
5847 case VAR_ANY:
5848 case VAR_VOID:
5849 case VAR_BOOL:
5850 case VAR_SPECIAL:
5851 case VAR_NUMBER:
5852 case VAR_FLOAT:
5853 case VAR_STRING:
5854 case VAR_BLOB:
5855 case VAR_INSTR:
5856 // Types that do not contain any other item
5857 break;
5858 }
5859
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005860 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005861}
5862
Bram Moolenaar8c711452005-01-14 21:53:12 +00005863/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005864 * Return a string with the string representation of a variable.
5865 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005866 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005867 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005868 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005869 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005870 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005871 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5872 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005873 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005874 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005875 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005876echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005877 typval_T *tv,
5878 char_u **tofree,
5879 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005880 int copyID,
5881 int echo_style,
5882 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005883 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005884{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005885 static int recurse = 0;
5886 char_u *r = NULL;
5887
Bram Moolenaar33570922005-01-25 22:26:29 +00005888 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005889 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005890 if (!did_echo_string_emsg)
5891 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005892 // Only give this message once for a recursive call to avoid
5893 // flooding the user with errors. And stop iterating over lists
5894 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005895 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005896 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005897 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005898 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005899 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005900 }
5901 ++recurse;
5902
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005903 switch (tv->v_type)
5904 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005905 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005906 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005907 {
5908 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005909 r = tv->vval.v_string;
5910 if (r == NULL)
5911 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005912 }
5913 else
5914 {
5915 *tofree = string_quote(tv->vval.v_string, FALSE);
5916 r = *tofree;
5917 }
5918 break;
5919
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005920 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005921 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005922 char_u buf[MAX_FUNC_NAME_LEN];
5923
5924 if (echo_style)
5925 {
LemonBoya5d35902022-04-29 21:15:02 +01005926 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5927 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005928 buf, MAX_FUNC_NAME_LEN);
5929 if (r == buf)
5930 {
5931 r = vim_strsave(buf);
5932 *tofree = r;
5933 }
5934 else
5935 *tofree = NULL;
5936 }
5937 else
5938 {
5939 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5940 : make_ufunc_name_readable(
5941 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5942 TRUE);
5943 r = *tofree;
5944 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005945 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005946 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005947
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005948 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005949 {
5950 partial_T *pt = tv->vval.v_partial;
5951 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005952 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005953 garray_T ga;
5954 int i;
5955 char_u *tf;
5956
5957 ga_init2(&ga, 1, 100);
5958 ga_concat(&ga, (char_u *)"function(");
5959 if (fname != NULL)
5960 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005961 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005962 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005963 && vim_isupper(fname[1]))
5964 {
5965 ga_concat(&ga, (char_u *)"'g:");
5966 ga_concat(&ga, fname + 1);
5967 }
5968 else
5969 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005970 vim_free(fname);
5971 }
5972 if (pt != NULL && pt->pt_argc > 0)
5973 {
5974 ga_concat(&ga, (char_u *)", [");
5975 for (i = 0; i < pt->pt_argc; ++i)
5976 {
5977 if (i > 0)
5978 ga_concat(&ga, (char_u *)", ");
5979 ga_concat(&ga,
5980 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5981 vim_free(tf);
5982 }
5983 ga_concat(&ga, (char_u *)"]");
5984 }
5985 if (pt != NULL && pt->pt_dict != NULL)
5986 {
5987 typval_T dtv;
5988
5989 ga_concat(&ga, (char_u *)", ");
5990 dtv.v_type = VAR_DICT;
5991 dtv.vval.v_dict = pt->pt_dict;
5992 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5993 vim_free(tf);
5994 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005995 // terminate with ')' and a NUL
5996 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005997
5998 *tofree = ga.ga_data;
5999 r = *tofree;
6000 break;
6001 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006002
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006003 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006004 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006005 break;
6006
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006008 if (tv->vval.v_list == NULL)
6009 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02006010 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006011 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02006012 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006013 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006014 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6015 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006016 {
6017 *tofree = NULL;
6018 r = (char_u *)"[...]";
6019 }
6020 else
6021 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006022 int old_copyID = tv->vval.v_list->lv_copyID;
6023
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006024 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006025 *tofree = list2string(tv, copyID, restore_copyID);
6026 if (restore_copyID)
6027 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006028 r = *tofree;
6029 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006030 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006031
Bram Moolenaar8c711452005-01-14 21:53:12 +00006032 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006033 if (tv->vval.v_dict == NULL)
6034 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006035 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006036 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006037 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006038 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006039 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6040 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006041 {
6042 *tofree = NULL;
6043 r = (char_u *)"{...}";
6044 }
6045 else
6046 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006047 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006048
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006049 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006050 *tofree = dict2string(tv, copyID, restore_copyID);
6051 if (restore_copyID)
6052 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006053 r = *tofree;
6054 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006055 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006056
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006057 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006058 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006059 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006060 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006061 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006062 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006063 break;
6064
Bram Moolenaar835dc632016-02-07 14:27:38 +01006065 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006066 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006067#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00006068 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02006069 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
6070 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006071 if (composite_val)
6072 {
6073 *tofree = string_quote(r, FALSE);
6074 r = *tofree;
6075 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006076#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006077 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006078
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006079 case VAR_INSTR:
6080 *tofree = NULL;
6081 r = (char_u *)"instructions";
6082 break;
6083
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006084 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006085 {
6086 class_T *cl = tv->vval.v_class;
6087 size_t len = 6 + (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
6088 r = *tofree = alloc(len);
6089 vim_snprintf((char *)r, len, "class %s",
6090 cl == NULL ? "[unknown]" : (char *)cl->class_name);
6091 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006092 break;
6093
6094 case VAR_OBJECT:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006095 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006096 garray_T ga;
6097 ga_init2(&ga, 1, 50);
6098 ga_concat(&ga, (char_u *)"object of ");
6099 object_T *obj = tv->vval.v_object;
6100 class_T *cl = obj == NULL ? NULL : obj->obj_class;
6101 ga_concat(&ga, cl == NULL ? (char_u *)"[unknown]"
6102 : cl->class_name);
6103 if (cl != NULL)
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006104 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006105 ga_concat(&ga, (char_u *)" {");
6106 for (int i = 0; i < cl->class_obj_member_count; ++i)
6107 {
6108 if (i > 0)
6109 ga_concat(&ga, (char_u *)", ");
Bram Moolenaard505d172022-12-18 21:42:55 +00006110 ocmember_T *m = &cl->class_obj_members[i];
6111 ga_concat(&ga, m->ocm_name);
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006112 ga_concat(&ga, (char_u *)": ");
6113 char_u *tf = NULL;
6114 ga_concat(&ga, echo_string_core(
6115 (typval_T *)(obj + 1) + i,
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006116 &tf, numbuf, copyID, echo_style,
6117 restore_copyID, composite_val));
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006118 vim_free(tf);
6119 }
6120 ga_concat(&ga, (char_u *)"}");
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006121 }
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006122
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006123 *tofree = r = ga.ga_data;
6124 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006125 break;
6126
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006127 case VAR_FLOAT:
6128 *tofree = NULL;
6129 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6130 r = numbuf;
6131 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006132
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006133 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006134 case VAR_SPECIAL:
6135 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006136 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006137 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006138 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006139
Bram Moolenaar8502c702014-06-17 12:51:16 +02006140 if (--recurse == 0)
6141 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006142 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006143}
6144
6145/*
6146 * Return a string with the string representation of a variable.
6147 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6148 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006149 * Does not put quotes around strings, as ":echo" displays values.
6150 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6151 * May return NULL.
6152 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006153 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006154echo_string(
6155 typval_T *tv,
6156 char_u **tofree,
6157 char_u *numbuf,
6158 int copyID)
6159{
6160 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6161}
6162
6163/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006164 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6165 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006166 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006167 */
6168 int
6169buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6170{
6171 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006172 char_u *t;
6173 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006174
6175 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6176 return -1;
6177
6178 if (lnum > buf->b_ml.ml_line_count)
6179 lnum = buf->b_ml.ml_line_count;
6180
6181 str = ml_get_buf(buf, lnum, FALSE);
6182 if (str == NULL)
6183 return -1;
6184
6185 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006186 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006187
Bram Moolenaar91458462021-01-13 20:08:38 +01006188 // count the number of characters
6189 t = str;
6190 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6191 t += mb_ptr2len(t);
6192
6193 // In insert mode, when the cursor is at the end of a non-empty line,
6194 // byteidx points to the NUL character immediately past the end of the
6195 // string. In this case, add one to the character count.
6196 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6197 count++;
6198
6199 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006200}
6201
6202/*
6203 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006204 * byte index. Works only for loaded buffers. Returns -1 on failure.
6205 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006206 */
6207 int
6208buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6209{
6210 char_u *str;
6211 char_u *t;
6212
6213 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6214 return -1;
6215
6216 if (lnum > buf->b_ml.ml_line_count)
6217 lnum = buf->b_ml.ml_line_count;
6218
6219 str = ml_get_buf(buf, lnum, FALSE);
6220 if (str == NULL)
6221 return -1;
6222
6223 // Convert the character offset to a byte offset
6224 t = str;
6225 while (*t != NUL && --charidx > 0)
6226 t += mb_ptr2len(t);
6227
Bram Moolenaar91458462021-01-13 20:08:38 +01006228 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006229}
6230
6231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006233 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006235 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006236var2fpos(
6237 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006238 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006239 int *fnum, // set to fnum for '0, 'A, etc.
6240 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006242 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006244 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006246 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006247 if (varp->v_type == VAR_LIST)
6248 {
6249 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006250 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006251 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006252 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006253
6254 l = varp->vval.v_list;
6255 if (l == NULL)
6256 return NULL;
6257
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006258 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006259 pos.lnum = list_find_nr(l, 0L, &error);
6260 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006261 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006262 if (charcol)
6263 len = (long)mb_charlen(ml_get(pos.lnum));
6264 else
6265 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006266
Bram Moolenaarec65d772020-08-20 22:29:12 +02006267 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006268 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006269 li = list_find(l, 1L);
6270 if (li != NULL && li->li_tv.v_type == VAR_STRING
6271 && li->li_tv.vval.v_string != NULL
6272 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006273 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006274 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006275 }
6276 else
6277 {
6278 pos.col = list_find_nr(l, 1L, &error);
6279 if (error)
6280 return NULL;
6281 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006282
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006283 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006284 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006285 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006286 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006287
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006288 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006289 pos.coladd = list_find_nr(l, 2L, &error);
6290 if (error)
6291 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006292
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006293 return &pos;
6294 }
6295
Bram Moolenaarc5809432021-03-27 21:23:30 +01006296 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6297 return NULL;
6298
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006299 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006300 if (name == NULL)
6301 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006302
6303 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006304 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006305 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006306 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006307 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006308 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006309 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006310 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006311 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006312 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006313 pos = VIsual;
6314 else
6315 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006316 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006317 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006318 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006320 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006321 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6323 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006324 pos = *pp;
6325 }
6326 if (pos.lnum != 0)
6327 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006328 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006329 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6330 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006332
Bram Moolenaara5525202006-03-02 22:52:09 +00006333 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006334
Bram Moolenaar477933c2007-07-17 14:32:23 +00006335 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006336 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006337 // the "w_valid" flags are not reset when moving the cursor, but they
6338 // do matter for update_topline() and validate_botline().
6339 check_cursor_moved(curwin);
6340
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006341 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006342 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006343 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006344 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006345 // In silent Ex mode topline is zero, but that's not a valid line
6346 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006347 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006348 return &pos;
6349 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006350 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006351 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006352 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006353 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006354 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006355 return &pos;
6356 }
6357 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006358 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006360 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 {
6362 pos.lnum = curbuf->b_ml.ml_line_count;
6363 pos.col = 0;
6364 }
6365 else
6366 {
6367 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006368 if (charcol)
6369 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6370 else
6371 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 }
6373 return &pos;
6374 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006375 if (in_vim9script())
6376 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 return NULL;
6378}
6379
6380/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006381 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006382 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006383 * Note that the column is passed on as-is, the caller may want to decrement
6384 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006385 * If "charcol" is TRUE use the column as the character index instead of the
6386 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006387 * Return FAIL when conversion is not possible, doesn't check the position for
6388 * validity.
6389 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006390 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006391list2fpos(
6392 typval_T *arg,
6393 pos_T *posp,
6394 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006395 colnr_T *curswantp,
6396 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006397{
6398 list_T *l = arg->vval.v_list;
6399 long i = 0;
6400 long n;
6401
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006402 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6403 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006404 if (arg->v_type != VAR_LIST
6405 || l == NULL
6406 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006407 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006408 return FAIL;
6409
6410 if (fnump != NULL)
6411 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006412 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006413 if (n < 0)
6414 return FAIL;
6415 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006416 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006417 *fnump = n;
6418 }
6419
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006420 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006421 if (n < 0)
6422 return FAIL;
6423 posp->lnum = n;
6424
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006425 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006426 if (n < 0)
6427 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006428 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006429 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006430 if (charcol)
6431 {
6432 buf_T *buf;
6433
6434 // Get the text for the specified line in a loaded buffer
6435 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6436 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6437 return FAIL;
6438
Bram Moolenaar79f23442022-10-10 12:42:57 +01006439 n = buf_charidx_to_byteidx(buf,
6440 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006441 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006442 posp->col = n;
6443
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006444 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006445 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006446 posp->coladd = 0;
6447 else
6448 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006449
Bram Moolenaar493c1782014-05-28 14:34:46 +02006450 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006451 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006452
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006453 return OK;
6454}
6455
6456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 * Get the length of an environment variable name.
6458 * Advance "arg" to the first character after the name.
6459 * Return 0 for error.
6460 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006461 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006462get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463{
6464 char_u *p;
6465 int len;
6466
6467 for (p = *arg; vim_isIDc(*p); ++p)
6468 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006469 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 return 0;
6471
6472 len = (int)(p - *arg);
6473 *arg = p;
6474 return len;
6475}
6476
6477/*
6478 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006479 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 * Return 0 if something is wrong.
6481 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006482 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006483get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484{
6485 char_u *p;
6486 int len;
6487
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006488 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006490 {
6491 if (*p == ':')
6492 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006493 // "s:" is start of "s:var", but "n:" is not and can be used in
6494 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006495 len = (int)(p - *arg);
6496 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6497 || len > 1)
6498 break;
6499 }
6500 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006501 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 return 0;
6503
6504 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006505 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506
6507 return len;
6508}
6509
6510/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006511 * Get the length of the name of a variable or function.
6512 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006514 * Return -1 if curly braces expansion failed.
6515 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 * If the name contains 'magic' {}'s, expand them and return the
6517 * expanded name in an allocated string via 'alias' - caller must free.
6518 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006519 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006520get_name_len(
6521 char_u **arg,
6522 char_u **alias,
6523 int evaluate,
6524 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525{
6526 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 char_u *p;
6528 char_u *expr_start;
6529 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006531 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532
6533 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6534 && (*arg)[2] == (int)KE_SNR)
6535 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006536 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 *arg += 3;
6538 return get_id_len(arg) + 3;
6539 }
6540 len = eval_fname_script(*arg);
6541 if (len > 0)
6542 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006543 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 *arg += len;
6545 }
6546
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006548 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006550 p = find_name_end(*arg, &expr_start, &expr_end,
6551 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 if (expr_start != NULL)
6553 {
6554 char_u *temp_string;
6555
6556 if (!evaluate)
6557 {
6558 len += (int)(p - *arg);
6559 *arg = skipwhite(p);
6560 return len;
6561 }
6562
6563 /*
6564 * Include any <SID> etc in the expanded string:
6565 * Thus the -len here.
6566 */
6567 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6568 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006569 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 *alias = temp_string;
6571 *arg = skipwhite(p);
6572 return (int)STRLEN(temp_string);
6573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574
6575 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006576 // Only give an error when there is something, otherwise it will be
6577 // reported at a higher level.
6578 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006579 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580
6581 return len;
6582}
6583
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006584/*
6585 * Find the end of a variable or function name, taking care of magic braces.
6586 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6587 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006588 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006589 * Return a pointer to just after the name. Equal to "arg" if there is no
6590 * valid name.
6591 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006592 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006593find_name_end(
6594 char_u *arg,
6595 char_u **expr_start,
6596 char_u **expr_end,
6597 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006599 int mb_nest = 0;
6600 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006602 int len;
zeertzjqa93d9cd2023-05-02 16:25:47 +01006603 int allow_curly = (flags & FNE_ALLOW_CURLY) || !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006605 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006607 *expr_start = NULL;
6608 *expr_end = NULL;
6609 }
6610
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006611 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006612 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006613 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006614 return arg;
6615
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006616 for (p = arg; *p != NUL
6617 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006618 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006619 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006620 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006621 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006622 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006623 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006624 if (*p == '\'')
6625 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006626 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006627 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006628 ;
6629 if (*p == NUL)
6630 break;
6631 }
6632 else if (*p == '"')
6633 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006634 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006635 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006636 if (*p == '\\' && p[1] != NUL)
6637 ++p;
6638 if (*p == NUL)
6639 break;
6640 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006641 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6642 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006643 // "s:" is start of "s:var", but "n:" is not and can be used in
6644 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006645 len = (int)(p - arg);
6646 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006647 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006648 break;
6649 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006650
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006651 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006653 if (*p == '[')
6654 ++br_nest;
6655 else if (*p == ']')
6656 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006658
zeertzjqa93d9cd2023-05-02 16:25:47 +01006659 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006661 if (*p == '{')
6662 {
6663 mb_nest++;
6664 if (expr_start != NULL && *expr_start == NULL)
6665 *expr_start = p;
6666 }
6667 else if (*p == '}')
6668 {
6669 mb_nest--;
6670 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6671 *expr_end = p;
6672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 }
6675
6676 return p;
6677}
6678
6679/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006680 * Expands out the 'magic' {}'s in a variable/function name.
6681 * Note that this can call itself recursively, to deal with
6682 * constructs like foo{bar}{baz}{bam}
6683 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6684 * "in_start" ^
6685 * "expr_start" ^
6686 * "expr_end" ^
6687 * "in_end" ^
6688 *
6689 * Returns a new allocated string, which the caller must free.
6690 * Returns NULL for failure.
6691 */
6692 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006693make_expanded_name(
6694 char_u *in_start,
6695 char_u *expr_start,
6696 char_u *expr_end,
6697 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006698{
6699 char_u c1;
6700 char_u *retval = NULL;
6701 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006702
6703 if (expr_end == NULL || in_end == NULL)
6704 return NULL;
6705 *expr_start = NUL;
6706 *expr_end = NUL;
6707 c1 = *in_end;
6708 *in_end = NUL;
6709
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006710 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006711 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006712 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006713 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6714 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006715 if (retval != NULL)
6716 {
6717 STRCPY(retval, in_start);
6718 STRCAT(retval, temp_result);
6719 STRCAT(retval, expr_end + 1);
6720 }
6721 }
6722 vim_free(temp_result);
6723
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006724 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006725 *expr_start = '{';
6726 *expr_end = '}';
6727
6728 if (retval != NULL)
6729 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006730 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006731 if (expr_start != NULL)
6732 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006733 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006734 temp_result = make_expanded_name(retval, expr_start,
6735 expr_end, temp_result);
6736 vim_free(retval);
6737 retval = temp_result;
6738 }
6739 }
6740
6741 return retval;
6742}
6743
6744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006746 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006748 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006749eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006751 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006752}
6753
6754/*
6755 * Return TRUE if character "c" can be used as the first character in a
6756 * variable or function name (excluding '{' and '}').
6757 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006758 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006759eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006760{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006761 return ASCII_ISALPHA(c) || c == '_';
6762}
6763
6764/*
6765 * Return TRUE if character "c" can be used as the first character of a
6766 * dictionary key.
6767 */
6768 int
6769eval_isdictc(int c)
6770{
6771 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772}
6773
6774/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006775 * Handle:
6776 * - expr[expr], expr[expr:expr] subscript
6777 * - ".name" lookup
6778 * - function call with Funcref variable: func(expr)
6779 * - method call: var->method()
6780 *
6781 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006782 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006783 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006784 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006785handle_subscript(
6786 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006787 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006788 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006789 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006790 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006791{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006792 int evaluate = evalarg != NULL
6793 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006794 int ret = OK;
6795 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006796 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006797 int getnext;
6798 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006799
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006800 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006801 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006802 // When at the end of the line and ".name" or "->{" or "->X" follows in
6803 // the next line then consume the line break.
6804 p = eval_next_non_blank(*arg, evalarg, &getnext);
6805 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00006806 && ((*p == '.'
6807 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
6808 || rettv->v_type == VAR_CLASS
6809 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02006810 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6811 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6812 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006813 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006814 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006815 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006816 check_white = FALSE;
6817 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006818
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006819 if (rettv->v_type == VAR_ANY)
6820 {
6821 char_u *exp_name;
6822 int cc;
6823 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006824 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006825 type_T *type;
6826
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006827 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006828 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006829 if (**arg != '.')
6830 {
6831 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006832 semsg(_(e_expected_dot_after_name_str),
6833 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006834 ret = FAIL;
6835 break;
6836 }
6837 ++*arg;
6838 if (IS_WHITE_OR_NUL(**arg))
6839 {
6840 if (verbose)
6841 emsg(_(e_no_white_space_allowed_after_dot));
6842 ret = FAIL;
6843 break;
6844 }
6845
6846 // isolate the name
6847 exp_name = *arg;
6848 while (eval_isnamec(**arg))
6849 ++*arg;
6850 cc = **arg;
6851 **arg = NUL;
6852
6853 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01006854 evalarg == NULL ? NULL : evalarg->eval_cctx,
6855 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006856 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006857
6858 if (idx < 0 && ufunc == NULL)
6859 {
6860 ret = FAIL;
6861 break;
6862 }
6863 if (idx >= 0)
6864 {
6865 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6866 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6867
6868 copy_tv(sv->sv_tv, rettv);
6869 }
6870 else
6871 {
6872 rettv->v_type = VAR_FUNC;
6873 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6874 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006875 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006876 }
6877
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006878 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6879 || rettv->v_type == VAR_PARTIAL))
6880 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006881 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006882 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6883 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006884
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006885 // Stop the expression evaluation when immediately aborting on
6886 // error, or when an interrupt occurred or an exception was thrown
6887 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006888 if (aborting())
6889 {
6890 if (ret == OK)
6891 clear_tv(rettv);
6892 ret = FAIL;
6893 }
6894 dict_unref(selfdict);
6895 selfdict = NULL;
6896 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006897 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006898 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006899 if (in_vim9script())
6900 *arg = skipwhite(p + 2);
6901 else
6902 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00006903 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006904 {
zeertzjqc481ad32023-03-11 16:18:51 +00006905 emsg(_(e_no_white_space_allowed_before_parenthesis));
6906 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006907 }
zeertzjqc481ad32023-03-11 16:18:51 +00006908 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
6909 // expr->{lambda}() or expr->(lambda)()
6910 ret = eval_lambda(arg, rettv, evalarg, verbose);
6911 else
6912 // expr->name()
6913 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02006914 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006915 // "." is ".name" lookup when we found a dict or when evaluating and
6916 // scriptversion is at least 2, where string concatenation is "..".
6917 else if (**arg == '['
6918 || (**arg == '.' && (rettv->v_type == VAR_DICT
6919 || (!evaluate
6920 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006921 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006922 {
6923 dict_unref(selfdict);
6924 if (rettv->v_type == VAR_DICT)
6925 {
6926 selfdict = rettv->vval.v_dict;
6927 if (selfdict != NULL)
6928 ++selfdict->dv_refcount;
6929 }
6930 else
6931 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006932 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006933 {
6934 clear_tv(rettv);
6935 ret = FAIL;
6936 }
6937 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006938 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
6939 || rettv->v_type == VAR_OBJECT))
6940 {
6941 // class member: SomeClass.varname
6942 // class method: SomeClass.SomeMethod()
6943 // class constructor: SomeClass.new()
6944 // object member: someObject.varname
6945 // object method: someObject.SomeMethod()
6946 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
6947 {
6948 clear_tv(rettv);
6949 ret = FAIL;
6950 }
6951 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006952 else
6953 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006954 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006955
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006956 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6957 // Don't do this when "Func" is already a partial that was bound
6958 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006959 if (selfdict != NULL
6960 && (rettv->v_type == VAR_FUNC
6961 || (rettv->v_type == VAR_PARTIAL
6962 && (rettv->vval.v_partial->pt_auto
6963 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006964 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006965
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006966 dict_unref(selfdict);
6967 return ret;
6968}
6969
6970/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006971 * Make a copy of an item.
6972 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006973 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006974 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6975 * reference to an already copied list/dict can be used.
6976 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006977 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006978 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006979item_copy(
6980 typval_T *from,
6981 typval_T *to,
6982 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006983 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006984 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006985{
6986 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006987 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006988
Bram Moolenaar33570922005-01-25 22:26:29 +00006989 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006990 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006991 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006992 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006993 }
6994 ++recurse;
6995
6996 switch (from->v_type)
6997 {
6998 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006999 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007000 case VAR_STRING:
7001 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007002 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01007003 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007004 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007005 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007006 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007007 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007008 case VAR_CLASS:
7009 case VAR_OBJECT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007010 copy_tv(from, to);
7011 break;
7012 case VAR_LIST:
7013 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007014 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007015 if (from->vval.v_list == NULL)
7016 to->vval.v_list = NULL;
7017 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7018 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007019 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007020 to->vval.v_list = from->vval.v_list->lv_copylist;
7021 ++to->vval.v_list->lv_refcount;
7022 }
7023 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007024 to->vval.v_list = list_copy(from->vval.v_list,
7025 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007026 if (to->vval.v_list == NULL)
7027 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007028 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007029 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007030 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007031 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007032 case VAR_DICT:
7033 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007034 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007035 if (from->vval.v_dict == NULL)
7036 to->vval.v_dict = NULL;
7037 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7038 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007039 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007040 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7041 ++to->vval.v_dict->dv_refcount;
7042 }
7043 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007044 to->vval.v_dict = dict_copy(from->vval.v_dict,
7045 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007046 if (to->vval.v_dict == NULL)
7047 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007048 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007049 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007050 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007051 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007052 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007053 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007054 }
7055 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007056 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007057}
7058
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007059 void
7060echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7061{
7062 char_u *tofree;
7063 char_u numbuf[NUMBUFLEN];
7064 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7065
7066 if (*atstart)
7067 {
7068 *atstart = FALSE;
7069 // Call msg_start() after eval1(), evaluating the expression
7070 // may cause a message to appear.
7071 if (with_space)
7072 {
7073 // Mark the saved text as finishing the line, so that what
7074 // follows is displayed on a new line when scrolling back
7075 // at the more prompt.
7076 msg_sb_eol();
7077 msg_start();
7078 }
7079 }
7080 else if (with_space)
7081 msg_puts_attr(" ", echo_attr);
7082
7083 if (p != NULL)
7084 for ( ; *p != NUL && !got_int; ++p)
7085 {
7086 if (*p == '\n' || *p == '\r' || *p == TAB)
7087 {
7088 if (*p != TAB && *needclr)
7089 {
7090 // remove any text still there from the command
7091 msg_clr_eos();
7092 *needclr = FALSE;
7093 }
7094 msg_putchar_attr(*p, echo_attr);
7095 }
7096 else
7097 {
7098 if (has_mbyte)
7099 {
7100 int i = (*mb_ptr2len)(p);
7101
7102 (void)msg_outtrans_len_attr(p, i, echo_attr);
7103 p += i - 1;
7104 }
7105 else
7106 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7107 }
7108 }
7109 vim_free(tofree);
7110}
7111
Bram Moolenaare9a41262005-01-15 22:18:47 +00007112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 * ":echo expr1 ..." print each argument separated with a space, add a
7114 * newline at the end.
7115 * ":echon expr1 ..." print each argument plain.
7116 */
7117 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007118ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119{
7120 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007121 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007122 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 int needclr = TRUE;
7124 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007125 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007126 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007127 evalarg_T evalarg;
7128
Bram Moolenaare707c882020-07-01 13:07:37 +02007129 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130
7131 if (eap->skip)
7132 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007133 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007135 // If eval1() causes an error message the text from the command may
7136 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007137 need_clr_eos = needclr;
7138
Bram Moolenaar68db9962021-05-09 23:19:22 +02007139 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007140 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 {
7142 /*
7143 * Report the invalid expression unless the expression evaluation
7144 * has been cancelled due to an aborting error, an interrupt, or an
7145 * exception.
7146 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007147 if (!aborting() && did_emsg == did_emsg_before
7148 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007149 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007150 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 break;
7152 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007153 need_clr_eos = FALSE;
7154
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007156 {
7157 if (rettv.v_type == VAR_VOID)
7158 {
7159 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7160 break;
7161 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007162 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007163 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007164
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007165 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 arg = skipwhite(arg);
7167 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007168 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007169 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170
7171 if (eap->skip)
7172 --emsg_skip;
7173 else
7174 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007175 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 if (needclr)
7177 msg_clr_eos();
7178 if (eap->cmdidx == CMD_echo)
7179 msg_end();
7180 }
7181}
7182
7183/*
7184 * ":echohl {name}".
7185 */
7186 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007187ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007189 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190}
7191
7192/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007193 * Returns the :echo attribute
7194 */
7195 int
7196get_echo_attr(void)
7197{
7198 return echo_attr;
7199}
7200
7201/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 * ":execute expr1 ..." execute the result of an expression.
7203 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007204 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007206 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 * Each gets spaces around each argument and a newline at the end for
7208 * echo commands
7209 */
7210 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007211ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212{
7213 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007214 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 int ret = OK;
7216 char_u *p;
7217 garray_T ga;
7218 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007219 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220
7221 ga_init2(&ga, 1, 80);
7222
7223 if (eap->skip)
7224 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007225 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007227 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007228 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230
7231 if (!eap->skip)
7232 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007233 char_u buf[NUMBUFLEN];
7234
7235 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007236 {
7237 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7238 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007239 semsg(_(e_using_invalid_value_as_string_str),
7240 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007241 p = NULL;
7242 }
7243 else
7244 p = tv_get_string_buf(&rettv, buf);
7245 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007246 else
7247 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007248 if (p == NULL)
7249 {
7250 clear_tv(&rettv);
7251 ret = FAIL;
7252 break;
7253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 len = (int)STRLEN(p);
7255 if (ga_grow(&ga, len + 2) == FAIL)
7256 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007257 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 ret = FAIL;
7259 break;
7260 }
7261 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 ga.ga_len += len;
7265 }
7266
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007267 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 arg = skipwhite(arg);
7269 }
7270
7271 if (ret != FAIL && ga.ga_data != NULL)
7272 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007273 // use the first line of continuation lines for messages
7274 SOURCING_LNUM = start_lnum;
7275
Bram Moolenaar37fef162022-08-29 18:16:32 +01007276 if (eap->cmdidx == CMD_echomsg
7277 || eap->cmdidx == CMD_echowindow
7278 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007279 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007280 // Mark the already saved text as finishing the line, so that what
7281 // follows is displayed on a new line when scrolling back at the
7282 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007283 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007284 }
7285
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007286 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007287 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007288 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007289 out_flush();
7290 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007291 else if (eap->cmdidx == CMD_echowindow)
7292 {
7293#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007294 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007295#endif
7296 msg_attr(ga.ga_data, echo_attr);
7297#ifdef HAS_MESSAGE_WINDOW
7298 end_echowindow();
7299#endif
7300 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007301 else if (eap->cmdidx == CMD_echoconsole)
7302 {
7303 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7304 ui_write((char_u *)"\r\n", 2, TRUE);
7305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 else if (eap->cmdidx == CMD_echoerr)
7307 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007308 int save_did_emsg = did_emsg;
7309
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007310 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007311 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 if (!force_abort)
7313 did_emsg = save_did_emsg;
7314 }
7315 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007316 {
7317 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7318
7319 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7320 sticky_cmdmod_flags = cmdmod.cmod_flags
7321 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 do_cmdline((char_u *)ga.ga_data,
7323 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007324 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 }
7327
7328 ga_clear(&ga);
7329
7330 if (eap->skip)
7331 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007332 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333}
7334
7335/*
7336 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7337 * "arg" points to the "&" or '+' when called, to "option" when returning.
7338 * Returns NULL when no option name found. Otherwise pointer to the char
7339 * after the option name.
7340 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007341 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007342find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343{
7344 char_u *p = *arg;
7345
7346 ++p;
7347 if (*p == 'g' && p[1] == ':')
7348 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007349 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 p += 2;
7351 }
7352 else if (*p == 'l' && p[1] == ':')
7353 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007354 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 p += 2;
7356 }
7357 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007358 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359
7360 if (!ASCII_ISALPHA(*p))
7361 return NULL;
7362 *arg = p;
7363
7364 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007365 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 else
7367 while (ASCII_ISALPHA(*p))
7368 ++p;
7369 return p;
7370}
7371
7372/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007373 * Display script name where an item was last set.
7374 * Should only be invoked when 'verbose' is non-zero.
7375 */
7376 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007377last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007378{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007379 char_u *p;
7380
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007381 if (script_ctx.sc_sid == 0)
7382 return;
7383
7384 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7385 if (p == NULL)
7386 return;
7387
7388 verbose_enter();
7389 msg_puts(_("\n\tLast set from "));
7390 msg_puts((char *)p);
7391 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007392 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007393 msg_puts(_(line_msg));
7394 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007395 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007396 verbose_leave();
7397 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007398}
7399
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007400#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401
7402/*
7403 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007404 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 * "flags" can be "g" to do a global substitute.
7406 * Returns an allocated string, NULL for error.
7407 */
7408 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007409do_string_sub(
7410 char_u *str,
7411 char_u *pat,
7412 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007413 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007414 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415{
7416 int sublen;
7417 regmatch_T regmatch;
7418 int i;
7419 int do_all;
7420 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007421 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 garray_T ga;
7423 char_u *ret;
7424 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007425 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007427 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007429 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430
7431 ga_init2(&ga, 1, 200);
7432
7433 do_all = (flags[0] == 'g');
7434
7435 regmatch.rm_ic = p_ic;
7436 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7437 if (regmatch.regprog != NULL)
7438 {
7439 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007440 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7442 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007443 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007444 if (regmatch.startp[0] == regmatch.endp[0])
7445 {
7446 if (zero_width == regmatch.startp[0])
7447 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007448 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007449 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007450 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7451 (size_t)i);
7452 ga.ga_len += i;
7453 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007454 continue;
7455 }
7456 zero_width = regmatch.startp[0];
7457 }
7458
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 /*
7460 * Get some space for a temporary buffer to do the substitution
7461 * into. It will contain:
7462 * - The text up to where the match is.
7463 * - The substituted text.
7464 * - The text after the match.
7465 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007466 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007467 if (sublen <= 0)
7468 {
7469 ga_clear(&ga);
7470 break;
7471 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007472 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7474 {
7475 ga_clear(&ga);
7476 break;
7477 }
7478
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007479 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 i = (int)(regmatch.startp[0] - tail);
7481 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007482 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007483 (void)vim_regsub(&regmatch, sub, expr,
7484 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7485 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007487 tail = regmatch.endp[0];
7488 if (*tail == NUL)
7489 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 if (!do_all)
7491 break;
7492 }
7493
7494 if (ga.ga_data != NULL)
7495 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7496
Bram Moolenaar473de612013-06-08 18:19:48 +02007497 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 }
7499
7500 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7501 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007502 if (p_cpo == empty_option)
7503 p_cpo = save_cpo;
7504 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007505 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007506 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007507 // If it's still empty it was changed and restored, need to restore in
7508 // the complicated way.
7509 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007510 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007511 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513
7514 return ret;
7515}