blob: a0bbabfe3c28d07fc5454a56d4cd27c6292c73eb [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]".
Bram Moolenaar82418262022-09-28 16:16:15 +0100255 * "fc_arg" is from eval_expr_get_funccal() or NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100256 * Return the result in "rettv" and OK or FAIL.
257 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200258 int
Bram Moolenaar82418262022-09-28 16:16:15 +0100259eval_expr_typval(
260 typval_T *expr,
261 typval_T *argv,
262 int argc,
263 funccall_T *fc_arg,
264 typval_T *rettv)
Bram Moolenaar48570482017-10-30 21:48:41 +0100265{
266 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100267 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200268 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100269
270 if (expr->v_type == VAR_FUNC)
271 {
272 s = expr->vval.v_string;
273 if (s == NULL || *s == NUL)
274 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200275 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000276 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200277 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100278 return FAIL;
279 }
280 else if (expr->v_type == VAR_PARTIAL)
281 {
282 partial_T *partial = expr->vval.v_partial;
283
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200284 if (partial == NULL)
285 return FAIL;
286
Bram Moolenaar822ba242020-05-24 23:00:18 +0200287 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200288 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100289 {
Bram Moolenaar82418262022-09-28 16:16:15 +0100290 funccall_T *fc = fc_arg != NULL ? fc_arg
291 : create_funccal(partial->pt_func, rettv);
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100292 int r;
293
294 if (fc == NULL)
295 return FAIL;
296
Bram Moolenaar69082912022-09-22 21:35:19 +0100297 // Shortcut to call a compiled function with minimal overhead.
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100298 r = call_def_function(partial->pt_func, argc, argv,
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000299 DEF_USE_PT_ARGV, partial, NULL, fc, rettv);
Bram Moolenaar82418262022-09-28 16:16:15 +0100300 if (fc_arg == NULL)
301 remove_funccal();
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100302 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100303 return FAIL;
304 }
305 else
306 {
307 s = partial_name(partial);
308 if (s == NULL || *s == NUL)
309 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200310 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000311 funcexe.fe_evaluate = TRUE;
312 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100313 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
314 return FAIL;
315 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100316 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200317 else if (expr->v_type == VAR_INSTR)
318 {
319 return exe_typval_instr(expr, rettv);
320 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100321 else
322 {
Bram Moolenaarfc4c4482022-01-26 21:17:04 +0000323 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
Bram Moolenaar48570482017-10-30 21:48:41 +0100324 if (s == NULL)
325 return FAIL;
326 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200327 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100328 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200329 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100330 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200331 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200332 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100333 return FAIL;
334 }
335 }
336 return OK;
337}
338
339/*
340 * Like eval_to_bool() but using a typval_T instead of a string.
341 * Works for string, funcref and partial.
342 */
343 int
344eval_expr_to_bool(typval_T *expr, int *error)
345{
346 typval_T rettv;
347 int res;
348
Bram Moolenaar82418262022-09-28 16:16:15 +0100349 if (eval_expr_typval(expr, NULL, 0, NULL, &rettv) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100350 {
351 *error = TRUE;
352 return FALSE;
353 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200354 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100355 clear_tv(&rettv);
356 return res;
357}
358
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359/*
360 * Top level evaluation function, returning a string. If "skip" is TRUE,
361 * only parsing to "nextcmd" is done, without reporting errors. Return
362 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
363 */
364 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100365eval_to_string_skip(
366 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200367 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100368 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369{
Bram Moolenaar33570922005-01-25 22:26:29 +0000370 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200372 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373
Bram Moolenaar37c83712020-06-30 21:18:36 +0200374 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 if (skip)
376 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200377 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 retval = NULL;
379 else
380 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100381 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000382 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 }
384 if (skip)
385 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200386 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387
388 return retval;
389}
390
391/*
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100392 * Initialize "evalarg" for use.
393 */
394 void
395init_evalarg(evalarg_T *evalarg)
396{
397 CLEAR_POINTER(evalarg);
398 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
399}
400
401/*
402 * If "evalarg->eval_tofree" is not NULL free it later.
403 * Caller is expected to overwrite "evalarg->eval_tofree" next.
404 */
405 static void
406free_eval_tofree_later(evalarg_T *evalarg)
407{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000408 if (evalarg->eval_tofree == NULL)
409 return;
410
411 if (ga_grow(&evalarg->eval_tofree_ga, 1) == OK)
412 ((char_u **)evalarg->eval_tofree_ga.ga_data)
413 [evalarg->eval_tofree_ga.ga_len++]
414 = evalarg->eval_tofree;
415 else
416 vim_free(evalarg->eval_tofree);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100417}
418
419/*
420 * After using "evalarg" filled from "eap": free the memory.
421 */
422 void
423clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
424{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000425 if (evalarg == NULL)
426 return;
427
428 garray_T *etga = &evalarg->eval_tofree_ga;
429
430 if (evalarg->eval_tofree != NULL || evalarg->eval_using_cmdline)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100431 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000432 if (eap != NULL)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100433 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000434 // We may need to keep the original command line, e.g. for
435 // ":let" it has the variable names. But we may also need
436 // the new one, "nextcmd" points into it. Keep both.
437 vim_free(eap->cmdline_tofree);
438 eap->cmdline_tofree = *eap->cmdlinep;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100439
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000440 if (evalarg->eval_using_cmdline && etga->ga_len > 0)
441 {
442 // "nextcmd" points into the last line in eval_tofree_ga,
443 // need to keep it around.
444 --etga->ga_len;
445 *eap->cmdlinep = ((char_u **)etga->ga_data)[etga->ga_len];
446 vim_free(evalarg->eval_tofree);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100447 }
448 else
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000449 *eap->cmdlinep = evalarg->eval_tofree;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100450 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000451 else
452 vim_free(evalarg->eval_tofree);
453 evalarg->eval_tofree = NULL;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100454 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000455
456 ga_clear_strings(etga);
457 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100458}
459
460/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000461 * Skip over an expression at "*pp".
462 * Return FAIL for an error, OK otherwise.
463 */
464 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200465skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000466{
Bram Moolenaar33570922005-01-25 22:26:29 +0000467 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000468
469 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200470 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000471}
472
473/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200474 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200475 * If in Vim9 script and line breaks are encountered, the lines are
476 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200477 * "arg" is advanced to just after the expression.
478 * "start" is set to the start of the expression, "end" to just after the end.
479 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200480 * Return FAIL for an error, OK otherwise.
481 */
482 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200483skip_expr_concatenate(
484 char_u **arg,
485 char_u **start,
486 char_u **end,
487 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200488{
489 typval_T rettv;
490 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200491 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200492 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200493 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200494 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200495 int evaluate = evalarg == NULL
496 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200497
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200498 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200499 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200500 {
501 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200502 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200503 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200504 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200505 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200506 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200507 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200508
509 // Don't evaluate the expression.
510 if (evalarg != NULL)
511 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200512 *arg = skipwhite(*arg);
513 res = eval1(arg, &rettv, evalarg);
514 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200515 if (evalarg != NULL)
516 evalarg->eval_flags = save_flags;
517
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200518 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200519 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200520 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200521 if (evalarg->eval_ga.ga_len == 1)
522 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200523 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200524 ga_clear(gap);
525 gap->ga_itemsize = 0;
526 }
527 else
528 {
529 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200530 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200531
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200532 // Line breaks encountered, concatenate all the lines.
533 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200534 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200535
536 // free the lines only when using getsourceline()
537 if (evalarg->eval_cookie != NULL)
538 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200539 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200540 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200541 // Do not free the last line, "arg" points into it, free it
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100542 // later. Also free "eval_tofree" later if needed.
543 free_eval_tofree_later(evalarg);
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200544 evalarg->eval_tofree =
545 ((char_u **)gap->ga_data)[gap->ga_len - 1];
546 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200547 ga_clear_strings(gap);
548 }
549 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200550 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200551 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200552
553 // free lines that were explicitly marked for freeing
554 ga_clear_strings(freegap);
555 }
556
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200557 gap->ga_itemsize = 0;
558 if (p == NULL)
559 return FAIL;
560 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200561 vim_free(evalarg->eval_tofree_lambda);
562 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200563 // Compute "end" relative to the end.
564 *end = *start + STRLEN(*start) - endoff;
565 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200566 }
567
568 return res;
569}
570
571/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100572 * Convert "tv" to a string.
573 * When "convert" is TRUE convert a List into a sequence of lines and convert
574 * a Float to a String.
575 * Returns an allocated string (NULL when out of memory).
576 */
577 char_u *
578typval2string(typval_T *tv, int convert)
579{
580 garray_T ga;
581 char_u *retval;
Bram Moolenaar883cf972021-01-15 18:04:43 +0100582 char_u numbuf[NUMBUFLEN];
Bram Moolenaar883cf972021-01-15 18:04:43 +0100583
584 if (convert && tv->v_type == VAR_LIST)
585 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000586 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100587 if (tv->vval.v_list != NULL)
588 {
589 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
590 if (tv->vval.v_list->lv_len > 0)
591 ga_append(&ga, NL);
592 }
593 ga_append(&ga, NUL);
594 retval = (char_u *)ga.ga_data;
595 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100596 else if (convert && tv->v_type == VAR_FLOAT)
597 {
598 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
599 retval = vim_strsave(numbuf);
600 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100601 else
602 retval = vim_strsave(tv_get_string(tv));
603 return retval;
604}
605
606/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200607 * Top level evaluation function, returning a string. Does not handle line
608 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000609 * When "convert" is TRUE convert a List into a sequence of lines and convert
610 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 * Return pointer to allocated memory, or NULL for failure.
612 */
613 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100614eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100615 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100616 int convert,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100617 exarg_T *eap,
618 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619{
Bram Moolenaar33570922005-01-25 22:26:29 +0000620 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100622 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100623 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100625 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100626 if (use_simple_function)
627 r = eval0_simple_funccal(arg, &tv, NULL, &evalarg);
628 else
629 r = eval0(arg, &tv, NULL, &evalarg);
630 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 retval = NULL;
632 else
633 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100634 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000635 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100637 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638
639 return retval;
640}
641
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100642 char_u *
643eval_to_string(
644 char_u *arg,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100645 int convert,
646 int use_simple_function)
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100647{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100648 return eval_to_string_eap(arg, convert, NULL, use_simple_function);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100649}
650
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000652 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100653 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200654 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 */
656 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100657eval_to_string_safe(
658 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000659 int use_sandbox,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100660 int keep_script_version,
661 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662{
663 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200664 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200665 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200666 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
Bram Moolenaar9530b582022-01-22 13:39:08 +0000668 if (!keep_script_version)
669 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200670 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000671 if (use_sandbox)
672 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100673 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200674 may_garbage_collect = FALSE;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100675 retval = eval_to_string(arg, FALSE, use_simple_function);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000676 if (use_sandbox)
677 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100678 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200679 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200680 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200681 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 return retval;
683}
684
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685/*
686 * Top level evaluation function, returning a number.
687 * Evaluates "expr" silently.
688 * Returns -1 for an error.
689 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200690 varnumber_T
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100691eval_to_number(char_u *expr, int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692{
Bram Moolenaar33570922005-01-25 22:26:29 +0000693 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200694 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000695 char_u *p = skipwhite(expr);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100696 int r = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697
698 ++emsg_off;
699
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100700 if (use_simple_function)
701 r = may_call_simple_func(expr, &rettv);
702 if (r == NOTDONE)
703 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
704 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 retval = -1;
706 else
707 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100708 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000709 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 }
711 --emsg_off;
712
713 return retval;
714}
715
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000716/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000717 * Top level evaluation function.
718 * Returns an allocated typval_T with the result.
719 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000720 */
721 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200722eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000723{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100724 return eval_expr_ext(arg, eap, FALSE);
725}
726
727 typval_T *
728eval_expr_ext(char_u *arg, exarg_T *eap, int use_simple_function)
729{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000730 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200731 evalarg_T evalarg;
732
733 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000734
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200735 tv = ALLOC_ONE(typval_T);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100736 if (tv != NULL)
737 {
738 int r = NOTDONE;
739
740 if (use_simple_function)
741 r = eval0_simple_funccal(arg, tv, eap, &evalarg);
742 if (r == NOTDONE)
743 r = eval0(arg, tv, eap, &evalarg);
744
745 if (r == FAIL)
746 VIM_CLEAR(tv);
747 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000748
Bram Moolenaar37c83712020-06-30 21:18:36 +0200749 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000750 return tv;
751}
752
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000754 * "*arg" points to what can be a function name in the form of "import.Name" or
755 * "Funcref". Return the name of the function. Set "tofree" to something that
756 * was allocated.
757 * If "verbose" is FALSE no errors are given.
758 * Return NULL for any failure.
759 */
760 static char_u *
761deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000762 char_u **arg,
763 char_u **tofree,
764 evalarg_T *evalarg,
765 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000766{
767 typval_T ref;
768 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100769 int save_flags = 0;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000770
771 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100772 if (evalarg != NULL)
773 {
774 // need to evaluate this to get an import, like in "a.Func"
775 save_flags = evalarg->eval_flags;
776 evalarg->eval_flags |= EVAL_EVALUATE;
777 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100778 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100779 {
780 dictitem_T *v;
781
782 // If <SID>VarName was used it would not be found, try another way.
783 v = find_var_also_in_script(name, NULL, FALSE);
784 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100785 {
786 name = NULL;
787 goto theend;
788 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100789 copy_tv(&v->di_tv, &ref);
790 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000791 if (*skipwhite(*arg) != NUL)
792 {
793 if (verbose)
794 semsg(_(e_trailing_characters_str), *arg);
795 name = NULL;
796 }
797 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
798 {
799 name = ref.vval.v_string;
800 ref.vval.v_string = NULL;
801 *tofree = name;
802 }
803 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
804 {
805 if (ref.vval.v_partial->pt_argc > 0
806 || ref.vval.v_partial->pt_dict != NULL)
807 {
808 if (verbose)
809 emsg(_(e_cannot_use_partial_here));
810 name = NULL;
811 }
812 else
813 {
814 name = vim_strsave(partial_name(ref.vval.v_partial));
815 *tofree = name;
816 }
817 }
818 else
819 {
820 if (verbose)
821 semsg(_(e_not_callable_type_str), name);
822 name = NULL;
823 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100824
825theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000826 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100827 if (evalarg != NULL)
828 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000829 return name;
830}
831
832/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100833 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200834 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
835 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000836 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200838 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100839call_vim_function(
840 char_u *func,
841 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200842 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200843 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000845 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200846 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000847 char_u *arg;
848 char_u *name;
849 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000850 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100852 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200853 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000854 funcexe.fe_firstline = curwin->w_cursor.lnum;
855 funcexe.fe_lastline = curwin->w_cursor.lnum;
856 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000857
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000858 // The name might be "import.Func" or "Funcref". We don't know, we need to
859 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000860 // autoload script has errors. Guess that when there is a dot in the name
861 // showing errors is the right choice.
862 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000863 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000864 if (ignore_errors)
865 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000866 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000867 if (ignore_errors)
868 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000869 if (name == NULL)
870 name = func;
871
872 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
873
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000874 if (ret == FAIL)
875 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000876 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000877
878 return ret;
879}
880
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100881/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100882 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000883 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
884 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000885 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000886 */
887 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100888call_func_retstr(
889 char_u *func,
890 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200891 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000892{
893 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000894 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000895
Bram Moolenaarded27a12018-08-01 19:06:03 +0200896 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000897 return NULL;
898
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100899 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000900 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 return retval;
902}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000903
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000904/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100905 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000906 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000907 * Returns NULL when there is something wrong.
Bram Moolenaar55e93662022-09-10 13:52:26 +0100908 * Gives an error when the returned value is not a list.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000909 */
910 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100911call_func_retlist(
912 char_u *func,
913 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200914 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000915{
916 typval_T rettv;
917
Bram Moolenaarded27a12018-08-01 19:06:03 +0200918 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000919 return NULL;
920
921 if (rettv.v_type != VAR_LIST)
922 {
Bram Moolenaar55e93662022-09-10 13:52:26 +0100923 semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
924 vartype_name(rettv.v_type));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000925 clear_tv(&rettv);
926 return NULL;
927 }
928
929 return rettv.vval.v_list;
930}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931
Bram Moolenaare70dd112022-01-21 16:31:11 +0000932#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100934 * Evaluate "arg", which is 'foldexpr'.
935 * Note: caller must set "curwin" to match "arg".
936 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
937 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 */
939 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000940eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000942 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000943 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200944 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000946 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000947 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100948 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100950 arg = skipwhite(wp->w_p_fde);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000951 current_sctx = wp->w_p_script_ctx[WV_FDE];
952
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000954 if (use_sandbox)
955 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100956 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 *cp = NUL;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100958
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100959 // Evaluate the expression. If the expression is "FuncName()" call the
960 // function directly.
961 if (eval0_simple_funccal(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 retval = 0;
963 else
964 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100965 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000966 if (tv.v_type == VAR_NUMBER)
967 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000968 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 retval = 0;
970 else
971 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100972 // If the result is a string, check if there is a non-digit before
973 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000974 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 if (!VIM_ISDIGIT(*s) && *s != '-')
976 *cp = *s++;
977 retval = atol((char *)s);
978 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000979 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 }
981 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000982 if (use_sandbox)
983 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100984 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200985 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000986 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200988 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989}
990#endif
991
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000993 * Get an lval: variable, Dict item or List item that can be assigned a value
994 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
995 * "name.key", "name.key[expr]" etc.
996 * Indexing only works if "name" is an existing List or Dictionary.
997 * "name" points to the start of the name.
998 * If "rettv" is not NULL it points to the value to be assigned.
999 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1000 * wrong; must end in space or cmd separator.
1001 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001002 * flags:
1003 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001004 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001005 * GLV_NO_AUTOLOAD: do not use script autoloading
1006 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001007 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001008 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001009 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001010 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001011 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001012get_lval(
1013 char_u *name,
1014 typval_T *rettv,
1015 lval_T *lp,
1016 int unlet,
1017 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001018 int flags, // GLV_ values
1019 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001020{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001021 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001022 char_u *expr_start, *expr_end;
1023 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001024 dictitem_T *v;
1025 typval_T var1;
1026 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001027 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001028 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001029 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01001030 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001031 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001032 int writing;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001033 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001034
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001035 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02001036 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001037
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001038 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001039 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001040 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001041 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02001042 lp->ll_name_end = find_name_end(name, NULL, NULL,
1043 FNE_INCL_BR | fne_flags);
1044 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001045 }
1046
Bram Moolenaara749a422022-02-12 19:52:25 +00001047 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001048 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00001049 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
1050 {
1051 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
1052 return NULL;
1053 }
1054
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001055 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001056 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001057 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001058 if (expr_start != NULL)
1059 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001060 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01001061 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001062 && *p != '[' && *p != '.')
1063 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001064 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001065 return NULL;
1066 }
1067
1068 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1069 if (lp->ll_exp_name == NULL)
1070 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001071 // Report an invalid expression in braces, unless the
1072 // expression evaluation has been cancelled due to an
1073 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001074 if (!aborting() && !quiet)
1075 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001076 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001077 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001078 return NULL;
1079 }
1080 }
1081 lp->ll_name = lp->ll_exp_name;
1082 }
1083 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001085 lp->ll_name = name;
1086
Bram Moolenaar4525a572022-02-13 11:57:33 +00001087 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001089 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001090 // However, "g:[key]" is indexing a dictionary.
1091 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001092 {
1093 --p;
1094 lp->ll_name_end = p;
1095 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001096 if (*skipwhite(p) == ':')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001097 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001098 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001099
Bram Moolenaar9510d222022-09-11 15:14:05 +01001100 if (is_scoped_variable(name))
1101 {
1102 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1103 return NULL;
1104 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001105 if (VIM_ISWHITE(*p))
1106 {
1107 semsg(_(e_no_white_space_allowed_before_colon_str), p);
1108 return NULL;
1109 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001110 if (tp == p + 1 && !quiet)
1111 {
1112 semsg(_(e_white_space_required_after_str_str), ":", p);
1113 return NULL;
1114 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001115 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001116 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001117 semsg(_(e_using_type_not_in_script_context_str), p);
1118 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001119 }
1120
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001121 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001122 lp->ll_type = parse_type(&tp,
1123 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1124 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001125 if (lp->ll_type == NULL && !quiet)
1126 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001127 lp->ll_name_end = tp;
1128 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001129 }
1130 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001131 if (lp->ll_name == NULL)
1132 return p;
1133
Bram Moolenaar62aec932022-01-29 21:45:34 +00001134 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001135 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001136 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001137
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001138 if (import != NULL)
1139 {
1140 ufunc_T *ufunc;
1141 type_T *type;
1142
Bram Moolenaar753885b2022-08-24 16:30:36 +01001143 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001144 lp->ll_sid = import->imp_sid;
1145 lp->ll_name = skipwhite(p + 1);
1146 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1147 lp->ll_name_end = p;
1148
1149 // check the item is exported
1150 cc = *p;
1151 *p = NUL;
1152 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001153 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001154 {
1155 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001156 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001157 }
1158 *p = cc;
1159 }
1160 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001161
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001162 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001163 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001164 return p;
1165
Bram Moolenaar4525a572022-02-13 11:57:33 +00001166 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001167 {
1168 // using local variable
1169 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001170 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001171 }
1172 else
1173 {
1174 cc = *p;
1175 *p = NUL;
1176 // When we would write to the variable pass &ht and prevent autoload.
1177 writing = !(flags & GLV_READ_ONLY);
1178 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001179 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001180 if (v == NULL && !quiet)
1181 semsg(_(e_undefined_variable_str), lp->ll_name);
1182 *p = cc;
1183 if (v == NULL)
1184 return NULL;
1185 lp->ll_tv = &v->di_tv;
1186 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001187
Bram Moolenaar4525a572022-02-13 11:57:33 +00001188 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001189 {
1190 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001191 semsg(_(e_variable_already_declared_str), lp->ll_name);
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001192 return NULL;
1193 }
1194
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001195 /*
1196 * Loop until no more [idx] or .key is following.
1197 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001198 var1.v_type = VAR_UNKNOWN;
1199 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001200 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001201 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001202 vartype_T v_type = lp->ll_tv->v_type;
1203
1204 if (*p == '.' && v_type != VAR_DICT
1205 && v_type != VAR_OBJECT
1206 && v_type != VAR_CLASS)
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001207 {
1208 if (!quiet)
1209 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1210 return NULL;
1211 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001212 if (v_type != VAR_LIST
1213 && v_type != VAR_DICT
1214 && v_type != VAR_BLOB
1215 && v_type != VAR_OBJECT
1216 && v_type != VAR_CLASS)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001217 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001218 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001219 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001220 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001221 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001222
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001223 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001224 int r = OK;
Bram Moolenaard505d172022-12-18 21:42:55 +00001225 if (v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001226 r = rettv_list_alloc(lp->ll_tv);
Bram Moolenaard505d172022-12-18 21:42:55 +00001227 else if (v_type == VAR_BLOB
Bram Moolenaare65081d2021-06-27 15:04:05 +02001228 && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001229 r = rettv_blob_alloc(lp->ll_tv);
1230 if (r == FAIL)
1231 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001232
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001233 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001234 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001235 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001236 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001237 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001238 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001239
Bram Moolenaar4525a572022-02-13 11:57:33 +00001240 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001241 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001242 && lp->ll_tv == &v->di_tv
1243 && ht != NULL && ht == get_script_local_ht())
1244 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001245 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001246
1247 // Vim9 script local variable: get the type
1248 if (sv != NULL)
1249 lp->ll_valtype = sv->sv_type;
1250 }
1251
Bram Moolenaar8c711452005-01-14 21:53:12 +00001252 len = -1;
1253 if (*p == '.')
1254 {
1255 key = p + 1;
1256 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1257 ;
1258 if (len == 0)
1259 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001260 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001261 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001262 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001263 }
1264 p = key + len;
1265 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001266 else
1267 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001268 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001269 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001270 if (*p == ':')
1271 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001272 else
1273 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001274 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001275 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001276 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001277 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001278 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001279 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001280 clear_tv(&var1);
1281 return NULL;
1282 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001283 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001284 }
1285
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001286 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001287 if (*p == ':')
1288 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001289 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001290 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001291 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001292 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001293 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001294 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001295 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001296 if (rettv != NULL
1297 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001298 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001299 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001300 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001301 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001302 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001303 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001304 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001305 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001306 }
1307 p = skipwhite(p + 1);
1308 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001309 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001310 else
1311 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001312 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001313 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001314 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001315 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001316 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001317 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001318 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001319 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001320 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001321 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001322 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001323 clear_tv(&var2);
1324 return NULL;
1325 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001326 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001327 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001328 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001329 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001330 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001331
Bram Moolenaar8c711452005-01-14 21:53:12 +00001332 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001333 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001334 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001335 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001336 clear_tv(&var1);
1337 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001338 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001339 }
1340
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001341 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001342 ++p;
1343 }
1344
Bram Moolenaard505d172022-12-18 21:42:55 +00001345 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001346 {
1347 if (len == -1)
1348 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001349 // "[key]": get key from "var1"
1350 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001351 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001352 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001353 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001354 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001355 }
1356 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001357 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001358
1359 // a NULL dict is equivalent with an empty dict
1360 if (lp->ll_tv->vval.v_dict == NULL)
1361 {
1362 lp->ll_tv->vval.v_dict = dict_alloc();
1363 if (lp->ll_tv->vval.v_dict == NULL)
1364 {
1365 clear_tv(&var1);
1366 return NULL;
1367 }
1368 ++lp->ll_tv->vval.v_dict->dv_refcount;
1369 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001370 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001371
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001372 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001373
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001374 // When assigning to a scope dictionary check that a function and
1375 // variable name is valid (only variable name unless it is l: or
1376 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001377 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001378 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001379 int prevval;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001380
1381 if (len != -1)
1382 {
1383 prevval = key[len];
1384 key[len] = NUL;
1385 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001386 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001387 prevval = 0; // avoid compiler warning
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001388 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
zeertzjq91c75d12022-11-05 20:21:58 +00001389 && (rettv->v_type == VAR_FUNC
1390 || rettv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001391 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001392 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001393 if (len != -1)
1394 key[len] = prevval;
1395 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001396 {
1397 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001398 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001399 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001400 }
1401
Bram Moolenaar10c65862020-10-08 21:16:42 +02001402 if (lp->ll_valtype != NULL)
1403 // use the type of the member
1404 lp->ll_valtype = lp->ll_valtype->tt_member;
1405
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001406 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001407 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001408 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001409 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001410 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001411 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001412 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001413 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001414 return NULL;
1415 }
1416
Bram Moolenaar31b81602019-02-10 22:14:27 +01001417 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001418 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001419 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001420 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001421 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001422 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001423 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001424 }
1425 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001426 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001427 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001428 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001429 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001430 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001431 p = NULL;
1432 break;
1433 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001434 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001435 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001436 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1437 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001438 {
1439 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001440 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001441 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001442
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001443 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001444 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001445 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001446 else if (v_type == VAR_BLOB)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001447 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001448 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1449
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001450 /*
1451 * Get the number and item for the only or first index of the List.
1452 */
1453 if (empty1)
1454 lp->ll_n1 = 0;
1455 else
1456 // is number or string
1457 lp->ll_n1 = (long)tv_get_number(&var1);
1458 clear_tv(&var1);
1459
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001460 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001461 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001462 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001463 return NULL;
1464 }
1465 if (lp->ll_range && !lp->ll_empty2)
1466 {
1467 lp->ll_n2 = (long)tv_get_number(&var2);
1468 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001469 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1470 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001471 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001472 }
1473 lp->ll_blob = lp->ll_tv->vval.v_blob;
1474 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001475 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001476 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001477 else if (v_type == VAR_LIST)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001478 {
1479 /*
1480 * Get the number and item for the only or first index of the List.
1481 */
1482 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001483 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001484 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001485 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001486 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001487 clear_tv(&var1);
1488
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001489 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001490 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001491 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1492 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001493 if (lp->ll_li == NULL)
1494 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001495 clear_tv(&var2);
1496 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001497 }
1498
Bram Moolenaar10c65862020-10-08 21:16:42 +02001499 if (lp->ll_valtype != NULL)
1500 // use the type of the member
1501 lp->ll_valtype = lp->ll_valtype->tt_member;
1502
Bram Moolenaar8c711452005-01-14 21:53:12 +00001503 /*
1504 * May need to find the item or absolute index for the second
1505 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001506 * When no index given: "lp->ll_empty2" is TRUE.
1507 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001508 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001509 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001510 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001511 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001512 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001513 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001514 if (check_range_index_two(lp->ll_list,
1515 &lp->ll_n1, lp->ll_li,
1516 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001517 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001518 }
1519
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001520 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001521 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001522 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001523 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001524 class_T *cl = (v_type == VAR_OBJECT
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001525 && lp->ll_tv->vval.v_object != NULL)
1526 ? lp->ll_tv->vval.v_object->obj_class
1527 : lp->ll_tv->vval.v_class;
1528 // TODO: what if class is NULL?
1529 if (cl != NULL)
1530 {
1531 lp->ll_valtype = NULL;
Bram Moolenaard505d172022-12-18 21:42:55 +00001532 int count = v_type == VAR_OBJECT ? cl->class_obj_member_count
1533 : cl->class_class_member_count;
1534 ocmember_T *members = v_type == VAR_OBJECT
1535 ? cl->class_obj_members
1536 : cl->class_class_members;
1537 for (int i = 0; i < count; ++i)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001538 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001539 ocmember_T *om = members + i;
1540 if (STRNCMP(om->ocm_name, key, p - key) == 0
1541 && om->ocm_name[p - key] == NUL)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001542 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001543 switch (om->ocm_access)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001544 {
1545 case ACCESS_PRIVATE:
Bram Moolenaard505d172022-12-18 21:42:55 +00001546 semsg(_(e_cannot_access_private_member_str),
1547 om->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001548 return NULL;
1549 case ACCESS_READ:
1550 if (!(flags & GLV_READ_ONLY))
1551 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001552 semsg(_(e_member_is_not_writable_str),
1553 om->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001554 return NULL;
1555 }
1556 break;
1557 case ACCESS_ALL:
1558 break;
1559 }
1560
Bram Moolenaard505d172022-12-18 21:42:55 +00001561 lp->ll_valtype = om->ocm_type;
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001562
Bram Moolenaard505d172022-12-18 21:42:55 +00001563 if (v_type == VAR_OBJECT)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001564 lp->ll_tv = ((typval_T *)(
1565 lp->ll_tv->vval.v_object + 1)) + i;
Bram Moolenaard505d172022-12-18 21:42:55 +00001566 else
1567 lp->ll_tv = &cl->class_members_tv[i];
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001568 break;
1569 }
1570 }
1571 if (lp->ll_valtype == NULL)
1572 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001573 if (v_type == VAR_OBJECT)
1574 semsg(_(e_object_member_not_found_str), key);
1575 else
1576 semsg(_(e_class_member_not_found_str), key);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001577 return NULL;
1578 }
1579 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001580 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001581 }
1582
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001583 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001584 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001585 return p;
1586}
1587
1588/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001589 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001590 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001591 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001592clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001593{
1594 vim_free(lp->ll_exp_name);
1595 vim_free(lp->ll_newkey);
1596}
1597
1598/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001599 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001600 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001601 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1602 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001603 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001604 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001605set_var_lval(
1606 lval_T *lp,
1607 char_u *endp,
1608 typval_T *rettv,
1609 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001610 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1611 char_u *op,
1612 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001613{
1614 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001615 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001616
1617 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001618 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001619 cc = *endp;
1620 *endp = NUL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001621 if (in_vim9script() && check_reserved_name(lp->ll_name, NULL) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001622 return;
1623
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001624 if (lp->ll_blob != NULL)
1625 {
1626 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001627
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001628 if (op != NULL && *op != '=')
1629 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001630 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001631 return;
1632 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001633 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001634 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001635
1636 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1637 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001638 if (lp->ll_empty2)
1639 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1640
Bram Moolenaar68452172021-04-12 21:21:02 +02001641 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1642 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001643 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001644 }
1645 else
1646 {
1647 val = (int)tv_get_number_chk(rettv, &error);
1648 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001649 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001650 }
1651 }
1652 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001653 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001654 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001655
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001656 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1657 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001658 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001659 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001660 *endp = cc;
1661 return;
1662 }
1663
Bram Moolenaarff697e62019-02-12 22:28:33 +01001664 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001665 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001666 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001667 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001668 {
1669 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001670 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1671 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001672 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001673 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001674 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001675 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001676 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001677 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001678 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001679 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001680 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1681 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001682 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001683 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001684 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001685 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001686 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001687 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001688 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001689 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001690 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001691 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001692 else if (lp->ll_range)
1693 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001694 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1695 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001696 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001697 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001698 return;
1699 }
1700
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001701 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1702 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001703 }
1704 else
1705 {
1706 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001707 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001708 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001709 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1710 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001711 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001712 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001713 return;
1714 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001715
1716 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001717 && check_typval_arg_type(lp->ll_valtype, rettv,
1718 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001719 return;
1720
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001721 if (lp->ll_newkey != NULL)
1722 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001723 if (op != NULL && *op != '=')
1724 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001725 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001726 return;
1727 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001728 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1729 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001730 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001731
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001732 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001733 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001734 if (di == NULL)
1735 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001736 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1737 {
1738 vim_free(di);
1739 return;
1740 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001741 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001742 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001743 else if (op != NULL && *op != '=')
1744 {
1745 tv_op(lp->ll_tv, rettv, op);
1746 return;
1747 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001748 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001749 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001750
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001751 /*
1752 * Assign the value to the variable or list item.
1753 */
1754 if (copy)
1755 copy_tv(rettv, lp->ll_tv);
1756 else
1757 {
1758 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001759 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001760 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001761 }
1762 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001763}
1764
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001765/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001766 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1767 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001768 * Returns OK or FAIL.
1769 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001770 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001771tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001772{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001773 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001774 char_u numbuf[NUMBUFLEN];
1775 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001776 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001777
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001778 // Can't do anything with a Funcref or Dict on the right.
1779 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001780 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001781 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1782 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001783 {
1784 switch (tv1->v_type)
1785 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001786 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001787 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001789 case VAR_DICT:
1790 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001791 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001792 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001793 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001794 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001795 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001796 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001797 case VAR_CLASS:
1798 case VAR_OBJECT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001799 break;
1800
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001801 case VAR_BLOB:
1802 if (*op != '+' || tv2->v_type != VAR_BLOB)
1803 break;
1804 // BLOB += BLOB
1805 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1806 {
1807 blob_T *b1 = tv1->vval.v_blob;
1808 blob_T *b2 = tv2->vval.v_blob;
1809 int i, len = blob_len(b2);
1810 for (i = 0; i < len; i++)
1811 ga_append(&b1->bv_ga, blob_get(b2, i));
1812 }
1813 return OK;
1814
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001815 case VAR_LIST:
1816 if (*op != '+' || tv2->v_type != VAR_LIST)
1817 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001818 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001819 if (tv2->vval.v_list != NULL)
1820 {
1821 if (tv1->vval.v_list == NULL)
1822 {
1823 tv1->vval.v_list = tv2->vval.v_list;
1824 ++tv1->vval.v_list->lv_refcount;
1825 }
1826 else
1827 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1828 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001829 return OK;
1830
1831 case VAR_NUMBER:
1832 case VAR_STRING:
1833 if (tv2->v_type == VAR_LIST)
1834 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001835 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001836 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001837 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001838 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001839 if (tv2->v_type == VAR_FLOAT)
1840 {
1841 float_T f = n;
1842
Bram Moolenaarff697e62019-02-12 22:28:33 +01001843 if (*op == '%')
1844 break;
1845 switch (*op)
1846 {
1847 case '+': f += tv2->vval.v_float; break;
1848 case '-': f -= tv2->vval.v_float; break;
1849 case '*': f *= tv2->vval.v_float; break;
1850 case '/': f /= tv2->vval.v_float; break;
1851 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001852 clear_tv(tv1);
1853 tv1->v_type = VAR_FLOAT;
1854 tv1->vval.v_float = f;
1855 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001856 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001857 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001858 switch (*op)
1859 {
1860 case '+': n += tv_get_number(tv2); break;
1861 case '-': n -= tv_get_number(tv2); break;
1862 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001863 case '/': n = num_divide(n, tv_get_number(tv2),
1864 &failed); break;
1865 case '%': n = num_modulus(n, tv_get_number(tv2),
1866 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001867 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001868 clear_tv(tv1);
1869 tv1->v_type = VAR_NUMBER;
1870 tv1->vval.v_number = n;
1871 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001872 }
1873 else
1874 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001875 if (tv2->v_type == VAR_FLOAT)
1876 break;
1877
Bram Moolenaarff697e62019-02-12 22:28:33 +01001878 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001879 s = tv_get_string(tv1);
1880 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001881 clear_tv(tv1);
1882 tv1->v_type = VAR_STRING;
1883 tv1->vval.v_string = s;
1884 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001885 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001886
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001887 case VAR_FLOAT:
1888 {
1889 float_T f;
1890
Bram Moolenaarff697e62019-02-12 22:28:33 +01001891 if (*op == '%' || *op == '.'
1892 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001893 && tv2->v_type != VAR_NUMBER
1894 && tv2->v_type != VAR_STRING))
1895 break;
1896 if (tv2->v_type == VAR_FLOAT)
1897 f = tv2->vval.v_float;
1898 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001899 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001900 switch (*op)
1901 {
1902 case '+': tv1->vval.v_float += f; break;
1903 case '-': tv1->vval.v_float -= f; break;
1904 case '*': tv1->vval.v_float *= f; break;
1905 case '/': tv1->vval.v_float /= f; break;
1906 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001907 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001908 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001909 }
1910 }
1911
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001912 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001913 return FAIL;
1914}
1915
1916/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001917 * Evaluate the expression used in a ":for var in expr" command.
1918 * "arg" points to "var".
1919 * Set "*errp" to TRUE for an error, FALSE otherwise;
1920 * Return a pointer that holds the info. Null when there is an error.
1921 */
1922 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001923eval_for_line(
1924 char_u *arg,
1925 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001926 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001927 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928{
Bram Moolenaar33570922005-01-25 22:26:29 +00001929 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001930 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001931 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001932 typval_T tv;
1933 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001934 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001936 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001937
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001938 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001939 if (fi == NULL)
1940 return NULL;
1941
Bram Moolenaar404557e2021-07-05 21:41:48 +02001942 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1943 &fi->fi_semicolon, FALSE);
1944 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001945 return fi;
1946
Bram Moolenaar404557e2021-07-05 21:41:48 +02001947 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001948 if (expr[0] != 'i' || expr[1] != 'n'
1949 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001951 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1952 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1953 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001954 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001955 return fi;
1956 }
1957
1958 if (skip)
1959 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001960 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1961 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001962 {
1963 *errp = FALSE;
1964 if (!skip)
1965 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001966 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001967 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001968 l = tv.vval.v_list;
1969 if (l == NULL)
1970 {
1971 // a null list is like an empty list: do nothing
1972 clear_tv(&tv);
1973 }
1974 else
1975 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001977 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001978
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001979 // No need to increment the refcount, it's already set for
1980 // the list being used in "tv".
1981 fi->fi_list = l;
1982 list_add_watch(l, &fi->fi_lw);
1983 fi->fi_lw.lw_item = l->lv_first;
1984 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001985 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001986 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001987 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001988 fi->fi_bi = 0;
1989 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001990 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001991 typval_T btv;
1992
1993 // Make a copy, so that the iteration still works when the
1994 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001995 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001996 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001997 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001998 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001999 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002000 else if (tv.v_type == VAR_STRING)
2001 {
2002 fi->fi_byte_idx = 0;
2003 fi->fi_string = tv.vval.v_string;
2004 tv.vval.v_string = NULL;
2005 if (fi->fi_string == NULL)
2006 fi->fi_string = vim_strsave((char_u *)"");
2007 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002008 else
2009 {
Sean Dewar80d73952021-08-04 19:25:54 +02002010 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002011 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002012 }
2013 }
2014 }
2015 if (skip)
2016 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002017 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002018
2019 return fi;
2020}
2021
2022/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002023 * Used when looping over a :for line, skip the "in expr" part.
2024 */
2025 void
2026skip_for_lines(void *fi_void, evalarg_T *evalarg)
2027{
2028 forinfo_T *fi = (forinfo_T *)fi_void;
2029 int i;
2030
2031 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002032 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002033}
2034
2035/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002036 * Use the first item in a ":for" list. Advance to the next.
2037 * Assign the values to the variable (list). "arg" points to the first one.
2038 * Return TRUE when a valid item was found, FALSE when at end of list or
2039 * something wrong.
2040 */
2041 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002042next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002043{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002044 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002045 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002046 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002047 ? (ASSIGN_FINAL
2048 // first round: error if variable exists
2049 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002050 | ASSIGN_NO_MEMBER_TYPE
2051 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002052 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002053 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002054 int skip_assign = in_vim9script() && arg[0] == '_'
2055 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002056
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002057 if (fi->fi_blob != NULL)
2058 {
2059 typval_T tv;
2060
2061 if (fi->fi_bi >= blob_len(fi->fi_blob))
2062 return FALSE;
2063 tv.v_type = VAR_NUMBER;
2064 tv.v_lock = VAR_FIXED;
2065 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2066 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002067 if (skip_assign)
2068 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002069 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002070 fi->fi_varcount, flag, NULL) == OK;
2071 }
2072
2073 if (fi->fi_string != NULL)
2074 {
2075 typval_T tv;
2076 int len;
2077
2078 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2079 if (len == 0)
2080 return FALSE;
2081 tv.v_type = VAR_STRING;
2082 tv.v_lock = VAR_FIXED;
2083 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2084 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002085 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002086 if (skip_assign)
2087 result = TRUE;
2088 else
2089 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002090 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002091 vim_free(tv.vval.v_string);
2092 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002093 }
2094
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002095 item = fi->fi_lw.lw_item;
2096 if (item == NULL)
2097 result = FALSE;
2098 else
2099 {
2100 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002101 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002102 if (skip_assign)
2103 result = TRUE;
2104 else
2105 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002106 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002107 }
2108 return result;
2109}
2110
2111/*
2112 * Free the structure used to store info used by ":for".
2113 */
2114 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002115free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002116{
Bram Moolenaar33570922005-01-25 22:26:29 +00002117 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002118
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002119 if (fi == NULL)
2120 return;
2121 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002122 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002123 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002124 list_unref(fi->fi_list);
2125 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002126 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002127 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002128 else
2129 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002130 vim_free(fi);
2131}
2132
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002134set_context_for_expression(
2135 expand_T *xp,
2136 char_u *arg,
2137 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002139 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002141 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002143 if (cmdidx == CMD_let || cmdidx == CMD_var
2144 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002145 {
2146 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002147 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002148 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002149 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002150 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002151 {
2152 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002153 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002154 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002155 break;
2156 }
2157 return;
2158 }
2159 }
2160 else
2161 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2162 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 while ((xp->xp_pattern = vim_strpbrk(arg,
2164 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2165 {
2166 c = *xp->xp_pattern;
2167 if (c == '&')
2168 {
2169 c = xp->xp_pattern[1];
2170 if (c == '&')
2171 {
2172 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002173 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 }
2175 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002176 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002178 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2179 xp->xp_pattern += 2;
2180
2181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 }
2183 else if (c == '$')
2184 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002185 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 xp->xp_context = EXPAND_ENV_VARS;
2187 }
2188 else if (c == '=')
2189 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002190 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 xp->xp_context = EXPAND_EXPRESSION;
2192 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002193 else if (c == '#'
2194 && xp->xp_context == EXPAND_EXPRESSION)
2195 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002196 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002197 break;
2198 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002199 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 && xp->xp_context == EXPAND_FUNCTIONS
2201 && vim_strchr(xp->xp_pattern, '(') == NULL)
2202 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002203 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 break;
2205 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002206 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002208 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 {
2210 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2211 if (c == '\\' && xp->xp_pattern[1] != NUL)
2212 ++xp->xp_pattern;
2213 xp->xp_context = EXPAND_NOTHING;
2214 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002215 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002217 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2219 /* skip */ ;
2220 xp->xp_context = EXPAND_NOTHING;
2221 }
2222 else if (c == '|')
2223 {
2224 if (xp->xp_pattern[1] == '|')
2225 {
2226 ++xp->xp_pattern;
2227 xp->xp_context = EXPAND_EXPRESSION;
2228 }
2229 else
2230 xp->xp_context = EXPAND_COMMANDS;
2231 }
2232 else
2233 xp->xp_context = EXPAND_EXPRESSION;
2234 }
2235 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002236 // Doesn't look like something valid, expand as an expression
2237 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002238 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 arg = xp->xp_pattern;
2240 if (*arg != NUL)
2241 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2242 /* skip */ ;
2243 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002244
2245 // ":exe one two" completes "two"
2246 if ((cmdidx == CMD_execute
2247 || cmdidx == CMD_echo
2248 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002249 || cmdidx == CMD_echomsg
2250 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002251 && xp->xp_context == EXPAND_EXPRESSION)
2252 {
2253 for (;;)
2254 {
2255 char_u *n = skiptowhite(arg);
2256
2257 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2258 break;
2259 arg = skipwhite(n);
2260 }
2261 }
2262
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 xp->xp_pattern = arg;
2264}
2265
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002267 * Return TRUE if "pat" matches "text".
2268 * Does not use 'cpo' and always uses 'magic'.
2269 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002270 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002271pattern_match(char_u *pat, char_u *text, int ic)
2272{
2273 int matches = FALSE;
2274 char_u *save_cpo;
2275 regmatch_T regmatch;
2276
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002277 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002278 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002279 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002280 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2281 if (regmatch.regprog != NULL)
2282 {
2283 regmatch.rm_ic = ic;
2284 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2285 vim_regfree(regmatch.regprog);
2286 }
2287 p_cpo = save_cpo;
2288 return matches;
2289}
2290
2291/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002292 * Handle a name followed by "(". Both for just "name(arg)" and for
2293 * "expr->name(arg)".
2294 * Returns OK or FAIL.
2295 */
2296 static int
2297eval_func(
2298 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002299 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002300 char_u *name,
2301 int name_len,
2302 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002303 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002304 typval_T *basetv) // "expr" for "expr->name(arg)"
2305{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002306 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002307 char_u *s = name;
2308 int len = name_len;
2309 partial_T *partial;
2310 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002311 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002312 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002313
2314 if (!evaluate)
2315 check_vars(s, len);
2316
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002317 // If "s" is the name of a variable of type VAR_FUNC
2318 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002319 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002320 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002321
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002322 // Need to make a copy, in case evaluating the arguments makes
2323 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002324 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002325 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002326 ret = FAIL;
2327 else
2328 {
2329 funcexe_T funcexe;
2330
2331 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002332 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002333 funcexe.fe_firstline = curwin->w_cursor.lnum;
2334 funcexe.fe_lastline = curwin->w_cursor.lnum;
2335 funcexe.fe_evaluate = evaluate;
2336 funcexe.fe_partial = partial;
2337 funcexe.fe_basetv = basetv;
2338 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002339 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002340 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002341 }
2342 vim_free(s);
2343
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002344 // If evaluate is FALSE rettv->v_type was not set in
2345 // get_func_tv, but it's needed in handle_subscript() to parse
2346 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002347 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2348 {
2349 rettv->vval.v_string = NULL;
2350 rettv->v_type = VAR_FUNC;
2351 }
2352
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002353 // Stop the expression evaluation when immediately
2354 // aborting on error, or when an interrupt occurred or
2355 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002356 if (evaluate && aborting())
2357 {
2358 if (ret == OK)
2359 clear_tv(rettv);
2360 ret = FAIL;
2361 }
2362 return ret;
2363}
2364
2365/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002366 * After a NL, skip over empty lines and comment-only lines.
2367 */
2368 static char_u *
2369newline_skip_comments(char_u *arg)
2370{
2371 char_u *p = arg + 1;
2372
2373 for (;;)
2374 {
2375 p = skipwhite(p);
2376
2377 if (*p == NUL)
2378 break;
2379 if (vim9_comment_start(p))
2380 {
2381 char_u *nl = vim_strchr(p, NL);
2382
2383 if (nl == NULL)
2384 break;
2385 p = nl;
2386 }
2387 if (*p != NL)
2388 break;
2389 ++p; // skip another NL
2390 }
2391 return p;
2392}
2393
2394/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002395 * Get the next line source line without advancing. But do skip over comment
2396 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002397 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002398 */
2399 static char_u *
2400getline_peek_skip_comments(evalarg_T *evalarg)
2401{
2402 for (;;)
2403 {
2404 char_u *next = getline_peek(evalarg->eval_getline,
2405 evalarg->eval_cookie);
2406 char_u *p;
2407
2408 if (next == NULL)
2409 break;
2410 p = skipwhite(next);
2411 if (*p != NUL && !vim9_comment_start(p))
2412 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002413 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002414 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002415 }
2416 return NULL;
2417}
2418
2419/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002420 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2421 * comment) and there is a next line, return the next line (skipping blanks)
2422 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002423 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2424 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002425 * "arg" must point somewhere inside a line, not at the start.
2426 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002427 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002428eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2429{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002430 char_u *p = skipwhite(arg);
2431
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002432 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002433 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002434 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002435 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2436 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002437 && (*p == NUL || *p == NL
2438 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002439 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002440 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002441
Bram Moolenaare442d592022-05-05 12:20:28 +01002442 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002443 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002444 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002445 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002446 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002447 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002448
Bram Moolenaar9d489562020-07-30 20:08:50 +02002449 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002450 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002451 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002452 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002453 }
2454 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002455 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002456}
2457
2458/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002459 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002460 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002461 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002462 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002463eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002464{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002465 garray_T *gap = &evalarg->eval_ga;
2466 char_u *line;
2467
Bram Moolenaar39be4982022-05-06 21:51:50 +01002468 if (arg != NULL)
2469 {
2470 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002471 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002472 // Truncate before a trailing comment, so that concatenating the lines
2473 // won't turn the rest into a comment.
2474 if (*skipwhite(arg) == '#')
2475 *arg = NUL;
2476 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002477
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002478 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002479 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2480 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002481 else
2482 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002483 if (line == NULL)
2484 return NULL;
2485
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002486 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002487 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2488 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002489 char_u *p = skipwhite(line);
2490
2491 // Going to concatenate the lines after parsing. For an empty or
2492 // comment line use an empty string.
2493 if (*p == NUL || vim9_comment_start(p))
2494 {
2495 vim_free(line);
2496 line = vim_strsave((char_u *)"");
2497 }
2498
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002499 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2500 ++gap->ga_len;
2501 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002502 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002503 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002504 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002505 evalarg->eval_tofree = line;
2506 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002507
2508 // Advanced to the next line, "arg" no longer points into the previous
2509 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002510 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002511 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002512}
2513
Bram Moolenaare6b53242020-07-01 17:28:33 +02002514/*
2515 * Call eval_next_non_blank() and get the next line if needed.
2516 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002517 char_u *
2518skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2519{
2520 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002521 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002522
Bram Moolenaar962d7212020-07-04 14:15:00 +02002523 if (evalarg == NULL)
2524 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002525 eval_next_non_blank(p, evalarg, &getnext);
2526 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002527 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002528 return p;
2529}
2530
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002531/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002532 * The "eval" functions have an "evalarg" argument: When NULL or
2533 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
2534 * parsed but not executed. The functions may return OK, but the rettv will be
2535 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 */
2537
2538/*
2539 * Handle zero level expression.
2540 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002541 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002542 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002543 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 * Return OK or FAIL.
2545 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002546 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002547eval0(
2548 char_u *arg,
2549 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002550 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002551 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002553 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2554}
2555
2556/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002557 * If "arg" is a simple function call without arguments then call it and return
2558 * the result. Otherwise return NOTDONE.
2559 */
2560 int
2561may_call_simple_func(
2562 char_u *arg,
2563 typval_T *rettv)
2564{
2565 char_u *parens = (char_u *)strstr((char *)arg, "()");
2566 int r = NOTDONE;
2567
2568 // If the expression is "FuncName()" then we can skip a lot of overhead.
2569 if (parens != NULL && *skipwhite(parens + 2) == NUL)
2570 {
2571 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
2572
2573 if (to_name_end(p, TRUE) == parens)
2574 r = call_simple_func(arg, (int)(parens - arg), rettv);
2575 }
2576 return r;
2577}
2578
2579/*
2580 * Handle zero level expression with optimization for a simple function call.
2581 * Same arguments and return value as eval0().
2582 */
2583 int
2584eval0_simple_funccal(
2585 char_u *arg,
2586 typval_T *rettv,
2587 exarg_T *eap,
2588 evalarg_T *evalarg)
2589{
2590 int r = may_call_simple_func(arg, rettv);
2591
2592 if (r == NOTDONE)
2593 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
2594 return r;
2595}
2596
2597/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002598 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2599 * expression and don't check what comes after the expression.
2600 */
2601 int
2602eval0_retarg(
2603 char_u *arg,
2604 typval_T *rettv,
2605 exarg_T *eap,
2606 evalarg_T *evalarg,
2607 char_u **retarg)
2608{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 int ret;
2610 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002611 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002612 int did_emsg_before = did_emsg;
2613 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002614 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002615 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002616 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617
2618 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002619 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002620
Bram Moolenaar79481362022-06-27 20:15:10 +01002621 if (ret != FAIL)
2622 {
2623 expr_end = p;
2624 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002625
Bram Moolenaar79481362022-06-27 20:15:10 +01002626 // In Vim9 script a command block is not split at NL characters for
2627 // commands using an expression argument. Skip over a '#' comment to
2628 // check for a following NL. Require white space before the '#'.
2629 if (in_vim9script() && p > expr_end && retarg == NULL)
2630 while (*p == '#')
2631 {
2632 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002633
Bram Moolenaar79481362022-06-27 20:15:10 +01002634 if (nl == NULL)
2635 break;
2636 p = skipwhite(nl + 1);
2637 if (eap != NULL && *p != NUL)
2638 eap->nextcmd = p;
2639 check_for_end = FALSE;
2640 }
2641
2642 if (check_for_end)
2643 end_error = !ends_excmd2(arg, p);
2644 }
2645
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002646 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 {
2648 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002649 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 /*
2651 * Report the invalid expression unless the expression evaluation has
2652 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002653 * exception, or we already gave a more specific error.
2654 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002656 if (!aborting()
2657 && did_emsg == did_emsg_before
2658 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002659 && (flags & EVAL_CONSTANT) == 0
2660 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002661 {
2662 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002663 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002664 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002665 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002666 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002667
2668 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002669 // a next command to avoid more errors, unless "|" is following, which
2670 // could only be a command separator.
Bram Moolenaar79481362022-06-27 20:15:10 +01002671 if (eap != NULL && p != NULL
2672 && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
Bram Moolenaar8143a532020-12-13 20:26:29 +01002673 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002674 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002676
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002677 if (retarg != NULL)
2678 *retarg = p;
2679 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002680 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002681
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 return ret;
2683}
2684
2685/*
2686 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002687 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002688 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 *
2690 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002691 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002693 * Note: "rettv.v_lock" is not set.
2694 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 * Return OK or FAIL.
2696 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002697 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002698eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002700 char_u *p;
2701 int getnext;
2702
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002703 CLEAR_POINTER(rettv);
2704
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 /*
2706 * Get the first variable.
2707 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002708 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 return FAIL;
2710
Bram Moolenaar793648f2020-06-26 21:28:25 +02002711 p = eval_next_non_blank(*arg, evalarg, &getnext);
2712 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002714 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002715 int result;
2716 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002717 evalarg_T *evalarg_used = evalarg;
2718 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002719 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002720 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002721 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002722
2723 if (evalarg == NULL)
2724 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002725 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002726 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002727 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002728 orig_flags = evalarg_used->eval_flags;
2729 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002730
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002731 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002732 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002733 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002734 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002735 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002736 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002737 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002738 clear_tv(rettv);
2739 return FAIL;
2740 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002741 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002742 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002743
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002745 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002747 int error = FALSE;
2748
Bram Moolenaar13106602020-10-04 16:06:05 +02002749 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002750 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002751 else if (vim9script)
2752 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002753 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002755 if (error || !op_falsy || !result)
2756 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002757 if (error)
2758 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 }
2760
2761 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002762 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002764 if (op_falsy)
2765 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002766 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002767 {
mityu4ac198c2021-05-28 17:52:40 +02002768 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002769 clear_tv(rettv);
2770 return FAIL;
2771 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002772 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002773 evalarg_used->eval_flags = (op_falsy ? !result : result)
2774 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2775 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002776 {
2777 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002779 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002780 if (!op_falsy || !result)
2781 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002783 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002785 /*
2786 * Check for the ":".
2787 */
2788 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2789 if (*p != ':')
2790 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002791 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002792 if (evaluate && result)
2793 clear_tv(rettv);
2794 evalarg_used->eval_flags = orig_flags;
2795 return FAIL;
2796 }
2797 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002798 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002799 else
2800 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002801 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002802 {
2803 error_white_both(p, 1);
2804 clear_tv(rettv);
2805 evalarg_used->eval_flags = orig_flags;
2806 return FAIL;
2807 }
2808 *arg = p;
2809 }
2810
2811 /*
2812 * Get the third variable. Recursive!
2813 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002814 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002815 {
mityu4ac198c2021-05-28 17:52:40 +02002816 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002817 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002818 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002819 return FAIL;
2820 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002821 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2822 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002823 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002824 if (eval1(arg, &var2, evalarg_used) == FAIL)
2825 {
2826 if (evaluate && result)
2827 clear_tv(rettv);
2828 evalarg_used->eval_flags = orig_flags;
2829 return FAIL;
2830 }
2831 if (evaluate && !result)
2832 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002834
2835 if (evalarg == NULL)
2836 clear_evalarg(&local_evalarg, NULL);
2837 else
2838 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 }
2840
2841 return OK;
2842}
2843
2844/*
2845 * Handle first level expression:
2846 * expr2 || expr2 || expr2 logical OR
2847 *
2848 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002849 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 *
2851 * Return OK or FAIL.
2852 */
2853 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002854eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002856 char_u *p;
2857 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858
2859 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002860 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002862 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 return FAIL;
2864
2865 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002866 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002868 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002869 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002871 evalarg_T *evalarg_used = evalarg;
2872 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002873 int evaluate;
2874 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002875 long result = FALSE;
2876 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002877 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002878 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002879
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002880 if (evalarg == NULL)
2881 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002882 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002883 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002884 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002885 orig_flags = evalarg_used->eval_flags;
2886 evaluate = orig_flags & EVAL_EVALUATE;
2887 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002888 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002889 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002890 result = tv_get_bool_chk(rettv, &error);
2891 else if (tv_get_number_chk(rettv, &error) != 0)
2892 result = TRUE;
2893 clear_tv(rettv);
2894 if (error)
2895 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 }
2897
2898 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002899 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002901 while (p[0] == '|' && p[1] == '|')
2902 {
2903 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002904 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002905 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002906 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002907 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002908 {
2909 error_white_both(p, 2);
2910 clear_tv(rettv);
2911 return FAIL;
2912 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002913 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002914 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002915
2916 /*
2917 * Get the second variable.
2918 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002919 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002920 {
mityu4ac198c2021-05-28 17:52:40 +02002921 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002922 clear_tv(rettv);
2923 return FAIL;
2924 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002925 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2926 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002927 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002928 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002929 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002930
2931 /*
2932 * Compute the result.
2933 */
2934 if (evaluate && !result)
2935 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002936 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002937 result = tv_get_bool_chk(&var2, &error);
2938 else if (tv_get_number_chk(&var2, &error) != 0)
2939 result = TRUE;
2940 clear_tv(&var2);
2941 if (error)
2942 return FAIL;
2943 }
2944 if (evaluate)
2945 {
2946 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002947 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002948 rettv->v_type = VAR_BOOL;
2949 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002950 }
2951 else
2952 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002953 rettv->v_type = VAR_NUMBER;
2954 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002955 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002956 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002957
2958 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002960
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002961 if (evalarg == NULL)
2962 clear_evalarg(&local_evalarg, NULL);
2963 else
2964 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 }
2966
2967 return OK;
2968}
2969
2970/*
2971 * Handle second level expression:
2972 * expr3 && expr3 && expr3 logical AND
2973 *
2974 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002975 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 *
2977 * Return OK or FAIL.
2978 */
2979 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002980eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002982 char_u *p;
2983 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984
2985 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002986 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002988 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 return FAIL;
2990
2991 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002992 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002994 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002995 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002997 evalarg_T *evalarg_used = evalarg;
2998 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002999 int orig_flags;
3000 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003001 long result = TRUE;
3002 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003003 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003004 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003005
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003006 if (evalarg == NULL)
3007 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003008 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003009 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003010 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003011 orig_flags = evalarg_used->eval_flags;
3012 evaluate = orig_flags & EVAL_EVALUATE;
3013 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003014 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003015 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003016 result = tv_get_bool_chk(rettv, &error);
3017 else if (tv_get_number_chk(rettv, &error) == 0)
3018 result = FALSE;
3019 clear_tv(rettv);
3020 if (error)
3021 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 }
3023
3024 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003025 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003027 while (p[0] == '&' && p[1] == '&')
3028 {
3029 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003030 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003031 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003032 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003033 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003034 {
3035 error_white_both(p, 2);
3036 clear_tv(rettv);
3037 return FAIL;
3038 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003039 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003040 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003041
3042 /*
3043 * Get the second variable.
3044 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003045 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003046 {
mityu4ac198c2021-05-28 17:52:40 +02003047 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003048 clear_tv(rettv);
3049 return FAIL;
3050 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003051 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3052 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003053 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003054 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003055 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003056 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003057
3058 /*
3059 * Compute the result.
3060 */
3061 if (evaluate && result)
3062 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003063 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003064 result = tv_get_bool_chk(&var2, &error);
3065 else if (tv_get_number_chk(&var2, &error) == 0)
3066 result = FALSE;
3067 clear_tv(&var2);
3068 if (error)
3069 return FAIL;
3070 }
3071 if (evaluate)
3072 {
3073 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003074 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003075 rettv->v_type = VAR_BOOL;
3076 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003077 }
3078 else
3079 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003080 rettv->v_type = VAR_NUMBER;
3081 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003082 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003083 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003084
3085 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003087
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003088 if (evalarg == NULL)
3089 clear_evalarg(&local_evalarg, NULL);
3090 else
3091 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 }
3093
3094 return OK;
3095}
3096
3097/*
3098 * Handle third level expression:
3099 * var1 == var2
3100 * var1 =~ var2
3101 * var1 != var2
3102 * var1 !~ var2
3103 * var1 > var2
3104 * var1 >= var2
3105 * var1 < var2
3106 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003107 * var1 is var2
3108 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 *
3110 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003111 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 *
3113 * Return OK or FAIL.
3114 */
3115 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003116eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003119 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003120 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003122 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123
3124 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003125 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003127 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 return FAIL;
3129
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003130 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003131
Bram Moolenaar696ba232020-07-29 21:20:41 +02003132 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133
3134 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003135 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003137 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003139 typval_T var2;
3140 int ic;
3141 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003142 int evaluate = evalarg == NULL
3143 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003144 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003145
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003146 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003147 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003148 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003149 p = *arg;
3150 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003151 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3152 {
mityu4ac198c2021-05-28 17:52:40 +02003153 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003154 clear_tv(rettv);
3155 return FAIL;
3156 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003157
Bram Moolenaar696ba232020-07-29 21:20:41 +02003158 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3159 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003160 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003161 clear_tv(rettv);
3162 return FAIL;
3163 }
3164
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003165 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 if (p[len] == '?')
3167 {
3168 ic = TRUE;
3169 ++len;
3170 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003171 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 else if (p[len] == '#')
3173 {
3174 ic = FALSE;
3175 ++len;
3176 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003177 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003179 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180
3181 /*
3182 * Get the second variable.
3183 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003184 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3185 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003186 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003187 clear_tv(rettv);
3188 return FAIL;
3189 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003190 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003191 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003193 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 return FAIL;
3195 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003196 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003197 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003198 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003199
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003200 // use the line of the comparison for messages
3201 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003202 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003203 {
3204 ret = FAIL;
3205 clear_tv(rettv);
3206 }
3207 else
3208 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003209 clear_tv(&var2);
3210 return ret;
3211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 }
3213
3214 return OK;
3215}
3216
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003217/*
3218 * Make a copy of blob "tv1" and append blob "tv2".
3219 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 void
3221eval_addblob(typval_T *tv1, typval_T *tv2)
3222{
3223 blob_T *b1 = tv1->vval.v_blob;
3224 blob_T *b2 = tv2->vval.v_blob;
3225 blob_T *b = blob_alloc();
3226 int i;
3227
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003228 if (b == NULL)
3229 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003230
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003231 for (i = 0; i < blob_len(b1); i++)
3232 ga_append(&b->bv_ga, blob_get(b1, i));
3233 for (i = 0; i < blob_len(b2); i++)
3234 ga_append(&b->bv_ga, blob_get(b2, i));
3235
3236 clear_tv(tv1);
3237 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238}
3239
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003240/*
3241 * Make a copy of list "tv1" and append list "tv2".
3242 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243 int
3244eval_addlist(typval_T *tv1, typval_T *tv2)
3245{
3246 typval_T var3;
3247
3248 // concatenate Lists
3249 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3250 {
3251 clear_tv(tv1);
3252 clear_tv(tv2);
3253 return FAIL;
3254 }
3255 clear_tv(tv1);
3256 *tv1 = var3;
3257 return OK;
3258}
3259
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003261 * Handle the bitwise left/right shift operator expression:
3262 * var1 << var2
3263 * var1 >> var2
3264 *
3265 * "arg" must point to the first non-white of the expression.
3266 * "arg" is advanced to just after the recognized expression.
3267 *
3268 * Return OK or FAIL.
3269 */
3270 static int
3271eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3272{
3273 /*
3274 * Get the first expression.
3275 */
3276 if (eval6(arg, rettv, evalarg) == FAIL)
3277 return FAIL;
3278
3279 /*
3280 * Repeat computing, until no '<<' or '>>' is following.
3281 */
3282 for (;;)
3283 {
3284 char_u *p;
3285 int getnext;
3286 exprtype_T type;
3287 int evaluate;
3288 typval_T var2;
3289 int vim9script;
3290
3291 p = eval_next_non_blank(*arg, evalarg, &getnext);
3292 if (p[0] == '<' && p[1] == '<')
3293 type = EXPR_LSHIFT;
3294 else if (p[0] == '>' && p[1] == '>')
3295 type = EXPR_RSHIFT;
3296 else
3297 return OK;
3298
3299 // Handle a bitwise left or right shift operator
3300 if (rettv->v_type != VAR_NUMBER)
3301 {
3302 // left operand should be a number
3303 emsg(_(e_bitshift_ops_must_be_number));
3304 clear_tv(rettv);
3305 return FAIL;
3306 }
3307
3308 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3309 vim9script = in_vim9script();
3310 if (getnext)
3311 {
3312 *arg = eval_next_line(*arg, evalarg);
3313 p = *arg;
3314 }
3315 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3316 {
3317 error_white_both(*arg, 2);
3318 clear_tv(rettv);
3319 return FAIL;
3320 }
3321
3322 /*
3323 * Get the second variable.
3324 */
3325 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3326 {
3327 error_white_both(p, 2);
3328 clear_tv(rettv);
3329 return FAIL;
3330 }
3331 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3332 if (eval6(arg, &var2, evalarg) == FAIL)
3333 {
3334 clear_tv(rettv);
3335 return FAIL;
3336 }
3337
3338 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3339 {
3340 // right operand should be a positive number
3341 if (var2.v_type != VAR_NUMBER)
3342 emsg(_(e_bitshift_ops_must_be_number));
3343 else
dundargocc57b5bc2022-11-02 13:30:51 +00003344 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003345 clear_tv(rettv);
3346 clear_tv(&var2);
3347 return FAIL;
3348 }
3349
3350 if (evaluate)
3351 {
3352 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3353 // shifting more bits than we have always results in zero
3354 rettv->vval.v_number = 0;
3355 else if (type == EXPR_LSHIFT)
3356 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003357 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003358 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003359 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003360 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003361 }
3362
3363 clear_tv(&var2);
3364 }
3365
3366 return OK;
3367}
3368
3369/*
3370 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003371 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003373 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003374 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 *
3376 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003377 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 *
3379 * Return OK or FAIL.
3380 */
3381 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003382eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003385 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003387 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 return FAIL;
3389
3390 /*
3391 * Repeat computing, until no '+', '-' or '.' is following.
3392 */
3393 for (;;)
3394 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003395 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003396 int getnext;
3397 char_u *p;
3398 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003399 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003400 int concat;
3401 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003402 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003403
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003404 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003405 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003406 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003407 p = eval_next_non_blank(*arg, evalarg, &getnext);
3408 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003409 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003410 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3411 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003413 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3414 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003415
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003416 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003417 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003418 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003419 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003420 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003421 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003422 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003423 {
mityu4ac198c2021-05-28 17:52:40 +02003424 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003425 clear_tv(rettv);
3426 return FAIL;
3427 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003428 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003429 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003430 if ((op != '+' || (rettv->v_type != VAR_LIST
3431 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003432 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003433 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003434 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003435 int error = FALSE;
3436
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003437 // For "list + ...", an illegal use of the first operand as
3438 // a number cannot be determined before evaluating the 2nd
3439 // operand: if this is also a list, all is ok.
3440 // For "something . ...", "something - ..." or "non-list + ...",
3441 // we know that the first operand needs to be a string or number
3442 // without evaluating the 2nd operand. So check before to avoid
3443 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003444 if (op != '.')
3445 tv_get_number_chk(rettv, &error);
3446 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003447 {
3448 clear_tv(rettv);
3449 return FAIL;
3450 }
3451 }
3452
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 /*
3454 * Get the second variable.
3455 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003456 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003457 {
mityu89dcb4d2021-05-28 13:50:17 +02003458 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003459 clear_tv(rettv);
3460 return FAIL;
3461 }
3462 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003463 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003465 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 return FAIL;
3467 }
3468
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003469 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 {
3471 /*
3472 * Compute the result.
3473 */
3474 if (op == '.')
3475 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003476 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3477 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003478 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003479
Bram Moolenaar2e086612020-08-25 22:37:48 +02003480 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003481 || var2.v_type == VAR_CHANNEL
3482 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003483 semsg(_(e_using_invalid_value_as_string_str),
3484 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003485 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003486 {
3487 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3488 var2.vval.v_float);
3489 s2 = buf2;
3490 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003491 else
3492 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003493 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003494 {
3495 clear_tv(rettv);
3496 clear_tv(&var2);
3497 return FAIL;
3498 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003499 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003500 clear_tv(rettv);
3501 rettv->v_type = VAR_STRING;
3502 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003504 else if (op == '+' && rettv->v_type == VAR_BLOB
3505 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003506 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003507 else if (op == '+' && rettv->v_type == VAR_LIST
3508 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003509 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003511 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 else
3514 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003515 int error = FALSE;
3516 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003517 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003518
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003519 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003520 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003521 f1 = rettv->vval.v_float;
3522 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003523 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003524 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003525 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003526 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003527 if (error)
3528 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003529 // This can only happen for "list + non-list" or
3530 // "blob + non-blob". For "non-list + ..." or
3531 // "something - ...", we returned before evaluating the
3532 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003533 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003534 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003535 return FAIL;
3536 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003537 if (var2.v_type == VAR_FLOAT)
3538 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003539 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003540 if (var2.v_type == VAR_FLOAT)
3541 {
3542 f2 = var2.vval.v_float;
3543 n2 = 0;
3544 }
3545 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003546 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003547 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003548 if (error)
3549 {
3550 clear_tv(rettv);
3551 clear_tv(&var2);
3552 return FAIL;
3553 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003554 if (rettv->v_type == VAR_FLOAT)
3555 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003556 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003557 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003558
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003559 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003560 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3561 {
3562 if (op == '+')
3563 f1 = f1 + f2;
3564 else
3565 f1 = f1 - f2;
3566 rettv->v_type = VAR_FLOAT;
3567 rettv->vval.v_float = f1;
3568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003570 {
3571 if (op == '+')
3572 n1 = n1 + n2;
3573 else
3574 n1 = n1 - n2;
3575 rettv->v_type = VAR_NUMBER;
3576 rettv->vval.v_number = n1;
3577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003579 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 }
3581 }
3582 return OK;
3583}
3584
3585/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003586 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 * * number multiplication
3588 * / number division
3589 * % number modulo
3590 *
3591 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003592 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 *
3594 * Return OK or FAIL.
3595 */
3596 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003597eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003598 char_u **arg,
3599 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003600 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003601 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003603 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604
3605 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003606 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003608 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 return FAIL;
3610
3611 /*
3612 * Repeat computing, until no '*', '/' or '%' is following.
3613 */
3614 for (;;)
3615 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003616 int evaluate;
3617 int getnext;
3618 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003619 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003620 int op;
3621 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003622 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003623 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003624
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003625 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003626 p = eval_next_non_blank(*arg, evalarg, &getnext);
3627 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003628 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003630
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003631 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003632 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003633 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003634 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003635 {
3636 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3637 {
mityu4ac198c2021-05-28 17:52:40 +02003638 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003639 clear_tv(rettv);
3640 return FAIL;
3641 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003642 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003645 f1 = 0;
3646 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003647 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003648 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003650 if (rettv->v_type == VAR_FLOAT)
3651 {
3652 f1 = rettv->vval.v_float;
3653 use_float = TRUE;
3654 n1 = 0;
3655 }
3656 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003657 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003658 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003659 if (error)
3660 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 }
3662 else
3663 n1 = 0;
3664
3665 /*
3666 * Get the second variable.
3667 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003668 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3669 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003670 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003671 clear_tv(rettv);
3672 return FAIL;
3673 }
3674 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003675 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 return FAIL;
3677
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003678 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003680 if (var2.v_type == VAR_FLOAT)
3681 {
3682 if (!use_float)
3683 {
3684 f1 = n1;
3685 use_float = TRUE;
3686 }
3687 f2 = var2.vval.v_float;
3688 n2 = 0;
3689 }
3690 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003691 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003692 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003693 clear_tv(&var2);
3694 if (error)
3695 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003696 if (use_float)
3697 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699
3700 /*
3701 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003702 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003704 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003706 if (op == '*')
3707 f1 = f1 * f2;
3708 else if (op == '/')
3709 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003710#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003711 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003712 if (f2 == 0.0)
3713 {
3714 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003715 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003716 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003717 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003718 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003719 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003720 }
3721 else
3722 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003723#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003724 // We rely on the floating point library to handle divide
3725 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003726 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003727#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003730 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003731 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003732 return FAIL;
3733 }
3734 rettv->v_type = VAR_FLOAT;
3735 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 }
3737 else
3738 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003739 int failed = FALSE;
3740
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003741 if (op == '*')
3742 n1 = n1 * n2;
3743 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003744 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003746 n1 = num_modulus(n1, n2, &failed);
3747 if (failed)
3748 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003749
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003750 rettv->v_type = VAR_NUMBER;
3751 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 }
3754 }
3755
3756 return OK;
3757}
3758
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003759/*
3760 * Handle a type cast before a base level expression.
3761 * "arg" must point to the first non-white of the expression.
3762 * "arg" is advanced to just after the recognized expression.
3763 * Return OK or FAIL.
3764 */
3765 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003766eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003767 char_u **arg,
3768 typval_T *rettv,
3769 evalarg_T *evalarg,
3770 int want_string) // after "." operator
3771{
3772 type_T *want_type = NULL;
3773 garray_T type_list; // list of pointers to allocated types
3774 int res;
3775 int evaluate = evalarg == NULL ? 0
3776 : (evalarg->eval_flags & EVAL_EVALUATE);
3777
3778 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003779 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3780 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003781 {
3782 ++*arg;
3783 ga_init2(&type_list, sizeof(type_T *), 10);
3784 want_type = parse_type(arg, &type_list, TRUE);
3785 if (want_type == NULL && (evaluate || **arg != '>'))
3786 {
3787 clear_type_list(&type_list);
3788 return FAIL;
3789 }
3790
3791 if (**arg != '>')
3792 {
3793 if (*skipwhite(*arg) == '>')
3794 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3795 else
3796 emsg(_(e_missing_gt));
3797 clear_type_list(&type_list);
3798 return FAIL;
3799 }
3800 ++*arg;
3801 *arg = skipwhite_and_linebreak(*arg, evalarg);
3802 }
3803
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003804 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003805
3806 if (want_type != NULL && evaluate)
3807 {
3808 if (res == OK)
3809 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003810 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3811 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003812
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003813 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003814 {
3815 if (want_type == &t_bool && actual != &t_bool
3816 && (actual->tt_flags & TTFLAG_BOOL_OK))
3817 {
3818 int n = tv2bool(rettv);
3819
3820 // can use "0" and "1" for boolean in some places
3821 clear_tv(rettv);
3822 rettv->v_type = VAR_BOOL;
3823 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3824 }
3825 else
3826 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003827 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003828
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003829 where.wt_variable = TRUE;
3830 res = check_type(want_type, actual, TRUE, where);
3831 }
3832 }
3833 }
3834 clear_type_list(&type_list);
3835 }
3836
3837 return res;
3838}
3839
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003840 int
3841eval_leader(char_u **arg, int vim9)
3842{
3843 char_u *s = *arg;
3844 char_u *p = *arg;
3845
3846 while (*p == '!' || *p == '-' || *p == '+')
3847 {
3848 char_u *n = skipwhite(p + 1);
3849
3850 // ++, --, -+ and +- are not accepted in Vim9 script
3851 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3852 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003853 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003854 return FAIL;
3855 }
3856 p = n;
3857 }
3858 *arg = p;
3859 return OK;
3860}
3861
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003863 * Check for a predefined value "true", "false" and "null.*".
3864 * Return OK when recognized.
3865 */
3866 int
3867handle_predefined(char_u *s, int len, typval_T *rettv)
3868{
3869 switch (len)
3870 {
3871 case 4: if (STRNCMP(s, "true", 4) == 0)
3872 {
3873 rettv->v_type = VAR_BOOL;
3874 rettv->vval.v_number = VVAL_TRUE;
3875 return OK;
3876 }
3877 if (STRNCMP(s, "null", 4) == 0)
3878 {
3879 rettv->v_type = VAR_SPECIAL;
3880 rettv->vval.v_number = VVAL_NULL;
3881 return OK;
3882 }
3883 break;
3884 case 5: if (STRNCMP(s, "false", 5) == 0)
3885 {
3886 rettv->v_type = VAR_BOOL;
3887 rettv->vval.v_number = VVAL_FALSE;
3888 return OK;
3889 }
3890 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003891 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3892 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003893#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003894 rettv->v_type = VAR_JOB;
3895 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003896#else
3897 rettv->v_type = VAR_SPECIAL;
3898 rettv->vval.v_number = VVAL_NULL;
3899#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003900 return OK;
3901 }
3902 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003903 case 9:
3904 if (STRNCMP(s, "null_", 5) != 0)
3905 break;
3906 if (STRNCMP(s + 5, "list", 4) == 0)
3907 {
3908 rettv->v_type = VAR_LIST;
3909 rettv->vval.v_list = NULL;
3910 return OK;
3911 }
3912 if (STRNCMP(s + 5, "dict", 4) == 0)
3913 {
3914 rettv->v_type = VAR_DICT;
3915 rettv->vval.v_dict = NULL;
3916 return OK;
3917 }
3918 if (STRNCMP(s + 5, "blob", 4) == 0)
3919 {
3920 rettv->v_type = VAR_BLOB;
3921 rettv->vval.v_blob = NULL;
3922 return OK;
3923 }
3924 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003925 case 10: if (STRNCMP(s, "null_class", 10) == 0)
3926 {
3927 rettv->v_type = VAR_CLASS;
3928 rettv->vval.v_class = NULL;
3929 return OK;
3930 }
3931 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003932 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3933 {
3934 rettv->v_type = VAR_STRING;
3935 rettv->vval.v_string = NULL;
3936 return OK;
3937 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003938 if (STRNCMP(s, "null_object", 11) == 0)
3939 {
3940 rettv->v_type = VAR_OBJECT;
3941 rettv->vval.v_object = NULL;
3942 return OK;
3943 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003944 break;
3945 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003946 if (STRNCMP(s, "null_channel", 12) == 0)
3947 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003948#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003949 rettv->v_type = VAR_CHANNEL;
3950 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003951#else
3952 rettv->v_type = VAR_SPECIAL;
3953 rettv->vval.v_number = VVAL_NULL;
3954#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003955 return OK;
3956 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003957 if (STRNCMP(s, "null_partial", 12) == 0)
3958 {
3959 rettv->v_type = VAR_PARTIAL;
3960 rettv->vval.v_partial = NULL;
3961 return OK;
3962 }
3963 break;
3964 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3965 {
3966 rettv->v_type = VAR_FUNC;
3967 rettv->vval.v_string = NULL;
3968 return OK;
3969 }
3970 break;
3971 }
3972 return FAIL;
3973}
3974
3975/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 * Handle sixth level expression:
3977 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003978 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003979 * "string" string constant
3980 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 * &option-name option value
3982 * @r register contents
3983 * identifier variable value
3984 * function() function call
3985 * $VAR environment variable
3986 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003987 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003988 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003989 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003990 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 *
3992 * Also handle:
3993 * ! in front logical NOT
3994 * - in front unary minus
3995 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003996 * trailing [] subscript in String or List
3997 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003998 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 *
4000 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004001 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 *
4003 * Return OK or FAIL.
4004 */
4005 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004006eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004007 char_u **arg,
4008 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004009 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004012 int evaluate = evalarg != NULL
4013 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 int len;
4015 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004016 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 char_u *start_leader, *end_leader;
4018 int ret = OK;
4019 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004020 static int recurse = 0;
4021 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022
4023 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004024 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004025 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004027 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028
4029 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004030 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 */
4032 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004033 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004034 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 end_leader = *arg;
4036
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004037 if (**arg == '.' && (!isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004038 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004039 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004040 ++*arg;
4041 return FAIL;
4042 }
4043
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004044 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004045 // and crash. With MSVC the stack is smaller.
4046 if (recurse ==
4047#ifdef _MSC_VER
4048 300
4049#else
4050 1000
4051#endif
4052 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004053 {
4054 semsg(_(e_expression_too_recursive_str), *arg);
4055 return FAIL;
4056 }
4057 ++recurse;
4058
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 switch (**arg)
4060 {
4061 /*
4062 * Number constant.
4063 */
4064 case '0':
4065 case '1':
4066 case '2':
4067 case '3':
4068 case '4':
4069 case '5':
4070 case '6':
4071 case '7':
4072 case '8':
4073 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004074 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004075
4076 // Apply prefixed "-" and "+" now. Matters especially when
4077 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004078 if (ret == OK && evaluate && end_leader > start_leader
4079 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004080 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 break;
4082
4083 /*
4084 * String constant: "string".
4085 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004086 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 break;
4088
4089 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004090 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004092 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004093 break;
4094
4095 /*
4096 * List: [expr, expr]
4097 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004098 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 break;
4100
4101 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004102 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004103 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004104 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004105 {
4106 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4107 }
4108 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004109 {
4110 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004111 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004112 }
4113 else
4114 ret = NOTDONE;
4115 break;
4116
4117 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004118 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004119 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004120 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004121 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004122 ret = NOTDONE;
4123 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004124 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004125 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004126 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004127 break;
4128
4129 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004130 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004132 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 break;
4134
4135 /*
4136 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004137 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 */
LemonBoy2eaef102022-05-06 13:14:50 +01004139 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4140 ret = eval_interp_string(arg, rettv, evaluate);
4141 else
4142 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 break;
4144
4145 /*
4146 * Register contents: @r.
4147 */
4148 case '@': ++*arg;
4149 if (evaluate)
4150 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004151 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004152 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004153 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004154 emsg_invreg(**arg);
4155 else
4156 {
4157 rettv->v_type = VAR_STRING;
4158 rettv->vval.v_string = get_reg_contents(**arg,
4159 GREG_EXPR_SRC);
4160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 }
4162 if (**arg != NUL)
4163 ++*arg;
4164 break;
4165
4166 /*
4167 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004168 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004170 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004171 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004172 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004173 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004174 if (ret == OK && evaluate)
4175 {
4176 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4177
Bram Moolenaara9931532021-06-12 15:58:16 +02004178 // Compile it here to get the return type. The return
4179 // type is optional, when it's missing use t_unknown.
4180 // This is recognized in compile_return().
4181 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4182 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004183 if (compile_def_function(ufunc, FALSE,
4184 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004185 {
4186 clear_tv(rettv);
4187 ret = FAIL;
4188 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004189 }
4190 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004191 if (ret == NOTDONE)
4192 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004193 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004194 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004195
Bram Moolenaar9215f012020-06-27 21:18:00 +02004196 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004197 if (**arg == ')')
4198 ++*arg;
4199 else if (ret == OK)
4200 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004201 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004202 clear_tv(rettv);
4203 ret = FAIL;
4204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 }
4206 break;
4207
Bram Moolenaar8c711452005-01-14 21:53:12 +00004208 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 break;
4210 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004211
4212 if (ret == NOTDONE)
4213 {
4214 /*
4215 * Must be a variable or function name.
4216 * Can also be a curly-braces kind of name: {expr}.
4217 */
4218 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004219 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004220 if (alias != NULL)
4221 s = alias;
4222
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004223 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004224 ret = FAIL;
4225 else
4226 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004227 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4228
Bram Moolenaar4525a572022-02-13 11:57:33 +00004229 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004230 {
4231 emsg(_(e_cannot_use_underscore_here));
4232 ret = FAIL;
4233 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004234 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004235 && s[0] == 's' && s[1] == ':')
4236 {
4237 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4238 ret = FAIL;
4239 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004240 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004241 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004242 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004243 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004244 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004245 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02004246 else if (flags & EVAL_CONSTANT)
4247 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004248 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004249 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004250 // get the value of "true", "false", etc. or a variable
4251 ret = FAIL;
4252 if (vim9script)
4253 ret = handle_predefined(s, len, rettv);
4254 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004255 {
4256 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004257 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004258 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004259 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004260 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004261 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004262 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004263 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004264 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004265 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004266 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004267 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004268 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004269 }
4270
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004271 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4272 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004273 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004274 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275
4276 /*
4277 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4278 */
4279 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004280 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004281
4282 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004283 return ret;
4284}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004285
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004286/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004287 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004288 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004289 * Adjusts "end_leaderp" until it is at "start_leader".
4290 */
4291 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004292eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004293 typval_T *rettv,
4294 int numeric_only,
4295 char_u *start_leader,
4296 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004297{
4298 char_u *end_leader = *end_leaderp;
4299 int ret = OK;
4300 int error = FALSE;
4301 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004302 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004303 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004304 float_T f = 0.0;
4305
4306 if (rettv->v_type == VAR_FLOAT)
4307 f = rettv->vval.v_float;
4308 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004309 {
4310 while (VIM_ISWHITE(end_leader[-1]))
4311 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004312 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004313 val = tv2bool(rettv);
4314 else
4315 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004316 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004317 if (error)
4318 {
4319 clear_tv(rettv);
4320 ret = FAIL;
4321 }
4322 else
4323 {
4324 while (end_leader > start_leader)
4325 {
4326 --end_leader;
4327 if (*end_leader == '!')
4328 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004329 if (numeric_only)
4330 {
4331 ++end_leader;
4332 break;
4333 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004334 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004335 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004336 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004337 {
4338 rettv->v_type = VAR_BOOL;
4339 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4340 }
4341 else
4342 f = !f;
4343 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004344 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004345 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004346 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004347 type = VAR_BOOL;
4348 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004349 }
4350 else if (*end_leader == '-')
4351 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004352 if (rettv->v_type == VAR_FLOAT)
4353 f = -f;
4354 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004355 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004356 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004357 type = VAR_NUMBER;
4358 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004359 }
4360 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004361 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004363 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004364 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004366 else
4367 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004368 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004369 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004370 rettv->v_type = type;
4371 else
4372 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004373 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004376 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 return ret;
4378}
4379
4380/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004381 * Call the function referred to in "rettv".
4382 */
4383 static int
4384call_func_rettv(
4385 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004386 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004387 typval_T *rettv,
4388 int evaluate,
4389 dict_T *selfdict,
4390 typval_T *basetv)
4391{
4392 partial_T *pt = NULL;
4393 funcexe_T funcexe;
4394 typval_T functv;
4395 char_u *s;
4396 int ret;
4397
4398 // need to copy the funcref so that we can clear rettv
4399 if (evaluate)
4400 {
4401 functv = *rettv;
4402 rettv->v_type = VAR_UNKNOWN;
4403
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004404 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004405 if (functv.v_type == VAR_PARTIAL)
4406 {
4407 pt = functv.vval.v_partial;
4408 s = partial_name(pt);
4409 }
4410 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004411 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004412 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004413 if (s == NULL || *s == NUL)
4414 {
4415 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004416 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004417 goto theend;
4418 }
4419 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004420 }
4421 else
4422 s = (char_u *)"";
4423
Bram Moolenaara80faa82020-04-12 19:37:17 +02004424 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004425 funcexe.fe_firstline = curwin->w_cursor.lnum;
4426 funcexe.fe_lastline = curwin->w_cursor.lnum;
4427 funcexe.fe_evaluate = evaluate;
4428 funcexe.fe_partial = pt;
4429 funcexe.fe_selfdict = selfdict;
4430 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004431 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004432
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004433theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004434 // Clear the funcref afterwards, so that deleting it while
4435 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004436 if (evaluate)
4437 clear_tv(&functv);
4438
4439 return ret;
4440}
4441
4442/*
4443 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004444 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004445 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4446 */
4447 static int
4448eval_lambda(
4449 char_u **arg,
4450 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004451 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004452 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004453{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004454 int evaluate = evalarg != NULL
4455 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004456 typval_T base = *rettv;
4457 int ret;
4458
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004459 rettv->v_type = VAR_UNKNOWN;
4460
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004461 if (**arg == '{')
4462 {
4463 // ->{lambda}()
4464 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4465 }
4466 else
4467 {
4468 // ->(lambda)()
4469 ++*arg;
4470 ret = eval1(arg, rettv, evalarg);
4471 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004472 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004473 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004474 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004475 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004476 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004477 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4478 && rettv->v_type != VAR_PARTIAL)
4479 {
4480 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4481 return FAIL;
4482 }
4483 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004484 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004485 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004486 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004487
4488 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004489 {
4490 if (verbose)
4491 {
4492 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004493 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004494 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004495 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004496 }
4497 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004498 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004499 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004500 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004501 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004502
4503 // Clear the funcref afterwards, so that deleting it while
4504 // evaluating the arguments is possible (see test55).
4505 if (evaluate)
4506 clear_tv(&base);
4507
4508 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004509}
4510
4511/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004512 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004513 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004514 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4515 */
4516 static int
4517eval_method(
4518 char_u **arg,
4519 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004520 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004521 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004522{
4523 char_u *name;
4524 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004525 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004526 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004527 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004528 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004529 int evaluate = evalarg != NULL
4530 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004531
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004532 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004533
Bram Moolenaarac92e252019-08-03 21:58:38 +02004534 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004535 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004536 if (alias != NULL)
4537 name = alias;
4538
4539 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004540 {
4541 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004542 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004543 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004544 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004545 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004546 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004547 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004548
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004549 // If there is no "(" immediately following, but there is further on,
4550 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4551 // Does not handle anything where "(" is part of the expression.
4552 *arg = skipwhite(*arg);
4553
4554 if (**arg != '(' && alias == NULL
4555 && (paren = vim_strchr(*arg, '(')) != NULL)
4556 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004557 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00004558
4559 // Truncate the name a the "(". Avoid trying to get another line
4560 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004561 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00004562 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
4563 if (evalarg != NULL)
4564 {
4565 getline = evalarg->eval_getline;
4566 evalarg->eval_getline = NULL;
4567 }
4568
4569 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004570 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004571 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004572 *arg = name + len;
4573 ret = FAIL;
4574 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004575 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004576 {
4577 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004578 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004579 }
Bram Moolenaar34820942022-12-19 20:28:38 +00004580
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004581 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00004582 if (getline != NULL)
4583 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004584 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004585
4586 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004587 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004588 *arg = skipwhite(*arg);
4589
4590 if (**arg != '(')
4591 {
4592 if (verbose)
4593 semsg(_(e_missing_parenthesis_str), name);
4594 ret = FAIL;
4595 }
4596 else if (VIM_ISWHITE((*arg)[-1]))
4597 {
4598 if (verbose)
4599 emsg(_(e_no_white_space_allowed_before_parenthesis));
4600 ret = FAIL;
4601 }
4602 else
4603 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004604 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004605 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004606 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004607
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004608 // Clear the funcref afterwards, so that deleting it while
4609 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004610 if (evaluate)
4611 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004612 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004613
Bram Moolenaarac92e252019-08-03 21:58:38 +02004614 return ret;
4615}
4616
4617/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004618 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4619 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004620 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4621 */
4622 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004623eval_index(
4624 char_u **arg,
4625 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004626 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004627 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004628{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004629 int evaluate = evalarg != NULL
4630 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004631 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004632 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004633 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004634 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004635 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004636 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004637
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004638 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4639 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004640
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004641 init_tv(&var1);
4642 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004643 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004644 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004645 /*
4646 * dict.name
4647 */
4648 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004649 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004650 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004651 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004652 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004653 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004654 }
4655 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004656 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004657 /*
4658 * something[idx]
4659 *
4660 * Get the (first) variable from inside the [].
4661 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004662 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004663 if (**arg == ':')
4664 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004665 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004666 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004667 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004668 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004669 semsg(_(e_white_space_required_before_and_after_str_at_str),
4670 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004671 clear_tv(&var1);
4672 return FAIL;
4673 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004674 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004675 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004676 int error = FALSE;
4677
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004678 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004679 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004680 && var1.v_type == VAR_FLOAT)
4681 {
4682 var1.vval.v_string = typval_tostring(&var1, TRUE);
4683 var1.v_type = VAR_STRING;
4684 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004685
Bram Moolenaar4525a572022-02-13 11:57:33 +00004686 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004687 tv_get_number_chk(&var1, &error);
4688 else
4689 error = tv_get_string_chk(&var1) == NULL;
4690 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004691 {
4692 // not a number or string
4693 clear_tv(&var1);
4694 return FAIL;
4695 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004696 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004697
4698 /*
4699 * Get the second variable from inside the [:].
4700 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004701 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004702 if (**arg == ':')
4703 {
4704 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004705 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004706 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004707 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004708 semsg(_(e_white_space_required_before_and_after_str_at_str),
4709 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004710 if (!empty1)
4711 clear_tv(&var1);
4712 return FAIL;
4713 }
4714 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004715 if (**arg == ']')
4716 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004717 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004718 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004719 if (!empty1)
4720 clear_tv(&var1);
4721 return FAIL;
4722 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004723 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004724 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004725 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004726 if (!empty1)
4727 clear_tv(&var1);
4728 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004729 return FAIL;
4730 }
4731 }
4732
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004733 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004734 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004735 if (**arg != ']')
4736 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004737 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004738 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004739 clear_tv(&var1);
4740 if (range)
4741 clear_tv(&var2);
4742 return FAIL;
4743 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004744 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004745 }
4746
4747 if (evaluate)
4748 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004749 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004750 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004751 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004752
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004753 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004754 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004755 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004756 clear_tv(&var2);
4757 return res;
4758 }
4759 return OK;
4760}
4761
4762/*
4763 * Check if "rettv" can have an [index] or [sli:ce]
4764 */
4765 int
4766check_can_index(typval_T *rettv, int evaluate, int verbose)
4767{
4768 switch (rettv->v_type)
4769 {
4770 case VAR_FUNC:
4771 case VAR_PARTIAL:
4772 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004773 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004774 return FAIL;
4775 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004776 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004777 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004778 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004779 case VAR_BOOL:
4780 case VAR_SPECIAL:
4781 case VAR_JOB:
4782 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004783 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004784 case VAR_CLASS:
4785 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004786 if (verbose)
4787 emsg(_(e_cannot_index_special_variable));
4788 return FAIL;
4789 case VAR_UNKNOWN:
4790 case VAR_ANY:
4791 case VAR_VOID:
4792 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004793 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004794 emsg(_(e_cannot_index_special_variable));
4795 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004796 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004797 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004798
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004799 case VAR_STRING:
4800 case VAR_LIST:
4801 case VAR_DICT:
4802 case VAR_BLOB:
4803 break;
4804 case VAR_NUMBER:
4805 if (in_vim9script())
4806 emsg(_(e_cannot_index_number));
4807 break;
4808 }
4809 return OK;
4810}
4811
4812/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004813 * slice() function
4814 */
4815 void
4816f_slice(typval_T *argvars, typval_T *rettv)
4817{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004818 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004819 && ((argvars[0].v_type != VAR_STRING
4820 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004821 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004822 && check_for_list_arg(argvars, 0) == FAIL)
4823 || check_for_number_arg(argvars, 1) == FAIL
4824 || check_for_opt_number_arg(argvars, 2) == FAIL))
4825 return;
4826
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00004827 if (check_can_index(argvars, TRUE, FALSE) != OK)
4828 return;
4829
4830 copy_tv(argvars, rettv);
4831 eval_index_inner(rettv, TRUE, argvars + 1,
4832 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4833 TRUE, NULL, 0, FALSE);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004834}
4835
4836/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004837 * Apply index or range to "rettv".
4838 * "var1" is the first index, NULL for [:expr].
4839 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004840 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4841 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004842 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4843 */
4844 int
4845eval_index_inner(
4846 typval_T *rettv,
4847 int is_range,
4848 typval_T *var1,
4849 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004850 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004851 char_u *key,
4852 int keylen,
4853 int verbose)
4854{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004855 varnumber_T n1, n2 = 0;
4856 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004857
4858 n1 = 0;
4859 if (var1 != NULL && rettv->v_type != VAR_DICT)
4860 n1 = tv_get_number(var1);
4861
4862 if (is_range)
4863 {
4864 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004865 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004866 if (verbose)
4867 emsg(_(e_cannot_slice_dictionary));
4868 return FAIL;
4869 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004870 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004871 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004872 else
4873 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004874 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004875
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004876 switch (rettv->v_type)
4877 {
4878 case VAR_UNKNOWN:
4879 case VAR_ANY:
4880 case VAR_VOID:
4881 case VAR_FUNC:
4882 case VAR_PARTIAL:
4883 case VAR_FLOAT:
4884 case VAR_BOOL:
4885 case VAR_SPECIAL:
4886 case VAR_JOB:
4887 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004888 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004889 case VAR_CLASS:
4890 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004891 break; // not evaluating, skipping over subscript
4892
4893 case VAR_NUMBER:
4894 case VAR_STRING:
4895 {
4896 char_u *s = tv_get_string(rettv);
4897
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004898 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004899 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004900 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004901 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004902 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004903 else
4904 s = char_from_string(s, n1);
4905 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004906 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004907 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004908 // The resulting variable is a substring. If the indexes
4909 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004910 if (n1 < 0)
4911 {
4912 n1 = len + n1;
4913 if (n1 < 0)
4914 n1 = 0;
4915 }
4916 if (n2 < 0)
4917 n2 = len + n2;
4918 else if (n2 >= len)
4919 n2 = len;
4920 if (n1 >= len || n2 < 0 || n1 > n2)
4921 s = NULL;
4922 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004923 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004924 }
4925 else
4926 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004927 // The resulting variable is a string of a single
4928 // character. If the index is too big or negative the
4929 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004930 if (n1 >= len || n1 < 0)
4931 s = NULL;
4932 else
4933 s = vim_strnsave(s + n1, 1);
4934 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004935 clear_tv(rettv);
4936 rettv->v_type = VAR_STRING;
4937 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004938 }
4939 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004940
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004941 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004942 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4943 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004944 break;
4945
4946 case VAR_LIST:
4947 if (var1 == NULL)
4948 n1 = 0;
4949 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004950 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004951 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004952 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004953 return FAIL;
4954 break;
4955
4956 case VAR_DICT:
4957 {
4958 dictitem_T *item;
4959 typval_T tmp;
4960
4961 if (key == NULL)
4962 {
4963 key = tv_get_string_chk(var1);
4964 if (key == NULL)
4965 return FAIL;
4966 }
4967
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004968 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004969
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004970 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004971 {
4972 if (verbose)
4973 {
4974 if (keylen > 0)
4975 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004976 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004977 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004978 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004979 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004980
4981 copy_tv(&item->di_tv, &tmp);
4982 clear_tv(rettv);
4983 *rettv = tmp;
4984 }
4985 break;
4986 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004987 return OK;
4988}
4989
4990/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004991 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004992 */
4993 char_u *
4994partial_name(partial_T *pt)
4995{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004996 if (pt != NULL)
4997 {
4998 if (pt->pt_name != NULL)
4999 return pt->pt_name;
5000 if (pt->pt_func != NULL)
5001 return pt->pt_func->uf_name;
5002 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005003 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005004}
5005
Bram Moolenaarddecc252016-04-06 22:59:37 +02005006 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005007partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005008{
5009 int i;
5010
5011 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005012 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005013 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005014 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005015 if (pt->pt_name != NULL)
5016 {
5017 func_unref(pt->pt_name);
5018 vim_free(pt->pt_name);
5019 }
5020 else
5021 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005022
Bram Moolenaar54656012021-06-09 20:50:46 +02005023 // "out_up" is no longer used, decrement refcount on partial that owns it.
5024 partial_unref(pt->pt_outer.out_up_partial);
5025
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005026 // Using pt_outer from another partial.
5027 partial_unref(pt->pt_outer_partial);
5028
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005029 // Decrease the reference count for the context of a closure. If down
5030 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005031 if (pt->pt_funcstack != NULL)
5032 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005033 --pt->pt_funcstack->fs_refcount;
5034 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005035 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005036 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005037 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5038 if (pt->pt_loopvars[i] != NULL)
5039 {
5040 --pt->pt_loopvars[i]->lvs_refcount;
5041 loopvars_check_refcount(pt->pt_loopvars[i]);
5042 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005043
Bram Moolenaarddecc252016-04-06 22:59:37 +02005044 vim_free(pt);
5045}
5046
5047/*
5048 * Unreference a closure: decrement the reference count and free it when it
5049 * becomes zero.
5050 */
5051 void
5052partial_unref(partial_T *pt)
5053{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005054 if (pt == NULL)
5055 return;
5056
5057 int done = FALSE;
5058
5059 if (--pt->pt_refcount <= 0)
5060 partial_free(pt);
5061
5062 // If the reference count goes down to one, the funcstack may be the
5063 // only reference and can be freed if no other partials reference it.
5064 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005065 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005066 // careful: if the funcstack is freed it may contain this partial
5067 // and it gets freed as well
5068 if (pt->pt_funcstack != NULL)
5069 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005070
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005071 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005072 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005073 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005074
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005075 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5076 if (pt->pt_loopvars[depth] != NULL
5077 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005078 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005079 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005080 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005081}
5082
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005083/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005084 * Return the next (unique) copy ID.
5085 * Used for serializing nested structures.
5086 */
5087 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005088get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005089{
5090 current_copyID += COPYID_INC;
5091 return current_copyID;
5092}
5093
5094/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005095 * Garbage collection for lists and dictionaries.
5096 *
5097 * We use reference counts to be able to free most items right away when they
5098 * are no longer used. But for composite items it's possible that it becomes
5099 * unused while the reference count is > 0: When there is a recursive
5100 * reference. Example:
5101 * :let l = [1, 2, 3]
5102 * :let d = {9: l}
5103 * :let l[1] = d
5104 *
5105 * Since this is quite unusual we handle this with garbage collection: every
5106 * once in a while find out which lists and dicts are not referenced from any
5107 * variable.
5108 *
5109 * Here is a good reference text about garbage collection (refers to Python
5110 * but it applies to all reference-counting mechanisms):
5111 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005112 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005113
5114/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005115 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005116 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005117 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005118 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005119 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005120garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005121{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005122 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005123 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005124 buf_T *buf;
5125 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005126 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005127 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005128
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005129 if (!testing)
5130 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005131 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005132 want_garbage_collect = FALSE;
5133 may_garbage_collect = FALSE;
5134 garbage_collect_at_exit = FALSE;
5135 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005136
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005137 // The execution stack can grow big, limit the size.
5138 if (exestack.ga_maxlen - exestack.ga_len > 500)
5139 {
5140 size_t new_len;
5141 char_u *pp;
5142 int n;
5143
5144 // Keep 150% of the current size, with a minimum of the growth size.
5145 n = exestack.ga_len / 2;
5146 if (n < exestack.ga_growsize)
5147 n = exestack.ga_growsize;
5148
5149 // Don't make it bigger though.
5150 if (exestack.ga_len + n < exestack.ga_maxlen)
5151 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005152 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005153 pp = vim_realloc(exestack.ga_data, new_len);
5154 if (pp == NULL)
5155 return FAIL;
5156 exestack.ga_maxlen = exestack.ga_len + n;
5157 exestack.ga_data = pp;
5158 }
5159 }
5160
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005161 // We advance by two because we add one for items referenced through
5162 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005163 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005164
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005165 /*
5166 * 1. Go through all accessible variables and mark all lists and dicts
5167 * with copyID.
5168 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005169
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005170 // Don't free variables in the previous_funccal list unless they are only
5171 // referenced through previous_funccal. This must be first, because if
5172 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005173 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005174
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005175 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005176 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005177
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005178 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005179 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005180 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5181 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005182
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005183 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005184 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005185 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5186 NULL, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005187 // window-local variables in autocmd windows
Bram Moolenaare76062c2022-11-28 18:51:43 +00005188 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005189 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00005190 abort = abort || set_ref_in_item(
5191 &aucmd_win[i].auc_win->w_winvar.di_tv, copyID, NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005192#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005193 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005194 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5195 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005196 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005197 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005198 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5199 NULL, NULL);
5200#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005201
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005202 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005203 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005204 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5205 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005206 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005207 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005208
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005209 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005210 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005211
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005212 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005213 abort = abort || set_ref_in_functions(copyID);
5214
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005215 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005216 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005217
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005218 // funcstacks keep variables for closures
5219 abort = abort || set_ref_in_funcstacks(copyID);
5220
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005221 // loopvars keep variables for loop blocks
5222 abort = abort || set_ref_in_loopvars(copyID);
5223
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005224 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005225 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005226
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005227 // callbacks in buffers
5228 abort = abort || set_ref_in_buffers(copyID);
5229
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005230 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5231 abort = abort || set_ref_in_insexpand_funcs(copyID);
5232
5233 // 'operatorfunc' callback
5234 abort = abort || set_ref_in_opfunc(copyID);
5235
5236 // 'tagfunc' callback
5237 abort = abort || set_ref_in_tagfunc(copyID);
5238
5239 // 'imactivatefunc' and 'imstatusfunc' callbacks
5240 abort = abort || set_ref_in_im_funcs(copyID);
5241
Bram Moolenaar1dced572012-04-05 16:54:08 +02005242#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005243 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005244#endif
5245
Bram Moolenaardb913952012-06-29 12:54:53 +02005246#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005247 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005248#endif
5249
5250#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005251 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005252#endif
5253
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005254#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005255 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005256 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005257#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005258#ifdef FEAT_NETBEANS_INTG
5259 abort = abort || set_ref_in_nb_channel(copyID);
5260#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005261
Bram Moolenaare3188e22016-05-31 21:13:04 +02005262#ifdef FEAT_TIMERS
5263 abort = abort || set_ref_in_timer(copyID);
5264#endif
5265
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005266#ifdef FEAT_QUICKFIX
5267 abort = abort || set_ref_in_quickfix(copyID);
5268#endif
5269
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005270#ifdef FEAT_TERMINAL
5271 abort = abort || set_ref_in_term(copyID);
5272#endif
5273
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005274#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005275 abort = abort || set_ref_in_popups(copyID);
5276#endif
5277
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005278 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005279 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005280 /*
5281 * 2. Free lists and dictionaries that are not referenced.
5282 */
5283 did_free = free_unref_items(copyID);
5284
5285 /*
5286 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005287 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005288 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005289 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005290 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005291 else if (p_verbose > 0)
5292 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005293 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005294 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005295
5296 return did_free;
5297}
5298
5299/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005300 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005301 */
5302 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005303free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005304{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005305 int did_free = FALSE;
5306
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005307 // Let all "free" functions know that we are here. This means no
5308 // dictionaries, lists, channels or jobs are to be freed, because we will
5309 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005310 in_free_unref_items = TRUE;
5311
5312 /*
5313 * PASS 1: free the contents of the items. We don't free the items
5314 * themselves yet, so that it is possible to decrement refcount counters
5315 */
5316
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005317 // Go through the list of dicts and free items without this copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005318 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005319
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005320 // Go through the list of lists and free items without this copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005321 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005322
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005323 // Go through the list of objects and free items without this copyID.
5324 did_free |= object_free_nonref(copyID);
5325
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005326#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005327 // Go through the list of jobs and free items without the copyID. This
5328 // must happen before doing channels, because jobs refer to channels, but
5329 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005330 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5331
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005332 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005333 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5334#endif
5335
5336 /*
5337 * PASS 2: free the items themselves.
5338 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005339 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005340 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005341
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005342#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005343 // Go through the list of jobs and free items without the copyID. This
5344 // must happen before doing channels, because jobs refer to channels, but
5345 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005346 free_unused_jobs(copyID, COPYID_MASK);
5347
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005348 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005349 free_unused_channels(copyID, COPYID_MASK);
5350#endif
5351
5352 in_free_unref_items = FALSE;
5353
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005354 return did_free;
5355}
5356
5357/*
5358 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005359 * "list_stack" is used to add lists to be marked. Can be NULL.
5360 *
5361 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005362 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005363 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005364set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005365{
5366 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005367 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005368 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005369 hashtab_T *cur_ht;
5370 ht_stack_T *ht_stack = NULL;
5371 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005372
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005373 cur_ht = ht;
5374 for (;;)
5375 {
5376 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005377 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005378 // Mark each item in the hashtab. If the item contains a hashtab
5379 // it is added to ht_stack, if it contains a list it is added to
5380 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005381 todo = (int)cur_ht->ht_used;
5382 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5383 if (!HASHITEM_EMPTY(hi))
5384 {
5385 --todo;
5386 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5387 &ht_stack, list_stack);
5388 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005389 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005390
5391 if (ht_stack == NULL)
5392 break;
5393
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005394 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005395 cur_ht = ht_stack->ht;
5396 tempitem = ht_stack;
5397 ht_stack = ht_stack->prev;
5398 free(tempitem);
5399 }
5400
5401 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005402}
5403
Dominique Pelle748b3082022-01-08 12:41:16 +00005404#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5405 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005406/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005407 * Mark a dict and its items with "copyID".
5408 * Returns TRUE if setting references failed somehow.
5409 */
5410 int
5411set_ref_in_dict(dict_T *d, int copyID)
5412{
5413 if (d != NULL && d->dv_copyID != copyID)
5414 {
5415 d->dv_copyID = copyID;
5416 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5417 }
5418 return FALSE;
5419}
Dominique Pelle748b3082022-01-08 12:41:16 +00005420#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005421
5422/*
5423 * Mark a list and its items with "copyID".
5424 * Returns TRUE if setting references failed somehow.
5425 */
5426 int
5427set_ref_in_list(list_T *ll, int copyID)
5428{
5429 if (ll != NULL && ll->lv_copyID != copyID)
5430 {
5431 ll->lv_copyID = copyID;
5432 return set_ref_in_list_items(ll, copyID, NULL);
5433 }
5434 return FALSE;
5435}
5436
5437/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005438 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005439 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5440 *
5441 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005442 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005443 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005444set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005445{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005446 listitem_T *li;
5447 int abort = FALSE;
5448 list_T *cur_l;
5449 list_stack_T *list_stack = NULL;
5450 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005451
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005452 cur_l = l;
5453 for (;;)
5454 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005455 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005456 // Mark each item in the list. If the item contains a hashtab
5457 // it is added to ht_stack, if it contains a list it is added to
5458 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005459 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5460 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5461 ht_stack, &list_stack);
5462 if (list_stack == NULL)
5463 break;
5464
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005465 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005466 cur_l = list_stack->list;
5467 tempitem = list_stack;
5468 list_stack = list_stack->prev;
5469 free(tempitem);
5470 }
5471
5472 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005473}
5474
5475/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005476 * Mark the partial in callback 'cb' with "copyID".
5477 */
5478 int
5479set_ref_in_callback(callback_T *cb, int copyID)
5480{
5481 typval_T tv;
5482
5483 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5484 return FALSE;
5485
5486 tv.v_type = VAR_PARTIAL;
5487 tv.vval.v_partial = cb->cb_partial;
5488 return set_ref_in_item(&tv, copyID, NULL, NULL);
5489}
5490
5491/*
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005492 * Mark the dict "dd" with "copyID".
5493 * Also see set_ref_in_item().
5494 */
5495 static int
5496set_ref_in_item_dict(
5497 dict_T *dd,
5498 int copyID,
5499 ht_stack_T **ht_stack,
5500 list_stack_T **list_stack)
5501{
5502 if (dd == NULL || dd->dv_copyID == copyID)
5503 return FALSE;
5504
5505 // Didn't see this dict yet.
5506 dd->dv_copyID = copyID;
5507 if (ht_stack == NULL)
5508 return set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5509
5510 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5511 if (newitem == NULL)
5512 return TRUE;
5513
5514 newitem->ht = &dd->dv_hashtab;
5515 newitem->prev = *ht_stack;
5516 *ht_stack = newitem;
5517
5518 return FALSE;
5519}
5520
5521/*
5522 * Mark the list "ll" with "copyID".
5523 * Also see set_ref_in_item().
5524 */
5525 static int
5526set_ref_in_item_list(
5527 list_T *ll,
5528 int copyID,
5529 ht_stack_T **ht_stack,
5530 list_stack_T **list_stack)
5531{
5532 if (ll == NULL || ll->lv_copyID == copyID)
5533 return FALSE;
5534
5535 // Didn't see this list yet.
5536 ll->lv_copyID = copyID;
5537 if (list_stack == NULL)
5538 return set_ref_in_list_items(ll, copyID, ht_stack);
5539
5540 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5541 if (newitem == NULL)
5542 return TRUE;
5543
5544 newitem->list = ll;
5545 newitem->prev = *list_stack;
5546 *list_stack = newitem;
5547
5548 return FALSE;
5549}
5550
5551/*
5552 * Mark the partial "pt" with "copyID".
5553 * Also see set_ref_in_item().
5554 */
5555 static int
5556set_ref_in_item_partial(
5557 partial_T *pt,
5558 int copyID,
5559 ht_stack_T **ht_stack,
5560 list_stack_T **list_stack)
5561{
5562 if (pt == NULL || pt->pt_copyID == copyID)
5563 return FALSE;
5564
5565 // Didn't see this partial yet.
5566 pt->pt_copyID = copyID;
5567
5568 int abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
5569
5570 if (pt->pt_dict != NULL)
5571 {
5572 typval_T dtv;
5573
5574 dtv.v_type = VAR_DICT;
5575 dtv.vval.v_dict = pt->pt_dict;
5576 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5577 }
5578
5579 for (int i = 0; i < pt->pt_argc; ++i)
5580 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5581 ht_stack, list_stack);
5582 // pt_funcstack is handled in set_ref_in_funcstacks()
5583 // pt_loopvars is handled in set_ref_in_loopvars()
5584
5585 return abort;
5586}
5587
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005588#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005589/*
5590 * Mark the job "pt" with "copyID".
5591 * Also see set_ref_in_item().
5592 */
5593 static int
5594set_ref_in_item_job(
5595 job_T *job,
5596 int copyID,
5597 ht_stack_T **ht_stack,
5598 list_stack_T **list_stack)
5599{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005600 typval_T dtv;
5601
5602 if (job == NULL || job->jv_copyID == copyID)
5603 return FALSE;
5604
5605 job->jv_copyID = copyID;
5606 if (job->jv_channel != NULL)
5607 {
5608 dtv.v_type = VAR_CHANNEL;
5609 dtv.vval.v_channel = job->jv_channel;
5610 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5611 }
5612 if (job->jv_exit_cb.cb_partial != NULL)
5613 {
5614 dtv.v_type = VAR_PARTIAL;
5615 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
5616 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5617 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005618
5619 return FALSE;
5620}
5621
5622/*
5623 * Mark the channel "ch" with "copyID".
5624 * Also see set_ref_in_item().
5625 */
5626 static int
5627set_ref_in_item_channel(
5628 channel_T *ch,
5629 int copyID,
5630 ht_stack_T **ht_stack,
5631 list_stack_T **list_stack)
5632{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005633 typval_T dtv;
5634
5635 if (ch == NULL || ch->ch_copyID == copyID)
5636 return FALSE;
5637
5638 ch->ch_copyID = copyID;
5639 for (ch_part_T part = PART_SOCK; part < PART_COUNT; ++part)
5640 {
5641 for (jsonq_T *jq = ch->ch_part[part].ch_json_head.jq_next;
5642 jq != NULL; jq = jq->jq_next)
5643 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5644 for (cbq_T *cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5645 cq = cq->cq_next)
5646 if (cq->cq_callback.cb_partial != NULL)
5647 {
5648 dtv.v_type = VAR_PARTIAL;
5649 dtv.vval.v_partial = cq->cq_callback.cb_partial;
5650 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5651 }
5652 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
5653 {
5654 dtv.v_type = VAR_PARTIAL;
5655 dtv.vval.v_partial = ch->ch_part[part].ch_callback.cb_partial;
5656 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5657 }
5658 }
5659 if (ch->ch_callback.cb_partial != NULL)
5660 {
5661 dtv.v_type = VAR_PARTIAL;
5662 dtv.vval.v_partial = ch->ch_callback.cb_partial;
5663 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5664 }
5665 if (ch->ch_close_cb.cb_partial != NULL)
5666 {
5667 dtv.v_type = VAR_PARTIAL;
5668 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
5669 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5670 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005671
5672 return FALSE;
5673}
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005674#endif
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005675
5676/*
5677 * Mark the class "cl" with "copyID".
5678 * Also see set_ref_in_item().
5679 */
5680 static int
5681set_ref_in_item_class(
5682 class_T *cl,
5683 int copyID,
5684 ht_stack_T **ht_stack,
5685 list_stack_T **list_stack)
5686{
5687 int abort = FALSE;
5688
5689 if (cl == NULL || cl->class_copyID == copyID
5690 || (cl->class_flags & CLASS_INTERFACE) != 0)
5691 return FALSE;
5692
5693 cl->class_copyID = copyID;
5694 for (int i = 0; !abort && i < cl->class_class_member_count; ++i)
5695 abort = abort || set_ref_in_item(
5696 &cl->class_members_tv[i],
5697 copyID, ht_stack, list_stack);
5698
5699 for (int i = 0; !abort && i < cl->class_class_function_count; ++i)
5700 abort = abort || set_ref_in_func(NULL,
5701 cl->class_class_functions[i], copyID);
5702
5703 for (int i = 0; !abort && i < cl->class_obj_method_count; ++i)
5704 abort = abort || set_ref_in_func(NULL,
5705 cl->class_obj_methods[i], copyID);
5706
5707 return abort;
5708}
5709
5710/*
5711 * Mark the object "cl" with "copyID".
5712 * Also see set_ref_in_item().
5713 */
5714 static int
5715set_ref_in_item_object(
5716 object_T *obj,
5717 int copyID,
5718 ht_stack_T **ht_stack,
5719 list_stack_T **list_stack)
5720{
5721 int abort = FALSE;
5722
5723 if (obj == NULL || obj->obj_copyID == copyID)
5724 return FALSE;
5725
5726 obj->obj_copyID = copyID;
5727
5728 // The typval_T array is right after the object_T.
5729 typval_T *mtv = (typval_T *)(obj + 1);
5730 for (int i = 0; !abort
5731 && i < obj->obj_class->class_obj_member_count; ++i)
5732 abort = abort || set_ref_in_item(mtv + i, copyID,
5733 ht_stack, list_stack);
5734
5735 return abort;
5736}
5737
5738/*
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005739 * Mark all lists, dicts and other container types referenced through typval
5740 * "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005741 * "list_stack" is used to add lists to be marked. Can be NULL.
5742 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5743 *
5744 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005745 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005746 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005747set_ref_in_item(
5748 typval_T *tv,
5749 int copyID,
5750 ht_stack_T **ht_stack,
5751 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005752{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005753 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005754
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005755 switch (tv->v_type)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005756 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005757 case VAR_DICT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005758 return set_ref_in_item_dict(tv->vval.v_dict, copyID,
5759 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005760
5761 case VAR_LIST:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005762 return set_ref_in_item_list(tv->vval.v_list, copyID,
5763 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005764
5765 case VAR_FUNC:
5766 {
5767 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
5768 break;
5769 }
5770
5771 case VAR_PARTIAL:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005772 return set_ref_in_item_partial(tv->vval.v_partial, copyID,
5773 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005774
5775 case VAR_JOB:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005776#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005777 return set_ref_in_item_job(tv->vval.v_job, copyID,
5778 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005779#else
5780 break;
5781#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005782
5783 case VAR_CHANNEL:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005784#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005785 return set_ref_in_item_channel(tv->vval.v_channel, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005786 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005787#else
5788 break;
5789#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005790
5791 case VAR_CLASS:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005792 return set_ref_in_item_class(tv->vval.v_class, copyID,
5793 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005794
5795 case VAR_OBJECT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005796 return set_ref_in_item_object(tv->vval.v_object, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005797 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005798
5799 case VAR_UNKNOWN:
5800 case VAR_ANY:
5801 case VAR_VOID:
5802 case VAR_BOOL:
5803 case VAR_SPECIAL:
5804 case VAR_NUMBER:
5805 case VAR_FLOAT:
5806 case VAR_STRING:
5807 case VAR_BLOB:
5808 case VAR_INSTR:
5809 // Types that do not contain any other item
5810 break;
5811 }
5812
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005813 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005814}
5815
Bram Moolenaar8c711452005-01-14 21:53:12 +00005816/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005817 * Return a string with the string representation of a variable.
5818 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005819 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005820 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005821 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005822 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005823 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005824 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5825 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005826 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005827 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005828 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005829echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005830 typval_T *tv,
5831 char_u **tofree,
5832 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005833 int copyID,
5834 int echo_style,
5835 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005836 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005837{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005838 static int recurse = 0;
5839 char_u *r = NULL;
5840
Bram Moolenaar33570922005-01-25 22:26:29 +00005841 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005842 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005843 if (!did_echo_string_emsg)
5844 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005845 // Only give this message once for a recursive call to avoid
5846 // flooding the user with errors. And stop iterating over lists
5847 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005848 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005849 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005850 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005851 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005852 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005853 }
5854 ++recurse;
5855
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005856 switch (tv->v_type)
5857 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005858 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005859 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005860 {
5861 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005862 r = tv->vval.v_string;
5863 if (r == NULL)
5864 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005865 }
5866 else
5867 {
5868 *tofree = string_quote(tv->vval.v_string, FALSE);
5869 r = *tofree;
5870 }
5871 break;
5872
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005873 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005874 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005875 char_u buf[MAX_FUNC_NAME_LEN];
5876
5877 if (echo_style)
5878 {
LemonBoya5d35902022-04-29 21:15:02 +01005879 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5880 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005881 buf, MAX_FUNC_NAME_LEN);
5882 if (r == buf)
5883 {
5884 r = vim_strsave(buf);
5885 *tofree = r;
5886 }
5887 else
5888 *tofree = NULL;
5889 }
5890 else
5891 {
5892 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5893 : make_ufunc_name_readable(
5894 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5895 TRUE);
5896 r = *tofree;
5897 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005898 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005899 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005900
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005901 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005902 {
5903 partial_T *pt = tv->vval.v_partial;
5904 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005905 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005906 garray_T ga;
5907 int i;
5908 char_u *tf;
5909
5910 ga_init2(&ga, 1, 100);
5911 ga_concat(&ga, (char_u *)"function(");
5912 if (fname != NULL)
5913 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005914 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005915 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005916 && vim_isupper(fname[1]))
5917 {
5918 ga_concat(&ga, (char_u *)"'g:");
5919 ga_concat(&ga, fname + 1);
5920 }
5921 else
5922 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005923 vim_free(fname);
5924 }
5925 if (pt != NULL && pt->pt_argc > 0)
5926 {
5927 ga_concat(&ga, (char_u *)", [");
5928 for (i = 0; i < pt->pt_argc; ++i)
5929 {
5930 if (i > 0)
5931 ga_concat(&ga, (char_u *)", ");
5932 ga_concat(&ga,
5933 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5934 vim_free(tf);
5935 }
5936 ga_concat(&ga, (char_u *)"]");
5937 }
5938 if (pt != NULL && pt->pt_dict != NULL)
5939 {
5940 typval_T dtv;
5941
5942 ga_concat(&ga, (char_u *)", ");
5943 dtv.v_type = VAR_DICT;
5944 dtv.vval.v_dict = pt->pt_dict;
5945 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5946 vim_free(tf);
5947 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005948 // terminate with ')' and a NUL
5949 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005950
5951 *tofree = ga.ga_data;
5952 r = *tofree;
5953 break;
5954 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005955
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005956 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005957 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005958 break;
5959
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005960 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005961 if (tv->vval.v_list == NULL)
5962 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005963 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005964 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005965 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005966 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005967 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5968 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005969 {
5970 *tofree = NULL;
5971 r = (char_u *)"[...]";
5972 }
5973 else
5974 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005975 int old_copyID = tv->vval.v_list->lv_copyID;
5976
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005977 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005978 *tofree = list2string(tv, copyID, restore_copyID);
5979 if (restore_copyID)
5980 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005981 r = *tofree;
5982 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005983 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005984
Bram Moolenaar8c711452005-01-14 21:53:12 +00005985 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005986 if (tv->vval.v_dict == NULL)
5987 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005988 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005989 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005990 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005991 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005992 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5993 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005994 {
5995 *tofree = NULL;
5996 r = (char_u *)"{...}";
5997 }
5998 else
5999 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006000 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006001
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006002 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006003 *tofree = dict2string(tv, copyID, restore_copyID);
6004 if (restore_copyID)
6005 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006006 r = *tofree;
6007 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006008 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006009
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006010 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006011 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006012 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006013 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006014 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006015 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006016 break;
6017
Bram Moolenaar835dc632016-02-07 14:27:38 +01006018 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006019 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006020#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00006021 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02006022 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
6023 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006024 if (composite_val)
6025 {
6026 *tofree = string_quote(r, FALSE);
6027 r = *tofree;
6028 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006029#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006031
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006032 case VAR_INSTR:
6033 *tofree = NULL;
6034 r = (char_u *)"instructions";
6035 break;
6036
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006037 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006038 {
6039 class_T *cl = tv->vval.v_class;
6040 size_t len = 6 + (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
6041 r = *tofree = alloc(len);
6042 vim_snprintf((char *)r, len, "class %s",
6043 cl == NULL ? "[unknown]" : (char *)cl->class_name);
6044 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006045 break;
6046
6047 case VAR_OBJECT:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006048 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006049 garray_T ga;
6050 ga_init2(&ga, 1, 50);
6051 ga_concat(&ga, (char_u *)"object of ");
6052 object_T *obj = tv->vval.v_object;
6053 class_T *cl = obj == NULL ? NULL : obj->obj_class;
6054 ga_concat(&ga, cl == NULL ? (char_u *)"[unknown]"
6055 : cl->class_name);
6056 if (cl != NULL)
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006057 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006058 ga_concat(&ga, (char_u *)" {");
6059 for (int i = 0; i < cl->class_obj_member_count; ++i)
6060 {
6061 if (i > 0)
6062 ga_concat(&ga, (char_u *)", ");
Bram Moolenaard505d172022-12-18 21:42:55 +00006063 ocmember_T *m = &cl->class_obj_members[i];
6064 ga_concat(&ga, m->ocm_name);
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006065 ga_concat(&ga, (char_u *)": ");
6066 char_u *tf = NULL;
6067 ga_concat(&ga, echo_string_core(
6068 (typval_T *)(obj + 1) + i,
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006069 &tf, numbuf, copyID, echo_style,
6070 restore_copyID, composite_val));
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006071 vim_free(tf);
6072 }
6073 ga_concat(&ga, (char_u *)"}");
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006074 }
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006075
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006076 *tofree = r = ga.ga_data;
6077 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006078 break;
6079
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006080 case VAR_FLOAT:
6081 *tofree = NULL;
6082 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6083 r = numbuf;
6084 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006085
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006086 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006087 case VAR_SPECIAL:
6088 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006089 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006090 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006091 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006092
Bram Moolenaar8502c702014-06-17 12:51:16 +02006093 if (--recurse == 0)
6094 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006095 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006096}
6097
6098/*
6099 * Return a string with the string representation of a variable.
6100 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6101 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006102 * Does not put quotes around strings, as ":echo" displays values.
6103 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6104 * May return NULL.
6105 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006106 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006107echo_string(
6108 typval_T *tv,
6109 char_u **tofree,
6110 char_u *numbuf,
6111 int copyID)
6112{
6113 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6114}
6115
6116/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006117 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6118 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006119 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006120 */
6121 int
6122buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6123{
6124 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006125 char_u *t;
6126 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006127
6128 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6129 return -1;
6130
6131 if (lnum > buf->b_ml.ml_line_count)
6132 lnum = buf->b_ml.ml_line_count;
6133
6134 str = ml_get_buf(buf, lnum, FALSE);
6135 if (str == NULL)
6136 return -1;
6137
6138 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006139 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006140
Bram Moolenaar91458462021-01-13 20:08:38 +01006141 // count the number of characters
6142 t = str;
6143 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6144 t += mb_ptr2len(t);
6145
6146 // In insert mode, when the cursor is at the end of a non-empty line,
6147 // byteidx points to the NUL character immediately past the end of the
6148 // string. In this case, add one to the character count.
6149 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6150 count++;
6151
6152 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006153}
6154
6155/*
6156 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006157 * byte index. Works only for loaded buffers. Returns -1 on failure.
6158 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006159 */
6160 int
6161buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6162{
6163 char_u *str;
6164 char_u *t;
6165
6166 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6167 return -1;
6168
6169 if (lnum > buf->b_ml.ml_line_count)
6170 lnum = buf->b_ml.ml_line_count;
6171
6172 str = ml_get_buf(buf, lnum, FALSE);
6173 if (str == NULL)
6174 return -1;
6175
6176 // Convert the character offset to a byte offset
6177 t = str;
6178 while (*t != NUL && --charidx > 0)
6179 t += mb_ptr2len(t);
6180
Bram Moolenaar91458462021-01-13 20:08:38 +01006181 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006182}
6183
6184/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006186 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006188 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006189var2fpos(
6190 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006191 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006192 int *fnum, // set to fnum for '0, 'A, etc.
6193 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006195 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006197 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006199 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006200 if (varp->v_type == VAR_LIST)
6201 {
6202 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006203 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006204 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006205 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006206
6207 l = varp->vval.v_list;
6208 if (l == NULL)
6209 return NULL;
6210
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006211 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006212 pos.lnum = list_find_nr(l, 0L, &error);
6213 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006214 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006215 if (charcol)
6216 len = (long)mb_charlen(ml_get(pos.lnum));
6217 else
6218 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006219
Bram Moolenaarec65d772020-08-20 22:29:12 +02006220 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006221 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006222 li = list_find(l, 1L);
6223 if (li != NULL && li->li_tv.v_type == VAR_STRING
6224 && li->li_tv.vval.v_string != NULL
6225 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006226 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006227 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006228 }
6229 else
6230 {
6231 pos.col = list_find_nr(l, 1L, &error);
6232 if (error)
6233 return NULL;
6234 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006235
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006236 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006237 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006238 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006239 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006240
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006241 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006242 pos.coladd = list_find_nr(l, 2L, &error);
6243 if (error)
6244 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006245
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006246 return &pos;
6247 }
6248
Bram Moolenaarc5809432021-03-27 21:23:30 +01006249 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6250 return NULL;
6251
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006252 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006253 if (name == NULL)
6254 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006255
6256 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006257 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006258 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006259 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006260 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006261 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006262 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006263 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006264 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006265 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006266 pos = VIsual;
6267 else
6268 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006269 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006270 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006271 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006273 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006274 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6276 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006277 pos = *pp;
6278 }
6279 if (pos.lnum != 0)
6280 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006281 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006282 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6283 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006285
Bram Moolenaara5525202006-03-02 22:52:09 +00006286 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006287
Bram Moolenaar477933c2007-07-17 14:32:23 +00006288 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006289 {
6290 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006291 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006292 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006293 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006294 // In silent Ex mode topline is zero, but that's not a valid line
6295 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006296 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006297 return &pos;
6298 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006299 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006300 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006301 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006302 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006303 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006304 return &pos;
6305 }
6306 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006307 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006309 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 {
6311 pos.lnum = curbuf->b_ml.ml_line_count;
6312 pos.col = 0;
6313 }
6314 else
6315 {
6316 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006317 if (charcol)
6318 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6319 else
6320 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 }
6322 return &pos;
6323 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006324 if (in_vim9script())
6325 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 return NULL;
6327}
6328
6329/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006330 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006331 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006332 * Note that the column is passed on as-is, the caller may want to decrement
6333 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006334 * If "charcol" is TRUE use the column as the character index instead of the
6335 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006336 * Return FAIL when conversion is not possible, doesn't check the position for
6337 * validity.
6338 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006339 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006340list2fpos(
6341 typval_T *arg,
6342 pos_T *posp,
6343 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006344 colnr_T *curswantp,
6345 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006346{
6347 list_T *l = arg->vval.v_list;
6348 long i = 0;
6349 long n;
6350
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006351 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6352 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006353 if (arg->v_type != VAR_LIST
6354 || l == NULL
6355 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006356 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006357 return FAIL;
6358
6359 if (fnump != NULL)
6360 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006361 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006362 if (n < 0)
6363 return FAIL;
6364 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006365 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006366 *fnump = n;
6367 }
6368
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006369 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006370 if (n < 0)
6371 return FAIL;
6372 posp->lnum = n;
6373
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006374 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006375 if (n < 0)
6376 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006377 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006378 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006379 if (charcol)
6380 {
6381 buf_T *buf;
6382
6383 // Get the text for the specified line in a loaded buffer
6384 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6385 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6386 return FAIL;
6387
Bram Moolenaar79f23442022-10-10 12:42:57 +01006388 n = buf_charidx_to_byteidx(buf,
6389 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006390 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006391 posp->col = n;
6392
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006393 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006394 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006395 posp->coladd = 0;
6396 else
6397 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006398
Bram Moolenaar493c1782014-05-28 14:34:46 +02006399 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006400 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006401
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006402 return OK;
6403}
6404
6405/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 * Get the length of an environment variable name.
6407 * Advance "arg" to the first character after the name.
6408 * Return 0 for error.
6409 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006410 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006411get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412{
6413 char_u *p;
6414 int len;
6415
6416 for (p = *arg; vim_isIDc(*p); ++p)
6417 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006418 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 return 0;
6420
6421 len = (int)(p - *arg);
6422 *arg = p;
6423 return len;
6424}
6425
6426/*
6427 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006428 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 * Return 0 if something is wrong.
6430 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006431 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006432get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433{
6434 char_u *p;
6435 int len;
6436
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006437 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006439 {
6440 if (*p == ':')
6441 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006442 // "s:" is start of "s:var", but "n:" is not and can be used in
6443 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006444 len = (int)(p - *arg);
6445 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6446 || len > 1)
6447 break;
6448 }
6449 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006450 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 return 0;
6452
6453 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006454 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455
6456 return len;
6457}
6458
6459/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006460 * Get the length of the name of a variable or function.
6461 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006463 * Return -1 if curly braces expansion failed.
6464 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 * If the name contains 'magic' {}'s, expand them and return the
6466 * expanded name in an allocated string via 'alias' - caller must free.
6467 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006468 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006469get_name_len(
6470 char_u **arg,
6471 char_u **alias,
6472 int evaluate,
6473 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474{
6475 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 char_u *p;
6477 char_u *expr_start;
6478 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006480 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481
6482 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6483 && (*arg)[2] == (int)KE_SNR)
6484 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006485 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 *arg += 3;
6487 return get_id_len(arg) + 3;
6488 }
6489 len = eval_fname_script(*arg);
6490 if (len > 0)
6491 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006492 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 *arg += len;
6494 }
6495
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006497 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006499 p = find_name_end(*arg, &expr_start, &expr_end,
6500 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 if (expr_start != NULL)
6502 {
6503 char_u *temp_string;
6504
6505 if (!evaluate)
6506 {
6507 len += (int)(p - *arg);
6508 *arg = skipwhite(p);
6509 return len;
6510 }
6511
6512 /*
6513 * Include any <SID> etc in the expanded string:
6514 * Thus the -len here.
6515 */
6516 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6517 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006518 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 *alias = temp_string;
6520 *arg = skipwhite(p);
6521 return (int)STRLEN(temp_string);
6522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523
6524 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006525 // Only give an error when there is something, otherwise it will be
6526 // reported at a higher level.
6527 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006528 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529
6530 return len;
6531}
6532
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006533/*
6534 * Find the end of a variable or function name, taking care of magic braces.
6535 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6536 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006537 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006538 * Return a pointer to just after the name. Equal to "arg" if there is no
6539 * valid name.
6540 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006541 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006542find_name_end(
6543 char_u *arg,
6544 char_u **expr_start,
6545 char_u **expr_end,
6546 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006548 int mb_nest = 0;
6549 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006551 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02006552 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006554 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006556 *expr_start = NULL;
6557 *expr_end = NULL;
6558 }
6559
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006560 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006561 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
6562 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006563 return arg;
6564
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006565 for (p = arg; *p != NUL
6566 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006567 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006568 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006569 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006570 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006571 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006572 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006573 if (*p == '\'')
6574 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006575 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006576 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006577 ;
6578 if (*p == NUL)
6579 break;
6580 }
6581 else if (*p == '"')
6582 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006583 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006584 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006585 if (*p == '\\' && p[1] != NUL)
6586 ++p;
6587 if (*p == NUL)
6588 break;
6589 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006590 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6591 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006592 // "s:" is start of "s:var", but "n:" is not and can be used in
6593 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006594 len = (int)(p - arg);
6595 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006596 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006597 break;
6598 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006599
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006600 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006602 if (*p == '[')
6603 ++br_nest;
6604 else if (*p == ']')
6605 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006607
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006608 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006610 if (*p == '{')
6611 {
6612 mb_nest++;
6613 if (expr_start != NULL && *expr_start == NULL)
6614 *expr_start = p;
6615 }
6616 else if (*p == '}')
6617 {
6618 mb_nest--;
6619 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6620 *expr_end = p;
6621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 }
6624
6625 return p;
6626}
6627
6628/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006629 * Expands out the 'magic' {}'s in a variable/function name.
6630 * Note that this can call itself recursively, to deal with
6631 * constructs like foo{bar}{baz}{bam}
6632 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6633 * "in_start" ^
6634 * "expr_start" ^
6635 * "expr_end" ^
6636 * "in_end" ^
6637 *
6638 * Returns a new allocated string, which the caller must free.
6639 * Returns NULL for failure.
6640 */
6641 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006642make_expanded_name(
6643 char_u *in_start,
6644 char_u *expr_start,
6645 char_u *expr_end,
6646 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006647{
6648 char_u c1;
6649 char_u *retval = NULL;
6650 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006651
6652 if (expr_end == NULL || in_end == NULL)
6653 return NULL;
6654 *expr_start = NUL;
6655 *expr_end = NUL;
6656 c1 = *in_end;
6657 *in_end = NUL;
6658
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006659 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006660 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006661 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006662 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6663 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006664 if (retval != NULL)
6665 {
6666 STRCPY(retval, in_start);
6667 STRCAT(retval, temp_result);
6668 STRCAT(retval, expr_end + 1);
6669 }
6670 }
6671 vim_free(temp_result);
6672
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006673 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006674 *expr_start = '{';
6675 *expr_end = '}';
6676
6677 if (retval != NULL)
6678 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006679 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006680 if (expr_start != NULL)
6681 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006682 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006683 temp_result = make_expanded_name(retval, expr_start,
6684 expr_end, temp_result);
6685 vim_free(retval);
6686 retval = temp_result;
6687 }
6688 }
6689
6690 return retval;
6691}
6692
6693/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006695 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006697 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006698eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006700 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006701}
6702
6703/*
6704 * Return TRUE if character "c" can be used as the first character in a
6705 * variable or function name (excluding '{' and '}').
6706 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006707 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006708eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006709{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006710 return ASCII_ISALPHA(c) || c == '_';
6711}
6712
6713/*
6714 * Return TRUE if character "c" can be used as the first character of a
6715 * dictionary key.
6716 */
6717 int
6718eval_isdictc(int c)
6719{
6720 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721}
6722
6723/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006724 * Handle:
6725 * - expr[expr], expr[expr:expr] subscript
6726 * - ".name" lookup
6727 * - function call with Funcref variable: func(expr)
6728 * - method call: var->method()
6729 *
6730 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006731 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006732 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006733 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006734handle_subscript(
6735 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006736 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006737 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006738 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006739 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006740{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006741 int evaluate = evalarg != NULL
6742 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006743 int ret = OK;
6744 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006745 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006746 int getnext;
6747 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006748
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006749 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006750 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006751 // When at the end of the line and ".name" or "->{" or "->X" follows in
6752 // the next line then consume the line break.
6753 p = eval_next_non_blank(*arg, evalarg, &getnext);
6754 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00006755 && ((*p == '.'
6756 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
6757 || rettv->v_type == VAR_CLASS
6758 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02006759 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6760 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6761 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006762 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006763 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006764 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006765 check_white = FALSE;
6766 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006767
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006768 if (rettv->v_type == VAR_ANY)
6769 {
6770 char_u *exp_name;
6771 int cc;
6772 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006773 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006774 type_T *type;
6775
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006776 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006777 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006778 if (**arg != '.')
6779 {
6780 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006781 semsg(_(e_expected_dot_after_name_str),
6782 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006783 ret = FAIL;
6784 break;
6785 }
6786 ++*arg;
6787 if (IS_WHITE_OR_NUL(**arg))
6788 {
6789 if (verbose)
6790 emsg(_(e_no_white_space_allowed_after_dot));
6791 ret = FAIL;
6792 break;
6793 }
6794
6795 // isolate the name
6796 exp_name = *arg;
6797 while (eval_isnamec(**arg))
6798 ++*arg;
6799 cc = **arg;
6800 **arg = NUL;
6801
6802 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01006803 evalarg == NULL ? NULL : evalarg->eval_cctx,
6804 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006805 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006806
6807 if (idx < 0 && ufunc == NULL)
6808 {
6809 ret = FAIL;
6810 break;
6811 }
6812 if (idx >= 0)
6813 {
6814 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6815 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6816
6817 copy_tv(sv->sv_tv, rettv);
6818 }
6819 else
6820 {
6821 rettv->v_type = VAR_FUNC;
6822 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6823 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006824 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006825 }
6826
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006827 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6828 || rettv->v_type == VAR_PARTIAL))
6829 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006830 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006831 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6832 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006833
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006834 // Stop the expression evaluation when immediately aborting on
6835 // error, or when an interrupt occurred or an exception was thrown
6836 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006837 if (aborting())
6838 {
6839 if (ret == OK)
6840 clear_tv(rettv);
6841 ret = FAIL;
6842 }
6843 dict_unref(selfdict);
6844 selfdict = NULL;
6845 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006846 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006847 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006848 if (in_vim9script())
6849 *arg = skipwhite(p + 2);
6850 else
6851 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006852 if (ret == OK)
6853 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006854 if (VIM_ISWHITE(**arg))
6855 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006856 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006857 ret = FAIL;
6858 }
6859 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006860 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006861 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006862 else
6863 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006864 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006865 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006866 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006867 // "." is ".name" lookup when we found a dict or when evaluating and
6868 // scriptversion is at least 2, where string concatenation is "..".
6869 else if (**arg == '['
6870 || (**arg == '.' && (rettv->v_type == VAR_DICT
6871 || (!evaluate
6872 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006873 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006874 {
6875 dict_unref(selfdict);
6876 if (rettv->v_type == VAR_DICT)
6877 {
6878 selfdict = rettv->vval.v_dict;
6879 if (selfdict != NULL)
6880 ++selfdict->dv_refcount;
6881 }
6882 else
6883 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006884 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006885 {
6886 clear_tv(rettv);
6887 ret = FAIL;
6888 }
6889 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006890 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
6891 || rettv->v_type == VAR_OBJECT))
6892 {
6893 // class member: SomeClass.varname
6894 // class method: SomeClass.SomeMethod()
6895 // class constructor: SomeClass.new()
6896 // object member: someObject.varname
6897 // object method: someObject.SomeMethod()
6898 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
6899 {
6900 clear_tv(rettv);
6901 ret = FAIL;
6902 }
6903 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006904 else
6905 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006906 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006907
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006908 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6909 // Don't do this when "Func" is already a partial that was bound
6910 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006911 if (selfdict != NULL
6912 && (rettv->v_type == VAR_FUNC
6913 || (rettv->v_type == VAR_PARTIAL
6914 && (rettv->vval.v_partial->pt_auto
6915 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006916 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006917
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006918 dict_unref(selfdict);
6919 return ret;
6920}
6921
6922/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006923 * Make a copy of an item.
6924 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006925 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006926 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6927 * reference to an already copied list/dict can be used.
6928 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006929 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006930 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006931item_copy(
6932 typval_T *from,
6933 typval_T *to,
6934 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006935 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006936 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006937{
6938 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006939 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006940
Bram Moolenaar33570922005-01-25 22:26:29 +00006941 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006942 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006943 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006944 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006945 }
6946 ++recurse;
6947
6948 switch (from->v_type)
6949 {
6950 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006951 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006952 case VAR_STRING:
6953 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006954 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006955 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006956 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006957 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006958 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006959 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006960 case VAR_CLASS:
6961 case VAR_OBJECT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006962 copy_tv(from, to);
6963 break;
6964 case VAR_LIST:
6965 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006966 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006967 if (from->vval.v_list == NULL)
6968 to->vval.v_list = NULL;
6969 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6970 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006971 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006972 to->vval.v_list = from->vval.v_list->lv_copylist;
6973 ++to->vval.v_list->lv_refcount;
6974 }
6975 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006976 to->vval.v_list = list_copy(from->vval.v_list,
6977 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006978 if (to->vval.v_list == NULL)
6979 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006980 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006981 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006982 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006983 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006984 case VAR_DICT:
6985 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006986 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006987 if (from->vval.v_dict == NULL)
6988 to->vval.v_dict = NULL;
6989 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6990 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006991 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006992 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6993 ++to->vval.v_dict->dv_refcount;
6994 }
6995 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006996 to->vval.v_dict = dict_copy(from->vval.v_dict,
6997 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006998 if (to->vval.v_dict == NULL)
6999 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007000 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007001 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007002 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007003 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007004 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007005 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007006 }
7007 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007008 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007009}
7010
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007011 void
7012echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7013{
7014 char_u *tofree;
7015 char_u numbuf[NUMBUFLEN];
7016 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7017
7018 if (*atstart)
7019 {
7020 *atstart = FALSE;
7021 // Call msg_start() after eval1(), evaluating the expression
7022 // may cause a message to appear.
7023 if (with_space)
7024 {
7025 // Mark the saved text as finishing the line, so that what
7026 // follows is displayed on a new line when scrolling back
7027 // at the more prompt.
7028 msg_sb_eol();
7029 msg_start();
7030 }
7031 }
7032 else if (with_space)
7033 msg_puts_attr(" ", echo_attr);
7034
7035 if (p != NULL)
7036 for ( ; *p != NUL && !got_int; ++p)
7037 {
7038 if (*p == '\n' || *p == '\r' || *p == TAB)
7039 {
7040 if (*p != TAB && *needclr)
7041 {
7042 // remove any text still there from the command
7043 msg_clr_eos();
7044 *needclr = FALSE;
7045 }
7046 msg_putchar_attr(*p, echo_attr);
7047 }
7048 else
7049 {
7050 if (has_mbyte)
7051 {
7052 int i = (*mb_ptr2len)(p);
7053
7054 (void)msg_outtrans_len_attr(p, i, echo_attr);
7055 p += i - 1;
7056 }
7057 else
7058 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7059 }
7060 }
7061 vim_free(tofree);
7062}
7063
Bram Moolenaare9a41262005-01-15 22:18:47 +00007064/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 * ":echo expr1 ..." print each argument separated with a space, add a
7066 * newline at the end.
7067 * ":echon expr1 ..." print each argument plain.
7068 */
7069 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007070ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071{
7072 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007073 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007074 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 int needclr = TRUE;
7076 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007077 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007078 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007079 evalarg_T evalarg;
7080
Bram Moolenaare707c882020-07-01 13:07:37 +02007081 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082
7083 if (eap->skip)
7084 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007085 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007087 // If eval1() causes an error message the text from the command may
7088 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007089 need_clr_eos = needclr;
7090
Bram Moolenaar68db9962021-05-09 23:19:22 +02007091 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007092 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 {
7094 /*
7095 * Report the invalid expression unless the expression evaluation
7096 * has been cancelled due to an aborting error, an interrupt, or an
7097 * exception.
7098 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007099 if (!aborting() && did_emsg == did_emsg_before
7100 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007101 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007102 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 break;
7104 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007105 need_clr_eos = FALSE;
7106
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007108 {
7109 if (rettv.v_type == VAR_VOID)
7110 {
7111 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7112 break;
7113 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007114 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007115 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007116
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007117 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 arg = skipwhite(arg);
7119 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007120 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007121 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122
7123 if (eap->skip)
7124 --emsg_skip;
7125 else
7126 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007127 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 if (needclr)
7129 msg_clr_eos();
7130 if (eap->cmdidx == CMD_echo)
7131 msg_end();
7132 }
7133}
7134
7135/*
7136 * ":echohl {name}".
7137 */
7138 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007139ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007141 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142}
7143
7144/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007145 * Returns the :echo attribute
7146 */
7147 int
7148get_echo_attr(void)
7149{
7150 return echo_attr;
7151}
7152
7153/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 * ":execute expr1 ..." execute the result of an expression.
7155 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007156 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007158 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 * Each gets spaces around each argument and a newline at the end for
7160 * echo commands
7161 */
7162 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007163ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164{
7165 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007166 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 int ret = OK;
7168 char_u *p;
7169 garray_T ga;
7170 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007171 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172
7173 ga_init2(&ga, 1, 80);
7174
7175 if (eap->skip)
7176 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007177 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007179 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007180 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182
7183 if (!eap->skip)
7184 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007185 char_u buf[NUMBUFLEN];
7186
7187 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007188 {
7189 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7190 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007191 semsg(_(e_using_invalid_value_as_string_str),
7192 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007193 p = NULL;
7194 }
7195 else
7196 p = tv_get_string_buf(&rettv, buf);
7197 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007198 else
7199 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007200 if (p == NULL)
7201 {
7202 clear_tv(&rettv);
7203 ret = FAIL;
7204 break;
7205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 len = (int)STRLEN(p);
7207 if (ga_grow(&ga, len + 2) == FAIL)
7208 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007209 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 ret = FAIL;
7211 break;
7212 }
7213 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 ga.ga_len += len;
7217 }
7218
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007219 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 arg = skipwhite(arg);
7221 }
7222
7223 if (ret != FAIL && ga.ga_data != NULL)
7224 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007225 // use the first line of continuation lines for messages
7226 SOURCING_LNUM = start_lnum;
7227
Bram Moolenaar37fef162022-08-29 18:16:32 +01007228 if (eap->cmdidx == CMD_echomsg
7229 || eap->cmdidx == CMD_echowindow
7230 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007231 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007232 // Mark the already saved text as finishing the line, so that what
7233 // follows is displayed on a new line when scrolling back at the
7234 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007235 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007236 }
7237
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007238 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007239 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007240 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007241 out_flush();
7242 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007243 else if (eap->cmdidx == CMD_echowindow)
7244 {
7245#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007246 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007247#endif
7248 msg_attr(ga.ga_data, echo_attr);
7249#ifdef HAS_MESSAGE_WINDOW
7250 end_echowindow();
7251#endif
7252 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007253 else if (eap->cmdidx == CMD_echoconsole)
7254 {
7255 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7256 ui_write((char_u *)"\r\n", 2, TRUE);
7257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 else if (eap->cmdidx == CMD_echoerr)
7259 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007260 int save_did_emsg = did_emsg;
7261
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007262 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007263 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 if (!force_abort)
7265 did_emsg = save_did_emsg;
7266 }
7267 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007268 {
7269 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7270
7271 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7272 sticky_cmdmod_flags = cmdmod.cmod_flags
7273 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 do_cmdline((char_u *)ga.ga_data,
7275 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007276 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 }
7279
7280 ga_clear(&ga);
7281
7282 if (eap->skip)
7283 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007284 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285}
7286
7287/*
7288 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7289 * "arg" points to the "&" or '+' when called, to "option" when returning.
7290 * Returns NULL when no option name found. Otherwise pointer to the char
7291 * after the option name.
7292 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007293 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007294find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295{
7296 char_u *p = *arg;
7297
7298 ++p;
7299 if (*p == 'g' && p[1] == ':')
7300 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007301 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 p += 2;
7303 }
7304 else if (*p == 'l' && p[1] == ':')
7305 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007306 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 p += 2;
7308 }
7309 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007310 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311
7312 if (!ASCII_ISALPHA(*p))
7313 return NULL;
7314 *arg = p;
7315
7316 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007317 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 else
7319 while (ASCII_ISALPHA(*p))
7320 ++p;
7321 return p;
7322}
7323
7324/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007325 * Display script name where an item was last set.
7326 * Should only be invoked when 'verbose' is non-zero.
7327 */
7328 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007329last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007330{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007331 char_u *p;
7332
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007333 if (script_ctx.sc_sid == 0)
7334 return;
7335
7336 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7337 if (p == NULL)
7338 return;
7339
7340 verbose_enter();
7341 msg_puts(_("\n\tLast set from "));
7342 msg_puts((char *)p);
7343 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007344 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007345 msg_puts(_(line_msg));
7346 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007347 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007348 verbose_leave();
7349 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007350}
7351
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007352#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353
7354/*
7355 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007356 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 * "flags" can be "g" to do a global substitute.
7358 * Returns an allocated string, NULL for error.
7359 */
7360 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007361do_string_sub(
7362 char_u *str,
7363 char_u *pat,
7364 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007365 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007366 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367{
7368 int sublen;
7369 regmatch_T regmatch;
7370 int i;
7371 int do_all;
7372 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007373 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 garray_T ga;
7375 char_u *ret;
7376 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007377 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007379 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007381 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382
7383 ga_init2(&ga, 1, 200);
7384
7385 do_all = (flags[0] == 'g');
7386
7387 regmatch.rm_ic = p_ic;
7388 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7389 if (regmatch.regprog != NULL)
7390 {
7391 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007392 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7394 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007395 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007396 if (regmatch.startp[0] == regmatch.endp[0])
7397 {
7398 if (zero_width == regmatch.startp[0])
7399 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007400 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007401 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007402 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7403 (size_t)i);
7404 ga.ga_len += i;
7405 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007406 continue;
7407 }
7408 zero_width = regmatch.startp[0];
7409 }
7410
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 /*
7412 * Get some space for a temporary buffer to do the substitution
7413 * into. It will contain:
7414 * - The text up to where the match is.
7415 * - The substituted text.
7416 * - The text after the match.
7417 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007418 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007419 if (sublen <= 0)
7420 {
7421 ga_clear(&ga);
7422 break;
7423 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007424 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7426 {
7427 ga_clear(&ga);
7428 break;
7429 }
7430
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007431 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 i = (int)(regmatch.startp[0] - tail);
7433 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007434 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007435 (void)vim_regsub(&regmatch, sub, expr,
7436 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7437 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007439 tail = regmatch.endp[0];
7440 if (*tail == NUL)
7441 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 if (!do_all)
7443 break;
7444 }
7445
7446 if (ga.ga_data != NULL)
7447 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7448
Bram Moolenaar473de612013-06-08 18:19:48 +02007449 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 }
7451
7452 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7453 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007454 if (p_cpo == empty_option)
7455 p_cpo = save_cpo;
7456 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007457 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007458 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007459 // If it's still empty it was changed and restored, need to restore in
7460 // the complicated way.
7461 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007462 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007463 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465
7466 return ret;
7467}