blob: 523546957e1cd7527909a9e178bebbc8cfef9142 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020032static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
33static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
34static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
35static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010036static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020037static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010038static int eval8(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
39static int eval9(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
40static int eval9_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000041
Bram Moolenaar48e697e2016-01-23 22:17:30 +010042static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020043static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020044
Bram Moolenaare21c1582019-03-02 11:57:09 +010045/*
46 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010047 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010048 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020049 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010050num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010051{
52 varnumber_T result;
53
Bram Moolenaar99880f92021-01-20 21:23:14 +010054 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010055 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010056 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010057 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010058 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010059 if (failed != NULL)
60 *failed = TRUE;
61 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010062 if (n1 == 0)
63 result = VARNUM_MIN; // similar to NaN
64 else if (n1 < 0)
65 result = -VARNUM_MAX;
66 else
67 result = VARNUM_MAX;
68 }
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +010069 else if (n1 == VARNUM_MIN && n2 == -1)
70 {
71 // specific case: trying to do VARNUM_MIN / -1 results in a positive
72 // number that doesn't fit in varnumber_T and causes an FPE
73 result = VARNUM_MAX;
74 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010075 else
76 result = n1 / n2;
77
78 return result;
79}
80
81/*
82 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010083 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010084 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020085 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010086num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010087{
Bram Moolenaar99880f92021-01-20 21:23:14 +010088 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010089 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010090 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010091 if (failed != NULL)
92 *failed = TRUE;
93 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010094 return (n2 == 0) ? 0 : (n1 % n2);
95}
96
Bram Moolenaar33570922005-01-25 22:26:29 +000097/*
98 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +000099 */
100 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100101eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000102{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200103 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200104 func_init();
Bram Moolenaara7043832005-01-21 11:56:39 +0000105}
106
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000107#if defined(EXITFREE) || defined(PROTO)
108 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100109eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000110{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200111 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100112 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200113 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000114
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200115 // autoloaded script names
116 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000117
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200118 // unreferenced lists and dicts
119 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200120
121 // functions not garbage collected
122 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000123}
124#endif
125
Bram Moolenaare6b53242020-07-01 17:28:33 +0200126 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200127fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
128{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100129 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200130 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000131
132 if (eap == NULL)
133 return;
134
135 evalarg->eval_cstack = eap->cstack;
136 if (sourcing_a_script(eap) || eap->getline == get_list_line)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200137 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000138 evalarg->eval_getline = eap->getline;
139 evalarg->eval_cookie = eap->cookie;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200140 }
141}
142
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143/*
144 * Top level evaluation function, returning a boolean.
145 * Sets "error" to TRUE if there was an error.
146 * Return TRUE or FALSE.
147 */
148 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100149eval_to_bool(
150 char_u *arg,
151 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200152 exarg_T *eap,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100153 int skip, // only parse, don't execute
154 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155{
Bram Moolenaar33570922005-01-25 22:26:29 +0000156 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200157 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200158 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100159 int r;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200160
Bram Moolenaar37c83712020-06-30 21:18:36 +0200161 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162
163 if (skip)
164 ++emsg_skip;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100165 if (use_simple_function)
166 r = eval0_simple_funccal(arg, &tv, eap, &evalarg);
167 else
168 r = eval0(arg, &tv, eap, &evalarg);
169 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 else
172 {
173 *error = FALSE;
174 if (!skip)
175 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200176 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200177 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200178 else
179 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000180 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 }
182 }
183 if (skip)
184 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200185 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200187 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188}
189
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100190/*
191 * Call eval1() and give an error message if not done at a lower level.
192 */
193 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200194eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100195{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100196 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100197 int ret;
198 int did_emsg_before = did_emsg;
199 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200200 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100201
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200202 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
203
204 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100205 if (ret == FAIL)
206 {
207 // Report the invalid expression unless the expression evaluation has
208 // been cancelled due to an aborting error, an interrupt, or an
209 // exception, or we already gave a more specific error.
210 // Also check called_emsg for when using assert_fails().
211 if (!aborting() && did_emsg == did_emsg_before
212 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200213 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100214 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200215 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100216 return ret;
217}
218
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200220 * Return whether a typval is a valid expression to pass to eval_expr_typval()
221 * or eval_expr_to_bool(). An empty string returns FALSE;
222 */
223 int
224eval_expr_valid_arg(typval_T *tv)
225{
226 return tv->v_type != VAR_UNKNOWN
227 && (tv->v_type != VAR_STRING
228 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
229}
230
231/*
Bram Moolenaar82418262022-09-28 16:16:15 +0100232 * When calling eval_expr_typval() many times we only need one funccall_T.
233 * Returns NULL when no funccall_T is to be used.
234 * When returning non-NULL remove_funccal() must be called later.
235 */
236 funccall_T *
237eval_expr_get_funccal(typval_T *expr, typval_T *rettv)
238{
239 if (expr->v_type != VAR_PARTIAL)
240 return NULL;
241
242 partial_T *partial = expr->vval.v_partial;
243 if (partial == NULL)
244 return NULL;
245 if (partial->pt_func == NULL
246 || partial->pt_func->uf_def_status == UF_NOT_COMPILED)
247 return NULL;
248
249 return create_funccal(partial->pt_func, rettv);
250}
251
252/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253 * Evaluate an expression, which can be a function, partial or string.
254 * Pass arguments "argv[argc]".
zeertzjqad0c4422023-08-17 22:15:47 +0200255 * If "want_func" is TRUE treat a string as a function name, not an expression.
Bram Moolenaar82418262022-09-28 16:16:15 +0100256 * "fc_arg" is from eval_expr_get_funccal() or NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100257 * Return the result in "rettv" and OK or FAIL.
258 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200259 int
Bram Moolenaar82418262022-09-28 16:16:15 +0100260eval_expr_typval(
261 typval_T *expr,
zeertzjqad0c4422023-08-17 22:15:47 +0200262 int want_func,
Bram Moolenaar82418262022-09-28 16:16:15 +0100263 typval_T *argv,
264 int argc,
265 funccall_T *fc_arg,
266 typval_T *rettv)
Bram Moolenaar48570482017-10-30 21:48:41 +0100267{
268 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100269 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200270 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100271
zeertzjqad0c4422023-08-17 22:15:47 +0200272 if (expr->v_type == VAR_PARTIAL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100273 {
274 partial_T *partial = expr->vval.v_partial;
275
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200276 if (partial == NULL)
277 return FAIL;
278
Bram Moolenaar822ba242020-05-24 23:00:18 +0200279 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200280 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100281 {
Bram Moolenaar82418262022-09-28 16:16:15 +0100282 funccall_T *fc = fc_arg != NULL ? fc_arg
283 : create_funccal(partial->pt_func, rettv);
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100284 int r;
285
286 if (fc == NULL)
287 return FAIL;
288
Bram Moolenaar69082912022-09-22 21:35:19 +0100289 // Shortcut to call a compiled function with minimal overhead.
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100290 r = call_def_function(partial->pt_func, argc, argv,
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000291 DEF_USE_PT_ARGV, partial, NULL, fc, rettv);
Bram Moolenaar82418262022-09-28 16:16:15 +0100292 if (fc_arg == NULL)
293 remove_funccal();
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100294 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100295 return FAIL;
296 }
297 else
298 {
299 s = partial_name(partial);
300 if (s == NULL || *s == NUL)
301 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200302 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000303 funcexe.fe_evaluate = TRUE;
304 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
306 return FAIL;
307 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100308 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200309 else if (expr->v_type == VAR_INSTR)
310 {
311 return exe_typval_instr(expr, rettv);
312 }
zeertzjqad0c4422023-08-17 22:15:47 +0200313 else if (expr->v_type == VAR_FUNC || want_func)
314 {
315 s = expr->v_type == VAR_FUNC
316 ? expr->vval.v_string
317 : tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
318 if (s == NULL || *s == NUL)
319 return FAIL;
320 CLEAR_FIELD(funcexe);
321 funcexe.fe_evaluate = TRUE;
322 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
323 return FAIL;
324 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100325 else
326 {
Bram Moolenaarfc4c4482022-01-26 21:17:04 +0000327 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
Bram Moolenaar48570482017-10-30 21:48:41 +0100328 if (s == NULL)
329 return FAIL;
330 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200331 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100332 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200333 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100334 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200335 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200336 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100337 return FAIL;
338 }
339 }
340 return OK;
341}
342
343/*
344 * Like eval_to_bool() but using a typval_T instead of a string.
345 * Works for string, funcref and partial.
346 */
347 int
348eval_expr_to_bool(typval_T *expr, int *error)
349{
350 typval_T rettv;
351 int res;
352
zeertzjqad0c4422023-08-17 22:15:47 +0200353 if (eval_expr_typval(expr, FALSE, NULL, 0, NULL, &rettv) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100354 {
355 *error = TRUE;
356 return FALSE;
357 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200358 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100359 clear_tv(&rettv);
360 return res;
361}
362
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363/*
364 * Top level evaluation function, returning a string. If "skip" is TRUE,
365 * only parsing to "nextcmd" is done, without reporting errors. Return
366 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
367 */
368 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100369eval_to_string_skip(
370 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200371 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100372 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373{
Bram Moolenaar33570922005-01-25 22:26:29 +0000374 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200376 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377
Bram Moolenaar37c83712020-06-30 21:18:36 +0200378 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 if (skip)
380 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200381 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 retval = NULL;
383 else
384 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100385 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000386 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 }
388 if (skip)
389 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200390 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391
392 return retval;
393}
394
395/*
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100396 * Initialize "evalarg" for use.
397 */
398 void
399init_evalarg(evalarg_T *evalarg)
400{
401 CLEAR_POINTER(evalarg);
402 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
403}
404
405/*
406 * If "evalarg->eval_tofree" is not NULL free it later.
407 * Caller is expected to overwrite "evalarg->eval_tofree" next.
408 */
409 static void
410free_eval_tofree_later(evalarg_T *evalarg)
411{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000412 if (evalarg->eval_tofree == NULL)
413 return;
414
415 if (ga_grow(&evalarg->eval_tofree_ga, 1) == OK)
416 ((char_u **)evalarg->eval_tofree_ga.ga_data)
417 [evalarg->eval_tofree_ga.ga_len++]
418 = evalarg->eval_tofree;
419 else
420 vim_free(evalarg->eval_tofree);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100421}
422
423/*
424 * After using "evalarg" filled from "eap": free the memory.
425 */
426 void
427clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
428{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000429 if (evalarg == NULL)
430 return;
431
432 garray_T *etga = &evalarg->eval_tofree_ga;
433
434 if (evalarg->eval_tofree != NULL || evalarg->eval_using_cmdline)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100435 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000436 if (eap != NULL)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100437 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000438 // We may need to keep the original command line, e.g. for
439 // ":let" it has the variable names. But we may also need
440 // the new one, "nextcmd" points into it. Keep both.
441 vim_free(eap->cmdline_tofree);
442 eap->cmdline_tofree = *eap->cmdlinep;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100443
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000444 if (evalarg->eval_using_cmdline && etga->ga_len > 0)
445 {
446 // "nextcmd" points into the last line in eval_tofree_ga,
447 // need to keep it around.
448 --etga->ga_len;
449 *eap->cmdlinep = ((char_u **)etga->ga_data)[etga->ga_len];
450 vim_free(evalarg->eval_tofree);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100451 }
452 else
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000453 *eap->cmdlinep = evalarg->eval_tofree;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100454 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000455 else
456 vim_free(evalarg->eval_tofree);
457 evalarg->eval_tofree = NULL;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100458 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000459
460 ga_clear_strings(etga);
461 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100462}
463
464/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000465 * Skip over an expression at "*pp".
466 * Return FAIL for an error, OK otherwise.
467 */
468 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200469skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000470{
Bram Moolenaar33570922005-01-25 22:26:29 +0000471 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000472
473 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200474 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000475}
476
477/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200478 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200479 * If in Vim9 script and line breaks are encountered, the lines are
480 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200481 * "arg" is advanced to just after the expression.
482 * "start" is set to the start of the expression, "end" to just after the end.
483 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200484 * Return FAIL for an error, OK otherwise.
485 */
486 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200487skip_expr_concatenate(
488 char_u **arg,
489 char_u **start,
490 char_u **end,
491 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200492{
493 typval_T rettv;
494 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200495 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200496 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200497 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200498 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200499 int evaluate = evalarg == NULL
500 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200501
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200502 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200503 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200504 {
505 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200506 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200507 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200508 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200509 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200510 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200511 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200512
513 // Don't evaluate the expression.
514 if (evalarg != NULL)
515 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200516 *arg = skipwhite(*arg);
517 res = eval1(arg, &rettv, evalarg);
518 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200519 if (evalarg != NULL)
520 evalarg->eval_flags = save_flags;
521
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200522 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200523 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200524 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200525 if (evalarg->eval_ga.ga_len == 1)
526 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200527 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200528 ga_clear(gap);
529 gap->ga_itemsize = 0;
530 }
531 else
532 {
533 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200534 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200535
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200536 // Line breaks encountered, concatenate all the lines.
537 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200538 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200539
540 // free the lines only when using getsourceline()
541 if (evalarg->eval_cookie != NULL)
542 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200543 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200544 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200545 // Do not free the last line, "arg" points into it, free it
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100546 // later. Also free "eval_tofree" later if needed.
547 free_eval_tofree_later(evalarg);
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200548 evalarg->eval_tofree =
549 ((char_u **)gap->ga_data)[gap->ga_len - 1];
550 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200551 ga_clear_strings(gap);
552 }
553 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200554 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200555 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200556
557 // free lines that were explicitly marked for freeing
558 ga_clear_strings(freegap);
559 }
560
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200561 gap->ga_itemsize = 0;
562 if (p == NULL)
563 return FAIL;
564 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200565 vim_free(evalarg->eval_tofree_lambda);
566 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200567 // Compute "end" relative to the end.
568 *end = *start + STRLEN(*start) - endoff;
569 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200570 }
571
572 return res;
573}
574
575/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100576 * Convert "tv" to a string.
zeertzjq19dfa272023-06-15 10:56:41 +0100577 * When "convert" is TRUE convert a List into a sequence of lines.
Bram Moolenaar883cf972021-01-15 18:04:43 +0100578 * Returns an allocated string (NULL when out of memory).
579 */
580 char_u *
581typval2string(typval_T *tv, int convert)
582{
583 garray_T ga;
584 char_u *retval;
Bram Moolenaar883cf972021-01-15 18:04:43 +0100585
586 if (convert && tv->v_type == VAR_LIST)
587 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000588 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100589 if (tv->vval.v_list != NULL)
590 {
591 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
592 if (tv->vval.v_list->lv_len > 0)
593 ga_append(&ga, NL);
594 }
595 ga_append(&ga, NUL);
596 retval = (char_u *)ga.ga_data;
597 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100598 else
599 retval = vim_strsave(tv_get_string(tv));
600 return retval;
601}
602
603/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200604 * Top level evaluation function, returning a string. Does not handle line
605 * breaks.
zeertzjq19dfa272023-06-15 10:56:41 +0100606 * When "convert" is TRUE convert a List into a sequence of lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 * Return pointer to allocated memory, or NULL for failure.
608 */
609 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100610eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100611 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100612 int convert,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100613 exarg_T *eap,
614 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615{
Bram Moolenaar33570922005-01-25 22:26:29 +0000616 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100618 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100619 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100621 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100622 if (use_simple_function)
623 r = eval0_simple_funccal(arg, &tv, NULL, &evalarg);
624 else
625 r = eval0(arg, &tv, NULL, &evalarg);
626 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 retval = NULL;
628 else
629 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100630 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000631 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100633 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634
635 return retval;
636}
637
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100638 char_u *
639eval_to_string(
640 char_u *arg,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100641 int convert,
642 int use_simple_function)
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100643{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100644 return eval_to_string_eap(arg, convert, NULL, use_simple_function);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100645}
646
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000648 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100649 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200650 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 */
652 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100653eval_to_string_safe(
654 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000655 int use_sandbox,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100656 int keep_script_version,
657 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658{
659 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200660 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200661 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200662 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663
Bram Moolenaar9530b582022-01-22 13:39:08 +0000664 if (!keep_script_version)
665 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200666 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000667 if (use_sandbox)
668 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100669 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200670 may_garbage_collect = FALSE;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100671 retval = eval_to_string(arg, FALSE, use_simple_function);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000672 if (use_sandbox)
673 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100674 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200675 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200676 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200677 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 return retval;
679}
680
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681/*
682 * Top level evaluation function, returning a number.
683 * Evaluates "expr" silently.
684 * Returns -1 for an error.
685 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200686 varnumber_T
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100687eval_to_number(char_u *expr, int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688{
Bram Moolenaar33570922005-01-25 22:26:29 +0000689 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200690 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000691 char_u *p = skipwhite(expr);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100692 int r = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693
694 ++emsg_off;
695
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100696 if (use_simple_function)
697 r = may_call_simple_func(expr, &rettv);
698 if (r == NOTDONE)
699 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
700 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 retval = -1;
702 else
703 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100704 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000705 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 }
707 --emsg_off;
708
709 return retval;
710}
711
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000712/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000713 * Top level evaluation function.
714 * Returns an allocated typval_T with the result.
715 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000716 */
717 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200718eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000719{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100720 return eval_expr_ext(arg, eap, FALSE);
721}
722
723 typval_T *
724eval_expr_ext(char_u *arg, exarg_T *eap, int use_simple_function)
725{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000726 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200727 evalarg_T evalarg;
728
729 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000730
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200731 tv = ALLOC_ONE(typval_T);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100732 if (tv != NULL)
733 {
734 int r = NOTDONE;
735
736 if (use_simple_function)
737 r = eval0_simple_funccal(arg, tv, eap, &evalarg);
738 if (r == NOTDONE)
739 r = eval0(arg, tv, eap, &evalarg);
740
741 if (r == FAIL)
742 VIM_CLEAR(tv);
743 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000744
Bram Moolenaar37c83712020-06-30 21:18:36 +0200745 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000746 return tv;
747}
748
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000750 * "*arg" points to what can be a function name in the form of "import.Name" or
751 * "Funcref". Return the name of the function. Set "tofree" to something that
752 * was allocated.
753 * If "verbose" is FALSE no errors are given.
754 * Return NULL for any failure.
755 */
756 static char_u *
757deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000758 char_u **arg,
759 char_u **tofree,
760 evalarg_T *evalarg,
761 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000762{
763 typval_T ref;
764 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100765 int save_flags = 0;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000766
767 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100768 if (evalarg != NULL)
769 {
770 // need to evaluate this to get an import, like in "a.Func"
771 save_flags = evalarg->eval_flags;
772 evalarg->eval_flags |= EVAL_EVALUATE;
773 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100774 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100775 {
776 dictitem_T *v;
777
778 // If <SID>VarName was used it would not be found, try another way.
779 v = find_var_also_in_script(name, NULL, FALSE);
780 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100781 {
782 name = NULL;
783 goto theend;
784 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100785 copy_tv(&v->di_tv, &ref);
786 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000787 if (*skipwhite(*arg) != NUL)
788 {
789 if (verbose)
790 semsg(_(e_trailing_characters_str), *arg);
791 name = NULL;
792 }
793 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
794 {
795 name = ref.vval.v_string;
796 ref.vval.v_string = NULL;
797 *tofree = name;
798 }
799 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
800 {
801 if (ref.vval.v_partial->pt_argc > 0
802 || ref.vval.v_partial->pt_dict != NULL)
803 {
804 if (verbose)
805 emsg(_(e_cannot_use_partial_here));
806 name = NULL;
807 }
808 else
809 {
810 name = vim_strsave(partial_name(ref.vval.v_partial));
811 *tofree = name;
812 }
813 }
814 else
815 {
816 if (verbose)
817 semsg(_(e_not_callable_type_str), name);
818 name = NULL;
819 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100820
821theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000822 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100823 if (evalarg != NULL)
824 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000825 return name;
826}
827
828/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100829 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200830 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
831 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000832 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200834 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100835call_vim_function(
836 char_u *func,
837 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200838 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200839 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000841 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200842 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000843 char_u *arg;
844 char_u *name;
845 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000846 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100848 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200849 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000850 funcexe.fe_firstline = curwin->w_cursor.lnum;
851 funcexe.fe_lastline = curwin->w_cursor.lnum;
852 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000853
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000854 // The name might be "import.Func" or "Funcref". We don't know, we need to
855 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000856 // autoload script has errors. Guess that when there is a dot in the name
857 // showing errors is the right choice.
858 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000859 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000860 if (ignore_errors)
861 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000862 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000863 if (ignore_errors)
864 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000865 if (name == NULL)
866 name = func;
867
868 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
869
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000870 if (ret == FAIL)
871 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000872 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000873
874 return ret;
875}
876
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100877/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100878 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000879 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
880 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000881 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000882 */
883 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100884call_func_retstr(
885 char_u *func,
886 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200887 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000888{
889 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000890 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000891
Bram Moolenaarded27a12018-08-01 19:06:03 +0200892 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000893 return NULL;
894
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100895 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000896 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 return retval;
898}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000899
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000900/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100901 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000902 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000903 * Returns NULL when there is something wrong.
Bram Moolenaar55e93662022-09-10 13:52:26 +0100904 * Gives an error when the returned value is not a list.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000905 */
906 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100907call_func_retlist(
908 char_u *func,
909 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200910 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000911{
912 typval_T rettv;
913
Bram Moolenaarded27a12018-08-01 19:06:03 +0200914 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000915 return NULL;
916
917 if (rettv.v_type != VAR_LIST)
918 {
Bram Moolenaar55e93662022-09-10 13:52:26 +0100919 semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
920 vartype_name(rettv.v_type));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000921 clear_tv(&rettv);
922 return NULL;
923 }
924
925 return rettv.vval.v_list;
926}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927
Bram Moolenaare70dd112022-01-21 16:31:11 +0000928#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100930 * Evaluate "arg", which is 'foldexpr'.
931 * Note: caller must set "curwin" to match "arg".
932 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
933 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 */
935 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000936eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000938 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000939 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200940 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000942 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000943 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100944 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100946 arg = skipwhite(wp->w_p_fde);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000947 current_sctx = wp->w_p_script_ctx[WV_FDE];
948
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000950 if (use_sandbox)
951 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100952 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 *cp = NUL;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100954
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100955 // Evaluate the expression. If the expression is "FuncName()" call the
956 // function directly.
957 if (eval0_simple_funccal(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 retval = 0;
959 else
960 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100961 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000962 if (tv.v_type == VAR_NUMBER)
963 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000964 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 retval = 0;
966 else
967 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100968 // If the result is a string, check if there is a non-digit before
969 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000970 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 if (!VIM_ISDIGIT(*s) && *s != '-')
972 *cp = *s++;
973 retval = atol((char *)s);
974 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000975 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 }
977 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000978 if (use_sandbox)
979 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100980 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200981 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000982 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200984 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985}
986#endif
987
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000989 * Get an lval: variable, Dict item or List item that can be assigned a value
990 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
991 * "name.key", "name.key[expr]" etc.
992 * Indexing only works if "name" is an existing List or Dictionary.
993 * "name" points to the start of the name.
994 * If "rettv" is not NULL it points to the value to be assigned.
995 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
996 * wrong; must end in space or cmd separator.
997 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100998 * flags:
999 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001000 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001001 * GLV_NO_AUTOLOAD: do not use script autoloading
1002 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001003 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001004 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001005 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001006 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001007 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001008get_lval(
1009 char_u *name,
1010 typval_T *rettv,
1011 lval_T *lp,
1012 int unlet,
1013 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001014 int flags, // GLV_ values
1015 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001016{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001017 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001018 char_u *expr_start, *expr_end;
1019 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001020 dictitem_T *v;
1021 typval_T var1;
1022 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001023 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001024 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001025 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01001026 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001027 int quiet = flags & GLV_QUIET;
Ernie Rael86eda072023-09-05 07:33:48 +02001028 int writing = 0;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001029 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001030
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001031 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02001032 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001033
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001034 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001035 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001036 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001037 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02001038 lp->ll_name_end = find_name_end(name, NULL, NULL,
1039 FNE_INCL_BR | fne_flags);
1040 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001041 }
1042
Bram Moolenaara749a422022-02-12 19:52:25 +00001043 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001044 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00001045 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
1046 {
1047 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
1048 return NULL;
1049 }
1050
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001051 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001052 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001054 if (expr_start != NULL)
1055 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001056 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01001057 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001058 && *p != '[' && *p != '.')
1059 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001060 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001061 return NULL;
1062 }
1063
1064 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1065 if (lp->ll_exp_name == NULL)
1066 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001067 // Report an invalid expression in braces, unless the
1068 // expression evaluation has been cancelled due to an
1069 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001070 if (!aborting() && !quiet)
1071 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001072 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001073 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001074 return NULL;
1075 }
1076 }
1077 lp->ll_name = lp->ll_exp_name;
1078 }
1079 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001080 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001081 lp->ll_name = name;
1082
Bram Moolenaar4525a572022-02-13 11:57:33 +00001083 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001085 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001086 // However, "g:[key]" is indexing a dictionary.
1087 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001088 {
1089 --p;
1090 lp->ll_name_end = p;
1091 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001092 if (*skipwhite(p) == ':')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001093 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001094 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001095
Bram Moolenaar9510d222022-09-11 15:14:05 +01001096 if (is_scoped_variable(name))
1097 {
1098 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1099 return NULL;
1100 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001101 if (VIM_ISWHITE(*p))
1102 {
1103 semsg(_(e_no_white_space_allowed_before_colon_str), p);
1104 return NULL;
1105 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001106 if (tp == p + 1 && !quiet)
1107 {
1108 semsg(_(e_white_space_required_after_str_str), ":", p);
1109 return NULL;
1110 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001111 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001112 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001113 semsg(_(e_using_type_not_in_script_context_str), p);
1114 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001115 }
1116
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001117 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001118 lp->ll_type = parse_type(&tp,
1119 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1120 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001121 if (lp->ll_type == NULL && !quiet)
1122 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001123 lp->ll_name_end = tp;
1124 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001125 }
1126 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001127 if (lp->ll_name == NULL)
1128 return p;
1129
Bram Moolenaar62aec932022-01-29 21:45:34 +00001130 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001131 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001132 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001133
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001134 if (import != NULL)
1135 {
1136 ufunc_T *ufunc;
1137 type_T *type;
1138
Bram Moolenaar753885b2022-08-24 16:30:36 +01001139 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001140 lp->ll_sid = import->imp_sid;
1141 lp->ll_name = skipwhite(p + 1);
1142 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1143 lp->ll_name_end = p;
1144
1145 // check the item is exported
1146 cc = *p;
1147 *p = NUL;
1148 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001149 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001150 {
1151 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001152 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001153 }
1154 *p = cc;
1155 }
1156 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001157
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001158 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001159 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001160 return p;
1161
Bram Moolenaar4525a572022-02-13 11:57:33 +00001162 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001163 {
1164 // using local variable
1165 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001166 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001167 }
1168 else
1169 {
1170 cc = *p;
1171 *p = NUL;
1172 // When we would write to the variable pass &ht and prevent autoload.
1173 writing = !(flags & GLV_READ_ONLY);
1174 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001175 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001176 if (v == NULL && !quiet)
1177 semsg(_(e_undefined_variable_str), lp->ll_name);
1178 *p = cc;
1179 if (v == NULL)
1180 return NULL;
1181 lp->ll_tv = &v->di_tv;
1182 }
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;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001543 round <= 2; ++round)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001544 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001545 int m_idx;
1546 ufunc_T *fp;
1547
1548 fp = method_lookup(cl,
1549 round == 1 ? VAR_CLASS : VAR_OBJECT,
1550 key, p - key, &m_idx);
1551 if (fp != NULL)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001552 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001553 lp->ll_ufunc = fp;
1554 lp->ll_valtype = fp->uf_func_type;
1555 break;
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001556 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001557 }
1558 }
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001559
1560 if (lp->ll_valtype == NULL)
1561 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001562 int m_idx;
1563 ocmember_T *om;
1564
1565 om = member_lookup(cl, v_type, key, p - key, &m_idx);
1566 if (om != NULL)
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001567 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001568 switch (om->ocm_access)
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001569 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001570 case VIM_ACCESS_PRIVATE:
1571 semsg(_(e_cannot_access_private_member_str),
1572 om->ocm_name);
1573 return NULL;
1574 case VIM_ACCESS_READ:
1575 if ((flags & GLV_READ_ONLY) == 0)
1576 {
1577 semsg(_(e_member_is_not_writable_str),
1578 om->ocm_name);
1579 return NULL;
1580 }
1581 break;
1582 case VIM_ACCESS_ALL:
1583 break;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001584 }
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001585
1586 lp->ll_valtype = om->ocm_type;
1587
1588 if (v_type == VAR_OBJECT)
1589 lp->ll_tv = ((typval_T *)(
1590 lp->ll_tv->vval.v_object + 1)) + m_idx;
1591 else
1592 lp->ll_tv = &cl->class_members_tv[m_idx];
1593 break;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001594 }
1595 }
1596
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001597 if (lp->ll_valtype == NULL)
1598 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001599 member_not_found_msg(cl, v_type, key, p - key);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001600 return NULL;
1601 }
1602 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001603 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001604 }
1605
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001606 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001607 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001608 return p;
1609}
1610
1611/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001612 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001613 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001614 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001615clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001616{
1617 vim_free(lp->ll_exp_name);
1618 vim_free(lp->ll_newkey);
1619}
1620
1621/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001622 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001623 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001624 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1625 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001626 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001627 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001628set_var_lval(
1629 lval_T *lp,
1630 char_u *endp,
1631 typval_T *rettv,
1632 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001633 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1634 char_u *op,
1635 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001636{
1637 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001638 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001639
1640 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001641 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001642 cc = *endp;
1643 *endp = NUL;
h-east2bd6a092023-05-19 19:01:17 +01001644 if (in_vim9script() && check_reserved_name(lp->ll_name, FALSE) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001645 return;
1646
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001647 if (lp->ll_blob != NULL)
1648 {
1649 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001650
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001651 if (op != NULL && *op != '=')
1652 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001653 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001654 return;
1655 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001656 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001657 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001658
1659 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1660 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001661 if (lp->ll_empty2)
1662 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1663
Bram Moolenaar68452172021-04-12 21:21:02 +02001664 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1665 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001666 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001667 }
1668 else
1669 {
1670 val = (int)tv_get_number_chk(rettv, &error);
1671 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001672 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001673 }
1674 }
1675 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001676 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001677 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001678
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001679 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1680 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001681 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001682 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001683 *endp = cc;
1684 return;
1685 }
1686
Bram Moolenaarff697e62019-02-12 22:28:33 +01001687 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001688 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001689 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001690 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001691 {
1692 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001693 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1694 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001695 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001696 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001697 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001698 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001699 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001700 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001701 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001702 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001703 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1704 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001705 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001706 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001707 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001708 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001709 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001710 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001711 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001712 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001713 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001714 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001715 else if (lp->ll_range)
1716 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001717 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1718 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001719 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001720 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001721 return;
1722 }
1723
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001724 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1725 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001726 }
1727 else
1728 {
1729 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001730 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001731 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001732 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1733 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001734 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001735 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001736 return;
1737 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001738
1739 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001740 && check_typval_arg_type(lp->ll_valtype, rettv,
1741 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001742 return;
1743
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001744 if (lp->ll_newkey != NULL)
1745 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001746 if (op != NULL && *op != '=')
1747 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001748 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001749 return;
1750 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001751 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1752 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001753 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001754
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001755 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001756 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001757 if (di == NULL)
1758 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001759 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1760 {
1761 vim_free(di);
1762 return;
1763 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001764 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001765 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001766 else if (op != NULL && *op != '=')
1767 {
1768 tv_op(lp->ll_tv, rettv, op);
1769 return;
1770 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001771 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001772 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001773
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001774 /*
1775 * Assign the value to the variable or list item.
1776 */
1777 if (copy)
1778 copy_tv(rettv, lp->ll_tv);
1779 else
1780 {
1781 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001782 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001783 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001784 }
1785 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001786}
1787
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001788/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001789 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1790 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001791 * Returns OK or FAIL.
1792 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001793 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001794tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001795{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001796 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001797 char_u numbuf[NUMBUFLEN];
1798 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001799 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001800
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001801 // Can't do anything with a Funcref or Dict on the right.
1802 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001803 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001804 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1805 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001806 {
1807 switch (tv1->v_type)
1808 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001809 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001810 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001811 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001812 case VAR_DICT:
1813 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001814 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001815 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001816 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001817 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001818 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001819 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001820 case VAR_CLASS:
1821 case VAR_OBJECT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001822 break;
1823
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001824 case VAR_BLOB:
1825 if (*op != '+' || tv2->v_type != VAR_BLOB)
1826 break;
1827 // BLOB += BLOB
1828 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1829 {
1830 blob_T *b1 = tv1->vval.v_blob;
1831 blob_T *b2 = tv2->vval.v_blob;
1832 int i, len = blob_len(b2);
1833 for (i = 0; i < len; i++)
1834 ga_append(&b1->bv_ga, blob_get(b2, i));
1835 }
1836 return OK;
1837
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001838 case VAR_LIST:
1839 if (*op != '+' || tv2->v_type != VAR_LIST)
1840 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001841 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001842 if (tv2->vval.v_list != NULL)
1843 {
1844 if (tv1->vval.v_list == NULL)
1845 {
1846 tv1->vval.v_list = tv2->vval.v_list;
1847 ++tv1->vval.v_list->lv_refcount;
1848 }
1849 else
1850 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1851 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001852 return OK;
1853
1854 case VAR_NUMBER:
1855 case VAR_STRING:
1856 if (tv2->v_type == VAR_LIST)
1857 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001858 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001859 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001860 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001861 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001862 if (tv2->v_type == VAR_FLOAT)
1863 {
1864 float_T f = n;
1865
Bram Moolenaarff697e62019-02-12 22:28:33 +01001866 if (*op == '%')
1867 break;
1868 switch (*op)
1869 {
1870 case '+': f += tv2->vval.v_float; break;
1871 case '-': f -= tv2->vval.v_float; break;
1872 case '*': f *= tv2->vval.v_float; break;
1873 case '/': f /= tv2->vval.v_float; break;
1874 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001875 clear_tv(tv1);
1876 tv1->v_type = VAR_FLOAT;
1877 tv1->vval.v_float = f;
1878 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001879 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001880 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001881 switch (*op)
1882 {
1883 case '+': n += tv_get_number(tv2); break;
1884 case '-': n -= tv_get_number(tv2); break;
1885 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001886 case '/': n = num_divide(n, tv_get_number(tv2),
1887 &failed); break;
1888 case '%': n = num_modulus(n, tv_get_number(tv2),
1889 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001890 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001891 clear_tv(tv1);
1892 tv1->v_type = VAR_NUMBER;
1893 tv1->vval.v_number = n;
1894 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001895 }
1896 else
1897 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001898 if (tv2->v_type == VAR_FLOAT)
1899 break;
1900
Bram Moolenaarff697e62019-02-12 22:28:33 +01001901 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001902 s = tv_get_string(tv1);
1903 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001904 clear_tv(tv1);
1905 tv1->v_type = VAR_STRING;
1906 tv1->vval.v_string = s;
1907 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001908 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001909
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001910 case VAR_FLOAT:
1911 {
1912 float_T f;
1913
Bram Moolenaarff697e62019-02-12 22:28:33 +01001914 if (*op == '%' || *op == '.'
1915 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001916 && tv2->v_type != VAR_NUMBER
1917 && tv2->v_type != VAR_STRING))
1918 break;
1919 if (tv2->v_type == VAR_FLOAT)
1920 f = tv2->vval.v_float;
1921 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001922 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001923 switch (*op)
1924 {
1925 case '+': tv1->vval.v_float += f; break;
1926 case '-': tv1->vval.v_float -= f; break;
1927 case '*': tv1->vval.v_float *= f; break;
1928 case '/': tv1->vval.v_float /= f; break;
1929 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001930 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001931 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001932 }
1933 }
1934
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001935 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001936 return FAIL;
1937}
1938
1939/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001940 * Evaluate the expression used in a ":for var in expr" command.
1941 * "arg" points to "var".
1942 * Set "*errp" to TRUE for an error, FALSE otherwise;
1943 * Return a pointer that holds the info. Null when there is an error.
1944 */
1945 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001946eval_for_line(
1947 char_u *arg,
1948 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001949 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001950 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951{
Bram Moolenaar33570922005-01-25 22:26:29 +00001952 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001953 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001954 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001955 typval_T tv;
1956 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001957 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001959 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001961 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001962 if (fi == NULL)
1963 return NULL;
1964
Bram Moolenaar404557e2021-07-05 21:41:48 +02001965 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1966 &fi->fi_semicolon, FALSE);
1967 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001968 return fi;
1969
Bram Moolenaar404557e2021-07-05 21:41:48 +02001970 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001971 if (expr[0] != 'i' || expr[1] != 'n'
1972 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001974 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1975 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1976 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001977 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001978 return fi;
1979 }
1980
1981 if (skip)
1982 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001983 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1984 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001985 {
1986 *errp = FALSE;
1987 if (!skip)
1988 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001989 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001990 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001991 l = tv.vval.v_list;
1992 if (l == NULL)
1993 {
1994 // a null list is like an empty list: do nothing
1995 clear_tv(&tv);
1996 }
1997 else
1998 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001999 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002000 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002001
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002002 // No need to increment the refcount, it's already set for
2003 // the list being used in "tv".
2004 fi->fi_list = l;
2005 list_add_watch(l, &fi->fi_lw);
2006 fi->fi_lw.lw_item = l->lv_first;
2007 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002008 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002009 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002010 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002011 fi->fi_bi = 0;
2012 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002013 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002014 typval_T btv;
2015
2016 // Make a copy, so that the iteration still works when the
2017 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002018 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002019 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002020 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002021 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002022 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002023 else if (tv.v_type == VAR_STRING)
2024 {
2025 fi->fi_byte_idx = 0;
2026 fi->fi_string = tv.vval.v_string;
2027 tv.vval.v_string = NULL;
2028 if (fi->fi_string == NULL)
2029 fi->fi_string = vim_strsave((char_u *)"");
2030 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002031 else
2032 {
Sean Dewar80d73952021-08-04 19:25:54 +02002033 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002034 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002035 }
2036 }
2037 }
2038 if (skip)
2039 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002040 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002041
2042 return fi;
2043}
2044
2045/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002046 * Used when looping over a :for line, skip the "in expr" part.
2047 */
2048 void
2049skip_for_lines(void *fi_void, evalarg_T *evalarg)
2050{
2051 forinfo_T *fi = (forinfo_T *)fi_void;
2052 int i;
2053
2054 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002055 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002056}
2057
2058/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002059 * Use the first item in a ":for" list. Advance to the next.
2060 * Assign the values to the variable (list). "arg" points to the first one.
2061 * Return TRUE when a valid item was found, FALSE when at end of list or
2062 * something wrong.
2063 */
2064 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002065next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002066{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002067 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002068 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002069 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002070 ? (ASSIGN_FINAL
2071 // first round: error if variable exists
2072 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002073 | ASSIGN_NO_MEMBER_TYPE
2074 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002075 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002076 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002077 int skip_assign = in_vim9script() && arg[0] == '_'
2078 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002079
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002080 if (fi->fi_blob != NULL)
2081 {
2082 typval_T tv;
2083
2084 if (fi->fi_bi >= blob_len(fi->fi_blob))
2085 return FALSE;
2086 tv.v_type = VAR_NUMBER;
2087 tv.v_lock = VAR_FIXED;
2088 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2089 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002090 if (skip_assign)
2091 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002092 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002093 fi->fi_varcount, flag, NULL) == OK;
2094 }
2095
2096 if (fi->fi_string != NULL)
2097 {
2098 typval_T tv;
2099 int len;
2100
2101 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2102 if (len == 0)
2103 return FALSE;
2104 tv.v_type = VAR_STRING;
2105 tv.v_lock = VAR_FIXED;
2106 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2107 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002108 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002109 if (skip_assign)
2110 result = TRUE;
2111 else
2112 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002113 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002114 vim_free(tv.vval.v_string);
2115 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002116 }
2117
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002118 item = fi->fi_lw.lw_item;
2119 if (item == NULL)
2120 result = FALSE;
2121 else
2122 {
2123 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002124 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002125 if (skip_assign)
2126 result = TRUE;
2127 else
2128 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002129 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002130 }
2131 return result;
2132}
2133
2134/*
2135 * Free the structure used to store info used by ":for".
2136 */
2137 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002138free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002139{
Bram Moolenaar33570922005-01-25 22:26:29 +00002140 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002141
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002142 if (fi == NULL)
2143 return;
2144 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002145 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002146 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002147 list_unref(fi->fi_list);
2148 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002149 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002150 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002151 else
2152 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002153 vim_free(fi);
2154}
2155
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002157set_context_for_expression(
2158 expand_T *xp,
2159 char_u *arg,
2160 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002162 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002164 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002166 if (cmdidx == CMD_let || cmdidx == CMD_var
2167 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002168 {
2169 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002170 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002171 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002172 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002173 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002174 {
2175 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002176 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002177 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002178 break;
2179 }
2180 return;
2181 }
2182 }
2183 else
2184 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2185 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 while ((xp->xp_pattern = vim_strpbrk(arg,
2187 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2188 {
2189 c = *xp->xp_pattern;
2190 if (c == '&')
2191 {
2192 c = xp->xp_pattern[1];
2193 if (c == '&')
2194 {
2195 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002196 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 }
2198 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002199 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002201 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2202 xp->xp_pattern += 2;
2203
2204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 }
2206 else if (c == '$')
2207 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002208 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 xp->xp_context = EXPAND_ENV_VARS;
2210 }
2211 else if (c == '=')
2212 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002213 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 xp->xp_context = EXPAND_EXPRESSION;
2215 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002216 else if (c == '#'
2217 && xp->xp_context == EXPAND_EXPRESSION)
2218 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002219 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002220 break;
2221 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002222 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 && xp->xp_context == EXPAND_FUNCTIONS
2224 && vim_strchr(xp->xp_pattern, '(') == NULL)
2225 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002226 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 break;
2228 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002229 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002231 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 {
2233 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2234 if (c == '\\' && xp->xp_pattern[1] != NUL)
2235 ++xp->xp_pattern;
2236 xp->xp_context = EXPAND_NOTHING;
2237 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002238 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002240 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2242 /* skip */ ;
2243 xp->xp_context = EXPAND_NOTHING;
2244 }
2245 else if (c == '|')
2246 {
2247 if (xp->xp_pattern[1] == '|')
2248 {
2249 ++xp->xp_pattern;
2250 xp->xp_context = EXPAND_EXPRESSION;
2251 }
2252 else
2253 xp->xp_context = EXPAND_COMMANDS;
2254 }
2255 else
2256 xp->xp_context = EXPAND_EXPRESSION;
2257 }
2258 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002259 // Doesn't look like something valid, expand as an expression
2260 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002261 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 arg = xp->xp_pattern;
2263 if (*arg != NUL)
2264 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2265 /* skip */ ;
2266 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002267
2268 // ":exe one two" completes "two"
2269 if ((cmdidx == CMD_execute
2270 || cmdidx == CMD_echo
2271 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002272 || cmdidx == CMD_echomsg
2273 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002274 && xp->xp_context == EXPAND_EXPRESSION)
2275 {
2276 for (;;)
2277 {
2278 char_u *n = skiptowhite(arg);
2279
2280 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2281 break;
2282 arg = skipwhite(n);
2283 }
2284 }
2285
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 xp->xp_pattern = arg;
2287}
2288
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002290 * Return TRUE if "pat" matches "text".
2291 * Does not use 'cpo' and always uses 'magic'.
2292 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002293 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002294pattern_match(char_u *pat, char_u *text, int ic)
2295{
2296 int matches = FALSE;
2297 char_u *save_cpo;
2298 regmatch_T regmatch;
2299
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002300 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002301 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002302 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002303 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2304 if (regmatch.regprog != NULL)
2305 {
2306 regmatch.rm_ic = ic;
2307 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2308 vim_regfree(regmatch.regprog);
2309 }
2310 p_cpo = save_cpo;
2311 return matches;
2312}
2313
2314/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002315 * Handle a name followed by "(". Both for just "name(arg)" and for
2316 * "expr->name(arg)".
2317 * Returns OK or FAIL.
2318 */
2319 static int
2320eval_func(
2321 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002322 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002323 char_u *name,
2324 int name_len,
2325 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002326 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002327 typval_T *basetv) // "expr" for "expr->name(arg)"
2328{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002329 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002330 char_u *s = name;
2331 int len = name_len;
2332 partial_T *partial;
2333 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002334 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002335 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002336
2337 if (!evaluate)
2338 check_vars(s, len);
2339
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002340 // If "s" is the name of a variable of type VAR_FUNC
2341 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002342 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002343 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002344
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002345 // Need to make a copy, in case evaluating the arguments makes
2346 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002347 s = vim_strsave(s);
zeertzjqb7f6f932023-04-13 22:12:50 +01002348 if (s == NULL || (evaluate && *s == NUL))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002349 ret = FAIL;
2350 else
2351 {
2352 funcexe_T funcexe;
2353
2354 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002355 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002356 funcexe.fe_firstline = curwin->w_cursor.lnum;
2357 funcexe.fe_lastline = curwin->w_cursor.lnum;
2358 funcexe.fe_evaluate = evaluate;
2359 funcexe.fe_partial = partial;
2360 funcexe.fe_basetv = basetv;
2361 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002362 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002363 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002364 }
2365 vim_free(s);
2366
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002367 // If evaluate is FALSE rettv->v_type was not set in
2368 // get_func_tv, but it's needed in handle_subscript() to parse
2369 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002370 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2371 {
2372 rettv->vval.v_string = NULL;
2373 rettv->v_type = VAR_FUNC;
2374 }
2375
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002376 // Stop the expression evaluation when immediately
2377 // aborting on error, or when an interrupt occurred or
2378 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002379 if (evaluate && aborting())
2380 {
2381 if (ret == OK)
2382 clear_tv(rettv);
2383 ret = FAIL;
2384 }
2385 return ret;
2386}
2387
2388/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002389 * After a NL, skip over empty lines and comment-only lines.
2390 */
2391 static char_u *
2392newline_skip_comments(char_u *arg)
2393{
2394 char_u *p = arg + 1;
2395
2396 for (;;)
2397 {
2398 p = skipwhite(p);
2399
2400 if (*p == NUL)
2401 break;
2402 if (vim9_comment_start(p))
2403 {
2404 char_u *nl = vim_strchr(p, NL);
2405
2406 if (nl == NULL)
2407 break;
2408 p = nl;
2409 }
2410 if (*p != NL)
2411 break;
2412 ++p; // skip another NL
2413 }
2414 return p;
2415}
2416
2417/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002418 * Get the next line source line without advancing. But do skip over comment
2419 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002420 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002421 */
2422 static char_u *
2423getline_peek_skip_comments(evalarg_T *evalarg)
2424{
2425 for (;;)
2426 {
2427 char_u *next = getline_peek(evalarg->eval_getline,
2428 evalarg->eval_cookie);
2429 char_u *p;
2430
2431 if (next == NULL)
2432 break;
2433 p = skipwhite(next);
2434 if (*p != NUL && !vim9_comment_start(p))
2435 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002436 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002437 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002438 }
2439 return NULL;
2440}
2441
2442/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002443 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2444 * comment) and there is a next line, return the next line (skipping blanks)
2445 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002446 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2447 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002448 * "arg" must point somewhere inside a line, not at the start.
2449 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002450 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002451eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2452{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002453 char_u *p = skipwhite(arg);
2454
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002455 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002456 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002457 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002458 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2459 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002460 && (*p == NUL || *p == NL
2461 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002462 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002463 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002464
Bram Moolenaare442d592022-05-05 12:20:28 +01002465 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002466 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002467 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002468 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002469 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002470 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002471
Bram Moolenaar9d489562020-07-30 20:08:50 +02002472 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002473 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002474 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002475 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002476 }
2477 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002478 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002479}
2480
2481/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002482 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002483 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002484 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002485 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002486eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002487{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002488 garray_T *gap = &evalarg->eval_ga;
2489 char_u *line;
2490
Bram Moolenaar39be4982022-05-06 21:51:50 +01002491 if (arg != NULL)
2492 {
2493 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002494 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002495 // Truncate before a trailing comment, so that concatenating the lines
2496 // won't turn the rest into a comment.
2497 if (*skipwhite(arg) == '#')
2498 *arg = NUL;
2499 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002500
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002501 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002502 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2503 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002504 else
2505 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002506 if (line == NULL)
2507 return NULL;
2508
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002509 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002510 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2511 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002512 char_u *p = skipwhite(line);
2513
2514 // Going to concatenate the lines after parsing. For an empty or
2515 // comment line use an empty string.
2516 if (*p == NUL || vim9_comment_start(p))
2517 {
2518 vim_free(line);
2519 line = vim_strsave((char_u *)"");
2520 }
2521
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002522 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2523 ++gap->ga_len;
2524 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002525 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002526 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002527 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002528 evalarg->eval_tofree = line;
2529 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002530
2531 // Advanced to the next line, "arg" no longer points into the previous
2532 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002533 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002534 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002535}
2536
Bram Moolenaare6b53242020-07-01 17:28:33 +02002537/*
2538 * Call eval_next_non_blank() and get the next line if needed.
2539 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002540 char_u *
2541skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2542{
2543 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002544 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002545
Bram Moolenaar962d7212020-07-04 14:15:00 +02002546 if (evalarg == NULL)
2547 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002548 eval_next_non_blank(p, evalarg, &getnext);
2549 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002550 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002551 return p;
2552}
2553
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002554/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002555 * The "eval" functions have an "evalarg" argument: When NULL or
2556 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
2557 * parsed but not executed. The functions may return OK, but the rettv will be
2558 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 */
2560
2561/*
2562 * Handle zero level expression.
2563 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002564 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002565 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002566 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 * Return OK or FAIL.
2568 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002569 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002570eval0(
2571 char_u *arg,
2572 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002573 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002574 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002576 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2577}
2578
2579/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002580 * If "arg" is a simple function call without arguments then call it and return
2581 * the result. Otherwise return NOTDONE.
2582 */
2583 int
2584may_call_simple_func(
2585 char_u *arg,
2586 typval_T *rettv)
2587{
2588 char_u *parens = (char_u *)strstr((char *)arg, "()");
2589 int r = NOTDONE;
2590
2591 // If the expression is "FuncName()" then we can skip a lot of overhead.
2592 if (parens != NULL && *skipwhite(parens + 2) == NUL)
2593 {
2594 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
2595
2596 if (to_name_end(p, TRUE) == parens)
2597 r = call_simple_func(arg, (int)(parens - arg), rettv);
2598 }
2599 return r;
2600}
2601
2602/*
2603 * Handle zero level expression with optimization for a simple function call.
2604 * Same arguments and return value as eval0().
2605 */
2606 int
2607eval0_simple_funccal(
2608 char_u *arg,
2609 typval_T *rettv,
2610 exarg_T *eap,
2611 evalarg_T *evalarg)
2612{
2613 int r = may_call_simple_func(arg, rettv);
2614
2615 if (r == NOTDONE)
2616 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
2617 return r;
2618}
2619
2620/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002621 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2622 * expression and don't check what comes after the expression.
2623 */
2624 int
2625eval0_retarg(
2626 char_u *arg,
2627 typval_T *rettv,
2628 exarg_T *eap,
2629 evalarg_T *evalarg,
2630 char_u **retarg)
2631{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 int ret;
2633 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002634 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002635 int did_emsg_before = did_emsg;
2636 int called_emsg_before = called_emsg;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002637 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002638 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639
2640 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002641 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002642
Bram Moolenaar79481362022-06-27 20:15:10 +01002643 if (ret != FAIL)
2644 {
2645 expr_end = p;
2646 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002647
Bram Moolenaar79481362022-06-27 20:15:10 +01002648 // In Vim9 script a command block is not split at NL characters for
2649 // commands using an expression argument. Skip over a '#' comment to
2650 // check for a following NL. Require white space before the '#'.
2651 if (in_vim9script() && p > expr_end && retarg == NULL)
2652 while (*p == '#')
2653 {
2654 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002655
Bram Moolenaar79481362022-06-27 20:15:10 +01002656 if (nl == NULL)
2657 break;
2658 p = skipwhite(nl + 1);
2659 if (eap != NULL && *p != NUL)
2660 eap->nextcmd = p;
2661 check_for_end = FALSE;
2662 }
2663
2664 if (check_for_end)
2665 end_error = !ends_excmd2(arg, p);
2666 }
2667
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002668 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 {
2670 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002671 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 /*
2673 * Report the invalid expression unless the expression evaluation has
2674 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002675 * exception, or we already gave a more specific error.
2676 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002678 if (!aborting()
2679 && did_emsg == did_emsg_before
2680 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002681 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002682 {
2683 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002684 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002685 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002686 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002687 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002688
zeertzjqf2588b62023-05-05 17:22:35 +01002689 if (eap != NULL && p != NULL)
2690 {
2691 // Some of the expression may not have been consumed.
2692 // Only execute a next command if it cannot be a "||" operator.
2693 // The next command may be "catch".
2694 char_u *nextcmd = check_nextcmd(p);
2695 if (nextcmd != NULL && *nextcmd != '|')
2696 eap->nextcmd = nextcmd;
2697 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002698 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002700
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002701 if (retarg != NULL)
2702 *retarg = p;
2703 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002704 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002705
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 return ret;
2707}
2708
2709/*
2710 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002711 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002712 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 *
2714 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002715 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002717 * Note: "rettv.v_lock" is not set.
2718 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 * Return OK or FAIL.
2720 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002721 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002722eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002724 char_u *p;
2725 int getnext;
2726
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002727 CLEAR_POINTER(rettv);
2728
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 /*
2730 * Get the first variable.
2731 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002732 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 return FAIL;
2734
Bram Moolenaar793648f2020-06-26 21:28:25 +02002735 p = eval_next_non_blank(*arg, evalarg, &getnext);
2736 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002738 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002739 int result;
2740 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002741 evalarg_T *evalarg_used = evalarg;
2742 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002743 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002744 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002745 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002746
2747 if (evalarg == NULL)
2748 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002749 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002750 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002751 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002752 orig_flags = evalarg_used->eval_flags;
2753 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002754
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002755 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002756 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002757 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002758 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002759 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002760 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002761 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002762 clear_tv(rettv);
2763 return FAIL;
2764 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002765 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002766 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002767
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002769 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002771 int error = FALSE;
2772
Bram Moolenaar13106602020-10-04 16:06:05 +02002773 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002774 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002775 else if (vim9script)
2776 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002777 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002779 if (error || !op_falsy || !result)
2780 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002781 if (error)
2782 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 }
2784
2785 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002786 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002788 if (op_falsy)
2789 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002790 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002791 {
mityu4ac198c2021-05-28 17:52:40 +02002792 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002793 clear_tv(rettv);
2794 return FAIL;
2795 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002796 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002797 evalarg_used->eval_flags = (op_falsy ? !result : result)
zeertzjqb7f6f932023-04-13 22:12:50 +01002798 ? orig_flags : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002799 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002800 {
2801 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002803 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002804 if (!op_falsy || !result)
2805 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002807 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002809 /*
2810 * Check for the ":".
2811 */
2812 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2813 if (*p != ':')
2814 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002815 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002816 if (evaluate && result)
2817 clear_tv(rettv);
2818 evalarg_used->eval_flags = orig_flags;
2819 return FAIL;
2820 }
2821 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002822 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002823 else
2824 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002825 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002826 {
2827 error_white_both(p, 1);
2828 clear_tv(rettv);
2829 evalarg_used->eval_flags = orig_flags;
2830 return FAIL;
2831 }
2832 *arg = p;
2833 }
2834
2835 /*
2836 * Get the third variable. Recursive!
2837 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002838 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002839 {
mityu4ac198c2021-05-28 17:52:40 +02002840 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002841 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002842 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002843 return FAIL;
2844 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002845 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2846 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01002847 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002848 if (eval1(arg, &var2, evalarg_used) == FAIL)
2849 {
2850 if (evaluate && result)
2851 clear_tv(rettv);
2852 evalarg_used->eval_flags = orig_flags;
2853 return FAIL;
2854 }
2855 if (evaluate && !result)
2856 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002858
2859 if (evalarg == NULL)
2860 clear_evalarg(&local_evalarg, NULL);
2861 else
2862 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 }
2864
2865 return OK;
2866}
2867
2868/*
2869 * Handle first level expression:
2870 * expr2 || expr2 || expr2 logical OR
2871 *
2872 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002873 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 *
2875 * Return OK or FAIL.
2876 */
2877 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002878eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002880 char_u *p;
2881 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882
2883 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002884 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002886 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 return FAIL;
2888
2889 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002890 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002892 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002893 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002895 evalarg_T *evalarg_used = evalarg;
2896 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002897 int evaluate;
2898 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002899 long result = FALSE;
2900 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002901 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002902 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002903
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002904 if (evalarg == NULL)
2905 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002906 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002907 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002908 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002909 orig_flags = evalarg_used->eval_flags;
2910 evaluate = orig_flags & EVAL_EVALUATE;
2911 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002912 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002913 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002914 result = tv_get_bool_chk(rettv, &error);
2915 else if (tv_get_number_chk(rettv, &error) != 0)
2916 result = TRUE;
2917 clear_tv(rettv);
2918 if (error)
2919 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 }
2921
2922 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002923 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002925 while (p[0] == '|' && p[1] == '|')
2926 {
2927 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002928 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002929 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002930 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002931 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002932 {
2933 error_white_both(p, 2);
2934 clear_tv(rettv);
2935 return FAIL;
2936 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002937 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002938 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002939
2940 /*
2941 * Get the second variable.
2942 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002943 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002944 {
mityu4ac198c2021-05-28 17:52:40 +02002945 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002946 clear_tv(rettv);
2947 return FAIL;
2948 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002949 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2950 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01002951 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002952 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002953 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002954
2955 /*
2956 * Compute the result.
2957 */
2958 if (evaluate && !result)
2959 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002960 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002961 result = tv_get_bool_chk(&var2, &error);
2962 else if (tv_get_number_chk(&var2, &error) != 0)
2963 result = TRUE;
2964 clear_tv(&var2);
2965 if (error)
2966 return FAIL;
2967 }
2968 if (evaluate)
2969 {
2970 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002971 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002972 rettv->v_type = VAR_BOOL;
2973 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002974 }
2975 else
2976 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002977 rettv->v_type = VAR_NUMBER;
2978 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002979 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002980 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002981
2982 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002984
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002985 if (evalarg == NULL)
2986 clear_evalarg(&local_evalarg, NULL);
2987 else
2988 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 }
2990
2991 return OK;
2992}
2993
2994/*
2995 * Handle second level expression:
2996 * expr3 && expr3 && expr3 logical AND
2997 *
2998 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002999 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 *
3001 * Return OK or FAIL.
3002 */
3003 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003004eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003006 char_u *p;
3007 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008
3009 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003010 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003012 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 return FAIL;
3014
3015 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003016 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003018 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003019 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003021 evalarg_T *evalarg_used = evalarg;
3022 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003023 int orig_flags;
3024 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003025 long result = TRUE;
3026 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003027 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003028 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003029
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003030 if (evalarg == NULL)
3031 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003032 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003033 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003034 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003035 orig_flags = evalarg_used->eval_flags;
3036 evaluate = orig_flags & EVAL_EVALUATE;
3037 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003038 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003039 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003040 result = tv_get_bool_chk(rettv, &error);
3041 else if (tv_get_number_chk(rettv, &error) == 0)
3042 result = FALSE;
3043 clear_tv(rettv);
3044 if (error)
3045 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 }
3047
3048 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003049 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003051 while (p[0] == '&' && p[1] == '&')
3052 {
3053 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003054 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003055 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003056 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003057 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003058 {
3059 error_white_both(p, 2);
3060 clear_tv(rettv);
3061 return FAIL;
3062 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003063 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003064 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003065
3066 /*
3067 * Get the second variable.
3068 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003069 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003070 {
mityu4ac198c2021-05-28 17:52:40 +02003071 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003072 clear_tv(rettv);
3073 return FAIL;
3074 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003075 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3076 evalarg_used->eval_flags = result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003077 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003078 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003079 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003080 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003081
3082 /*
3083 * Compute the result.
3084 */
3085 if (evaluate && result)
3086 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003087 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003088 result = tv_get_bool_chk(&var2, &error);
3089 else if (tv_get_number_chk(&var2, &error) == 0)
3090 result = FALSE;
3091 clear_tv(&var2);
3092 if (error)
3093 return FAIL;
3094 }
3095 if (evaluate)
3096 {
3097 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003098 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003099 rettv->v_type = VAR_BOOL;
3100 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003101 }
3102 else
3103 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003104 rettv->v_type = VAR_NUMBER;
3105 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003106 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003107 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003108
3109 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003111
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003112 if (evalarg == NULL)
3113 clear_evalarg(&local_evalarg, NULL);
3114 else
3115 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 }
3117
3118 return OK;
3119}
3120
3121/*
3122 * Handle third level expression:
3123 * var1 == var2
3124 * var1 =~ var2
3125 * var1 != var2
3126 * var1 !~ var2
3127 * var1 > var2
3128 * var1 >= var2
3129 * var1 < var2
3130 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003131 * var1 is var2
3132 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 *
3134 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003135 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 *
3137 * Return OK or FAIL.
3138 */
3139 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003140eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003143 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003144 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003146 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147
3148 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003149 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003151 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 return FAIL;
3153
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003154 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003155
Bram Moolenaar696ba232020-07-29 21:20:41 +02003156 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157
3158 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003159 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003161 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003163 typval_T var2;
3164 int ic;
3165 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003166 int evaluate = evalarg == NULL
3167 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003168 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003169
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003170 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003171 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003172 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003173 p = *arg;
3174 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003175 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3176 {
mityu4ac198c2021-05-28 17:52:40 +02003177 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003178 clear_tv(rettv);
3179 return FAIL;
3180 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003181
Bram Moolenaar696ba232020-07-29 21:20:41 +02003182 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3183 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003184 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003185 clear_tv(rettv);
3186 return FAIL;
3187 }
3188
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003189 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 if (p[len] == '?')
3191 {
3192 ic = TRUE;
3193 ++len;
3194 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003195 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 else if (p[len] == '#')
3197 {
3198 ic = FALSE;
3199 ++len;
3200 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003201 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003203 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204
3205 /*
3206 * Get the second variable.
3207 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003208 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3209 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003210 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003211 clear_tv(rettv);
3212 return FAIL;
3213 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003214 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003215 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003217 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 return FAIL;
3219 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003220 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003221 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003222 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003223
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003224 // use the line of the comparison for messages
3225 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003226 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003227 {
3228 ret = FAIL;
3229 clear_tv(rettv);
3230 }
3231 else
3232 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003233 clear_tv(&var2);
3234 return ret;
3235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 }
3237
3238 return OK;
3239}
3240
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003241/*
3242 * Make a copy of blob "tv1" and append blob "tv2".
3243 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 void
3245eval_addblob(typval_T *tv1, typval_T *tv2)
3246{
3247 blob_T *b1 = tv1->vval.v_blob;
3248 blob_T *b2 = tv2->vval.v_blob;
3249 blob_T *b = blob_alloc();
3250 int i;
3251
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003252 if (b == NULL)
3253 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003255 for (i = 0; i < blob_len(b1); i++)
3256 ga_append(&b->bv_ga, blob_get(b1, i));
3257 for (i = 0; i < blob_len(b2); i++)
3258 ga_append(&b->bv_ga, blob_get(b2, i));
3259
3260 clear_tv(tv1);
3261 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262}
3263
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003264/*
3265 * Make a copy of list "tv1" and append list "tv2".
3266 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267 int
3268eval_addlist(typval_T *tv1, typval_T *tv2)
3269{
3270 typval_T var3;
3271
3272 // concatenate Lists
3273 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3274 {
3275 clear_tv(tv1);
3276 clear_tv(tv2);
3277 return FAIL;
3278 }
3279 clear_tv(tv1);
3280 *tv1 = var3;
3281 return OK;
3282}
3283
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003285 * Handle the bitwise left/right shift operator expression:
3286 * var1 << var2
3287 * var1 >> var2
3288 *
3289 * "arg" must point to the first non-white of the expression.
3290 * "arg" is advanced to just after the recognized expression.
3291 *
3292 * Return OK or FAIL.
3293 */
3294 static int
3295eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3296{
3297 /*
3298 * Get the first expression.
3299 */
3300 if (eval6(arg, rettv, evalarg) == FAIL)
3301 return FAIL;
3302
3303 /*
3304 * Repeat computing, until no '<<' or '>>' is following.
3305 */
3306 for (;;)
3307 {
3308 char_u *p;
3309 int getnext;
3310 exprtype_T type;
3311 int evaluate;
3312 typval_T var2;
3313 int vim9script;
3314
3315 p = eval_next_non_blank(*arg, evalarg, &getnext);
3316 if (p[0] == '<' && p[1] == '<')
3317 type = EXPR_LSHIFT;
3318 else if (p[0] == '>' && p[1] == '>')
3319 type = EXPR_RSHIFT;
3320 else
3321 return OK;
3322
3323 // Handle a bitwise left or right shift operator
3324 if (rettv->v_type != VAR_NUMBER)
3325 {
3326 // left operand should be a number
3327 emsg(_(e_bitshift_ops_must_be_number));
3328 clear_tv(rettv);
3329 return FAIL;
3330 }
3331
3332 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3333 vim9script = in_vim9script();
3334 if (getnext)
3335 {
3336 *arg = eval_next_line(*arg, evalarg);
3337 p = *arg;
3338 }
3339 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3340 {
3341 error_white_both(*arg, 2);
3342 clear_tv(rettv);
3343 return FAIL;
3344 }
3345
3346 /*
3347 * Get the second variable.
3348 */
3349 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3350 {
3351 error_white_both(p, 2);
3352 clear_tv(rettv);
3353 return FAIL;
3354 }
3355 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3356 if (eval6(arg, &var2, evalarg) == FAIL)
3357 {
3358 clear_tv(rettv);
3359 return FAIL;
3360 }
3361
3362 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3363 {
3364 // right operand should be a positive number
3365 if (var2.v_type != VAR_NUMBER)
3366 emsg(_(e_bitshift_ops_must_be_number));
3367 else
dundargocc57b5bc2022-11-02 13:30:51 +00003368 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003369 clear_tv(rettv);
3370 clear_tv(&var2);
3371 return FAIL;
3372 }
3373
3374 if (evaluate)
3375 {
3376 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3377 // shifting more bits than we have always results in zero
3378 rettv->vval.v_number = 0;
3379 else if (type == EXPR_LSHIFT)
3380 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003381 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003382 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003383 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003384 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003385 }
3386
3387 clear_tv(&var2);
3388 }
3389
3390 return OK;
3391}
3392
3393/*
3394 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003395 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003397 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003398 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 *
3400 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003401 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 *
3403 * Return OK or FAIL.
3404 */
3405 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003406eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003409 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003411 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 return FAIL;
3413
3414 /*
3415 * Repeat computing, until no '+', '-' or '.' is following.
3416 */
3417 for (;;)
3418 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003419 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003420 int getnext;
3421 char_u *p;
3422 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003423 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003424 int concat;
3425 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003426 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003427
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003428 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003429 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003430 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003431 p = eval_next_non_blank(*arg, evalarg, &getnext);
3432 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003433 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003434 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3435 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003437 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3438 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003439
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003440 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003441 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003442 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003443 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003444 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003445 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003446 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003447 {
mityu4ac198c2021-05-28 17:52:40 +02003448 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003449 clear_tv(rettv);
3450 return FAIL;
3451 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003452 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003453 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003454 if ((op != '+' || (rettv->v_type != VAR_LIST
3455 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003456 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003457 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003458 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003459 int error = FALSE;
3460
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003461 // For "list + ...", an illegal use of the first operand as
3462 // a number cannot be determined before evaluating the 2nd
3463 // operand: if this is also a list, all is ok.
3464 // For "something . ...", "something - ..." or "non-list + ...",
3465 // we know that the first operand needs to be a string or number
3466 // without evaluating the 2nd operand. So check before to avoid
3467 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003468 if (op != '.')
3469 tv_get_number_chk(rettv, &error);
3470 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003471 {
3472 clear_tv(rettv);
3473 return FAIL;
3474 }
3475 }
3476
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 /*
3478 * Get the second variable.
3479 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003480 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003481 {
mityu89dcb4d2021-05-28 13:50:17 +02003482 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003483 clear_tv(rettv);
3484 return FAIL;
3485 }
3486 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003487 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003489 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 return FAIL;
3491 }
3492
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003493 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 {
3495 /*
3496 * Compute the result.
3497 */
3498 if (op == '.')
3499 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003500 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3501 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003502 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003503
Bram Moolenaar2e086612020-08-25 22:37:48 +02003504 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003505 || var2.v_type == VAR_CHANNEL
3506 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003507 semsg(_(e_using_invalid_value_as_string_str),
3508 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003509 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003510 {
3511 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3512 var2.vval.v_float);
3513 s2 = buf2;
3514 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003515 else
3516 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003517 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003518 {
3519 clear_tv(rettv);
3520 clear_tv(&var2);
3521 return FAIL;
3522 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003523 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003524 clear_tv(rettv);
3525 rettv->v_type = VAR_STRING;
3526 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003528 else if (op == '+' && rettv->v_type == VAR_BLOB
3529 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003530 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003531 else if (op == '+' && rettv->v_type == VAR_LIST
3532 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003533 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003534 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003535 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 else
3538 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003539 int error = FALSE;
3540 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003541 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003542
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003543 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003544 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003545 f1 = rettv->vval.v_float;
3546 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003547 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003548 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003549 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003550 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003551 if (error)
3552 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003553 // This can only happen for "list + non-list" or
3554 // "blob + non-blob". For "non-list + ..." or
3555 // "something - ...", we returned before evaluating the
3556 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003557 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003558 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003559 return FAIL;
3560 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003561 if (var2.v_type == VAR_FLOAT)
3562 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003563 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003564 if (var2.v_type == VAR_FLOAT)
3565 {
3566 f2 = var2.vval.v_float;
3567 n2 = 0;
3568 }
3569 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003570 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003571 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003572 if (error)
3573 {
3574 clear_tv(rettv);
3575 clear_tv(&var2);
3576 return FAIL;
3577 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003578 if (rettv->v_type == VAR_FLOAT)
3579 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003580 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003581 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003582
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003583 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003584 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3585 {
3586 if (op == '+')
3587 f1 = f1 + f2;
3588 else
3589 f1 = f1 - f2;
3590 rettv->v_type = VAR_FLOAT;
3591 rettv->vval.v_float = f1;
3592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003594 {
3595 if (op == '+')
3596 n1 = n1 + n2;
3597 else
3598 n1 = n1 - n2;
3599 rettv->v_type = VAR_NUMBER;
3600 rettv->vval.v_number = n1;
3601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003603 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 }
3605 }
3606 return OK;
3607}
3608
3609/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003610 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 * * number multiplication
3612 * / number division
3613 * % number modulo
3614 *
3615 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003616 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 *
3618 * Return OK or FAIL.
3619 */
3620 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003621eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003622 char_u **arg,
3623 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003624 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003625 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003627 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628
3629 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003630 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003632 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 return FAIL;
3634
3635 /*
3636 * Repeat computing, until no '*', '/' or '%' is following.
3637 */
3638 for (;;)
3639 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003640 int evaluate;
3641 int getnext;
3642 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003643 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003644 int op;
3645 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003646 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003647 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003648
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003649 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003650 p = eval_next_non_blank(*arg, evalarg, &getnext);
3651 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003652 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003654
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003655 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003656 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003657 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003658 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003659 {
3660 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3661 {
mityu4ac198c2021-05-28 17:52:40 +02003662 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003663 clear_tv(rettv);
3664 return FAIL;
3665 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003666 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003669 f1 = 0;
3670 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003671 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003672 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003674 if (rettv->v_type == VAR_FLOAT)
3675 {
3676 f1 = rettv->vval.v_float;
3677 use_float = TRUE;
3678 n1 = 0;
3679 }
3680 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003681 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003682 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003683 if (error)
3684 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 }
3686 else
3687 n1 = 0;
3688
3689 /*
3690 * Get the second variable.
3691 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003692 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3693 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003694 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003695 clear_tv(rettv);
3696 return FAIL;
3697 }
3698 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003699 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 return FAIL;
3701
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003702 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003704 if (var2.v_type == VAR_FLOAT)
3705 {
3706 if (!use_float)
3707 {
3708 f1 = n1;
3709 use_float = TRUE;
3710 }
3711 f2 = var2.vval.v_float;
3712 n2 = 0;
3713 }
3714 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003715 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003716 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003717 clear_tv(&var2);
3718 if (error)
3719 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003720 if (use_float)
3721 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723
3724 /*
3725 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003726 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003728 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003730 if (op == '*')
3731 f1 = f1 * f2;
3732 else if (op == '/')
3733 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003734#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003735 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003736 if (f2 == 0.0)
3737 {
3738 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003739 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003740 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003741 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003742 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003743 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003744 }
3745 else
3746 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003747#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003748 // We rely on the floating point library to handle divide
3749 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003750 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003751#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003754 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003755 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003756 return FAIL;
3757 }
3758 rettv->v_type = VAR_FLOAT;
3759 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 }
3761 else
3762 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003763 int failed = FALSE;
3764
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003765 if (op == '*')
3766 n1 = n1 * n2;
3767 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003768 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003770 n1 = num_modulus(n1, n2, &failed);
3771 if (failed)
3772 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003773
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003774 rettv->v_type = VAR_NUMBER;
3775 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 }
3778 }
3779
3780 return OK;
3781}
3782
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003783/*
3784 * Handle a type cast before a base level expression.
3785 * "arg" must point to the first non-white of the expression.
3786 * "arg" is advanced to just after the recognized expression.
3787 * Return OK or FAIL.
3788 */
3789 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003790eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003791 char_u **arg,
3792 typval_T *rettv,
3793 evalarg_T *evalarg,
3794 int want_string) // after "." operator
3795{
3796 type_T *want_type = NULL;
3797 garray_T type_list; // list of pointers to allocated types
3798 int res;
3799 int evaluate = evalarg == NULL ? 0
3800 : (evalarg->eval_flags & EVAL_EVALUATE);
3801
3802 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003803 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3804 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003805 {
3806 ++*arg;
3807 ga_init2(&type_list, sizeof(type_T *), 10);
3808 want_type = parse_type(arg, &type_list, TRUE);
3809 if (want_type == NULL && (evaluate || **arg != '>'))
3810 {
3811 clear_type_list(&type_list);
3812 return FAIL;
3813 }
3814
3815 if (**arg != '>')
3816 {
3817 if (*skipwhite(*arg) == '>')
3818 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3819 else
3820 emsg(_(e_missing_gt));
3821 clear_type_list(&type_list);
3822 return FAIL;
3823 }
3824 ++*arg;
3825 *arg = skipwhite_and_linebreak(*arg, evalarg);
3826 }
3827
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003828 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003829
3830 if (want_type != NULL && evaluate)
3831 {
3832 if (res == OK)
3833 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003834 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3835 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003836
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003837 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003838 {
3839 if (want_type == &t_bool && actual != &t_bool
3840 && (actual->tt_flags & TTFLAG_BOOL_OK))
3841 {
3842 int n = tv2bool(rettv);
3843
3844 // can use "0" and "1" for boolean in some places
3845 clear_tv(rettv);
3846 rettv->v_type = VAR_BOOL;
3847 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3848 }
3849 else
3850 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003851 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003852
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003853 res = check_type(want_type, actual, TRUE, where);
3854 }
3855 }
3856 }
3857 clear_type_list(&type_list);
3858 }
3859
3860 return res;
3861}
3862
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003863 int
3864eval_leader(char_u **arg, int vim9)
3865{
3866 char_u *s = *arg;
3867 char_u *p = *arg;
3868
3869 while (*p == '!' || *p == '-' || *p == '+')
3870 {
3871 char_u *n = skipwhite(p + 1);
3872
3873 // ++, --, -+ and +- are not accepted in Vim9 script
3874 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3875 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003876 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003877 return FAIL;
3878 }
3879 p = n;
3880 }
3881 *arg = p;
3882 return OK;
3883}
3884
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003886 * Check for a predefined value "true", "false" and "null.*".
3887 * Return OK when recognized.
3888 */
3889 int
3890handle_predefined(char_u *s, int len, typval_T *rettv)
3891{
3892 switch (len)
3893 {
3894 case 4: if (STRNCMP(s, "true", 4) == 0)
3895 {
3896 rettv->v_type = VAR_BOOL;
3897 rettv->vval.v_number = VVAL_TRUE;
3898 return OK;
3899 }
3900 if (STRNCMP(s, "null", 4) == 0)
3901 {
3902 rettv->v_type = VAR_SPECIAL;
3903 rettv->vval.v_number = VVAL_NULL;
3904 return OK;
3905 }
3906 break;
3907 case 5: if (STRNCMP(s, "false", 5) == 0)
3908 {
3909 rettv->v_type = VAR_BOOL;
3910 rettv->vval.v_number = VVAL_FALSE;
3911 return OK;
3912 }
3913 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003914 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3915 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003916#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003917 rettv->v_type = VAR_JOB;
3918 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003919#else
3920 rettv->v_type = VAR_SPECIAL;
3921 rettv->vval.v_number = VVAL_NULL;
3922#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003923 return OK;
3924 }
3925 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003926 case 9:
3927 if (STRNCMP(s, "null_", 5) != 0)
3928 break;
3929 if (STRNCMP(s + 5, "list", 4) == 0)
3930 {
3931 rettv->v_type = VAR_LIST;
3932 rettv->vval.v_list = NULL;
3933 return OK;
3934 }
3935 if (STRNCMP(s + 5, "dict", 4) == 0)
3936 {
3937 rettv->v_type = VAR_DICT;
3938 rettv->vval.v_dict = NULL;
3939 return OK;
3940 }
3941 if (STRNCMP(s + 5, "blob", 4) == 0)
3942 {
3943 rettv->v_type = VAR_BLOB;
3944 rettv->vval.v_blob = NULL;
3945 return OK;
3946 }
3947 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003948 case 10: if (STRNCMP(s, "null_class", 10) == 0)
3949 {
3950 rettv->v_type = VAR_CLASS;
3951 rettv->vval.v_class = NULL;
3952 return OK;
3953 }
3954 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003955 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3956 {
3957 rettv->v_type = VAR_STRING;
3958 rettv->vval.v_string = NULL;
3959 return OK;
3960 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003961 if (STRNCMP(s, "null_object", 11) == 0)
3962 {
3963 rettv->v_type = VAR_OBJECT;
3964 rettv->vval.v_object = NULL;
3965 return OK;
3966 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003967 break;
3968 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003969 if (STRNCMP(s, "null_channel", 12) == 0)
3970 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003971#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003972 rettv->v_type = VAR_CHANNEL;
3973 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003974#else
3975 rettv->v_type = VAR_SPECIAL;
3976 rettv->vval.v_number = VVAL_NULL;
3977#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003978 return OK;
3979 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003980 if (STRNCMP(s, "null_partial", 12) == 0)
3981 {
3982 rettv->v_type = VAR_PARTIAL;
3983 rettv->vval.v_partial = NULL;
3984 return OK;
3985 }
3986 break;
3987 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3988 {
3989 rettv->v_type = VAR_FUNC;
3990 rettv->vval.v_string = NULL;
3991 return OK;
3992 }
3993 break;
3994 }
3995 return FAIL;
3996}
3997
3998/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 * Handle sixth level expression:
4000 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004001 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004002 * "string" string constant
4003 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 * &option-name option value
4005 * @r register contents
4006 * identifier variable value
4007 * function() function call
4008 * $VAR environment variable
4009 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004010 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004012 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004013 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 *
4015 * Also handle:
4016 * ! in front logical NOT
4017 * - in front unary minus
4018 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004019 * trailing [] subscript in String or List
4020 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004021 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 *
4023 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004024 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 *
4026 * Return OK or FAIL.
4027 */
4028 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004029eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004030 char_u **arg,
4031 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004032 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004033 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004035 int evaluate = evalarg != NULL
4036 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 int len;
4038 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004039 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 char_u *start_leader, *end_leader;
4041 int ret = OK;
4042 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004043 static int recurse = 0;
4044 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045
4046 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004047 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004048 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004050 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051
4052 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004053 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 */
4055 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004056 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004057 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 end_leader = *arg;
4059
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004060 if (**arg == '.' && (!isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004061 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004062 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004063 ++*arg;
4064 return FAIL;
4065 }
4066
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004067 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004068 // and crash. With MSVC the stack is smaller.
4069 if (recurse ==
4070#ifdef _MSC_VER
4071 300
4072#else
4073 1000
4074#endif
4075 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004076 {
4077 semsg(_(e_expression_too_recursive_str), *arg);
4078 return FAIL;
4079 }
4080 ++recurse;
4081
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 switch (**arg)
4083 {
4084 /*
4085 * Number constant.
4086 */
4087 case '0':
4088 case '1':
4089 case '2':
4090 case '3':
4091 case '4':
4092 case '5':
4093 case '6':
4094 case '7':
4095 case '8':
4096 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004097 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004098
4099 // Apply prefixed "-" and "+" now. Matters especially when
4100 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004101 if (ret == OK && evaluate && end_leader > start_leader
4102 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004103 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 break;
4105
4106 /*
4107 * String constant: "string".
4108 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004109 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 break;
4111
4112 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004113 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004115 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004116 break;
4117
4118 /*
4119 * List: [expr, expr]
4120 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004121 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 break;
4123
4124 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004125 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004126 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004127 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004128 {
4129 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4130 }
4131 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004132 {
4133 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004134 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004135 }
4136 else
4137 ret = NOTDONE;
4138 break;
4139
4140 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004141 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004142 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004143 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004144 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004145 ret = NOTDONE;
4146 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004147 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004148 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004149 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004150 break;
4151
4152 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004153 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004155 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 break;
4157
4158 /*
4159 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004160 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 */
LemonBoy2eaef102022-05-06 13:14:50 +01004162 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4163 ret = eval_interp_string(arg, rettv, evaluate);
4164 else
4165 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 break;
4167
4168 /*
4169 * Register contents: @r.
4170 */
4171 case '@': ++*arg;
4172 if (evaluate)
4173 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004174 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004175 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004176 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004177 emsg_invreg(**arg);
4178 else
4179 {
4180 rettv->v_type = VAR_STRING;
4181 rettv->vval.v_string = get_reg_contents(**arg,
4182 GREG_EXPR_SRC);
4183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 }
4185 if (**arg != NUL)
4186 ++*arg;
4187 break;
4188
4189 /*
4190 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004191 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004193 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004194 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004195 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004196 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004197 if (ret == OK && evaluate)
4198 {
4199 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4200
Bram Moolenaara9931532021-06-12 15:58:16 +02004201 // Compile it here to get the return type. The return
4202 // type is optional, when it's missing use t_unknown.
4203 // This is recognized in compile_return().
4204 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4205 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004206 if (compile_def_function(ufunc, FALSE,
4207 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004208 {
4209 clear_tv(rettv);
4210 ret = FAIL;
4211 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004212 }
4213 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004214 if (ret == NOTDONE)
4215 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004216 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004217 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004218
Bram Moolenaar9215f012020-06-27 21:18:00 +02004219 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004220 if (**arg == ')')
4221 ++*arg;
4222 else if (ret == OK)
4223 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004224 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004225 clear_tv(rettv);
4226 ret = FAIL;
4227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 }
4229 break;
4230
Bram Moolenaar8c711452005-01-14 21:53:12 +00004231 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 break;
4233 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004234
4235 if (ret == NOTDONE)
4236 {
4237 /*
4238 * Must be a variable or function name.
4239 * Can also be a curly-braces kind of name: {expr}.
4240 */
4241 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004242 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004243 if (alias != NULL)
4244 s = alias;
4245
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004246 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004247 ret = FAIL;
4248 else
4249 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004250 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4251
Bram Moolenaar4525a572022-02-13 11:57:33 +00004252 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004253 {
4254 emsg(_(e_cannot_use_underscore_here));
4255 ret = FAIL;
4256 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004257 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004258 && s[0] == 's' && s[1] == ':')
4259 {
4260 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4261 ret = FAIL;
4262 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004263 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004264 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004265 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004266 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004267 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004268 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004269 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004270 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004271 // get the value of "true", "false", etc. or a variable
4272 ret = FAIL;
4273 if (vim9script)
4274 ret = handle_predefined(s, len, rettv);
4275 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004276 {
4277 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004278 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004279 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004280 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004281 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004282 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004283 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004284 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004285 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004286 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004287 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004288 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004289 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004290 }
4291
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004292 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4293 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004294 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004295 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296
4297 /*
4298 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4299 */
4300 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004301 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004302
4303 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004304 return ret;
4305}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004306
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004307/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004308 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004309 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004310 * Adjusts "end_leaderp" until it is at "start_leader".
4311 */
4312 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004313eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004314 typval_T *rettv,
4315 int numeric_only,
4316 char_u *start_leader,
4317 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004318{
4319 char_u *end_leader = *end_leaderp;
4320 int ret = OK;
4321 int error = FALSE;
4322 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004323 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004324 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004325 float_T f = 0.0;
4326
4327 if (rettv->v_type == VAR_FLOAT)
4328 f = rettv->vval.v_float;
4329 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004330 {
4331 while (VIM_ISWHITE(end_leader[-1]))
4332 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004333 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004334 val = tv2bool(rettv);
4335 else
4336 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004337 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004338 if (error)
4339 {
4340 clear_tv(rettv);
4341 ret = FAIL;
4342 }
4343 else
4344 {
4345 while (end_leader > start_leader)
4346 {
4347 --end_leader;
4348 if (*end_leader == '!')
4349 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004350 if (numeric_only)
4351 {
4352 ++end_leader;
4353 break;
4354 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004355 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004356 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004357 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004358 {
4359 rettv->v_type = VAR_BOOL;
4360 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4361 }
4362 else
4363 f = !f;
4364 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004365 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004366 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004367 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004368 type = VAR_BOOL;
4369 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004370 }
4371 else if (*end_leader == '-')
4372 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004373 if (rettv->v_type == VAR_FLOAT)
4374 f = -f;
4375 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004376 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004377 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004378 type = VAR_NUMBER;
4379 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004380 }
4381 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004382 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004384 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004385 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004387 else
4388 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004389 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004390 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004391 rettv->v_type = type;
4392 else
4393 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004394 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004397 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 return ret;
4399}
4400
4401/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004402 * Call the function referred to in "rettv".
4403 */
4404 static int
4405call_func_rettv(
4406 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004407 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004408 typval_T *rettv,
4409 int evaluate,
4410 dict_T *selfdict,
4411 typval_T *basetv)
4412{
4413 partial_T *pt = NULL;
4414 funcexe_T funcexe;
4415 typval_T functv;
4416 char_u *s;
4417 int ret;
4418
4419 // need to copy the funcref so that we can clear rettv
4420 if (evaluate)
4421 {
4422 functv = *rettv;
4423 rettv->v_type = VAR_UNKNOWN;
4424
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004425 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004426 if (functv.v_type == VAR_PARTIAL)
4427 {
4428 pt = functv.vval.v_partial;
4429 s = partial_name(pt);
4430 }
4431 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004432 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004433 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004434 if (s == NULL || *s == NUL)
4435 {
4436 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004437 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004438 goto theend;
4439 }
4440 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004441 }
4442 else
4443 s = (char_u *)"";
4444
Bram Moolenaara80faa82020-04-12 19:37:17 +02004445 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004446 funcexe.fe_firstline = curwin->w_cursor.lnum;
4447 funcexe.fe_lastline = curwin->w_cursor.lnum;
4448 funcexe.fe_evaluate = evaluate;
4449 funcexe.fe_partial = pt;
4450 funcexe.fe_selfdict = selfdict;
4451 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004452 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004453
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004454theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004455 // Clear the funcref afterwards, so that deleting it while
4456 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004457 if (evaluate)
4458 clear_tv(&functv);
4459
4460 return ret;
4461}
4462
4463/*
4464 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004465 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004466 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4467 */
4468 static int
4469eval_lambda(
4470 char_u **arg,
4471 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004472 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004473 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004474{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004475 int evaluate = evalarg != NULL
4476 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004477 typval_T base = *rettv;
4478 int ret;
4479
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004480 rettv->v_type = VAR_UNKNOWN;
4481
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004482 if (**arg == '{')
4483 {
4484 // ->{lambda}()
4485 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4486 }
4487 else
4488 {
4489 // ->(lambda)()
4490 ++*arg;
4491 ret = eval1(arg, rettv, evalarg);
4492 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004493 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004494 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004495 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004496 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004497 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004498 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4499 && rettv->v_type != VAR_PARTIAL)
4500 {
4501 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4502 return FAIL;
4503 }
4504 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004505 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004506 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004507 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004508
4509 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004510 {
4511 if (verbose)
4512 {
4513 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004514 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004515 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004516 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004517 }
4518 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004519 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004520 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004521 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004522 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004523
4524 // Clear the funcref afterwards, so that deleting it while
4525 // evaluating the arguments is possible (see test55).
4526 if (evaluate)
4527 clear_tv(&base);
4528
4529 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004530}
4531
4532/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004533 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004534 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004535 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4536 */
4537 static int
4538eval_method(
4539 char_u **arg,
4540 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004541 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004542 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004543{
4544 char_u *name;
4545 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004546 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004547 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004548 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004549 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004550 int evaluate = evalarg != NULL
4551 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004552
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004553 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004554
Bram Moolenaarac92e252019-08-03 21:58:38 +02004555 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004556 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004557 if (alias != NULL)
4558 name = alias;
4559
4560 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004561 {
4562 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004563 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004564 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004565 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004566 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004567 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004568 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004569
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004570 // If there is no "(" immediately following, but there is further on,
4571 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4572 // Does not handle anything where "(" is part of the expression.
4573 *arg = skipwhite(*arg);
4574
4575 if (**arg != '(' && alias == NULL
4576 && (paren = vim_strchr(*arg, '(')) != NULL)
4577 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004578 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00004579
4580 // Truncate the name a the "(". Avoid trying to get another line
4581 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004582 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00004583 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
4584 if (evalarg != NULL)
4585 {
4586 getline = evalarg->eval_getline;
4587 evalarg->eval_getline = NULL;
4588 }
4589
4590 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004591 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004592 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004593 *arg = name + len;
4594 ret = FAIL;
4595 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004596 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004597 {
4598 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004599 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004600 }
Bram Moolenaar34820942022-12-19 20:28:38 +00004601
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004602 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00004603 if (getline != NULL)
4604 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004605 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004606
4607 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004608 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004609 *arg = skipwhite(*arg);
4610
4611 if (**arg != '(')
4612 {
4613 if (verbose)
4614 semsg(_(e_missing_parenthesis_str), name);
4615 ret = FAIL;
4616 }
4617 else if (VIM_ISWHITE((*arg)[-1]))
4618 {
4619 if (verbose)
4620 emsg(_(e_no_white_space_allowed_before_parenthesis));
4621 ret = FAIL;
4622 }
4623 else
4624 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004625 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004626 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004627 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004628
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004629 // Clear the funcref afterwards, so that deleting it while
4630 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004631 if (evaluate)
4632 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004633 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004634
Bram Moolenaarac92e252019-08-03 21:58:38 +02004635 return ret;
4636}
4637
4638/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004639 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4640 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004641 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4642 */
4643 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004644eval_index(
4645 char_u **arg,
4646 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004647 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004648 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004649{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004650 int evaluate = evalarg != NULL
4651 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004652 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004653 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004654 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004655 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004656 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004657 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004658
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004659 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4660 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004661
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004662 init_tv(&var1);
4663 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004664 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004665 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004666 /*
4667 * dict.name
4668 */
4669 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004670 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004671 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004672 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004673 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004674 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004675 }
4676 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004677 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004678 /*
4679 * something[idx]
4680 *
4681 * Get the (first) variable from inside the [].
4682 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004683 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004684 if (**arg == ':')
4685 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004686 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004687 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004688 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004689 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004690 semsg(_(e_white_space_required_before_and_after_str_at_str),
4691 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004692 clear_tv(&var1);
4693 return FAIL;
4694 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004695 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004696 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004697 int error = FALSE;
4698
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004699 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004700 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004701 && var1.v_type == VAR_FLOAT)
4702 {
4703 var1.vval.v_string = typval_tostring(&var1, TRUE);
4704 var1.v_type = VAR_STRING;
4705 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004706
Bram Moolenaar4525a572022-02-13 11:57:33 +00004707 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004708 tv_get_number_chk(&var1, &error);
4709 else
4710 error = tv_get_string_chk(&var1) == NULL;
4711 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004712 {
4713 // not a number or string
4714 clear_tv(&var1);
4715 return FAIL;
4716 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004717 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004718
4719 /*
4720 * Get the second variable from inside the [:].
4721 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004722 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004723 if (**arg == ':')
4724 {
4725 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004726 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004727 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004728 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004729 semsg(_(e_white_space_required_before_and_after_str_at_str),
4730 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004731 if (!empty1)
4732 clear_tv(&var1);
4733 return FAIL;
4734 }
4735 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004736 if (**arg == ']')
4737 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004738 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004739 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004740 if (!empty1)
4741 clear_tv(&var1);
4742 return FAIL;
4743 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004744 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004745 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004746 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004747 if (!empty1)
4748 clear_tv(&var1);
4749 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004750 return FAIL;
4751 }
4752 }
4753
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004754 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004755 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004756 if (**arg != ']')
4757 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004758 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004759 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004760 clear_tv(&var1);
4761 if (range)
4762 clear_tv(&var2);
4763 return FAIL;
4764 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004765 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004766 }
4767
4768 if (evaluate)
4769 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004770 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004771 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004772 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004773
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004774 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004775 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004776 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004777 clear_tv(&var2);
4778 return res;
4779 }
4780 return OK;
4781}
4782
4783/*
4784 * Check if "rettv" can have an [index] or [sli:ce]
4785 */
4786 int
4787check_can_index(typval_T *rettv, int evaluate, int verbose)
4788{
4789 switch (rettv->v_type)
4790 {
4791 case VAR_FUNC:
4792 case VAR_PARTIAL:
4793 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004794 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004795 return FAIL;
4796 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004797 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004798 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004799 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004800 case VAR_BOOL:
4801 case VAR_SPECIAL:
4802 case VAR_JOB:
4803 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004804 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004805 case VAR_CLASS:
4806 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004807 if (verbose)
4808 emsg(_(e_cannot_index_special_variable));
4809 return FAIL;
4810 case VAR_UNKNOWN:
4811 case VAR_ANY:
4812 case VAR_VOID:
4813 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004814 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004815 emsg(_(e_cannot_index_special_variable));
4816 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004817 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004818 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004819
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004820 case VAR_STRING:
4821 case VAR_LIST:
4822 case VAR_DICT:
4823 case VAR_BLOB:
4824 break;
4825 case VAR_NUMBER:
4826 if (in_vim9script())
4827 emsg(_(e_cannot_index_number));
4828 break;
4829 }
4830 return OK;
4831}
4832
4833/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004834 * slice() function
4835 */
4836 void
4837f_slice(typval_T *argvars, typval_T *rettv)
4838{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004839 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004840 && ((argvars[0].v_type != VAR_STRING
4841 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004842 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004843 && check_for_list_arg(argvars, 0) == FAIL)
4844 || check_for_number_arg(argvars, 1) == FAIL
4845 || check_for_opt_number_arg(argvars, 2) == FAIL))
4846 return;
4847
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00004848 if (check_can_index(argvars, TRUE, FALSE) != OK)
4849 return;
4850
4851 copy_tv(argvars, rettv);
4852 eval_index_inner(rettv, TRUE, argvars + 1,
4853 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4854 TRUE, NULL, 0, FALSE);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004855}
4856
4857/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004858 * Apply index or range to "rettv".
4859 * "var1" is the first index, NULL for [:expr].
4860 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004861 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4862 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004863 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4864 */
4865 int
4866eval_index_inner(
4867 typval_T *rettv,
4868 int is_range,
4869 typval_T *var1,
4870 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004871 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004872 char_u *key,
4873 int keylen,
4874 int verbose)
4875{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004876 varnumber_T n1, n2 = 0;
4877 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004878
4879 n1 = 0;
4880 if (var1 != NULL && rettv->v_type != VAR_DICT)
4881 n1 = tv_get_number(var1);
4882
4883 if (is_range)
4884 {
4885 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004886 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004887 if (verbose)
4888 emsg(_(e_cannot_slice_dictionary));
4889 return FAIL;
4890 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004891 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004892 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004893 else
4894 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004895 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004896
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004897 switch (rettv->v_type)
4898 {
4899 case VAR_UNKNOWN:
4900 case VAR_ANY:
4901 case VAR_VOID:
4902 case VAR_FUNC:
4903 case VAR_PARTIAL:
4904 case VAR_FLOAT:
4905 case VAR_BOOL:
4906 case VAR_SPECIAL:
4907 case VAR_JOB:
4908 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004909 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004910 case VAR_CLASS:
4911 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004912 break; // not evaluating, skipping over subscript
4913
4914 case VAR_NUMBER:
4915 case VAR_STRING:
4916 {
4917 char_u *s = tv_get_string(rettv);
4918
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004919 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004920 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004921 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004922 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004923 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004924 else
4925 s = char_from_string(s, n1);
4926 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004927 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004928 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004929 // The resulting variable is a substring. If the indexes
4930 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004931 if (n1 < 0)
4932 {
4933 n1 = len + n1;
4934 if (n1 < 0)
4935 n1 = 0;
4936 }
4937 if (n2 < 0)
4938 n2 = len + n2;
4939 else if (n2 >= len)
4940 n2 = len;
4941 if (n1 >= len || n2 < 0 || n1 > n2)
4942 s = NULL;
4943 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004944 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004945 }
4946 else
4947 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004948 // The resulting variable is a string of a single
4949 // character. If the index is too big or negative the
4950 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004951 if (n1 >= len || n1 < 0)
4952 s = NULL;
4953 else
4954 s = vim_strnsave(s + n1, 1);
4955 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004956 clear_tv(rettv);
4957 rettv->v_type = VAR_STRING;
4958 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004959 }
4960 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004961
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004962 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004963 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4964 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004965 break;
4966
4967 case VAR_LIST:
4968 if (var1 == NULL)
4969 n1 = 0;
4970 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004971 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004972 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004973 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004974 return FAIL;
4975 break;
4976
4977 case VAR_DICT:
4978 {
4979 dictitem_T *item;
4980 typval_T tmp;
4981
4982 if (key == NULL)
4983 {
4984 key = tv_get_string_chk(var1);
4985 if (key == NULL)
4986 return FAIL;
4987 }
4988
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004989 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004990
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004991 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004992 {
4993 if (verbose)
4994 {
4995 if (keylen > 0)
4996 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004997 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004998 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004999 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005000 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005001
5002 copy_tv(&item->di_tv, &tmp);
5003 clear_tv(rettv);
5004 *rettv = tmp;
5005 }
5006 break;
5007 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005008 return OK;
5009}
5010
5011/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005012 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005013 */
5014 char_u *
5015partial_name(partial_T *pt)
5016{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02005017 if (pt != NULL)
5018 {
5019 if (pt->pt_name != NULL)
5020 return pt->pt_name;
5021 if (pt->pt_func != NULL)
5022 return pt->pt_func->uf_name;
5023 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005024 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005025}
5026
Bram Moolenaarddecc252016-04-06 22:59:37 +02005027 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005028partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005029{
5030 int i;
5031
5032 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005033 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005034 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005035 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005036 if (pt->pt_name != NULL)
5037 {
5038 func_unref(pt->pt_name);
5039 vim_free(pt->pt_name);
5040 }
5041 else
5042 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005043
Bram Moolenaar54656012021-06-09 20:50:46 +02005044 // "out_up" is no longer used, decrement refcount on partial that owns it.
5045 partial_unref(pt->pt_outer.out_up_partial);
5046
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005047 // Using pt_outer from another partial.
5048 partial_unref(pt->pt_outer_partial);
5049
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005050 // Decrease the reference count for the context of a closure. If down
5051 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005052 if (pt->pt_funcstack != NULL)
5053 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005054 --pt->pt_funcstack->fs_refcount;
5055 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005056 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005057 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005058 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5059 if (pt->pt_loopvars[i] != NULL)
5060 {
5061 --pt->pt_loopvars[i]->lvs_refcount;
5062 loopvars_check_refcount(pt->pt_loopvars[i]);
5063 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005064
Bram Moolenaarddecc252016-04-06 22:59:37 +02005065 vim_free(pt);
5066}
5067
5068/*
5069 * Unreference a closure: decrement the reference count and free it when it
5070 * becomes zero.
5071 */
5072 void
5073partial_unref(partial_T *pt)
5074{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005075 if (pt == NULL)
5076 return;
5077
5078 int done = FALSE;
5079
5080 if (--pt->pt_refcount <= 0)
5081 partial_free(pt);
5082
5083 // If the reference count goes down to one, the funcstack may be the
5084 // only reference and can be freed if no other partials reference it.
5085 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005086 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005087 // careful: if the funcstack is freed it may contain this partial
5088 // and it gets freed as well
5089 if (pt->pt_funcstack != NULL)
5090 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005091
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005092 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005093 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005094 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005095
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005096 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5097 if (pt->pt_loopvars[depth] != NULL
5098 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005099 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005100 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005101 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005102}
5103
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005104/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005105 * Return the next (unique) copy ID.
5106 * Used for serializing nested structures.
5107 */
5108 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005109get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005110{
5111 current_copyID += COPYID_INC;
5112 return current_copyID;
5113}
5114
5115/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005116 * Garbage collection for lists and dictionaries.
5117 *
5118 * We use reference counts to be able to free most items right away when they
5119 * are no longer used. But for composite items it's possible that it becomes
5120 * unused while the reference count is > 0: When there is a recursive
5121 * reference. Example:
5122 * :let l = [1, 2, 3]
5123 * :let d = {9: l}
5124 * :let l[1] = d
5125 *
5126 * Since this is quite unusual we handle this with garbage collection: every
5127 * once in a while find out which lists and dicts are not referenced from any
5128 * variable.
5129 *
5130 * Here is a good reference text about garbage collection (refers to Python
5131 * but it applies to all reference-counting mechanisms):
5132 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005133 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005134
5135/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005136 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005137 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005138 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005139 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005140 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005141garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005142{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005143 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005144 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005145 buf_T *buf;
5146 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005147 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005148 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005149
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005150 if (!testing)
5151 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005152 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005153 want_garbage_collect = FALSE;
5154 may_garbage_collect = FALSE;
5155 garbage_collect_at_exit = FALSE;
5156 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005157
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005158 // The execution stack can grow big, limit the size.
5159 if (exestack.ga_maxlen - exestack.ga_len > 500)
5160 {
5161 size_t new_len;
5162 char_u *pp;
5163 int n;
5164
5165 // Keep 150% of the current size, with a minimum of the growth size.
5166 n = exestack.ga_len / 2;
5167 if (n < exestack.ga_growsize)
5168 n = exestack.ga_growsize;
5169
5170 // Don't make it bigger though.
5171 if (exestack.ga_len + n < exestack.ga_maxlen)
5172 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005173 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005174 pp = vim_realloc(exestack.ga_data, new_len);
5175 if (pp == NULL)
5176 return FAIL;
5177 exestack.ga_maxlen = exestack.ga_len + n;
5178 exestack.ga_data = pp;
5179 }
5180 }
5181
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005182 // We advance by two because we add one for items referenced through
5183 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005184 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005185
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005186 /*
5187 * 1. Go through all accessible variables and mark all lists and dicts
5188 * with copyID.
5189 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005190
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005191 // Don't free variables in the previous_funccal list unless they are only
5192 // referenced through previous_funccal. This must be first, because if
5193 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005194 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005195
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005196 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005197 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005198
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005199 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005200 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005201 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5202 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005203
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005204 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005205 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005206 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5207 NULL, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005208 // window-local variables in autocmd windows
Bram Moolenaare76062c2022-11-28 18:51:43 +00005209 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005210 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00005211 abort = abort || set_ref_in_item(
5212 &aucmd_win[i].auc_win->w_winvar.di_tv, copyID, NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005213#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005214 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005215 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5216 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005217 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005218 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005219 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5220 NULL, NULL);
5221#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005222
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005223 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005224 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005225 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5226 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005227 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005228 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005229
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005230 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005231 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005232
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005233 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005234 abort = abort || set_ref_in_functions(copyID);
5235
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005236 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005237 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005238
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005239 // funcstacks keep variables for closures
5240 abort = abort || set_ref_in_funcstacks(copyID);
5241
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005242 // loopvars keep variables for loop blocks
5243 abort = abort || set_ref_in_loopvars(copyID);
5244
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005245 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005246 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005247
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005248 // callbacks in buffers
5249 abort = abort || set_ref_in_buffers(copyID);
5250
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005251 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5252 abort = abort || set_ref_in_insexpand_funcs(copyID);
5253
5254 // 'operatorfunc' callback
5255 abort = abort || set_ref_in_opfunc(copyID);
5256
5257 // 'tagfunc' callback
5258 abort = abort || set_ref_in_tagfunc(copyID);
5259
5260 // 'imactivatefunc' and 'imstatusfunc' callbacks
5261 abort = abort || set_ref_in_im_funcs(copyID);
5262
Bram Moolenaar1dced572012-04-05 16:54:08 +02005263#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005264 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005265#endif
5266
Bram Moolenaardb913952012-06-29 12:54:53 +02005267#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005268 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005269#endif
5270
5271#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005272 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005273#endif
5274
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005275#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005276 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005277 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005278#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005279#ifdef FEAT_NETBEANS_INTG
5280 abort = abort || set_ref_in_nb_channel(copyID);
5281#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005282
Bram Moolenaare3188e22016-05-31 21:13:04 +02005283#ifdef FEAT_TIMERS
5284 abort = abort || set_ref_in_timer(copyID);
5285#endif
5286
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005287#ifdef FEAT_QUICKFIX
5288 abort = abort || set_ref_in_quickfix(copyID);
5289#endif
5290
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005291#ifdef FEAT_TERMINAL
5292 abort = abort || set_ref_in_term(copyID);
5293#endif
5294
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005295#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005296 abort = abort || set_ref_in_popups(copyID);
5297#endif
5298
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005299 abort = abort || set_ref_in_classes(copyID);
5300
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005301 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005302 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005303 /*
5304 * 2. Free lists and dictionaries that are not referenced.
5305 */
5306 did_free = free_unref_items(copyID);
5307
5308 /*
5309 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005310 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005311 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005312 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005313 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005314 else if (p_verbose > 0)
5315 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005316 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005317 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005318
5319 return did_free;
5320}
5321
5322/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005323 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005324 */
5325 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005326free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005327{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005328 int did_free = FALSE;
5329
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005330 // Let all "free" functions know that we are here. This means no
5331 // dictionaries, lists, channels or jobs are to be freed, because we will
5332 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005333 in_free_unref_items = TRUE;
5334
5335 /*
5336 * PASS 1: free the contents of the items. We don't free the items
5337 * themselves yet, so that it is possible to decrement refcount counters
5338 */
5339
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005340 // Go through the list of dicts and free items without this copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005341 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005342
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005343 // Go through the list of lists and free items without this copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005344 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005345
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005346 // Go through the list of objects and free items without this copyID.
5347 did_free |= object_free_nonref(copyID);
5348
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005349 // Go through the list of classes and free items without this copyID.
5350 did_free |= class_free_nonref(copyID);
5351
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005352#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005353 // Go through the list of jobs and free items without the copyID. This
5354 // must happen before doing channels, because jobs refer to channels, but
5355 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005356 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5357
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005358 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005359 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5360#endif
5361
5362 /*
5363 * PASS 2: free the items themselves.
5364 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005365 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005366 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005367
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005368#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005369 // Go through the list of jobs and free items without the copyID. This
5370 // must happen before doing channels, because jobs refer to channels, but
5371 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005372 free_unused_jobs(copyID, COPYID_MASK);
5373
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005374 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005375 free_unused_channels(copyID, COPYID_MASK);
5376#endif
5377
5378 in_free_unref_items = FALSE;
5379
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005380 return did_free;
5381}
5382
5383/*
5384 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005385 * "list_stack" is used to add lists to be marked. Can be NULL.
5386 *
5387 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005388 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005389 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005390set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005391{
5392 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005393 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005394 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005395 hashtab_T *cur_ht;
5396 ht_stack_T *ht_stack = NULL;
5397 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005398
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005399 cur_ht = ht;
5400 for (;;)
5401 {
5402 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005403 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005404 // Mark each item in the hashtab. If the item contains a hashtab
5405 // it is added to ht_stack, if it contains a list it is added to
5406 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005407 todo = (int)cur_ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00005408 FOR_ALL_HASHTAB_ITEMS(cur_ht, hi, todo)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005409 if (!HASHITEM_EMPTY(hi))
5410 {
5411 --todo;
5412 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5413 &ht_stack, list_stack);
5414 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005415 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005416
5417 if (ht_stack == NULL)
5418 break;
5419
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005420 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005421 cur_ht = ht_stack->ht;
5422 tempitem = ht_stack;
5423 ht_stack = ht_stack->prev;
5424 free(tempitem);
5425 }
5426
5427 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005428}
5429
Dominique Pelle748b3082022-01-08 12:41:16 +00005430#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5431 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005432/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005433 * Mark a dict and its items with "copyID".
5434 * Returns TRUE if setting references failed somehow.
5435 */
5436 int
5437set_ref_in_dict(dict_T *d, int copyID)
5438{
5439 if (d != NULL && d->dv_copyID != copyID)
5440 {
5441 d->dv_copyID = copyID;
5442 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5443 }
5444 return FALSE;
5445}
Dominique Pelle748b3082022-01-08 12:41:16 +00005446#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005447
5448/*
5449 * Mark a list and its items with "copyID".
5450 * Returns TRUE if setting references failed somehow.
5451 */
5452 int
5453set_ref_in_list(list_T *ll, int copyID)
5454{
5455 if (ll != NULL && ll->lv_copyID != copyID)
5456 {
5457 ll->lv_copyID = copyID;
5458 return set_ref_in_list_items(ll, copyID, NULL);
5459 }
5460 return FALSE;
5461}
5462
5463/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005464 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005465 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5466 *
5467 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005468 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005469 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005470set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005471{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005472 listitem_T *li;
5473 int abort = FALSE;
5474 list_T *cur_l;
5475 list_stack_T *list_stack = NULL;
5476 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005477
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005478 cur_l = l;
5479 for (;;)
5480 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005481 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005482 // Mark each item in the list. If the item contains a hashtab
5483 // it is added to ht_stack, if it contains a list it is added to
5484 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005485 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5486 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5487 ht_stack, &list_stack);
5488 if (list_stack == NULL)
5489 break;
5490
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005491 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005492 cur_l = list_stack->list;
5493 tempitem = list_stack;
5494 list_stack = list_stack->prev;
5495 free(tempitem);
5496 }
5497
5498 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005499}
5500
5501/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005502 * Mark the partial in callback 'cb' with "copyID".
5503 */
5504 int
5505set_ref_in_callback(callback_T *cb, int copyID)
5506{
5507 typval_T tv;
5508
5509 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5510 return FALSE;
5511
5512 tv.v_type = VAR_PARTIAL;
5513 tv.vval.v_partial = cb->cb_partial;
5514 return set_ref_in_item(&tv, copyID, NULL, NULL);
5515}
5516
5517/*
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005518 * Mark the dict "dd" with "copyID".
5519 * Also see set_ref_in_item().
5520 */
5521 static int
5522set_ref_in_item_dict(
5523 dict_T *dd,
5524 int copyID,
5525 ht_stack_T **ht_stack,
5526 list_stack_T **list_stack)
5527{
5528 if (dd == NULL || dd->dv_copyID == copyID)
5529 return FALSE;
5530
5531 // Didn't see this dict yet.
5532 dd->dv_copyID = copyID;
5533 if (ht_stack == NULL)
5534 return set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5535
5536 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5537 if (newitem == NULL)
5538 return TRUE;
5539
5540 newitem->ht = &dd->dv_hashtab;
5541 newitem->prev = *ht_stack;
5542 *ht_stack = newitem;
5543
5544 return FALSE;
5545}
5546
5547/*
5548 * Mark the list "ll" with "copyID".
5549 * Also see set_ref_in_item().
5550 */
5551 static int
5552set_ref_in_item_list(
5553 list_T *ll,
5554 int copyID,
5555 ht_stack_T **ht_stack,
5556 list_stack_T **list_stack)
5557{
5558 if (ll == NULL || ll->lv_copyID == copyID)
5559 return FALSE;
5560
5561 // Didn't see this list yet.
5562 ll->lv_copyID = copyID;
5563 if (list_stack == NULL)
5564 return set_ref_in_list_items(ll, copyID, ht_stack);
5565
5566 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5567 if (newitem == NULL)
5568 return TRUE;
5569
5570 newitem->list = ll;
5571 newitem->prev = *list_stack;
5572 *list_stack = newitem;
5573
5574 return FALSE;
5575}
5576
5577/*
5578 * Mark the partial "pt" with "copyID".
5579 * Also see set_ref_in_item().
5580 */
5581 static int
5582set_ref_in_item_partial(
5583 partial_T *pt,
5584 int copyID,
5585 ht_stack_T **ht_stack,
5586 list_stack_T **list_stack)
5587{
5588 if (pt == NULL || pt->pt_copyID == copyID)
5589 return FALSE;
5590
5591 // Didn't see this partial yet.
5592 pt->pt_copyID = copyID;
5593
5594 int abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
5595
5596 if (pt->pt_dict != NULL)
5597 {
5598 typval_T dtv;
5599
5600 dtv.v_type = VAR_DICT;
5601 dtv.vval.v_dict = pt->pt_dict;
5602 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5603 }
5604
5605 for (int i = 0; i < pt->pt_argc; ++i)
5606 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5607 ht_stack, list_stack);
5608 // pt_funcstack is handled in set_ref_in_funcstacks()
5609 // pt_loopvars is handled in set_ref_in_loopvars()
5610
5611 return abort;
5612}
5613
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005614#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005615/*
5616 * Mark the job "pt" with "copyID".
5617 * Also see set_ref_in_item().
5618 */
5619 static int
5620set_ref_in_item_job(
5621 job_T *job,
5622 int copyID,
5623 ht_stack_T **ht_stack,
5624 list_stack_T **list_stack)
5625{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005626 typval_T dtv;
5627
5628 if (job == NULL || job->jv_copyID == copyID)
5629 return FALSE;
5630
5631 job->jv_copyID = copyID;
5632 if (job->jv_channel != NULL)
5633 {
5634 dtv.v_type = VAR_CHANNEL;
5635 dtv.vval.v_channel = job->jv_channel;
5636 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5637 }
5638 if (job->jv_exit_cb.cb_partial != NULL)
5639 {
5640 dtv.v_type = VAR_PARTIAL;
5641 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
5642 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5643 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005644
5645 return FALSE;
5646}
5647
5648/*
5649 * Mark the channel "ch" with "copyID".
5650 * Also see set_ref_in_item().
5651 */
5652 static int
5653set_ref_in_item_channel(
5654 channel_T *ch,
5655 int copyID,
5656 ht_stack_T **ht_stack,
5657 list_stack_T **list_stack)
5658{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005659 typval_T dtv;
5660
5661 if (ch == NULL || ch->ch_copyID == copyID)
5662 return FALSE;
5663
5664 ch->ch_copyID = copyID;
5665 for (ch_part_T part = PART_SOCK; part < PART_COUNT; ++part)
5666 {
5667 for (jsonq_T *jq = ch->ch_part[part].ch_json_head.jq_next;
5668 jq != NULL; jq = jq->jq_next)
5669 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5670 for (cbq_T *cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5671 cq = cq->cq_next)
5672 if (cq->cq_callback.cb_partial != NULL)
5673 {
5674 dtv.v_type = VAR_PARTIAL;
5675 dtv.vval.v_partial = cq->cq_callback.cb_partial;
5676 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5677 }
5678 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
5679 {
5680 dtv.v_type = VAR_PARTIAL;
5681 dtv.vval.v_partial = ch->ch_part[part].ch_callback.cb_partial;
5682 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5683 }
5684 }
5685 if (ch->ch_callback.cb_partial != NULL)
5686 {
5687 dtv.v_type = VAR_PARTIAL;
5688 dtv.vval.v_partial = ch->ch_callback.cb_partial;
5689 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5690 }
5691 if (ch->ch_close_cb.cb_partial != NULL)
5692 {
5693 dtv.v_type = VAR_PARTIAL;
5694 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
5695 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5696 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005697
5698 return FALSE;
5699}
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005700#endif
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005701
5702/*
5703 * Mark the class "cl" with "copyID".
5704 * Also see set_ref_in_item().
5705 */
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005706 int
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005707set_ref_in_item_class(
5708 class_T *cl,
5709 int copyID,
5710 ht_stack_T **ht_stack,
5711 list_stack_T **list_stack)
5712{
5713 int abort = FALSE;
5714
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005715 if (cl == NULL || cl->class_copyID == copyID)
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005716 return FALSE;
5717
5718 cl->class_copyID = copyID;
Yegappan Lakshmanan544be0d2023-09-04 22:14:28 +02005719 if (cl->class_members_tv != NULL)
5720 {
5721 // The "class_members_tv" table is allocated only for regular classes
5722 // and not for interfaces.
5723 for (int i = 0; !abort && i < cl->class_class_member_count; ++i)
5724 abort = abort || set_ref_in_item(
5725 &cl->class_members_tv[i],
5726 copyID, ht_stack, list_stack);
5727 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005728
5729 for (int i = 0; !abort && i < cl->class_class_function_count; ++i)
5730 abort = abort || set_ref_in_func(NULL,
5731 cl->class_class_functions[i], copyID);
5732
5733 for (int i = 0; !abort && i < cl->class_obj_method_count; ++i)
5734 abort = abort || set_ref_in_func(NULL,
5735 cl->class_obj_methods[i], copyID);
5736
5737 return abort;
5738}
5739
5740/*
5741 * Mark the object "cl" with "copyID".
5742 * Also see set_ref_in_item().
5743 */
5744 static int
5745set_ref_in_item_object(
5746 object_T *obj,
5747 int copyID,
5748 ht_stack_T **ht_stack,
5749 list_stack_T **list_stack)
5750{
5751 int abort = FALSE;
5752
5753 if (obj == NULL || obj->obj_copyID == copyID)
5754 return FALSE;
5755
5756 obj->obj_copyID = copyID;
5757
5758 // The typval_T array is right after the object_T.
5759 typval_T *mtv = (typval_T *)(obj + 1);
5760 for (int i = 0; !abort
5761 && i < obj->obj_class->class_obj_member_count; ++i)
5762 abort = abort || set_ref_in_item(mtv + i, copyID,
5763 ht_stack, list_stack);
5764
5765 return abort;
5766}
5767
5768/*
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005769 * Mark all lists, dicts and other container types referenced through typval
5770 * "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005771 * "list_stack" is used to add lists to be marked. Can be NULL.
5772 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5773 *
5774 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005775 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005776 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005777set_ref_in_item(
5778 typval_T *tv,
5779 int copyID,
5780 ht_stack_T **ht_stack,
5781 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005782{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005783 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005784
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005785 switch (tv->v_type)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005786 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005787 case VAR_DICT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005788 return set_ref_in_item_dict(tv->vval.v_dict, copyID,
5789 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005790
5791 case VAR_LIST:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005792 return set_ref_in_item_list(tv->vval.v_list, copyID,
5793 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005794
5795 case VAR_FUNC:
5796 {
5797 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
5798 break;
5799 }
5800
5801 case VAR_PARTIAL:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005802 return set_ref_in_item_partial(tv->vval.v_partial, copyID,
5803 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005804
5805 case VAR_JOB:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005806#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005807 return set_ref_in_item_job(tv->vval.v_job, copyID,
5808 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005809#else
5810 break;
5811#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005812
5813 case VAR_CHANNEL:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005814#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005815 return set_ref_in_item_channel(tv->vval.v_channel, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005816 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005817#else
5818 break;
5819#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005820
5821 case VAR_CLASS:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005822 return set_ref_in_item_class(tv->vval.v_class, copyID,
5823 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005824
5825 case VAR_OBJECT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005826 return set_ref_in_item_object(tv->vval.v_object, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005827 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005828
5829 case VAR_UNKNOWN:
5830 case VAR_ANY:
5831 case VAR_VOID:
5832 case VAR_BOOL:
5833 case VAR_SPECIAL:
5834 case VAR_NUMBER:
5835 case VAR_FLOAT:
5836 case VAR_STRING:
5837 case VAR_BLOB:
5838 case VAR_INSTR:
5839 // Types that do not contain any other item
5840 break;
5841 }
5842
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005843 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005844}
5845
Bram Moolenaar8c711452005-01-14 21:53:12 +00005846/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005847 * Return a string with the string representation of a variable.
5848 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005849 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005850 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005851 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005852 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005853 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005854 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5855 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005856 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005857 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005858 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005859echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005860 typval_T *tv,
5861 char_u **tofree,
5862 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005863 int copyID,
5864 int echo_style,
5865 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005866 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005867{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005868 static int recurse = 0;
5869 char_u *r = NULL;
5870
Bram Moolenaar33570922005-01-25 22:26:29 +00005871 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005872 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005873 if (!did_echo_string_emsg)
5874 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005875 // Only give this message once for a recursive call to avoid
5876 // flooding the user with errors. And stop iterating over lists
5877 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005878 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005879 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005880 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005881 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005882 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005883 }
5884 ++recurse;
5885
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005886 switch (tv->v_type)
5887 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005888 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005889 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005890 {
5891 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005892 r = tv->vval.v_string;
5893 if (r == NULL)
5894 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005895 }
5896 else
5897 {
5898 *tofree = string_quote(tv->vval.v_string, FALSE);
5899 r = *tofree;
5900 }
5901 break;
5902
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005903 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005904 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005905 char_u buf[MAX_FUNC_NAME_LEN];
5906
5907 if (echo_style)
5908 {
LemonBoya5d35902022-04-29 21:15:02 +01005909 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5910 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005911 buf, MAX_FUNC_NAME_LEN);
5912 if (r == buf)
5913 {
5914 r = vim_strsave(buf);
5915 *tofree = r;
5916 }
5917 else
5918 *tofree = NULL;
5919 }
5920 else
5921 {
5922 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5923 : make_ufunc_name_readable(
5924 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5925 TRUE);
5926 r = *tofree;
5927 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005928 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005929 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005930
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005931 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005932 {
5933 partial_T *pt = tv->vval.v_partial;
5934 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005935 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005936 garray_T ga;
5937 int i;
5938 char_u *tf;
5939
5940 ga_init2(&ga, 1, 100);
5941 ga_concat(&ga, (char_u *)"function(");
5942 if (fname != NULL)
5943 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005944 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005945 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005946 && vim_isupper(fname[1]))
5947 {
5948 ga_concat(&ga, (char_u *)"'g:");
5949 ga_concat(&ga, fname + 1);
5950 }
5951 else
5952 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005953 vim_free(fname);
5954 }
5955 if (pt != NULL && pt->pt_argc > 0)
5956 {
5957 ga_concat(&ga, (char_u *)", [");
5958 for (i = 0; i < pt->pt_argc; ++i)
5959 {
5960 if (i > 0)
5961 ga_concat(&ga, (char_u *)", ");
5962 ga_concat(&ga,
5963 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5964 vim_free(tf);
5965 }
5966 ga_concat(&ga, (char_u *)"]");
5967 }
5968 if (pt != NULL && pt->pt_dict != NULL)
5969 {
5970 typval_T dtv;
5971
5972 ga_concat(&ga, (char_u *)", ");
5973 dtv.v_type = VAR_DICT;
5974 dtv.vval.v_dict = pt->pt_dict;
5975 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5976 vim_free(tf);
5977 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005978 // terminate with ')' and a NUL
5979 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005980
5981 *tofree = ga.ga_data;
5982 r = *tofree;
5983 break;
5984 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005985
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005986 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005987 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005988 break;
5989
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005991 if (tv->vval.v_list == NULL)
5992 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005993 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005994 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005995 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005996 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005997 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5998 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005999 {
6000 *tofree = NULL;
6001 r = (char_u *)"[...]";
6002 }
6003 else
6004 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006005 int old_copyID = tv->vval.v_list->lv_copyID;
6006
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006007 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006008 *tofree = list2string(tv, copyID, restore_copyID);
6009 if (restore_copyID)
6010 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006011 r = *tofree;
6012 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006013 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006014
Bram Moolenaar8c711452005-01-14 21:53:12 +00006015 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006016 if (tv->vval.v_dict == NULL)
6017 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006018 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006019 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006020 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006021 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006022 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6023 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006024 {
6025 *tofree = NULL;
6026 r = (char_u *)"{...}";
6027 }
6028 else
6029 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006030 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006031
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006032 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006033 *tofree = dict2string(tv, copyID, restore_copyID);
6034 if (restore_copyID)
6035 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006036 r = *tofree;
6037 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006038 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006039
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006040 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006041 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006042 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006043 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006044 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006045 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006046 break;
6047
Bram Moolenaar835dc632016-02-07 14:27:38 +01006048 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006049 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006050#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00006051 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02006052 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
6053 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006054 if (composite_val)
6055 {
6056 *tofree = string_quote(r, FALSE);
6057 r = *tofree;
6058 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006059#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006060 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006061
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006062 case VAR_INSTR:
6063 *tofree = NULL;
6064 r = (char_u *)"instructions";
6065 break;
6066
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006067 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006068 {
6069 class_T *cl = tv->vval.v_class;
6070 size_t len = 6 + (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
6071 r = *tofree = alloc(len);
6072 vim_snprintf((char *)r, len, "class %s",
6073 cl == NULL ? "[unknown]" : (char *)cl->class_name);
6074 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006075 break;
6076
6077 case VAR_OBJECT:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006078 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006079 garray_T ga;
6080 ga_init2(&ga, 1, 50);
6081 ga_concat(&ga, (char_u *)"object of ");
6082 object_T *obj = tv->vval.v_object;
6083 class_T *cl = obj == NULL ? NULL : obj->obj_class;
6084 ga_concat(&ga, cl == NULL ? (char_u *)"[unknown]"
6085 : cl->class_name);
6086 if (cl != NULL)
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006087 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006088 ga_concat(&ga, (char_u *)" {");
6089 for (int i = 0; i < cl->class_obj_member_count; ++i)
6090 {
6091 if (i > 0)
6092 ga_concat(&ga, (char_u *)", ");
Bram Moolenaard505d172022-12-18 21:42:55 +00006093 ocmember_T *m = &cl->class_obj_members[i];
6094 ga_concat(&ga, m->ocm_name);
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006095 ga_concat(&ga, (char_u *)": ");
6096 char_u *tf = NULL;
6097 ga_concat(&ga, echo_string_core(
6098 (typval_T *)(obj + 1) + i,
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006099 &tf, numbuf, copyID, echo_style,
6100 restore_copyID, composite_val));
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006101 vim_free(tf);
6102 }
6103 ga_concat(&ga, (char_u *)"}");
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006104 }
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006105
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006106 *tofree = r = ga.ga_data;
6107 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006108 break;
6109
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006110 case VAR_FLOAT:
6111 *tofree = NULL;
6112 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6113 r = numbuf;
6114 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006115
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006116 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006117 case VAR_SPECIAL:
6118 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006119 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006120 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006121 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006122
Bram Moolenaar8502c702014-06-17 12:51:16 +02006123 if (--recurse == 0)
6124 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006125 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006126}
6127
6128/*
6129 * Return a string with the string representation of a variable.
6130 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6131 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006132 * Does not put quotes around strings, as ":echo" displays values.
6133 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6134 * May return NULL.
6135 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006136 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006137echo_string(
6138 typval_T *tv,
6139 char_u **tofree,
6140 char_u *numbuf,
6141 int copyID)
6142{
6143 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6144}
6145
6146/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006147 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6148 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006149 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006150 */
6151 int
6152buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6153{
6154 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006155 char_u *t;
6156 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006157
6158 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6159 return -1;
6160
6161 if (lnum > buf->b_ml.ml_line_count)
6162 lnum = buf->b_ml.ml_line_count;
6163
6164 str = ml_get_buf(buf, lnum, FALSE);
6165 if (str == NULL)
6166 return -1;
6167
6168 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006169 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006170
Bram Moolenaar91458462021-01-13 20:08:38 +01006171 // count the number of characters
6172 t = str;
6173 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6174 t += mb_ptr2len(t);
6175
6176 // In insert mode, when the cursor is at the end of a non-empty line,
6177 // byteidx points to the NUL character immediately past the end of the
6178 // string. In this case, add one to the character count.
6179 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6180 count++;
6181
6182 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006183}
6184
6185/*
6186 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006187 * byte index. Works only for loaded buffers. Returns -1 on failure.
6188 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006189 */
6190 int
6191buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6192{
6193 char_u *str;
6194 char_u *t;
6195
6196 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6197 return -1;
6198
6199 if (lnum > buf->b_ml.ml_line_count)
6200 lnum = buf->b_ml.ml_line_count;
6201
6202 str = ml_get_buf(buf, lnum, FALSE);
6203 if (str == NULL)
6204 return -1;
6205
6206 // Convert the character offset to a byte offset
6207 t = str;
6208 while (*t != NUL && --charidx > 0)
6209 t += mb_ptr2len(t);
6210
Bram Moolenaar91458462021-01-13 20:08:38 +01006211 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006212}
6213
6214/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006216 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006218 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006219var2fpos(
6220 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006221 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006222 int *fnum, // set to fnum for '0, 'A, etc.
6223 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006225 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006227 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006229 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006230 if (varp->v_type == VAR_LIST)
6231 {
6232 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006233 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006234 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006235 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006236
6237 l = varp->vval.v_list;
6238 if (l == NULL)
6239 return NULL;
6240
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006241 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006242 pos.lnum = list_find_nr(l, 0L, &error);
6243 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006244 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006245 if (charcol)
6246 len = (long)mb_charlen(ml_get(pos.lnum));
6247 else
6248 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006249
Bram Moolenaarec65d772020-08-20 22:29:12 +02006250 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006251 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006252 li = list_find(l, 1L);
6253 if (li != NULL && li->li_tv.v_type == VAR_STRING
6254 && li->li_tv.vval.v_string != NULL
6255 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006256 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006257 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006258 }
6259 else
6260 {
6261 pos.col = list_find_nr(l, 1L, &error);
6262 if (error)
6263 return NULL;
6264 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006265
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006266 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006267 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006268 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006269 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006270
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006271 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006272 pos.coladd = list_find_nr(l, 2L, &error);
6273 if (error)
6274 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006275
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006276 return &pos;
6277 }
6278
Bram Moolenaarc5809432021-03-27 21:23:30 +01006279 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6280 return NULL;
6281
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006282 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006283 if (name == NULL)
6284 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006285
6286 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006287 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006288 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006289 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006290 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006291 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006292 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006293 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006294 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006295 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006296 pos = VIsual;
6297 else
6298 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006299 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006300 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006301 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006303 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006304 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6306 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006307 pos = *pp;
6308 }
6309 if (pos.lnum != 0)
6310 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006311 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006312 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6313 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006315
Bram Moolenaara5525202006-03-02 22:52:09 +00006316 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006317
Bram Moolenaar477933c2007-07-17 14:32:23 +00006318 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006319 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006320 // the "w_valid" flags are not reset when moving the cursor, but they
6321 // do matter for update_topline() and validate_botline().
6322 check_cursor_moved(curwin);
6323
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006324 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006325 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006326 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006327 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006328 // In silent Ex mode topline is zero, but that's not a valid line
6329 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006330 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006331 return &pos;
6332 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006333 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006334 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006335 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006336 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006337 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006338 return &pos;
6339 }
6340 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006341 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006343 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 {
6345 pos.lnum = curbuf->b_ml.ml_line_count;
6346 pos.col = 0;
6347 }
6348 else
6349 {
6350 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006351 if (charcol)
6352 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6353 else
6354 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 }
6356 return &pos;
6357 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006358 if (in_vim9script())
6359 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 return NULL;
6361}
6362
6363/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006364 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006365 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006366 * Note that the column is passed on as-is, the caller may want to decrement
6367 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006368 * If "charcol" is TRUE use the column as the character index instead of the
6369 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006370 * Return FAIL when conversion is not possible, doesn't check the position for
6371 * validity.
6372 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006373 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006374list2fpos(
6375 typval_T *arg,
6376 pos_T *posp,
6377 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006378 colnr_T *curswantp,
6379 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006380{
6381 list_T *l = arg->vval.v_list;
6382 long i = 0;
6383 long n;
6384
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006385 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6386 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006387 if (arg->v_type != VAR_LIST
6388 || l == NULL
6389 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006390 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006391 return FAIL;
6392
6393 if (fnump != NULL)
6394 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006395 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006396 if (n < 0)
6397 return FAIL;
6398 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006399 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006400 *fnump = n;
6401 }
6402
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006403 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006404 if (n < 0)
6405 return FAIL;
6406 posp->lnum = n;
6407
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006408 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006409 if (n < 0)
6410 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006411 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006412 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006413 if (charcol)
6414 {
6415 buf_T *buf;
6416
6417 // Get the text for the specified line in a loaded buffer
6418 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6419 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6420 return FAIL;
6421
Bram Moolenaar79f23442022-10-10 12:42:57 +01006422 n = buf_charidx_to_byteidx(buf,
6423 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006424 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006425 posp->col = n;
6426
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006427 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006428 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006429 posp->coladd = 0;
6430 else
6431 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006432
Bram Moolenaar493c1782014-05-28 14:34:46 +02006433 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006434 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006435
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006436 return OK;
6437}
6438
6439/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 * Get the length of an environment variable name.
6441 * Advance "arg" to the first character after the name.
6442 * Return 0 for error.
6443 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006444 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006445get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446{
6447 char_u *p;
6448 int len;
6449
6450 for (p = *arg; vim_isIDc(*p); ++p)
6451 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006452 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 return 0;
6454
6455 len = (int)(p - *arg);
6456 *arg = p;
6457 return len;
6458}
6459
6460/*
6461 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006462 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 * Return 0 if something is wrong.
6464 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006465 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006466get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467{
6468 char_u *p;
6469 int len;
6470
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006471 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006473 {
6474 if (*p == ':')
6475 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006476 // "s:" is start of "s:var", but "n:" is not and can be used in
6477 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006478 len = (int)(p - *arg);
6479 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6480 || len > 1)
6481 break;
6482 }
6483 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006484 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 return 0;
6486
6487 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006488 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489
6490 return len;
6491}
6492
6493/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006494 * Get the length of the name of a variable or function.
6495 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006497 * Return -1 if curly braces expansion failed.
6498 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 * If the name contains 'magic' {}'s, expand them and return the
6500 * expanded name in an allocated string via 'alias' - caller must free.
6501 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006502 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006503get_name_len(
6504 char_u **arg,
6505 char_u **alias,
6506 int evaluate,
6507 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508{
6509 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 char_u *p;
6511 char_u *expr_start;
6512 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006514 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515
6516 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6517 && (*arg)[2] == (int)KE_SNR)
6518 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006519 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 *arg += 3;
6521 return get_id_len(arg) + 3;
6522 }
6523 len = eval_fname_script(*arg);
6524 if (len > 0)
6525 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006526 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 *arg += len;
6528 }
6529
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006531 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006533 p = find_name_end(*arg, &expr_start, &expr_end,
6534 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 if (expr_start != NULL)
6536 {
6537 char_u *temp_string;
6538
6539 if (!evaluate)
6540 {
6541 len += (int)(p - *arg);
6542 *arg = skipwhite(p);
6543 return len;
6544 }
6545
6546 /*
6547 * Include any <SID> etc in the expanded string:
6548 * Thus the -len here.
6549 */
6550 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6551 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006552 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 *alias = temp_string;
6554 *arg = skipwhite(p);
6555 return (int)STRLEN(temp_string);
6556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557
6558 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006559 // Only give an error when there is something, otherwise it will be
6560 // reported at a higher level.
6561 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006562 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563
6564 return len;
6565}
6566
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006567/*
6568 * Find the end of a variable or function name, taking care of magic braces.
6569 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6570 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006571 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006572 * Return a pointer to just after the name. Equal to "arg" if there is no
6573 * valid name.
6574 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006575 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006576find_name_end(
6577 char_u *arg,
6578 char_u **expr_start,
6579 char_u **expr_end,
6580 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006582 int mb_nest = 0;
6583 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006585 int len;
zeertzjqa93d9cd2023-05-02 16:25:47 +01006586 int allow_curly = (flags & FNE_ALLOW_CURLY) || !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006588 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006590 *expr_start = NULL;
6591 *expr_end = NULL;
6592 }
6593
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006594 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006595 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006596 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006597 return arg;
6598
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006599 for (p = arg; *p != NUL
6600 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006601 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006602 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006603 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006604 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006605 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006606 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006607 if (*p == '\'')
6608 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006609 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006610 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006611 ;
6612 if (*p == NUL)
6613 break;
6614 }
6615 else if (*p == '"')
6616 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006617 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006618 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006619 if (*p == '\\' && p[1] != NUL)
6620 ++p;
6621 if (*p == NUL)
6622 break;
6623 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006624 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6625 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006626 // "s:" is start of "s:var", but "n:" is not and can be used in
6627 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006628 len = (int)(p - arg);
6629 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006630 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006631 break;
6632 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006633
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006634 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006636 if (*p == '[')
6637 ++br_nest;
6638 else if (*p == ']')
6639 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006641
zeertzjqa93d9cd2023-05-02 16:25:47 +01006642 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006644 if (*p == '{')
6645 {
6646 mb_nest++;
6647 if (expr_start != NULL && *expr_start == NULL)
6648 *expr_start = p;
6649 }
6650 else if (*p == '}')
6651 {
6652 mb_nest--;
6653 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6654 *expr_end = p;
6655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 }
6658
6659 return p;
6660}
6661
6662/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006663 * Expands out the 'magic' {}'s in a variable/function name.
6664 * Note that this can call itself recursively, to deal with
6665 * constructs like foo{bar}{baz}{bam}
6666 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6667 * "in_start" ^
6668 * "expr_start" ^
6669 * "expr_end" ^
6670 * "in_end" ^
6671 *
6672 * Returns a new allocated string, which the caller must free.
6673 * Returns NULL for failure.
6674 */
6675 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006676make_expanded_name(
6677 char_u *in_start,
6678 char_u *expr_start,
6679 char_u *expr_end,
6680 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006681{
6682 char_u c1;
6683 char_u *retval = NULL;
6684 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006685
6686 if (expr_end == NULL || in_end == NULL)
6687 return NULL;
6688 *expr_start = NUL;
6689 *expr_end = NUL;
6690 c1 = *in_end;
6691 *in_end = NUL;
6692
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006693 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006694 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006695 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006696 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6697 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006698 if (retval != NULL)
6699 {
6700 STRCPY(retval, in_start);
6701 STRCAT(retval, temp_result);
6702 STRCAT(retval, expr_end + 1);
6703 }
6704 }
6705 vim_free(temp_result);
6706
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006707 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006708 *expr_start = '{';
6709 *expr_end = '}';
6710
6711 if (retval != NULL)
6712 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006713 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006714 if (expr_start != NULL)
6715 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006716 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006717 temp_result = make_expanded_name(retval, expr_start,
6718 expr_end, temp_result);
6719 vim_free(retval);
6720 retval = temp_result;
6721 }
6722 }
6723
6724 return retval;
6725}
6726
6727/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006729 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006731 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006732eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006734 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006735}
6736
6737/*
6738 * Return TRUE if character "c" can be used as the first character in a
6739 * variable or function name (excluding '{' and '}').
6740 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006741 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006742eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006743{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006744 return ASCII_ISALPHA(c) || c == '_';
6745}
6746
6747/*
6748 * Return TRUE if character "c" can be used as the first character of a
6749 * dictionary key.
6750 */
6751 int
6752eval_isdictc(int c)
6753{
6754 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755}
6756
6757/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006758 * Handle:
6759 * - expr[expr], expr[expr:expr] subscript
6760 * - ".name" lookup
6761 * - function call with Funcref variable: func(expr)
6762 * - method call: var->method()
6763 *
6764 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006765 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006766 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006767 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006768handle_subscript(
6769 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006770 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006771 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006772 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006773 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006774{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006775 int evaluate = evalarg != NULL
6776 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006777 int ret = OK;
6778 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006779 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006780 int getnext;
6781 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006782
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006783 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006784 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006785 // When at the end of the line and ".name" or "->{" or "->X" follows in
6786 // the next line then consume the line break.
6787 p = eval_next_non_blank(*arg, evalarg, &getnext);
6788 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00006789 && ((*p == '.'
6790 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
6791 || rettv->v_type == VAR_CLASS
6792 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02006793 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6794 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6795 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006796 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006797 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006798 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006799 check_white = FALSE;
6800 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006801
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006802 if (rettv->v_type == VAR_ANY)
6803 {
6804 char_u *exp_name;
6805 int cc;
6806 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006807 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006808 type_T *type;
6809
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006810 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006811 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006812 if (**arg != '.')
6813 {
6814 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006815 semsg(_(e_expected_dot_after_name_str),
6816 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006817 ret = FAIL;
6818 break;
6819 }
6820 ++*arg;
6821 if (IS_WHITE_OR_NUL(**arg))
6822 {
6823 if (verbose)
6824 emsg(_(e_no_white_space_allowed_after_dot));
6825 ret = FAIL;
6826 break;
6827 }
6828
6829 // isolate the name
6830 exp_name = *arg;
6831 while (eval_isnamec(**arg))
6832 ++*arg;
6833 cc = **arg;
6834 **arg = NUL;
6835
6836 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01006837 evalarg == NULL ? NULL : evalarg->eval_cctx,
6838 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006839 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006840
6841 if (idx < 0 && ufunc == NULL)
6842 {
6843 ret = FAIL;
6844 break;
6845 }
6846 if (idx >= 0)
6847 {
6848 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6849 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6850
6851 copy_tv(sv->sv_tv, rettv);
6852 }
6853 else
6854 {
6855 rettv->v_type = VAR_FUNC;
6856 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6857 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006858 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006859 }
6860
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006861 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6862 || rettv->v_type == VAR_PARTIAL))
6863 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006864 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006865 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6866 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006867
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006868 // Stop the expression evaluation when immediately aborting on
6869 // error, or when an interrupt occurred or an exception was thrown
6870 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006871 if (aborting())
6872 {
6873 if (ret == OK)
6874 clear_tv(rettv);
6875 ret = FAIL;
6876 }
6877 dict_unref(selfdict);
6878 selfdict = NULL;
6879 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006880 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006881 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006882 if (in_vim9script())
6883 *arg = skipwhite(p + 2);
6884 else
6885 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00006886 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006887 {
zeertzjqc481ad32023-03-11 16:18:51 +00006888 emsg(_(e_no_white_space_allowed_before_parenthesis));
6889 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006890 }
zeertzjqc481ad32023-03-11 16:18:51 +00006891 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
6892 // expr->{lambda}() or expr->(lambda)()
6893 ret = eval_lambda(arg, rettv, evalarg, verbose);
6894 else
6895 // expr->name()
6896 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02006897 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006898 // "." is ".name" lookup when we found a dict or when evaluating and
6899 // scriptversion is at least 2, where string concatenation is "..".
6900 else if (**arg == '['
6901 || (**arg == '.' && (rettv->v_type == VAR_DICT
6902 || (!evaluate
6903 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006904 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006905 {
6906 dict_unref(selfdict);
6907 if (rettv->v_type == VAR_DICT)
6908 {
6909 selfdict = rettv->vval.v_dict;
6910 if (selfdict != NULL)
6911 ++selfdict->dv_refcount;
6912 }
6913 else
6914 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006915 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006916 {
6917 clear_tv(rettv);
6918 ret = FAIL;
6919 }
6920 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006921 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
6922 || rettv->v_type == VAR_OBJECT))
6923 {
6924 // class member: SomeClass.varname
6925 // class method: SomeClass.SomeMethod()
6926 // class constructor: SomeClass.new()
6927 // object member: someObject.varname
6928 // object method: someObject.SomeMethod()
6929 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
6930 {
6931 clear_tv(rettv);
6932 ret = FAIL;
6933 }
6934 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006935 else
6936 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006937 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006938
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006939 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6940 // Don't do this when "Func" is already a partial that was bound
6941 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006942 if (selfdict != NULL
6943 && (rettv->v_type == VAR_FUNC
6944 || (rettv->v_type == VAR_PARTIAL
6945 && (rettv->vval.v_partial->pt_auto
6946 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006947 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006948
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006949 dict_unref(selfdict);
6950 return ret;
6951}
6952
6953/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006954 * Make a copy of an item.
6955 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006956 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006957 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6958 * reference to an already copied list/dict can be used.
6959 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006960 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006961 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006962item_copy(
6963 typval_T *from,
6964 typval_T *to,
6965 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006966 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006967 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006968{
6969 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006970 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006971
Bram Moolenaar33570922005-01-25 22:26:29 +00006972 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006973 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006974 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006975 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006976 }
6977 ++recurse;
6978
6979 switch (from->v_type)
6980 {
6981 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006982 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006983 case VAR_STRING:
6984 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006985 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006986 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006987 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006988 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006989 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006990 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006991 case VAR_CLASS:
6992 case VAR_OBJECT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006993 copy_tv(from, to);
6994 break;
6995 case VAR_LIST:
6996 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006997 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006998 if (from->vval.v_list == NULL)
6999 to->vval.v_list = NULL;
7000 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7001 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007002 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007003 to->vval.v_list = from->vval.v_list->lv_copylist;
7004 ++to->vval.v_list->lv_refcount;
7005 }
7006 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007007 to->vval.v_list = list_copy(from->vval.v_list,
7008 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007009 if (to->vval.v_list == NULL)
7010 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007011 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007012 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007013 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007014 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007015 case VAR_DICT:
7016 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007017 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007018 if (from->vval.v_dict == NULL)
7019 to->vval.v_dict = NULL;
7020 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7021 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007022 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007023 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7024 ++to->vval.v_dict->dv_refcount;
7025 }
7026 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007027 to->vval.v_dict = dict_copy(from->vval.v_dict,
7028 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007029 if (to->vval.v_dict == NULL)
7030 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007031 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007032 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007033 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007034 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007035 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007036 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007037 }
7038 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007039 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007040}
7041
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007042 void
7043echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7044{
7045 char_u *tofree;
7046 char_u numbuf[NUMBUFLEN];
7047 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7048
7049 if (*atstart)
7050 {
7051 *atstart = FALSE;
7052 // Call msg_start() after eval1(), evaluating the expression
7053 // may cause a message to appear.
7054 if (with_space)
7055 {
7056 // Mark the saved text as finishing the line, so that what
7057 // follows is displayed on a new line when scrolling back
7058 // at the more prompt.
7059 msg_sb_eol();
7060 msg_start();
7061 }
7062 }
7063 else if (with_space)
7064 msg_puts_attr(" ", echo_attr);
7065
7066 if (p != NULL)
7067 for ( ; *p != NUL && !got_int; ++p)
7068 {
7069 if (*p == '\n' || *p == '\r' || *p == TAB)
7070 {
7071 if (*p != TAB && *needclr)
7072 {
7073 // remove any text still there from the command
7074 msg_clr_eos();
7075 *needclr = FALSE;
7076 }
7077 msg_putchar_attr(*p, echo_attr);
7078 }
7079 else
7080 {
7081 if (has_mbyte)
7082 {
7083 int i = (*mb_ptr2len)(p);
7084
7085 (void)msg_outtrans_len_attr(p, i, echo_attr);
7086 p += i - 1;
7087 }
7088 else
7089 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7090 }
7091 }
7092 vim_free(tofree);
7093}
7094
Bram Moolenaare9a41262005-01-15 22:18:47 +00007095/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 * ":echo expr1 ..." print each argument separated with a space, add a
7097 * newline at the end.
7098 * ":echon expr1 ..." print each argument plain.
7099 */
7100 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007101ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102{
7103 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007104 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007105 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 int needclr = TRUE;
7107 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007108 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007109 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007110 evalarg_T evalarg;
7111
Bram Moolenaare707c882020-07-01 13:07:37 +02007112 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113
7114 if (eap->skip)
7115 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007116 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007118 // If eval1() causes an error message the text from the command may
7119 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007120 need_clr_eos = needclr;
7121
Bram Moolenaar68db9962021-05-09 23:19:22 +02007122 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007123 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 {
7125 /*
7126 * Report the invalid expression unless the expression evaluation
7127 * has been cancelled due to an aborting error, an interrupt, or an
7128 * exception.
7129 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007130 if (!aborting() && did_emsg == did_emsg_before
7131 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007132 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007133 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 break;
7135 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007136 need_clr_eos = FALSE;
7137
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007139 {
7140 if (rettv.v_type == VAR_VOID)
7141 {
7142 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7143 break;
7144 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007145 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007146 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007147
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007148 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 arg = skipwhite(arg);
7150 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007151 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007152 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153
7154 if (eap->skip)
7155 --emsg_skip;
7156 else
7157 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007158 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 if (needclr)
7160 msg_clr_eos();
7161 if (eap->cmdidx == CMD_echo)
7162 msg_end();
7163 }
7164}
7165
7166/*
7167 * ":echohl {name}".
7168 */
7169 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007170ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007172 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173}
7174
7175/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007176 * Returns the :echo attribute
7177 */
7178 int
7179get_echo_attr(void)
7180{
7181 return echo_attr;
7182}
7183
7184/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 * ":execute expr1 ..." execute the result of an expression.
7186 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007187 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007189 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 * Each gets spaces around each argument and a newline at the end for
7191 * echo commands
7192 */
7193 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007194ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195{
7196 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007197 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 int ret = OK;
7199 char_u *p;
7200 garray_T ga;
7201 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007202 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203
7204 ga_init2(&ga, 1, 80);
7205
7206 if (eap->skip)
7207 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007208 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007210 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007211 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213
7214 if (!eap->skip)
7215 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007216 char_u buf[NUMBUFLEN];
7217
7218 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007219 {
7220 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7221 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007222 semsg(_(e_using_invalid_value_as_string_str),
7223 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007224 p = NULL;
7225 }
7226 else
7227 p = tv_get_string_buf(&rettv, buf);
7228 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007229 else
7230 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007231 if (p == NULL)
7232 {
7233 clear_tv(&rettv);
7234 ret = FAIL;
7235 break;
7236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 len = (int)STRLEN(p);
7238 if (ga_grow(&ga, len + 2) == FAIL)
7239 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007240 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 ret = FAIL;
7242 break;
7243 }
7244 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 ga.ga_len += len;
7248 }
7249
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007250 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 arg = skipwhite(arg);
7252 }
7253
7254 if (ret != FAIL && ga.ga_data != NULL)
7255 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007256 // use the first line of continuation lines for messages
7257 SOURCING_LNUM = start_lnum;
7258
Bram Moolenaar37fef162022-08-29 18:16:32 +01007259 if (eap->cmdidx == CMD_echomsg
7260 || eap->cmdidx == CMD_echowindow
7261 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007262 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007263 // Mark the already saved text as finishing the line, so that what
7264 // follows is displayed on a new line when scrolling back at the
7265 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007266 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007267 }
7268
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007269 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007270 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007271 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007272 out_flush();
7273 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007274 else if (eap->cmdidx == CMD_echowindow)
7275 {
7276#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007277 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007278#endif
7279 msg_attr(ga.ga_data, echo_attr);
7280#ifdef HAS_MESSAGE_WINDOW
7281 end_echowindow();
7282#endif
7283 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007284 else if (eap->cmdidx == CMD_echoconsole)
7285 {
7286 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7287 ui_write((char_u *)"\r\n", 2, TRUE);
7288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 else if (eap->cmdidx == CMD_echoerr)
7290 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007291 int save_did_emsg = did_emsg;
7292
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007293 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007294 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 if (!force_abort)
7296 did_emsg = save_did_emsg;
7297 }
7298 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007299 {
7300 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7301
7302 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7303 sticky_cmdmod_flags = cmdmod.cmod_flags
7304 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 do_cmdline((char_u *)ga.ga_data,
7306 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007307 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 }
7310
7311 ga_clear(&ga);
7312
7313 if (eap->skip)
7314 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007315 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316}
7317
7318/*
7319 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7320 * "arg" points to the "&" or '+' when called, to "option" when returning.
7321 * Returns NULL when no option name found. Otherwise pointer to the char
7322 * after the option name.
7323 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007324 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007325find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326{
7327 char_u *p = *arg;
7328
7329 ++p;
7330 if (*p == 'g' && p[1] == ':')
7331 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007332 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 p += 2;
7334 }
7335 else if (*p == 'l' && p[1] == ':')
7336 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007337 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 p += 2;
7339 }
7340 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007341 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342
7343 if (!ASCII_ISALPHA(*p))
7344 return NULL;
7345 *arg = p;
7346
7347 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007348 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 else
7350 while (ASCII_ISALPHA(*p))
7351 ++p;
7352 return p;
7353}
7354
7355/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007356 * Display script name where an item was last set.
7357 * Should only be invoked when 'verbose' is non-zero.
7358 */
7359 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007360last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007361{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007362 char_u *p;
7363
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007364 if (script_ctx.sc_sid == 0)
7365 return;
7366
7367 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7368 if (p == NULL)
7369 return;
7370
7371 verbose_enter();
7372 msg_puts(_("\n\tLast set from "));
7373 msg_puts((char *)p);
7374 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007375 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007376 msg_puts(_(line_msg));
7377 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007378 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007379 verbose_leave();
7380 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007381}
7382
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007383#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384
7385/*
7386 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007387 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 * "flags" can be "g" to do a global substitute.
7389 * Returns an allocated string, NULL for error.
7390 */
7391 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007392do_string_sub(
7393 char_u *str,
7394 char_u *pat,
7395 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007396 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007397 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398{
7399 int sublen;
7400 regmatch_T regmatch;
7401 int i;
7402 int do_all;
7403 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007404 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 garray_T ga;
7406 char_u *ret;
7407 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007408 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007410 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007412 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413
7414 ga_init2(&ga, 1, 200);
7415
7416 do_all = (flags[0] == 'g');
7417
7418 regmatch.rm_ic = p_ic;
7419 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7420 if (regmatch.regprog != NULL)
7421 {
7422 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007423 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7425 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007426 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007427 if (regmatch.startp[0] == regmatch.endp[0])
7428 {
7429 if (zero_width == regmatch.startp[0])
7430 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007431 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007432 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007433 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7434 (size_t)i);
7435 ga.ga_len += i;
7436 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007437 continue;
7438 }
7439 zero_width = regmatch.startp[0];
7440 }
7441
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 /*
7443 * Get some space for a temporary buffer to do the substitution
7444 * into. It will contain:
7445 * - The text up to where the match is.
7446 * - The substituted text.
7447 * - The text after the match.
7448 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007449 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007450 if (sublen <= 0)
7451 {
7452 ga_clear(&ga);
7453 break;
7454 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007455 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7457 {
7458 ga_clear(&ga);
7459 break;
7460 }
7461
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007462 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 i = (int)(regmatch.startp[0] - tail);
7464 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007465 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007466 (void)vim_regsub(&regmatch, sub, expr,
7467 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7468 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007470 tail = regmatch.endp[0];
7471 if (*tail == NUL)
7472 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 if (!do_all)
7474 break;
7475 }
7476
7477 if (ga.ga_data != NULL)
7478 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7479
Bram Moolenaar473de612013-06-08 18:19:48 +02007480 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 }
7482
7483 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7484 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007485 if (p_cpo == empty_option)
7486 p_cpo = save_cpo;
7487 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007488 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007489 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007490 // If it's still empty it was changed and restored, need to restore in
7491 // the complicated way.
7492 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007493 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007494 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496
7497 return ret;
7498}