blob: 89141bfe3ee7eb5f5c73fd2ff7a6936a7f0adb5f [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 Moolenaar99a7c0d2023-02-21 19:55:14 +00001532
1533 if (flags & GLV_PREFER_FUNC)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001534 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001535 // First look for a function with this name.
1536 // round 1: class functions (skipped for an object)
1537 // round 2: object methods
1538 for (int round = v_type == VAR_OBJECT ? 2 : 1;
1539 round <= 2; ++round)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001540 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001541 int count = round == 1
1542 ? cl->class_class_function_count
1543 : cl->class_obj_method_count;
1544 ufunc_T **funcs = round == 1
1545 ? cl->class_class_functions
1546 : cl->class_obj_methods;
1547 for (int i = 0; i < count; ++i)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001548 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001549 ufunc_T *fp = funcs[i];
1550 char_u *ufname = (char_u *)fp->uf_name;
1551 if (STRNCMP(ufname, key, p - key) == 0
1552 && ufname[p - key] == NUL)
1553 {
1554 lp->ll_ufunc = fp;
1555 lp->ll_valtype = fp->uf_func_type;
1556 round = 3;
1557 break;
1558 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001559 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001560 }
1561 }
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001562
1563 if (lp->ll_valtype == NULL)
1564 {
1565 int count = v_type == VAR_OBJECT
1566 ? cl->class_obj_member_count
1567 : cl->class_class_member_count;
1568 ocmember_T *members = v_type == VAR_OBJECT
1569 ? cl->class_obj_members
1570 : cl->class_class_members;
1571 for (int i = 0; i < count; ++i)
1572 {
1573 ocmember_T *om = members + i;
1574 if (STRNCMP(om->ocm_name, key, p - key) == 0
1575 && om->ocm_name[p - key] == NUL)
1576 {
1577 switch (om->ocm_access)
1578 {
=?UTF-8?q?Ola=20S=C3=B6der?=d8742472023-03-05 13:12:32 +00001579 case VIM_ACCESS_PRIVATE:
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001580 semsg(_(e_cannot_access_private_member_str),
1581 om->ocm_name);
1582 return NULL;
=?UTF-8?q?Ola=20S=C3=B6der?=d8742472023-03-05 13:12:32 +00001583 case VIM_ACCESS_READ:
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001584 if ((flags & GLV_READ_ONLY) == 0)
1585 {
1586 semsg(_(e_member_is_not_writable_str),
1587 om->ocm_name);
1588 return NULL;
1589 }
1590 break;
=?UTF-8?q?Ola=20S=C3=B6der?=d8742472023-03-05 13:12:32 +00001591 case VIM_ACCESS_ALL:
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001592 break;
1593 }
1594
1595 lp->ll_valtype = om->ocm_type;
1596
1597 if (v_type == VAR_OBJECT)
1598 lp->ll_tv = ((typval_T *)(
1599 lp->ll_tv->vval.v_object + 1)) + i;
1600 else
1601 lp->ll_tv = &cl->class_members_tv[i];
1602 break;
1603 }
1604 }
1605 }
1606
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001607 if (lp->ll_valtype == NULL)
1608 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001609 if (v_type == VAR_OBJECT)
1610 semsg(_(e_object_member_not_found_str), key);
1611 else
1612 semsg(_(e_class_member_not_found_str), key);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001613 return NULL;
1614 }
1615 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001616 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001617 }
1618
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001619 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001620 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001621 return p;
1622}
1623
1624/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001625 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001626 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001627 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001628clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001629{
1630 vim_free(lp->ll_exp_name);
1631 vim_free(lp->ll_newkey);
1632}
1633
1634/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001635 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001636 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001637 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1638 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001639 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001640 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001641set_var_lval(
1642 lval_T *lp,
1643 char_u *endp,
1644 typval_T *rettv,
1645 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001646 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1647 char_u *op,
1648 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001649{
1650 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001651 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001652
1653 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001654 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001655 cc = *endp;
1656 *endp = NUL;
h-east2bd6a092023-05-19 19:01:17 +01001657 if (in_vim9script() && check_reserved_name(lp->ll_name, FALSE) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001658 return;
1659
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001660 if (lp->ll_blob != NULL)
1661 {
1662 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001663
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001664 if (op != NULL && *op != '=')
1665 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001666 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001667 return;
1668 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001669 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001670 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001671
1672 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1673 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001674 if (lp->ll_empty2)
1675 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1676
Bram Moolenaar68452172021-04-12 21:21:02 +02001677 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1678 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001679 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001680 }
1681 else
1682 {
1683 val = (int)tv_get_number_chk(rettv, &error);
1684 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001685 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001686 }
1687 }
1688 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001689 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001690 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001691
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001692 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1693 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001694 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001695 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001696 *endp = cc;
1697 return;
1698 }
1699
Bram Moolenaarff697e62019-02-12 22:28:33 +01001700 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001701 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001702 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001703 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001704 {
1705 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001706 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1707 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001708 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001709 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001710 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001711 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001712 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001713 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001714 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001715 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001716 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1717 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001718 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001719 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001720 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001721 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001722 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001723 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001724 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001725 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001726 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001727 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001728 else if (lp->ll_range)
1729 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001730 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1731 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001732 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001733 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001734 return;
1735 }
1736
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001737 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1738 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001739 }
1740 else
1741 {
1742 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001743 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001744 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001745 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1746 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001747 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001748 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001749 return;
1750 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001751
1752 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001753 && check_typval_arg_type(lp->ll_valtype, rettv,
1754 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001755 return;
1756
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001757 if (lp->ll_newkey != NULL)
1758 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001759 if (op != NULL && *op != '=')
1760 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001761 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001762 return;
1763 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001764 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1765 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001766 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001767
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001768 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001769 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001770 if (di == NULL)
1771 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001772 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1773 {
1774 vim_free(di);
1775 return;
1776 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001777 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001778 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001779 else if (op != NULL && *op != '=')
1780 {
1781 tv_op(lp->ll_tv, rettv, op);
1782 return;
1783 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001784 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001785 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001786
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001787 /*
1788 * Assign the value to the variable or list item.
1789 */
1790 if (copy)
1791 copy_tv(rettv, lp->ll_tv);
1792 else
1793 {
1794 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001795 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001796 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001797 }
1798 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001799}
1800
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001801/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001802 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1803 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001804 * Returns OK or FAIL.
1805 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001806 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001807tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001808{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001809 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001810 char_u numbuf[NUMBUFLEN];
1811 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001812 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001813
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001814 // Can't do anything with a Funcref or Dict on the right.
1815 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001816 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001817 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1818 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001819 {
1820 switch (tv1->v_type)
1821 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001822 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001823 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001825 case VAR_DICT:
1826 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001827 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001828 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001829 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001830 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001831 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001832 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001833 case VAR_CLASS:
1834 case VAR_OBJECT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001835 break;
1836
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001837 case VAR_BLOB:
1838 if (*op != '+' || tv2->v_type != VAR_BLOB)
1839 break;
1840 // BLOB += BLOB
1841 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1842 {
1843 blob_T *b1 = tv1->vval.v_blob;
1844 blob_T *b2 = tv2->vval.v_blob;
1845 int i, len = blob_len(b2);
1846 for (i = 0; i < len; i++)
1847 ga_append(&b1->bv_ga, blob_get(b2, i));
1848 }
1849 return OK;
1850
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001851 case VAR_LIST:
1852 if (*op != '+' || tv2->v_type != VAR_LIST)
1853 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001854 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001855 if (tv2->vval.v_list != NULL)
1856 {
1857 if (tv1->vval.v_list == NULL)
1858 {
1859 tv1->vval.v_list = tv2->vval.v_list;
1860 ++tv1->vval.v_list->lv_refcount;
1861 }
1862 else
1863 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1864 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001865 return OK;
1866
1867 case VAR_NUMBER:
1868 case VAR_STRING:
1869 if (tv2->v_type == VAR_LIST)
1870 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001871 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001872 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001873 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001874 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001875 if (tv2->v_type == VAR_FLOAT)
1876 {
1877 float_T f = n;
1878
Bram Moolenaarff697e62019-02-12 22:28:33 +01001879 if (*op == '%')
1880 break;
1881 switch (*op)
1882 {
1883 case '+': f += tv2->vval.v_float; break;
1884 case '-': f -= tv2->vval.v_float; break;
1885 case '*': f *= tv2->vval.v_float; break;
1886 case '/': f /= tv2->vval.v_float; break;
1887 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001888 clear_tv(tv1);
1889 tv1->v_type = VAR_FLOAT;
1890 tv1->vval.v_float = f;
1891 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001892 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001893 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001894 switch (*op)
1895 {
1896 case '+': n += tv_get_number(tv2); break;
1897 case '-': n -= tv_get_number(tv2); break;
1898 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001899 case '/': n = num_divide(n, tv_get_number(tv2),
1900 &failed); break;
1901 case '%': n = num_modulus(n, tv_get_number(tv2),
1902 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001903 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001904 clear_tv(tv1);
1905 tv1->v_type = VAR_NUMBER;
1906 tv1->vval.v_number = n;
1907 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001908 }
1909 else
1910 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001911 if (tv2->v_type == VAR_FLOAT)
1912 break;
1913
Bram Moolenaarff697e62019-02-12 22:28:33 +01001914 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001915 s = tv_get_string(tv1);
1916 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001917 clear_tv(tv1);
1918 tv1->v_type = VAR_STRING;
1919 tv1->vval.v_string = s;
1920 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001921 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001922
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001923 case VAR_FLOAT:
1924 {
1925 float_T f;
1926
Bram Moolenaarff697e62019-02-12 22:28:33 +01001927 if (*op == '%' || *op == '.'
1928 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001929 && tv2->v_type != VAR_NUMBER
1930 && tv2->v_type != VAR_STRING))
1931 break;
1932 if (tv2->v_type == VAR_FLOAT)
1933 f = tv2->vval.v_float;
1934 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001935 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001936 switch (*op)
1937 {
1938 case '+': tv1->vval.v_float += f; break;
1939 case '-': tv1->vval.v_float -= f; break;
1940 case '*': tv1->vval.v_float *= f; break;
1941 case '/': tv1->vval.v_float /= f; break;
1942 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001943 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001944 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001945 }
1946 }
1947
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001948 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001949 return FAIL;
1950}
1951
1952/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001953 * Evaluate the expression used in a ":for var in expr" command.
1954 * "arg" points to "var".
1955 * Set "*errp" to TRUE for an error, FALSE otherwise;
1956 * Return a pointer that holds the info. Null when there is an error.
1957 */
1958 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001959eval_for_line(
1960 char_u *arg,
1961 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001962 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001963 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964{
Bram Moolenaar33570922005-01-25 22:26:29 +00001965 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001966 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001967 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001968 typval_T tv;
1969 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001970 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001971
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001972 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001974 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001975 if (fi == NULL)
1976 return NULL;
1977
Bram Moolenaar404557e2021-07-05 21:41:48 +02001978 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1979 &fi->fi_semicolon, FALSE);
1980 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001981 return fi;
1982
Bram Moolenaar404557e2021-07-05 21:41:48 +02001983 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001984 if (expr[0] != 'i' || expr[1] != 'n'
1985 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001987 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1988 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1989 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001990 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001991 return fi;
1992 }
1993
1994 if (skip)
1995 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001996 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1997 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001998 {
1999 *errp = FALSE;
2000 if (!skip)
2001 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002002 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002003 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002004 l = tv.vval.v_list;
2005 if (l == NULL)
2006 {
2007 // a null list is like an empty list: do nothing
2008 clear_tv(&tv);
2009 }
2010 else
2011 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002012 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002013 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002014
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002015 // No need to increment the refcount, it's already set for
2016 // the list being used in "tv".
2017 fi->fi_list = l;
2018 list_add_watch(l, &fi->fi_lw);
2019 fi->fi_lw.lw_item = l->lv_first;
2020 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002021 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002022 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002023 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002024 fi->fi_bi = 0;
2025 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002026 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002027 typval_T btv;
2028
2029 // Make a copy, so that the iteration still works when the
2030 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002032 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002033 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002034 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002035 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002036 else if (tv.v_type == VAR_STRING)
2037 {
2038 fi->fi_byte_idx = 0;
2039 fi->fi_string = tv.vval.v_string;
2040 tv.vval.v_string = NULL;
2041 if (fi->fi_string == NULL)
2042 fi->fi_string = vim_strsave((char_u *)"");
2043 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002044 else
2045 {
Sean Dewar80d73952021-08-04 19:25:54 +02002046 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002047 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002048 }
2049 }
2050 }
2051 if (skip)
2052 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002053 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002054
2055 return fi;
2056}
2057
2058/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002059 * Used when looping over a :for line, skip the "in expr" part.
2060 */
2061 void
2062skip_for_lines(void *fi_void, evalarg_T *evalarg)
2063{
2064 forinfo_T *fi = (forinfo_T *)fi_void;
2065 int i;
2066
2067 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002068 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002069}
2070
2071/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002072 * Use the first item in a ":for" list. Advance to the next.
2073 * Assign the values to the variable (list). "arg" points to the first one.
2074 * Return TRUE when a valid item was found, FALSE when at end of list or
2075 * something wrong.
2076 */
2077 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002078next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002079{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002080 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002081 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002082 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002083 ? (ASSIGN_FINAL
2084 // first round: error if variable exists
2085 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002086 | ASSIGN_NO_MEMBER_TYPE
2087 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002088 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002089 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002090 int skip_assign = in_vim9script() && arg[0] == '_'
2091 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002092
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002093 if (fi->fi_blob != NULL)
2094 {
2095 typval_T tv;
2096
2097 if (fi->fi_bi >= blob_len(fi->fi_blob))
2098 return FALSE;
2099 tv.v_type = VAR_NUMBER;
2100 tv.v_lock = VAR_FIXED;
2101 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2102 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002103 if (skip_assign)
2104 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002105 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002106 fi->fi_varcount, flag, NULL) == OK;
2107 }
2108
2109 if (fi->fi_string != NULL)
2110 {
2111 typval_T tv;
2112 int len;
2113
2114 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2115 if (len == 0)
2116 return FALSE;
2117 tv.v_type = VAR_STRING;
2118 tv.v_lock = VAR_FIXED;
2119 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2120 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002121 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002122 if (skip_assign)
2123 result = TRUE;
2124 else
2125 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002126 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002127 vim_free(tv.vval.v_string);
2128 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002129 }
2130
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002131 item = fi->fi_lw.lw_item;
2132 if (item == NULL)
2133 result = FALSE;
2134 else
2135 {
2136 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002137 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002138 if (skip_assign)
2139 result = TRUE;
2140 else
2141 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002142 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002143 }
2144 return result;
2145}
2146
2147/*
2148 * Free the structure used to store info used by ":for".
2149 */
2150 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002151free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002152{
Bram Moolenaar33570922005-01-25 22:26:29 +00002153 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002154
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002155 if (fi == NULL)
2156 return;
2157 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002158 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002159 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002160 list_unref(fi->fi_list);
2161 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002162 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002163 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002164 else
2165 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002166 vim_free(fi);
2167}
2168
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002170set_context_for_expression(
2171 expand_T *xp,
2172 char_u *arg,
2173 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002175 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002177 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002179 if (cmdidx == CMD_let || cmdidx == CMD_var
2180 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002181 {
2182 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002183 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002184 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002185 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002186 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002187 {
2188 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002189 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002190 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002191 break;
2192 }
2193 return;
2194 }
2195 }
2196 else
2197 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2198 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 while ((xp->xp_pattern = vim_strpbrk(arg,
2200 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2201 {
2202 c = *xp->xp_pattern;
2203 if (c == '&')
2204 {
2205 c = xp->xp_pattern[1];
2206 if (c == '&')
2207 {
2208 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002209 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 }
2211 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002212 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002214 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2215 xp->xp_pattern += 2;
2216
2217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 }
2219 else if (c == '$')
2220 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002221 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 xp->xp_context = EXPAND_ENV_VARS;
2223 }
2224 else if (c == '=')
2225 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002226 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 xp->xp_context = EXPAND_EXPRESSION;
2228 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002229 else if (c == '#'
2230 && xp->xp_context == EXPAND_EXPRESSION)
2231 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002232 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002233 break;
2234 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002235 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 && xp->xp_context == EXPAND_FUNCTIONS
2237 && vim_strchr(xp->xp_pattern, '(') == NULL)
2238 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002239 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 break;
2241 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002242 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002244 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 {
2246 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2247 if (c == '\\' && xp->xp_pattern[1] != NUL)
2248 ++xp->xp_pattern;
2249 xp->xp_context = EXPAND_NOTHING;
2250 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002251 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002253 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2255 /* skip */ ;
2256 xp->xp_context = EXPAND_NOTHING;
2257 }
2258 else if (c == '|')
2259 {
2260 if (xp->xp_pattern[1] == '|')
2261 {
2262 ++xp->xp_pattern;
2263 xp->xp_context = EXPAND_EXPRESSION;
2264 }
2265 else
2266 xp->xp_context = EXPAND_COMMANDS;
2267 }
2268 else
2269 xp->xp_context = EXPAND_EXPRESSION;
2270 }
2271 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002272 // Doesn't look like something valid, expand as an expression
2273 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002274 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 arg = xp->xp_pattern;
2276 if (*arg != NUL)
2277 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2278 /* skip */ ;
2279 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002280
2281 // ":exe one two" completes "two"
2282 if ((cmdidx == CMD_execute
2283 || cmdidx == CMD_echo
2284 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002285 || cmdidx == CMD_echomsg
2286 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002287 && xp->xp_context == EXPAND_EXPRESSION)
2288 {
2289 for (;;)
2290 {
2291 char_u *n = skiptowhite(arg);
2292
2293 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2294 break;
2295 arg = skipwhite(n);
2296 }
2297 }
2298
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 xp->xp_pattern = arg;
2300}
2301
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002303 * Return TRUE if "pat" matches "text".
2304 * Does not use 'cpo' and always uses 'magic'.
2305 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002306 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002307pattern_match(char_u *pat, char_u *text, int ic)
2308{
2309 int matches = FALSE;
2310 char_u *save_cpo;
2311 regmatch_T regmatch;
2312
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002313 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002314 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002315 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002316 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2317 if (regmatch.regprog != NULL)
2318 {
2319 regmatch.rm_ic = ic;
2320 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2321 vim_regfree(regmatch.regprog);
2322 }
2323 p_cpo = save_cpo;
2324 return matches;
2325}
2326
2327/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002328 * Handle a name followed by "(". Both for just "name(arg)" and for
2329 * "expr->name(arg)".
2330 * Returns OK or FAIL.
2331 */
2332 static int
2333eval_func(
2334 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002335 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002336 char_u *name,
2337 int name_len,
2338 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002339 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002340 typval_T *basetv) // "expr" for "expr->name(arg)"
2341{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002342 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002343 char_u *s = name;
2344 int len = name_len;
2345 partial_T *partial;
2346 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002347 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002348 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002349
2350 if (!evaluate)
2351 check_vars(s, len);
2352
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002353 // If "s" is the name of a variable of type VAR_FUNC
2354 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002355 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002356 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002357
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002358 // Need to make a copy, in case evaluating the arguments makes
2359 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002360 s = vim_strsave(s);
zeertzjqb7f6f932023-04-13 22:12:50 +01002361 if (s == NULL || (evaluate && *s == NUL))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002362 ret = FAIL;
2363 else
2364 {
2365 funcexe_T funcexe;
2366
2367 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002368 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002369 funcexe.fe_firstline = curwin->w_cursor.lnum;
2370 funcexe.fe_lastline = curwin->w_cursor.lnum;
2371 funcexe.fe_evaluate = evaluate;
2372 funcexe.fe_partial = partial;
2373 funcexe.fe_basetv = basetv;
2374 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002375 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002376 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002377 }
2378 vim_free(s);
2379
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002380 // If evaluate is FALSE rettv->v_type was not set in
2381 // get_func_tv, but it's needed in handle_subscript() to parse
2382 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002383 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2384 {
2385 rettv->vval.v_string = NULL;
2386 rettv->v_type = VAR_FUNC;
2387 }
2388
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002389 // Stop the expression evaluation when immediately
2390 // aborting on error, or when an interrupt occurred or
2391 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002392 if (evaluate && aborting())
2393 {
2394 if (ret == OK)
2395 clear_tv(rettv);
2396 ret = FAIL;
2397 }
2398 return ret;
2399}
2400
2401/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002402 * After a NL, skip over empty lines and comment-only lines.
2403 */
2404 static char_u *
2405newline_skip_comments(char_u *arg)
2406{
2407 char_u *p = arg + 1;
2408
2409 for (;;)
2410 {
2411 p = skipwhite(p);
2412
2413 if (*p == NUL)
2414 break;
2415 if (vim9_comment_start(p))
2416 {
2417 char_u *nl = vim_strchr(p, NL);
2418
2419 if (nl == NULL)
2420 break;
2421 p = nl;
2422 }
2423 if (*p != NL)
2424 break;
2425 ++p; // skip another NL
2426 }
2427 return p;
2428}
2429
2430/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002431 * Get the next line source line without advancing. But do skip over comment
2432 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002433 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002434 */
2435 static char_u *
2436getline_peek_skip_comments(evalarg_T *evalarg)
2437{
2438 for (;;)
2439 {
2440 char_u *next = getline_peek(evalarg->eval_getline,
2441 evalarg->eval_cookie);
2442 char_u *p;
2443
2444 if (next == NULL)
2445 break;
2446 p = skipwhite(next);
2447 if (*p != NUL && !vim9_comment_start(p))
2448 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002449 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002450 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002451 }
2452 return NULL;
2453}
2454
2455/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002456 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2457 * comment) and there is a next line, return the next line (skipping blanks)
2458 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002459 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2460 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002461 * "arg" must point somewhere inside a line, not at the start.
2462 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002463 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002464eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2465{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002466 char_u *p = skipwhite(arg);
2467
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002468 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002469 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002470 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002471 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2472 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002473 && (*p == NUL || *p == NL
2474 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002475 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002476 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002477
Bram Moolenaare442d592022-05-05 12:20:28 +01002478 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002479 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002480 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002481 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002482 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002483 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002484
Bram Moolenaar9d489562020-07-30 20:08:50 +02002485 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002486 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002487 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002488 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002489 }
2490 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002491 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002492}
2493
2494/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002495 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002496 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002497 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002498 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002499eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002500{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002501 garray_T *gap = &evalarg->eval_ga;
2502 char_u *line;
2503
Bram Moolenaar39be4982022-05-06 21:51:50 +01002504 if (arg != NULL)
2505 {
2506 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002507 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002508 // Truncate before a trailing comment, so that concatenating the lines
2509 // won't turn the rest into a comment.
2510 if (*skipwhite(arg) == '#')
2511 *arg = NUL;
2512 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002513
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002514 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002515 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2516 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002517 else
2518 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002519 if (line == NULL)
2520 return NULL;
2521
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002522 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002523 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2524 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002525 char_u *p = skipwhite(line);
2526
2527 // Going to concatenate the lines after parsing. For an empty or
2528 // comment line use an empty string.
2529 if (*p == NUL || vim9_comment_start(p))
2530 {
2531 vim_free(line);
2532 line = vim_strsave((char_u *)"");
2533 }
2534
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002535 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2536 ++gap->ga_len;
2537 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002538 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002539 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002540 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002541 evalarg->eval_tofree = line;
2542 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002543
2544 // Advanced to the next line, "arg" no longer points into the previous
2545 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002546 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002547 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002548}
2549
Bram Moolenaare6b53242020-07-01 17:28:33 +02002550/*
2551 * Call eval_next_non_blank() and get the next line if needed.
2552 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002553 char_u *
2554skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2555{
2556 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002557 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002558
Bram Moolenaar962d7212020-07-04 14:15:00 +02002559 if (evalarg == NULL)
2560 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002561 eval_next_non_blank(p, evalarg, &getnext);
2562 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002563 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002564 return p;
2565}
2566
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002567/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002568 * The "eval" functions have an "evalarg" argument: When NULL or
2569 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
2570 * parsed but not executed. The functions may return OK, but the rettv will be
2571 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 */
2573
2574/*
2575 * Handle zero level expression.
2576 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002577 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002578 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002579 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 * Return OK or FAIL.
2581 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002582 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002583eval0(
2584 char_u *arg,
2585 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002586 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002587 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002589 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2590}
2591
2592/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002593 * If "arg" is a simple function call without arguments then call it and return
2594 * the result. Otherwise return NOTDONE.
2595 */
2596 int
2597may_call_simple_func(
2598 char_u *arg,
2599 typval_T *rettv)
2600{
2601 char_u *parens = (char_u *)strstr((char *)arg, "()");
2602 int r = NOTDONE;
2603
2604 // If the expression is "FuncName()" then we can skip a lot of overhead.
2605 if (parens != NULL && *skipwhite(parens + 2) == NUL)
2606 {
2607 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
2608
2609 if (to_name_end(p, TRUE) == parens)
2610 r = call_simple_func(arg, (int)(parens - arg), rettv);
2611 }
2612 return r;
2613}
2614
2615/*
2616 * Handle zero level expression with optimization for a simple function call.
2617 * Same arguments and return value as eval0().
2618 */
2619 int
2620eval0_simple_funccal(
2621 char_u *arg,
2622 typval_T *rettv,
2623 exarg_T *eap,
2624 evalarg_T *evalarg)
2625{
2626 int r = may_call_simple_func(arg, rettv);
2627
2628 if (r == NOTDONE)
2629 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
2630 return r;
2631}
2632
2633/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002634 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2635 * expression and don't check what comes after the expression.
2636 */
2637 int
2638eval0_retarg(
2639 char_u *arg,
2640 typval_T *rettv,
2641 exarg_T *eap,
2642 evalarg_T *evalarg,
2643 char_u **retarg)
2644{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 int ret;
2646 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002647 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002648 int did_emsg_before = did_emsg;
2649 int called_emsg_before = called_emsg;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002650 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002651 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652
2653 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002654 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002655
Bram Moolenaar79481362022-06-27 20:15:10 +01002656 if (ret != FAIL)
2657 {
2658 expr_end = p;
2659 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002660
Bram Moolenaar79481362022-06-27 20:15:10 +01002661 // In Vim9 script a command block is not split at NL characters for
2662 // commands using an expression argument. Skip over a '#' comment to
2663 // check for a following NL. Require white space before the '#'.
2664 if (in_vim9script() && p > expr_end && retarg == NULL)
2665 while (*p == '#')
2666 {
2667 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002668
Bram Moolenaar79481362022-06-27 20:15:10 +01002669 if (nl == NULL)
2670 break;
2671 p = skipwhite(nl + 1);
2672 if (eap != NULL && *p != NUL)
2673 eap->nextcmd = p;
2674 check_for_end = FALSE;
2675 }
2676
2677 if (check_for_end)
2678 end_error = !ends_excmd2(arg, p);
2679 }
2680
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002681 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 {
2683 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002684 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 /*
2686 * Report the invalid expression unless the expression evaluation has
2687 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002688 * exception, or we already gave a more specific error.
2689 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002691 if (!aborting()
2692 && did_emsg == did_emsg_before
2693 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002694 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002695 {
2696 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002697 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002698 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002699 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002700 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002701
zeertzjqf2588b62023-05-05 17:22:35 +01002702 if (eap != NULL && p != NULL)
2703 {
2704 // Some of the expression may not have been consumed.
2705 // Only execute a next command if it cannot be a "||" operator.
2706 // The next command may be "catch".
2707 char_u *nextcmd = check_nextcmd(p);
2708 if (nextcmd != NULL && *nextcmd != '|')
2709 eap->nextcmd = nextcmd;
2710 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002711 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002713
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002714 if (retarg != NULL)
2715 *retarg = p;
2716 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002717 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002718
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 return ret;
2720}
2721
2722/*
2723 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002724 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002725 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 *
2727 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002728 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002730 * Note: "rettv.v_lock" is not set.
2731 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 * Return OK or FAIL.
2733 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002734 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002735eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002737 char_u *p;
2738 int getnext;
2739
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002740 CLEAR_POINTER(rettv);
2741
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 /*
2743 * Get the first variable.
2744 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002745 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 return FAIL;
2747
Bram Moolenaar793648f2020-06-26 21:28:25 +02002748 p = eval_next_non_blank(*arg, evalarg, &getnext);
2749 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002751 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002752 int result;
2753 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002754 evalarg_T *evalarg_used = evalarg;
2755 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002756 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002757 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002758 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002759
2760 if (evalarg == NULL)
2761 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002762 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002763 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002764 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002765 orig_flags = evalarg_used->eval_flags;
2766 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002767
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002768 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002769 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002770 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002771 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002772 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002773 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002774 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002775 clear_tv(rettv);
2776 return FAIL;
2777 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002778 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002779 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002780
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002782 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002784 int error = FALSE;
2785
Bram Moolenaar13106602020-10-04 16:06:05 +02002786 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002787 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002788 else if (vim9script)
2789 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002790 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002792 if (error || !op_falsy || !result)
2793 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002794 if (error)
2795 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 }
2797
2798 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002799 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002801 if (op_falsy)
2802 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002803 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002804 {
mityu4ac198c2021-05-28 17:52:40 +02002805 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002806 clear_tv(rettv);
2807 return FAIL;
2808 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002809 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002810 evalarg_used->eval_flags = (op_falsy ? !result : result)
zeertzjqb7f6f932023-04-13 22:12:50 +01002811 ? orig_flags : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002812 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002813 {
2814 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002816 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002817 if (!op_falsy || !result)
2818 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002820 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002822 /*
2823 * Check for the ":".
2824 */
2825 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2826 if (*p != ':')
2827 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002828 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002829 if (evaluate && result)
2830 clear_tv(rettv);
2831 evalarg_used->eval_flags = orig_flags;
2832 return FAIL;
2833 }
2834 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002835 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002836 else
2837 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002838 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002839 {
2840 error_white_both(p, 1);
2841 clear_tv(rettv);
2842 evalarg_used->eval_flags = orig_flags;
2843 return FAIL;
2844 }
2845 *arg = p;
2846 }
2847
2848 /*
2849 * Get the third variable. Recursive!
2850 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002851 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002852 {
mityu4ac198c2021-05-28 17:52:40 +02002853 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002854 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002855 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002856 return FAIL;
2857 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002858 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2859 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01002860 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002861 if (eval1(arg, &var2, evalarg_used) == FAIL)
2862 {
2863 if (evaluate && result)
2864 clear_tv(rettv);
2865 evalarg_used->eval_flags = orig_flags;
2866 return FAIL;
2867 }
2868 if (evaluate && !result)
2869 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002871
2872 if (evalarg == NULL)
2873 clear_evalarg(&local_evalarg, NULL);
2874 else
2875 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 }
2877
2878 return OK;
2879}
2880
2881/*
2882 * Handle first level expression:
2883 * expr2 || expr2 || expr2 logical OR
2884 *
2885 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002886 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 *
2888 * Return OK or FAIL.
2889 */
2890 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002891eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002893 char_u *p;
2894 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895
2896 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002897 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002899 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 return FAIL;
2901
2902 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002903 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002905 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002906 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002908 evalarg_T *evalarg_used = evalarg;
2909 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002910 int evaluate;
2911 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002912 long result = FALSE;
2913 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002914 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002915 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002916
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002917 if (evalarg == NULL)
2918 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002919 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002920 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002921 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002922 orig_flags = evalarg_used->eval_flags;
2923 evaluate = orig_flags & EVAL_EVALUATE;
2924 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002925 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002926 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002927 result = tv_get_bool_chk(rettv, &error);
2928 else if (tv_get_number_chk(rettv, &error) != 0)
2929 result = TRUE;
2930 clear_tv(rettv);
2931 if (error)
2932 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 }
2934
2935 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002936 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002938 while (p[0] == '|' && p[1] == '|')
2939 {
2940 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002941 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002942 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002943 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002944 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002945 {
2946 error_white_both(p, 2);
2947 clear_tv(rettv);
2948 return FAIL;
2949 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002950 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002951 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002952
2953 /*
2954 * Get the second variable.
2955 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002956 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002957 {
mityu4ac198c2021-05-28 17:52:40 +02002958 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002959 clear_tv(rettv);
2960 return FAIL;
2961 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002962 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2963 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01002964 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002965 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002966 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002967
2968 /*
2969 * Compute the result.
2970 */
2971 if (evaluate && !result)
2972 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002973 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002974 result = tv_get_bool_chk(&var2, &error);
2975 else if (tv_get_number_chk(&var2, &error) != 0)
2976 result = TRUE;
2977 clear_tv(&var2);
2978 if (error)
2979 return FAIL;
2980 }
2981 if (evaluate)
2982 {
2983 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002984 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002985 rettv->v_type = VAR_BOOL;
2986 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002987 }
2988 else
2989 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002990 rettv->v_type = VAR_NUMBER;
2991 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002992 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002993 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002994
2995 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002997
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002998 if (evalarg == NULL)
2999 clear_evalarg(&local_evalarg, NULL);
3000 else
3001 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 }
3003
3004 return OK;
3005}
3006
3007/*
3008 * Handle second level expression:
3009 * expr3 && expr3 && expr3 logical AND
3010 *
3011 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003012 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 *
3014 * Return OK or FAIL.
3015 */
3016 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003017eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003019 char_u *p;
3020 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021
3022 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003023 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003025 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 return FAIL;
3027
3028 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003029 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003031 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003032 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003034 evalarg_T *evalarg_used = evalarg;
3035 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003036 int orig_flags;
3037 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003038 long result = TRUE;
3039 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003040 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003041 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003042
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003043 if (evalarg == NULL)
3044 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003045 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003046 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003047 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003048 orig_flags = evalarg_used->eval_flags;
3049 evaluate = orig_flags & EVAL_EVALUATE;
3050 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003051 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003052 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003053 result = tv_get_bool_chk(rettv, &error);
3054 else if (tv_get_number_chk(rettv, &error) == 0)
3055 result = FALSE;
3056 clear_tv(rettv);
3057 if (error)
3058 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 }
3060
3061 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003062 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003064 while (p[0] == '&' && p[1] == '&')
3065 {
3066 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003067 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003068 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003069 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003070 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003071 {
3072 error_white_both(p, 2);
3073 clear_tv(rettv);
3074 return FAIL;
3075 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003076 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003077 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003078
3079 /*
3080 * Get the second variable.
3081 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003082 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003083 {
mityu4ac198c2021-05-28 17:52:40 +02003084 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003085 clear_tv(rettv);
3086 return FAIL;
3087 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003088 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3089 evalarg_used->eval_flags = result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003090 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003091 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003092 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003093 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003094
3095 /*
3096 * Compute the result.
3097 */
3098 if (evaluate && result)
3099 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003100 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003101 result = tv_get_bool_chk(&var2, &error);
3102 else if (tv_get_number_chk(&var2, &error) == 0)
3103 result = FALSE;
3104 clear_tv(&var2);
3105 if (error)
3106 return FAIL;
3107 }
3108 if (evaluate)
3109 {
3110 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003111 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003112 rettv->v_type = VAR_BOOL;
3113 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003114 }
3115 else
3116 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003117 rettv->v_type = VAR_NUMBER;
3118 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003119 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003120 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003121
3122 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003124
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003125 if (evalarg == NULL)
3126 clear_evalarg(&local_evalarg, NULL);
3127 else
3128 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 }
3130
3131 return OK;
3132}
3133
3134/*
3135 * Handle third level expression:
3136 * var1 == var2
3137 * var1 =~ var2
3138 * var1 != var2
3139 * var1 !~ var2
3140 * var1 > var2
3141 * var1 >= var2
3142 * var1 < var2
3143 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003144 * var1 is var2
3145 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 *
3147 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003148 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 *
3150 * Return OK or FAIL.
3151 */
3152 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003153eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003156 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003157 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003159 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160
3161 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003162 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003164 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 return FAIL;
3166
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003167 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003168
Bram Moolenaar696ba232020-07-29 21:20:41 +02003169 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170
3171 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003172 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003174 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003176 typval_T var2;
3177 int ic;
3178 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003179 int evaluate = evalarg == NULL
3180 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003181 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003182
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003183 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003184 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003185 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003186 p = *arg;
3187 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003188 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3189 {
mityu4ac198c2021-05-28 17:52:40 +02003190 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003191 clear_tv(rettv);
3192 return FAIL;
3193 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003194
Bram Moolenaar696ba232020-07-29 21:20:41 +02003195 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3196 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003197 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003198 clear_tv(rettv);
3199 return FAIL;
3200 }
3201
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003202 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 if (p[len] == '?')
3204 {
3205 ic = TRUE;
3206 ++len;
3207 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003208 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 else if (p[len] == '#')
3210 {
3211 ic = FALSE;
3212 ++len;
3213 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003214 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003216 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217
3218 /*
3219 * Get the second variable.
3220 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003221 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3222 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003223 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003224 clear_tv(rettv);
3225 return FAIL;
3226 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003227 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003228 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003230 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 return FAIL;
3232 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003233 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003234 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003235 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003236
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003237 // use the line of the comparison for messages
3238 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003239 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003240 {
3241 ret = FAIL;
3242 clear_tv(rettv);
3243 }
3244 else
3245 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003246 clear_tv(&var2);
3247 return ret;
3248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 }
3250
3251 return OK;
3252}
3253
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003254/*
3255 * Make a copy of blob "tv1" and append blob "tv2".
3256 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 void
3258eval_addblob(typval_T *tv1, typval_T *tv2)
3259{
3260 blob_T *b1 = tv1->vval.v_blob;
3261 blob_T *b2 = tv2->vval.v_blob;
3262 blob_T *b = blob_alloc();
3263 int i;
3264
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003265 if (b == NULL)
3266 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003268 for (i = 0; i < blob_len(b1); i++)
3269 ga_append(&b->bv_ga, blob_get(b1, i));
3270 for (i = 0; i < blob_len(b2); i++)
3271 ga_append(&b->bv_ga, blob_get(b2, i));
3272
3273 clear_tv(tv1);
3274 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275}
3276
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003277/*
3278 * Make a copy of list "tv1" and append list "tv2".
3279 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003280 int
3281eval_addlist(typval_T *tv1, typval_T *tv2)
3282{
3283 typval_T var3;
3284
3285 // concatenate Lists
3286 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3287 {
3288 clear_tv(tv1);
3289 clear_tv(tv2);
3290 return FAIL;
3291 }
3292 clear_tv(tv1);
3293 *tv1 = var3;
3294 return OK;
3295}
3296
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003298 * Handle the bitwise left/right shift operator expression:
3299 * var1 << var2
3300 * var1 >> var2
3301 *
3302 * "arg" must point to the first non-white of the expression.
3303 * "arg" is advanced to just after the recognized expression.
3304 *
3305 * Return OK or FAIL.
3306 */
3307 static int
3308eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3309{
3310 /*
3311 * Get the first expression.
3312 */
3313 if (eval6(arg, rettv, evalarg) == FAIL)
3314 return FAIL;
3315
3316 /*
3317 * Repeat computing, until no '<<' or '>>' is following.
3318 */
3319 for (;;)
3320 {
3321 char_u *p;
3322 int getnext;
3323 exprtype_T type;
3324 int evaluate;
3325 typval_T var2;
3326 int vim9script;
3327
3328 p = eval_next_non_blank(*arg, evalarg, &getnext);
3329 if (p[0] == '<' && p[1] == '<')
3330 type = EXPR_LSHIFT;
3331 else if (p[0] == '>' && p[1] == '>')
3332 type = EXPR_RSHIFT;
3333 else
3334 return OK;
3335
3336 // Handle a bitwise left or right shift operator
3337 if (rettv->v_type != VAR_NUMBER)
3338 {
3339 // left operand should be a number
3340 emsg(_(e_bitshift_ops_must_be_number));
3341 clear_tv(rettv);
3342 return FAIL;
3343 }
3344
3345 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3346 vim9script = in_vim9script();
3347 if (getnext)
3348 {
3349 *arg = eval_next_line(*arg, evalarg);
3350 p = *arg;
3351 }
3352 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3353 {
3354 error_white_both(*arg, 2);
3355 clear_tv(rettv);
3356 return FAIL;
3357 }
3358
3359 /*
3360 * Get the second variable.
3361 */
3362 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3363 {
3364 error_white_both(p, 2);
3365 clear_tv(rettv);
3366 return FAIL;
3367 }
3368 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3369 if (eval6(arg, &var2, evalarg) == FAIL)
3370 {
3371 clear_tv(rettv);
3372 return FAIL;
3373 }
3374
3375 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3376 {
3377 // right operand should be a positive number
3378 if (var2.v_type != VAR_NUMBER)
3379 emsg(_(e_bitshift_ops_must_be_number));
3380 else
dundargocc57b5bc2022-11-02 13:30:51 +00003381 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003382 clear_tv(rettv);
3383 clear_tv(&var2);
3384 return FAIL;
3385 }
3386
3387 if (evaluate)
3388 {
3389 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3390 // shifting more bits than we have always results in zero
3391 rettv->vval.v_number = 0;
3392 else if (type == EXPR_LSHIFT)
3393 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003394 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003395 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003396 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003397 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003398 }
3399
3400 clear_tv(&var2);
3401 }
3402
3403 return OK;
3404}
3405
3406/*
3407 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003408 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003410 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003411 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 *
3413 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003414 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 *
3416 * Return OK or FAIL.
3417 */
3418 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003419eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003422 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003424 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 return FAIL;
3426
3427 /*
3428 * Repeat computing, until no '+', '-' or '.' is following.
3429 */
3430 for (;;)
3431 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003432 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003433 int getnext;
3434 char_u *p;
3435 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003436 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003437 int concat;
3438 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003439 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003440
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003441 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003442 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003443 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003444 p = eval_next_non_blank(*arg, evalarg, &getnext);
3445 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003446 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003447 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3448 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003450 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3451 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003452
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003453 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003454 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003455 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003456 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003457 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003458 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003459 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003460 {
mityu4ac198c2021-05-28 17:52:40 +02003461 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003462 clear_tv(rettv);
3463 return FAIL;
3464 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003465 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003466 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003467 if ((op != '+' || (rettv->v_type != VAR_LIST
3468 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003469 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003470 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003471 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003472 int error = FALSE;
3473
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003474 // For "list + ...", an illegal use of the first operand as
3475 // a number cannot be determined before evaluating the 2nd
3476 // operand: if this is also a list, all is ok.
3477 // For "something . ...", "something - ..." or "non-list + ...",
3478 // we know that the first operand needs to be a string or number
3479 // without evaluating the 2nd operand. So check before to avoid
3480 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003481 if (op != '.')
3482 tv_get_number_chk(rettv, &error);
3483 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003484 {
3485 clear_tv(rettv);
3486 return FAIL;
3487 }
3488 }
3489
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 /*
3491 * Get the second variable.
3492 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003493 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003494 {
mityu89dcb4d2021-05-28 13:50:17 +02003495 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003496 clear_tv(rettv);
3497 return FAIL;
3498 }
3499 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003500 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003502 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 return FAIL;
3504 }
3505
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003506 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 {
3508 /*
3509 * Compute the result.
3510 */
3511 if (op == '.')
3512 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003513 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3514 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003515 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003516
Bram Moolenaar2e086612020-08-25 22:37:48 +02003517 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003518 || var2.v_type == VAR_CHANNEL
3519 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003520 semsg(_(e_using_invalid_value_as_string_str),
3521 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003522 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003523 {
3524 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3525 var2.vval.v_float);
3526 s2 = buf2;
3527 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003528 else
3529 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003530 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003531 {
3532 clear_tv(rettv);
3533 clear_tv(&var2);
3534 return FAIL;
3535 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003536 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003537 clear_tv(rettv);
3538 rettv->v_type = VAR_STRING;
3539 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003541 else if (op == '+' && rettv->v_type == VAR_BLOB
3542 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003544 else if (op == '+' && rettv->v_type == VAR_LIST
3545 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003546 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003547 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003548 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 else
3551 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003552 int error = FALSE;
3553 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003554 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003555
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003556 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003557 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003558 f1 = rettv->vval.v_float;
3559 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003560 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003561 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003562 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003563 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003564 if (error)
3565 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003566 // This can only happen for "list + non-list" or
3567 // "blob + non-blob". For "non-list + ..." or
3568 // "something - ...", we returned before evaluating the
3569 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003570 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003571 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003572 return FAIL;
3573 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003574 if (var2.v_type == VAR_FLOAT)
3575 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003576 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003577 if (var2.v_type == VAR_FLOAT)
3578 {
3579 f2 = var2.vval.v_float;
3580 n2 = 0;
3581 }
3582 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003583 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003584 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003585 if (error)
3586 {
3587 clear_tv(rettv);
3588 clear_tv(&var2);
3589 return FAIL;
3590 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003591 if (rettv->v_type == VAR_FLOAT)
3592 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003593 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003594 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003595
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003596 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003597 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3598 {
3599 if (op == '+')
3600 f1 = f1 + f2;
3601 else
3602 f1 = f1 - f2;
3603 rettv->v_type = VAR_FLOAT;
3604 rettv->vval.v_float = f1;
3605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003607 {
3608 if (op == '+')
3609 n1 = n1 + n2;
3610 else
3611 n1 = n1 - n2;
3612 rettv->v_type = VAR_NUMBER;
3613 rettv->vval.v_number = n1;
3614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003616 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 }
3618 }
3619 return OK;
3620}
3621
3622/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003623 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 * * number multiplication
3625 * / number division
3626 * % number modulo
3627 *
3628 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003629 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 *
3631 * Return OK or FAIL.
3632 */
3633 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003634eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003635 char_u **arg,
3636 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003637 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003638 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003640 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641
3642 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003643 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003645 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 return FAIL;
3647
3648 /*
3649 * Repeat computing, until no '*', '/' or '%' is following.
3650 */
3651 for (;;)
3652 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003653 int evaluate;
3654 int getnext;
3655 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003656 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003657 int op;
3658 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003659 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003660 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003661
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003662 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003663 p = eval_next_non_blank(*arg, evalarg, &getnext);
3664 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003665 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003667
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003668 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003669 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003670 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003671 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003672 {
3673 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3674 {
mityu4ac198c2021-05-28 17:52:40 +02003675 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003676 clear_tv(rettv);
3677 return FAIL;
3678 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003679 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003682 f1 = 0;
3683 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003684 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003685 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003687 if (rettv->v_type == VAR_FLOAT)
3688 {
3689 f1 = rettv->vval.v_float;
3690 use_float = TRUE;
3691 n1 = 0;
3692 }
3693 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003694 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003695 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003696 if (error)
3697 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 }
3699 else
3700 n1 = 0;
3701
3702 /*
3703 * Get the second variable.
3704 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003705 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3706 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003707 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003708 clear_tv(rettv);
3709 return FAIL;
3710 }
3711 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003712 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 return FAIL;
3714
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003715 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003717 if (var2.v_type == VAR_FLOAT)
3718 {
3719 if (!use_float)
3720 {
3721 f1 = n1;
3722 use_float = TRUE;
3723 }
3724 f2 = var2.vval.v_float;
3725 n2 = 0;
3726 }
3727 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003728 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003729 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003730 clear_tv(&var2);
3731 if (error)
3732 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003733 if (use_float)
3734 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736
3737 /*
3738 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003739 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003741 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003743 if (op == '*')
3744 f1 = f1 * f2;
3745 else if (op == '/')
3746 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003747#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003748 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003749 if (f2 == 0.0)
3750 {
3751 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003752 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003753 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003754 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003755 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003756 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003757 }
3758 else
3759 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003760#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003761 // We rely on the floating point library to handle divide
3762 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003763 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003764#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003767 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003768 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003769 return FAIL;
3770 }
3771 rettv->v_type = VAR_FLOAT;
3772 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 }
3774 else
3775 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003776 int failed = FALSE;
3777
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003778 if (op == '*')
3779 n1 = n1 * n2;
3780 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003781 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003783 n1 = num_modulus(n1, n2, &failed);
3784 if (failed)
3785 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003786
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003787 rettv->v_type = VAR_NUMBER;
3788 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 }
3791 }
3792
3793 return OK;
3794}
3795
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003796/*
3797 * Handle a type cast before a base level expression.
3798 * "arg" must point to the first non-white of the expression.
3799 * "arg" is advanced to just after the recognized expression.
3800 * Return OK or FAIL.
3801 */
3802 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003803eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003804 char_u **arg,
3805 typval_T *rettv,
3806 evalarg_T *evalarg,
3807 int want_string) // after "." operator
3808{
3809 type_T *want_type = NULL;
3810 garray_T type_list; // list of pointers to allocated types
3811 int res;
3812 int evaluate = evalarg == NULL ? 0
3813 : (evalarg->eval_flags & EVAL_EVALUATE);
3814
3815 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003816 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3817 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003818 {
3819 ++*arg;
3820 ga_init2(&type_list, sizeof(type_T *), 10);
3821 want_type = parse_type(arg, &type_list, TRUE);
3822 if (want_type == NULL && (evaluate || **arg != '>'))
3823 {
3824 clear_type_list(&type_list);
3825 return FAIL;
3826 }
3827
3828 if (**arg != '>')
3829 {
3830 if (*skipwhite(*arg) == '>')
3831 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3832 else
3833 emsg(_(e_missing_gt));
3834 clear_type_list(&type_list);
3835 return FAIL;
3836 }
3837 ++*arg;
3838 *arg = skipwhite_and_linebreak(*arg, evalarg);
3839 }
3840
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003841 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003842
3843 if (want_type != NULL && evaluate)
3844 {
3845 if (res == OK)
3846 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003847 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3848 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003849
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003850 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003851 {
3852 if (want_type == &t_bool && actual != &t_bool
3853 && (actual->tt_flags & TTFLAG_BOOL_OK))
3854 {
3855 int n = tv2bool(rettv);
3856
3857 // can use "0" and "1" for boolean in some places
3858 clear_tv(rettv);
3859 rettv->v_type = VAR_BOOL;
3860 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3861 }
3862 else
3863 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003864 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003865
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003866 where.wt_variable = TRUE;
3867 res = check_type(want_type, actual, TRUE, where);
3868 }
3869 }
3870 }
3871 clear_type_list(&type_list);
3872 }
3873
3874 return res;
3875}
3876
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003877 int
3878eval_leader(char_u **arg, int vim9)
3879{
3880 char_u *s = *arg;
3881 char_u *p = *arg;
3882
3883 while (*p == '!' || *p == '-' || *p == '+')
3884 {
3885 char_u *n = skipwhite(p + 1);
3886
3887 // ++, --, -+ and +- are not accepted in Vim9 script
3888 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3889 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003890 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003891 return FAIL;
3892 }
3893 p = n;
3894 }
3895 *arg = p;
3896 return OK;
3897}
3898
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003900 * Check for a predefined value "true", "false" and "null.*".
3901 * Return OK when recognized.
3902 */
3903 int
3904handle_predefined(char_u *s, int len, typval_T *rettv)
3905{
3906 switch (len)
3907 {
3908 case 4: if (STRNCMP(s, "true", 4) == 0)
3909 {
3910 rettv->v_type = VAR_BOOL;
3911 rettv->vval.v_number = VVAL_TRUE;
3912 return OK;
3913 }
3914 if (STRNCMP(s, "null", 4) == 0)
3915 {
3916 rettv->v_type = VAR_SPECIAL;
3917 rettv->vval.v_number = VVAL_NULL;
3918 return OK;
3919 }
3920 break;
3921 case 5: if (STRNCMP(s, "false", 5) == 0)
3922 {
3923 rettv->v_type = VAR_BOOL;
3924 rettv->vval.v_number = VVAL_FALSE;
3925 return OK;
3926 }
3927 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003928 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3929 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003930#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003931 rettv->v_type = VAR_JOB;
3932 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003933#else
3934 rettv->v_type = VAR_SPECIAL;
3935 rettv->vval.v_number = VVAL_NULL;
3936#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003937 return OK;
3938 }
3939 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003940 case 9:
3941 if (STRNCMP(s, "null_", 5) != 0)
3942 break;
3943 if (STRNCMP(s + 5, "list", 4) == 0)
3944 {
3945 rettv->v_type = VAR_LIST;
3946 rettv->vval.v_list = NULL;
3947 return OK;
3948 }
3949 if (STRNCMP(s + 5, "dict", 4) == 0)
3950 {
3951 rettv->v_type = VAR_DICT;
3952 rettv->vval.v_dict = NULL;
3953 return OK;
3954 }
3955 if (STRNCMP(s + 5, "blob", 4) == 0)
3956 {
3957 rettv->v_type = VAR_BLOB;
3958 rettv->vval.v_blob = NULL;
3959 return OK;
3960 }
3961 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003962 case 10: if (STRNCMP(s, "null_class", 10) == 0)
3963 {
3964 rettv->v_type = VAR_CLASS;
3965 rettv->vval.v_class = NULL;
3966 return OK;
3967 }
3968 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003969 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3970 {
3971 rettv->v_type = VAR_STRING;
3972 rettv->vval.v_string = NULL;
3973 return OK;
3974 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003975 if (STRNCMP(s, "null_object", 11) == 0)
3976 {
3977 rettv->v_type = VAR_OBJECT;
3978 rettv->vval.v_object = NULL;
3979 return OK;
3980 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003981 break;
3982 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003983 if (STRNCMP(s, "null_channel", 12) == 0)
3984 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003985#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003986 rettv->v_type = VAR_CHANNEL;
3987 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003988#else
3989 rettv->v_type = VAR_SPECIAL;
3990 rettv->vval.v_number = VVAL_NULL;
3991#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003992 return OK;
3993 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003994 if (STRNCMP(s, "null_partial", 12) == 0)
3995 {
3996 rettv->v_type = VAR_PARTIAL;
3997 rettv->vval.v_partial = NULL;
3998 return OK;
3999 }
4000 break;
4001 case 13: if (STRNCMP(s, "null_function", 13) == 0)
4002 {
4003 rettv->v_type = VAR_FUNC;
4004 rettv->vval.v_string = NULL;
4005 return OK;
4006 }
4007 break;
4008 }
4009 return FAIL;
4010}
4011
4012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 * Handle sixth level expression:
4014 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004015 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004016 * "string" string constant
4017 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 * &option-name option value
4019 * @r register contents
4020 * identifier variable value
4021 * function() function call
4022 * $VAR environment variable
4023 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004024 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004026 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004027 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 *
4029 * Also handle:
4030 * ! in front logical NOT
4031 * - in front unary minus
4032 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004033 * trailing [] subscript in String or List
4034 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004035 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 *
4037 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004038 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 *
4040 * Return OK or FAIL.
4041 */
4042 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004043eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004044 char_u **arg,
4045 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004046 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004047 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004049 int evaluate = evalarg != NULL
4050 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 int len;
4052 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004053 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 char_u *start_leader, *end_leader;
4055 int ret = OK;
4056 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004057 static int recurse = 0;
4058 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059
4060 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004061 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004062 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004064 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065
4066 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004067 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 */
4069 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004070 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004071 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 end_leader = *arg;
4073
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004074 if (**arg == '.' && (!isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004075 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004076 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004077 ++*arg;
4078 return FAIL;
4079 }
4080
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004081 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004082 // and crash. With MSVC the stack is smaller.
4083 if (recurse ==
4084#ifdef _MSC_VER
4085 300
4086#else
4087 1000
4088#endif
4089 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004090 {
4091 semsg(_(e_expression_too_recursive_str), *arg);
4092 return FAIL;
4093 }
4094 ++recurse;
4095
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 switch (**arg)
4097 {
4098 /*
4099 * Number constant.
4100 */
4101 case '0':
4102 case '1':
4103 case '2':
4104 case '3':
4105 case '4':
4106 case '5':
4107 case '6':
4108 case '7':
4109 case '8':
4110 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004111 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004112
4113 // Apply prefixed "-" and "+" now. Matters especially when
4114 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004115 if (ret == OK && evaluate && end_leader > start_leader
4116 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004117 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 break;
4119
4120 /*
4121 * String constant: "string".
4122 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004123 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 break;
4125
4126 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004127 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004129 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004130 break;
4131
4132 /*
4133 * List: [expr, expr]
4134 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004135 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 break;
4137
4138 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004139 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004140 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004141 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004142 {
4143 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4144 }
4145 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004146 {
4147 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004148 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004149 }
4150 else
4151 ret = NOTDONE;
4152 break;
4153
4154 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004155 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004156 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004157 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004158 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004159 ret = NOTDONE;
4160 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004161 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004162 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004163 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004164 break;
4165
4166 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004167 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004169 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 break;
4171
4172 /*
4173 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004174 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 */
LemonBoy2eaef102022-05-06 13:14:50 +01004176 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4177 ret = eval_interp_string(arg, rettv, evaluate);
4178 else
4179 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 break;
4181
4182 /*
4183 * Register contents: @r.
4184 */
4185 case '@': ++*arg;
4186 if (evaluate)
4187 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004188 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004189 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004190 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004191 emsg_invreg(**arg);
4192 else
4193 {
4194 rettv->v_type = VAR_STRING;
4195 rettv->vval.v_string = get_reg_contents(**arg,
4196 GREG_EXPR_SRC);
4197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 }
4199 if (**arg != NUL)
4200 ++*arg;
4201 break;
4202
4203 /*
4204 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004205 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004207 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004208 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004209 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004210 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004211 if (ret == OK && evaluate)
4212 {
4213 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4214
Bram Moolenaara9931532021-06-12 15:58:16 +02004215 // Compile it here to get the return type. The return
4216 // type is optional, when it's missing use t_unknown.
4217 // This is recognized in compile_return().
4218 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4219 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004220 if (compile_def_function(ufunc, FALSE,
4221 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004222 {
4223 clear_tv(rettv);
4224 ret = FAIL;
4225 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004226 }
4227 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004228 if (ret == NOTDONE)
4229 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004230 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004231 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004232
Bram Moolenaar9215f012020-06-27 21:18:00 +02004233 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004234 if (**arg == ')')
4235 ++*arg;
4236 else if (ret == OK)
4237 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004238 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004239 clear_tv(rettv);
4240 ret = FAIL;
4241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 }
4243 break;
4244
Bram Moolenaar8c711452005-01-14 21:53:12 +00004245 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 break;
4247 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004248
4249 if (ret == NOTDONE)
4250 {
4251 /*
4252 * Must be a variable or function name.
4253 * Can also be a curly-braces kind of name: {expr}.
4254 */
4255 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004256 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004257 if (alias != NULL)
4258 s = alias;
4259
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004260 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004261 ret = FAIL;
4262 else
4263 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004264 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4265
Bram Moolenaar4525a572022-02-13 11:57:33 +00004266 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004267 {
4268 emsg(_(e_cannot_use_underscore_here));
4269 ret = FAIL;
4270 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004271 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004272 && s[0] == 's' && s[1] == ':')
4273 {
4274 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4275 ret = FAIL;
4276 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004277 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004278 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004279 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004280 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004281 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004282 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004283 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004284 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004285 // get the value of "true", "false", etc. or a variable
4286 ret = FAIL;
4287 if (vim9script)
4288 ret = handle_predefined(s, len, rettv);
4289 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004290 {
4291 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004292 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004293 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004294 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004295 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004296 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004297 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004298 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004299 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004300 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004301 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004302 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004303 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004304 }
4305
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004306 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4307 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004308 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004309 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310
4311 /*
4312 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4313 */
4314 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004315 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004316
4317 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004318 return ret;
4319}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004320
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004321/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004322 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004323 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004324 * Adjusts "end_leaderp" until it is at "start_leader".
4325 */
4326 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004327eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004328 typval_T *rettv,
4329 int numeric_only,
4330 char_u *start_leader,
4331 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004332{
4333 char_u *end_leader = *end_leaderp;
4334 int ret = OK;
4335 int error = FALSE;
4336 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004337 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004338 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004339 float_T f = 0.0;
4340
4341 if (rettv->v_type == VAR_FLOAT)
4342 f = rettv->vval.v_float;
4343 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004344 {
4345 while (VIM_ISWHITE(end_leader[-1]))
4346 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004347 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004348 val = tv2bool(rettv);
4349 else
4350 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004351 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004352 if (error)
4353 {
4354 clear_tv(rettv);
4355 ret = FAIL;
4356 }
4357 else
4358 {
4359 while (end_leader > start_leader)
4360 {
4361 --end_leader;
4362 if (*end_leader == '!')
4363 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004364 if (numeric_only)
4365 {
4366 ++end_leader;
4367 break;
4368 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004369 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004370 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004371 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004372 {
4373 rettv->v_type = VAR_BOOL;
4374 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4375 }
4376 else
4377 f = !f;
4378 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004379 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004380 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004381 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004382 type = VAR_BOOL;
4383 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004384 }
4385 else if (*end_leader == '-')
4386 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004387 if (rettv->v_type == VAR_FLOAT)
4388 f = -f;
4389 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004390 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004391 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004392 type = VAR_NUMBER;
4393 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004394 }
4395 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004396 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004398 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004399 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004401 else
4402 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004403 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004404 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004405 rettv->v_type = type;
4406 else
4407 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004408 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004411 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 return ret;
4413}
4414
4415/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004416 * Call the function referred to in "rettv".
4417 */
4418 static int
4419call_func_rettv(
4420 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004421 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004422 typval_T *rettv,
4423 int evaluate,
4424 dict_T *selfdict,
4425 typval_T *basetv)
4426{
4427 partial_T *pt = NULL;
4428 funcexe_T funcexe;
4429 typval_T functv;
4430 char_u *s;
4431 int ret;
4432
4433 // need to copy the funcref so that we can clear rettv
4434 if (evaluate)
4435 {
4436 functv = *rettv;
4437 rettv->v_type = VAR_UNKNOWN;
4438
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004439 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004440 if (functv.v_type == VAR_PARTIAL)
4441 {
4442 pt = functv.vval.v_partial;
4443 s = partial_name(pt);
4444 }
4445 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004446 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004447 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004448 if (s == NULL || *s == NUL)
4449 {
4450 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004451 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004452 goto theend;
4453 }
4454 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004455 }
4456 else
4457 s = (char_u *)"";
4458
Bram Moolenaara80faa82020-04-12 19:37:17 +02004459 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004460 funcexe.fe_firstline = curwin->w_cursor.lnum;
4461 funcexe.fe_lastline = curwin->w_cursor.lnum;
4462 funcexe.fe_evaluate = evaluate;
4463 funcexe.fe_partial = pt;
4464 funcexe.fe_selfdict = selfdict;
4465 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004466 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004467
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004468theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004469 // Clear the funcref afterwards, so that deleting it while
4470 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004471 if (evaluate)
4472 clear_tv(&functv);
4473
4474 return ret;
4475}
4476
4477/*
4478 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004479 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004480 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4481 */
4482 static int
4483eval_lambda(
4484 char_u **arg,
4485 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004486 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004487 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004488{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004489 int evaluate = evalarg != NULL
4490 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004491 typval_T base = *rettv;
4492 int ret;
4493
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004494 rettv->v_type = VAR_UNKNOWN;
4495
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004496 if (**arg == '{')
4497 {
4498 // ->{lambda}()
4499 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4500 }
4501 else
4502 {
4503 // ->(lambda)()
4504 ++*arg;
4505 ret = eval1(arg, rettv, evalarg);
4506 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004507 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004508 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004509 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004510 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004511 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004512 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4513 && rettv->v_type != VAR_PARTIAL)
4514 {
4515 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4516 return FAIL;
4517 }
4518 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004519 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004520 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004521 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004522
4523 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004524 {
4525 if (verbose)
4526 {
4527 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004528 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004529 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004530 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004531 }
4532 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004533 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004534 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004535 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004536 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004537
4538 // Clear the funcref afterwards, so that deleting it while
4539 // evaluating the arguments is possible (see test55).
4540 if (evaluate)
4541 clear_tv(&base);
4542
4543 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004544}
4545
4546/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004547 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004548 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004549 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4550 */
4551 static int
4552eval_method(
4553 char_u **arg,
4554 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004555 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004556 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004557{
4558 char_u *name;
4559 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004560 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004561 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004562 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004563 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004564 int evaluate = evalarg != NULL
4565 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004566
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004567 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004568
Bram Moolenaarac92e252019-08-03 21:58:38 +02004569 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004570 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004571 if (alias != NULL)
4572 name = alias;
4573
4574 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004575 {
4576 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004577 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004578 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004579 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004580 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004581 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004582 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004583
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004584 // If there is no "(" immediately following, but there is further on,
4585 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4586 // Does not handle anything where "(" is part of the expression.
4587 *arg = skipwhite(*arg);
4588
4589 if (**arg != '(' && alias == NULL
4590 && (paren = vim_strchr(*arg, '(')) != NULL)
4591 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004592 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00004593
4594 // Truncate the name a the "(". Avoid trying to get another line
4595 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004596 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00004597 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
4598 if (evalarg != NULL)
4599 {
4600 getline = evalarg->eval_getline;
4601 evalarg->eval_getline = NULL;
4602 }
4603
4604 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004605 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004606 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004607 *arg = name + len;
4608 ret = FAIL;
4609 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004610 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004611 {
4612 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004613 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004614 }
Bram Moolenaar34820942022-12-19 20:28:38 +00004615
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004616 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00004617 if (getline != NULL)
4618 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004619 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004620
4621 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004622 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004623 *arg = skipwhite(*arg);
4624
4625 if (**arg != '(')
4626 {
4627 if (verbose)
4628 semsg(_(e_missing_parenthesis_str), name);
4629 ret = FAIL;
4630 }
4631 else if (VIM_ISWHITE((*arg)[-1]))
4632 {
4633 if (verbose)
4634 emsg(_(e_no_white_space_allowed_before_parenthesis));
4635 ret = FAIL;
4636 }
4637 else
4638 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004639 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004640 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004641 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004642
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004643 // Clear the funcref afterwards, so that deleting it while
4644 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004645 if (evaluate)
4646 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004647 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004648
Bram Moolenaarac92e252019-08-03 21:58:38 +02004649 return ret;
4650}
4651
4652/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004653 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4654 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004655 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4656 */
4657 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004658eval_index(
4659 char_u **arg,
4660 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004661 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004662 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004663{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004664 int evaluate = evalarg != NULL
4665 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004666 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004667 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004668 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004669 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004670 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004671 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004672
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004673 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4674 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004675
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004676 init_tv(&var1);
4677 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004678 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004679 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004680 /*
4681 * dict.name
4682 */
4683 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004684 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004685 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004686 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004687 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004688 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004689 }
4690 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004691 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004692 /*
4693 * something[idx]
4694 *
4695 * Get the (first) variable from inside the [].
4696 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004697 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004698 if (**arg == ':')
4699 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004700 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004701 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004702 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004703 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004704 semsg(_(e_white_space_required_before_and_after_str_at_str),
4705 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004706 clear_tv(&var1);
4707 return FAIL;
4708 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004709 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004710 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004711 int error = FALSE;
4712
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004713 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004714 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004715 && var1.v_type == VAR_FLOAT)
4716 {
4717 var1.vval.v_string = typval_tostring(&var1, TRUE);
4718 var1.v_type = VAR_STRING;
4719 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004720
Bram Moolenaar4525a572022-02-13 11:57:33 +00004721 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004722 tv_get_number_chk(&var1, &error);
4723 else
4724 error = tv_get_string_chk(&var1) == NULL;
4725 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004726 {
4727 // not a number or string
4728 clear_tv(&var1);
4729 return FAIL;
4730 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004731 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004732
4733 /*
4734 * Get the second variable from inside the [:].
4735 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004736 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004737 if (**arg == ':')
4738 {
4739 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004740 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004741 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004742 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004743 semsg(_(e_white_space_required_before_and_after_str_at_str),
4744 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004745 if (!empty1)
4746 clear_tv(&var1);
4747 return FAIL;
4748 }
4749 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004750 if (**arg == ']')
4751 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004752 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004753 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004754 if (!empty1)
4755 clear_tv(&var1);
4756 return FAIL;
4757 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004758 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004759 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004760 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004761 if (!empty1)
4762 clear_tv(&var1);
4763 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004764 return FAIL;
4765 }
4766 }
4767
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004768 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004769 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004770 if (**arg != ']')
4771 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004772 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004773 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004774 clear_tv(&var1);
4775 if (range)
4776 clear_tv(&var2);
4777 return FAIL;
4778 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004779 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004780 }
4781
4782 if (evaluate)
4783 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004784 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004785 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004786 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004787
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004788 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004789 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004790 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004791 clear_tv(&var2);
4792 return res;
4793 }
4794 return OK;
4795}
4796
4797/*
4798 * Check if "rettv" can have an [index] or [sli:ce]
4799 */
4800 int
4801check_can_index(typval_T *rettv, int evaluate, int verbose)
4802{
4803 switch (rettv->v_type)
4804 {
4805 case VAR_FUNC:
4806 case VAR_PARTIAL:
4807 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004808 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004809 return FAIL;
4810 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004811 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004812 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004813 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004814 case VAR_BOOL:
4815 case VAR_SPECIAL:
4816 case VAR_JOB:
4817 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004818 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004819 case VAR_CLASS:
4820 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004821 if (verbose)
4822 emsg(_(e_cannot_index_special_variable));
4823 return FAIL;
4824 case VAR_UNKNOWN:
4825 case VAR_ANY:
4826 case VAR_VOID:
4827 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004828 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004829 emsg(_(e_cannot_index_special_variable));
4830 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004831 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004832 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004833
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004834 case VAR_STRING:
4835 case VAR_LIST:
4836 case VAR_DICT:
4837 case VAR_BLOB:
4838 break;
4839 case VAR_NUMBER:
4840 if (in_vim9script())
4841 emsg(_(e_cannot_index_number));
4842 break;
4843 }
4844 return OK;
4845}
4846
4847/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004848 * slice() function
4849 */
4850 void
4851f_slice(typval_T *argvars, typval_T *rettv)
4852{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004853 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004854 && ((argvars[0].v_type != VAR_STRING
4855 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004856 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004857 && check_for_list_arg(argvars, 0) == FAIL)
4858 || check_for_number_arg(argvars, 1) == FAIL
4859 || check_for_opt_number_arg(argvars, 2) == FAIL))
4860 return;
4861
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00004862 if (check_can_index(argvars, TRUE, FALSE) != OK)
4863 return;
4864
4865 copy_tv(argvars, rettv);
4866 eval_index_inner(rettv, TRUE, argvars + 1,
4867 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4868 TRUE, NULL, 0, FALSE);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004869}
4870
4871/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004872 * Apply index or range to "rettv".
4873 * "var1" is the first index, NULL for [:expr].
4874 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004875 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4876 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004877 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4878 */
4879 int
4880eval_index_inner(
4881 typval_T *rettv,
4882 int is_range,
4883 typval_T *var1,
4884 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004885 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004886 char_u *key,
4887 int keylen,
4888 int verbose)
4889{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004890 varnumber_T n1, n2 = 0;
4891 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004892
4893 n1 = 0;
4894 if (var1 != NULL && rettv->v_type != VAR_DICT)
4895 n1 = tv_get_number(var1);
4896
4897 if (is_range)
4898 {
4899 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004900 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004901 if (verbose)
4902 emsg(_(e_cannot_slice_dictionary));
4903 return FAIL;
4904 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004905 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004906 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004907 else
4908 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004909 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004910
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004911 switch (rettv->v_type)
4912 {
4913 case VAR_UNKNOWN:
4914 case VAR_ANY:
4915 case VAR_VOID:
4916 case VAR_FUNC:
4917 case VAR_PARTIAL:
4918 case VAR_FLOAT:
4919 case VAR_BOOL:
4920 case VAR_SPECIAL:
4921 case VAR_JOB:
4922 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004923 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004924 case VAR_CLASS:
4925 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004926 break; // not evaluating, skipping over subscript
4927
4928 case VAR_NUMBER:
4929 case VAR_STRING:
4930 {
4931 char_u *s = tv_get_string(rettv);
4932
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004933 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004934 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004935 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004936 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004937 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004938 else
4939 s = char_from_string(s, n1);
4940 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004941 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004942 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004943 // The resulting variable is a substring. If the indexes
4944 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004945 if (n1 < 0)
4946 {
4947 n1 = len + n1;
4948 if (n1 < 0)
4949 n1 = 0;
4950 }
4951 if (n2 < 0)
4952 n2 = len + n2;
4953 else if (n2 >= len)
4954 n2 = len;
4955 if (n1 >= len || n2 < 0 || n1 > n2)
4956 s = NULL;
4957 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004958 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004959 }
4960 else
4961 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004962 // The resulting variable is a string of a single
4963 // character. If the index is too big or negative the
4964 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004965 if (n1 >= len || n1 < 0)
4966 s = NULL;
4967 else
4968 s = vim_strnsave(s + n1, 1);
4969 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004970 clear_tv(rettv);
4971 rettv->v_type = VAR_STRING;
4972 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004973 }
4974 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004975
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004976 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004977 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4978 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004979 break;
4980
4981 case VAR_LIST:
4982 if (var1 == NULL)
4983 n1 = 0;
4984 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004985 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004986 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004987 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004988 return FAIL;
4989 break;
4990
4991 case VAR_DICT:
4992 {
4993 dictitem_T *item;
4994 typval_T tmp;
4995
4996 if (key == NULL)
4997 {
4998 key = tv_get_string_chk(var1);
4999 if (key == NULL)
5000 return FAIL;
5001 }
5002
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005003 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005004
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005005 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005006 {
5007 if (verbose)
5008 {
5009 if (keylen > 0)
5010 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005011 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005012 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005013 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005014 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005015
5016 copy_tv(&item->di_tv, &tmp);
5017 clear_tv(rettv);
5018 *rettv = tmp;
5019 }
5020 break;
5021 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005022 return OK;
5023}
5024
5025/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005026 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005027 */
5028 char_u *
5029partial_name(partial_T *pt)
5030{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02005031 if (pt != NULL)
5032 {
5033 if (pt->pt_name != NULL)
5034 return pt->pt_name;
5035 if (pt->pt_func != NULL)
5036 return pt->pt_func->uf_name;
5037 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005038 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005039}
5040
Bram Moolenaarddecc252016-04-06 22:59:37 +02005041 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005042partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005043{
5044 int i;
5045
5046 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005047 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005048 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005049 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005050 if (pt->pt_name != NULL)
5051 {
5052 func_unref(pt->pt_name);
5053 vim_free(pt->pt_name);
5054 }
5055 else
5056 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005057
Bram Moolenaar54656012021-06-09 20:50:46 +02005058 // "out_up" is no longer used, decrement refcount on partial that owns it.
5059 partial_unref(pt->pt_outer.out_up_partial);
5060
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005061 // Using pt_outer from another partial.
5062 partial_unref(pt->pt_outer_partial);
5063
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005064 // Decrease the reference count for the context of a closure. If down
5065 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005066 if (pt->pt_funcstack != NULL)
5067 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005068 --pt->pt_funcstack->fs_refcount;
5069 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005070 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005071 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005072 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5073 if (pt->pt_loopvars[i] != NULL)
5074 {
5075 --pt->pt_loopvars[i]->lvs_refcount;
5076 loopvars_check_refcount(pt->pt_loopvars[i]);
5077 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005078
Bram Moolenaarddecc252016-04-06 22:59:37 +02005079 vim_free(pt);
5080}
5081
5082/*
5083 * Unreference a closure: decrement the reference count and free it when it
5084 * becomes zero.
5085 */
5086 void
5087partial_unref(partial_T *pt)
5088{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005089 if (pt == NULL)
5090 return;
5091
5092 int done = FALSE;
5093
5094 if (--pt->pt_refcount <= 0)
5095 partial_free(pt);
5096
5097 // If the reference count goes down to one, the funcstack may be the
5098 // only reference and can be freed if no other partials reference it.
5099 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005100 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005101 // careful: if the funcstack is freed it may contain this partial
5102 // and it gets freed as well
5103 if (pt->pt_funcstack != NULL)
5104 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005105
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005106 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005107 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005108 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005109
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005110 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5111 if (pt->pt_loopvars[depth] != NULL
5112 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005113 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005114 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005115 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005116}
5117
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005118/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005119 * Return the next (unique) copy ID.
5120 * Used for serializing nested structures.
5121 */
5122 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005123get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005124{
5125 current_copyID += COPYID_INC;
5126 return current_copyID;
5127}
5128
5129/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005130 * Garbage collection for lists and dictionaries.
5131 *
5132 * We use reference counts to be able to free most items right away when they
5133 * are no longer used. But for composite items it's possible that it becomes
5134 * unused while the reference count is > 0: When there is a recursive
5135 * reference. Example:
5136 * :let l = [1, 2, 3]
5137 * :let d = {9: l}
5138 * :let l[1] = d
5139 *
5140 * Since this is quite unusual we handle this with garbage collection: every
5141 * once in a while find out which lists and dicts are not referenced from any
5142 * variable.
5143 *
5144 * Here is a good reference text about garbage collection (refers to Python
5145 * but it applies to all reference-counting mechanisms):
5146 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005147 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005148
5149/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005150 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005151 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005152 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005153 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005154 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005155garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005156{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005157 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005158 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005159 buf_T *buf;
5160 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005161 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005162 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005163
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005164 if (!testing)
5165 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005166 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005167 want_garbage_collect = FALSE;
5168 may_garbage_collect = FALSE;
5169 garbage_collect_at_exit = FALSE;
5170 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005171
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005172 // The execution stack can grow big, limit the size.
5173 if (exestack.ga_maxlen - exestack.ga_len > 500)
5174 {
5175 size_t new_len;
5176 char_u *pp;
5177 int n;
5178
5179 // Keep 150% of the current size, with a minimum of the growth size.
5180 n = exestack.ga_len / 2;
5181 if (n < exestack.ga_growsize)
5182 n = exestack.ga_growsize;
5183
5184 // Don't make it bigger though.
5185 if (exestack.ga_len + n < exestack.ga_maxlen)
5186 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005187 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005188 pp = vim_realloc(exestack.ga_data, new_len);
5189 if (pp == NULL)
5190 return FAIL;
5191 exestack.ga_maxlen = exestack.ga_len + n;
5192 exestack.ga_data = pp;
5193 }
5194 }
5195
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005196 // We advance by two because we add one for items referenced through
5197 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005198 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005199
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005200 /*
5201 * 1. Go through all accessible variables and mark all lists and dicts
5202 * with copyID.
5203 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005204
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005205 // Don't free variables in the previous_funccal list unless they are only
5206 // referenced through previous_funccal. This must be first, because if
5207 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005208 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005209
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005210 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005211 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005212
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005213 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005214 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005215 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5216 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005217
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005218 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005219 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005220 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5221 NULL, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005222 // window-local variables in autocmd windows
Bram Moolenaare76062c2022-11-28 18:51:43 +00005223 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005224 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00005225 abort = abort || set_ref_in_item(
5226 &aucmd_win[i].auc_win->w_winvar.di_tv, copyID, NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005227#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005228 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005229 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5230 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005231 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005232 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005233 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5234 NULL, NULL);
5235#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005236
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005237 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005238 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005239 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5240 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005241 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005242 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005243
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005244 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005245 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005246
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005247 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005248 abort = abort || set_ref_in_functions(copyID);
5249
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005250 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005251 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005252
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005253 // funcstacks keep variables for closures
5254 abort = abort || set_ref_in_funcstacks(copyID);
5255
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005256 // loopvars keep variables for loop blocks
5257 abort = abort || set_ref_in_loopvars(copyID);
5258
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005259 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005260 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005261
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005262 // callbacks in buffers
5263 abort = abort || set_ref_in_buffers(copyID);
5264
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005265 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5266 abort = abort || set_ref_in_insexpand_funcs(copyID);
5267
5268 // 'operatorfunc' callback
5269 abort = abort || set_ref_in_opfunc(copyID);
5270
5271 // 'tagfunc' callback
5272 abort = abort || set_ref_in_tagfunc(copyID);
5273
5274 // 'imactivatefunc' and 'imstatusfunc' callbacks
5275 abort = abort || set_ref_in_im_funcs(copyID);
5276
Bram Moolenaar1dced572012-04-05 16:54:08 +02005277#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005278 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005279#endif
5280
Bram Moolenaardb913952012-06-29 12:54:53 +02005281#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005282 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005283#endif
5284
5285#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005286 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005287#endif
5288
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005289#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005290 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005291 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005292#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005293#ifdef FEAT_NETBEANS_INTG
5294 abort = abort || set_ref_in_nb_channel(copyID);
5295#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005296
Bram Moolenaare3188e22016-05-31 21:13:04 +02005297#ifdef FEAT_TIMERS
5298 abort = abort || set_ref_in_timer(copyID);
5299#endif
5300
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005301#ifdef FEAT_QUICKFIX
5302 abort = abort || set_ref_in_quickfix(copyID);
5303#endif
5304
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005305#ifdef FEAT_TERMINAL
5306 abort = abort || set_ref_in_term(copyID);
5307#endif
5308
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005309#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005310 abort = abort || set_ref_in_popups(copyID);
5311#endif
5312
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005313 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005314 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005315 /*
5316 * 2. Free lists and dictionaries that are not referenced.
5317 */
5318 did_free = free_unref_items(copyID);
5319
5320 /*
5321 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005322 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005323 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005324 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005325 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005326 else if (p_verbose > 0)
5327 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005328 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005329 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005330
5331 return did_free;
5332}
5333
5334/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005335 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005336 */
5337 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005338free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005339{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005340 int did_free = FALSE;
5341
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005342 // Let all "free" functions know that we are here. This means no
5343 // dictionaries, lists, channels or jobs are to be freed, because we will
5344 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005345 in_free_unref_items = TRUE;
5346
5347 /*
5348 * PASS 1: free the contents of the items. We don't free the items
5349 * themselves yet, so that it is possible to decrement refcount counters
5350 */
5351
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005352 // Go through the list of dicts and free items without this copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005353 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005354
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005355 // Go through the list of lists and free items without this copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005356 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005357
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005358 // Go through the list of objects and free items without this copyID.
5359 did_free |= object_free_nonref(copyID);
5360
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005361#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005362 // Go through the list of jobs and free items without the copyID. This
5363 // must happen before doing channels, because jobs refer to channels, but
5364 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005365 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5366
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005367 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005368 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5369#endif
5370
5371 /*
5372 * PASS 2: free the items themselves.
5373 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005374 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005375 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005376
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005377#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005378 // Go through the list of jobs and free items without the copyID. This
5379 // must happen before doing channels, because jobs refer to channels, but
5380 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005381 free_unused_jobs(copyID, COPYID_MASK);
5382
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005383 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005384 free_unused_channels(copyID, COPYID_MASK);
5385#endif
5386
5387 in_free_unref_items = FALSE;
5388
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005389 return did_free;
5390}
5391
5392/*
5393 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005394 * "list_stack" is used to add lists to be marked. Can be NULL.
5395 *
5396 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005397 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005398 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005399set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005400{
5401 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005402 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005403 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005404 hashtab_T *cur_ht;
5405 ht_stack_T *ht_stack = NULL;
5406 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005407
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005408 cur_ht = ht;
5409 for (;;)
5410 {
5411 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005412 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005413 // Mark each item in the hashtab. If the item contains a hashtab
5414 // it is added to ht_stack, if it contains a list it is added to
5415 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005416 todo = (int)cur_ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00005417 FOR_ALL_HASHTAB_ITEMS(cur_ht, hi, todo)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005418 if (!HASHITEM_EMPTY(hi))
5419 {
5420 --todo;
5421 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5422 &ht_stack, list_stack);
5423 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005424 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005425
5426 if (ht_stack == NULL)
5427 break;
5428
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005429 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005430 cur_ht = ht_stack->ht;
5431 tempitem = ht_stack;
5432 ht_stack = ht_stack->prev;
5433 free(tempitem);
5434 }
5435
5436 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005437}
5438
Dominique Pelle748b3082022-01-08 12:41:16 +00005439#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5440 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005441/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005442 * Mark a dict and its items with "copyID".
5443 * Returns TRUE if setting references failed somehow.
5444 */
5445 int
5446set_ref_in_dict(dict_T *d, int copyID)
5447{
5448 if (d != NULL && d->dv_copyID != copyID)
5449 {
5450 d->dv_copyID = copyID;
5451 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5452 }
5453 return FALSE;
5454}
Dominique Pelle748b3082022-01-08 12:41:16 +00005455#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005456
5457/*
5458 * Mark a list and its items with "copyID".
5459 * Returns TRUE if setting references failed somehow.
5460 */
5461 int
5462set_ref_in_list(list_T *ll, int copyID)
5463{
5464 if (ll != NULL && ll->lv_copyID != copyID)
5465 {
5466 ll->lv_copyID = copyID;
5467 return set_ref_in_list_items(ll, copyID, NULL);
5468 }
5469 return FALSE;
5470}
5471
5472/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005473 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005474 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5475 *
5476 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005477 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005478 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005479set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005480{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005481 listitem_T *li;
5482 int abort = FALSE;
5483 list_T *cur_l;
5484 list_stack_T *list_stack = NULL;
5485 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005486
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005487 cur_l = l;
5488 for (;;)
5489 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005490 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005491 // Mark each item in the list. If the item contains a hashtab
5492 // it is added to ht_stack, if it contains a list it is added to
5493 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005494 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5495 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5496 ht_stack, &list_stack);
5497 if (list_stack == NULL)
5498 break;
5499
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005500 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005501 cur_l = list_stack->list;
5502 tempitem = list_stack;
5503 list_stack = list_stack->prev;
5504 free(tempitem);
5505 }
5506
5507 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005508}
5509
5510/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005511 * Mark the partial in callback 'cb' with "copyID".
5512 */
5513 int
5514set_ref_in_callback(callback_T *cb, int copyID)
5515{
5516 typval_T tv;
5517
5518 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5519 return FALSE;
5520
5521 tv.v_type = VAR_PARTIAL;
5522 tv.vval.v_partial = cb->cb_partial;
5523 return set_ref_in_item(&tv, copyID, NULL, NULL);
5524}
5525
5526/*
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005527 * Mark the dict "dd" with "copyID".
5528 * Also see set_ref_in_item().
5529 */
5530 static int
5531set_ref_in_item_dict(
5532 dict_T *dd,
5533 int copyID,
5534 ht_stack_T **ht_stack,
5535 list_stack_T **list_stack)
5536{
5537 if (dd == NULL || dd->dv_copyID == copyID)
5538 return FALSE;
5539
5540 // Didn't see this dict yet.
5541 dd->dv_copyID = copyID;
5542 if (ht_stack == NULL)
5543 return set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5544
5545 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5546 if (newitem == NULL)
5547 return TRUE;
5548
5549 newitem->ht = &dd->dv_hashtab;
5550 newitem->prev = *ht_stack;
5551 *ht_stack = newitem;
5552
5553 return FALSE;
5554}
5555
5556/*
5557 * Mark the list "ll" with "copyID".
5558 * Also see set_ref_in_item().
5559 */
5560 static int
5561set_ref_in_item_list(
5562 list_T *ll,
5563 int copyID,
5564 ht_stack_T **ht_stack,
5565 list_stack_T **list_stack)
5566{
5567 if (ll == NULL || ll->lv_copyID == copyID)
5568 return FALSE;
5569
5570 // Didn't see this list yet.
5571 ll->lv_copyID = copyID;
5572 if (list_stack == NULL)
5573 return set_ref_in_list_items(ll, copyID, ht_stack);
5574
5575 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5576 if (newitem == NULL)
5577 return TRUE;
5578
5579 newitem->list = ll;
5580 newitem->prev = *list_stack;
5581 *list_stack = newitem;
5582
5583 return FALSE;
5584}
5585
5586/*
5587 * Mark the partial "pt" with "copyID".
5588 * Also see set_ref_in_item().
5589 */
5590 static int
5591set_ref_in_item_partial(
5592 partial_T *pt,
5593 int copyID,
5594 ht_stack_T **ht_stack,
5595 list_stack_T **list_stack)
5596{
5597 if (pt == NULL || pt->pt_copyID == copyID)
5598 return FALSE;
5599
5600 // Didn't see this partial yet.
5601 pt->pt_copyID = copyID;
5602
5603 int abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
5604
5605 if (pt->pt_dict != NULL)
5606 {
5607 typval_T dtv;
5608
5609 dtv.v_type = VAR_DICT;
5610 dtv.vval.v_dict = pt->pt_dict;
5611 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5612 }
5613
5614 for (int i = 0; i < pt->pt_argc; ++i)
5615 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5616 ht_stack, list_stack);
5617 // pt_funcstack is handled in set_ref_in_funcstacks()
5618 // pt_loopvars is handled in set_ref_in_loopvars()
5619
5620 return abort;
5621}
5622
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005623#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005624/*
5625 * Mark the job "pt" with "copyID".
5626 * Also see set_ref_in_item().
5627 */
5628 static int
5629set_ref_in_item_job(
5630 job_T *job,
5631 int copyID,
5632 ht_stack_T **ht_stack,
5633 list_stack_T **list_stack)
5634{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005635 typval_T dtv;
5636
5637 if (job == NULL || job->jv_copyID == copyID)
5638 return FALSE;
5639
5640 job->jv_copyID = copyID;
5641 if (job->jv_channel != NULL)
5642 {
5643 dtv.v_type = VAR_CHANNEL;
5644 dtv.vval.v_channel = job->jv_channel;
5645 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5646 }
5647 if (job->jv_exit_cb.cb_partial != NULL)
5648 {
5649 dtv.v_type = VAR_PARTIAL;
5650 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
5651 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5652 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005653
5654 return FALSE;
5655}
5656
5657/*
5658 * Mark the channel "ch" with "copyID".
5659 * Also see set_ref_in_item().
5660 */
5661 static int
5662set_ref_in_item_channel(
5663 channel_T *ch,
5664 int copyID,
5665 ht_stack_T **ht_stack,
5666 list_stack_T **list_stack)
5667{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005668 typval_T dtv;
5669
5670 if (ch == NULL || ch->ch_copyID == copyID)
5671 return FALSE;
5672
5673 ch->ch_copyID = copyID;
5674 for (ch_part_T part = PART_SOCK; part < PART_COUNT; ++part)
5675 {
5676 for (jsonq_T *jq = ch->ch_part[part].ch_json_head.jq_next;
5677 jq != NULL; jq = jq->jq_next)
5678 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5679 for (cbq_T *cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5680 cq = cq->cq_next)
5681 if (cq->cq_callback.cb_partial != NULL)
5682 {
5683 dtv.v_type = VAR_PARTIAL;
5684 dtv.vval.v_partial = cq->cq_callback.cb_partial;
5685 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5686 }
5687 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
5688 {
5689 dtv.v_type = VAR_PARTIAL;
5690 dtv.vval.v_partial = ch->ch_part[part].ch_callback.cb_partial;
5691 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5692 }
5693 }
5694 if (ch->ch_callback.cb_partial != NULL)
5695 {
5696 dtv.v_type = VAR_PARTIAL;
5697 dtv.vval.v_partial = ch->ch_callback.cb_partial;
5698 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5699 }
5700 if (ch->ch_close_cb.cb_partial != NULL)
5701 {
5702 dtv.v_type = VAR_PARTIAL;
5703 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
5704 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5705 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005706
5707 return FALSE;
5708}
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005709#endif
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005710
5711/*
5712 * Mark the class "cl" with "copyID".
5713 * Also see set_ref_in_item().
5714 */
5715 static int
5716set_ref_in_item_class(
5717 class_T *cl,
5718 int copyID,
5719 ht_stack_T **ht_stack,
5720 list_stack_T **list_stack)
5721{
5722 int abort = FALSE;
5723
5724 if (cl == NULL || cl->class_copyID == copyID
5725 || (cl->class_flags & CLASS_INTERFACE) != 0)
5726 return FALSE;
5727
5728 cl->class_copyID = copyID;
5729 for (int i = 0; !abort && i < cl->class_class_member_count; ++i)
5730 abort = abort || set_ref_in_item(
5731 &cl->class_members_tv[i],
5732 copyID, ht_stack, list_stack);
5733
5734 for (int i = 0; !abort && i < cl->class_class_function_count; ++i)
5735 abort = abort || set_ref_in_func(NULL,
5736 cl->class_class_functions[i], copyID);
5737
5738 for (int i = 0; !abort && i < cl->class_obj_method_count; ++i)
5739 abort = abort || set_ref_in_func(NULL,
5740 cl->class_obj_methods[i], copyID);
5741
5742 return abort;
5743}
5744
5745/*
5746 * Mark the object "cl" with "copyID".
5747 * Also see set_ref_in_item().
5748 */
5749 static int
5750set_ref_in_item_object(
5751 object_T *obj,
5752 int copyID,
5753 ht_stack_T **ht_stack,
5754 list_stack_T **list_stack)
5755{
5756 int abort = FALSE;
5757
5758 if (obj == NULL || obj->obj_copyID == copyID)
5759 return FALSE;
5760
5761 obj->obj_copyID = copyID;
5762
5763 // The typval_T array is right after the object_T.
5764 typval_T *mtv = (typval_T *)(obj + 1);
5765 for (int i = 0; !abort
5766 && i < obj->obj_class->class_obj_member_count; ++i)
5767 abort = abort || set_ref_in_item(mtv + i, copyID,
5768 ht_stack, list_stack);
5769
5770 return abort;
5771}
5772
5773/*
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005774 * Mark all lists, dicts and other container types referenced through typval
5775 * "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005776 * "list_stack" is used to add lists to be marked. Can be NULL.
5777 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5778 *
5779 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005780 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005781 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005782set_ref_in_item(
5783 typval_T *tv,
5784 int copyID,
5785 ht_stack_T **ht_stack,
5786 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005787{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005788 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005789
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005790 switch (tv->v_type)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005791 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005792 case VAR_DICT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005793 return set_ref_in_item_dict(tv->vval.v_dict, copyID,
5794 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005795
5796 case VAR_LIST:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005797 return set_ref_in_item_list(tv->vval.v_list, copyID,
5798 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005799
5800 case VAR_FUNC:
5801 {
5802 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
5803 break;
5804 }
5805
5806 case VAR_PARTIAL:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005807 return set_ref_in_item_partial(tv->vval.v_partial, copyID,
5808 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005809
5810 case VAR_JOB:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005811#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005812 return set_ref_in_item_job(tv->vval.v_job, copyID,
5813 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005814#else
5815 break;
5816#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005817
5818 case VAR_CHANNEL:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005819#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005820 return set_ref_in_item_channel(tv->vval.v_channel, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005821 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005822#else
5823 break;
5824#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005825
5826 case VAR_CLASS:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005827 return set_ref_in_item_class(tv->vval.v_class, copyID,
5828 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005829
5830 case VAR_OBJECT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005831 return set_ref_in_item_object(tv->vval.v_object, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005832 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005833
5834 case VAR_UNKNOWN:
5835 case VAR_ANY:
5836 case VAR_VOID:
5837 case VAR_BOOL:
5838 case VAR_SPECIAL:
5839 case VAR_NUMBER:
5840 case VAR_FLOAT:
5841 case VAR_STRING:
5842 case VAR_BLOB:
5843 case VAR_INSTR:
5844 // Types that do not contain any other item
5845 break;
5846 }
5847
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005848 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005849}
5850
Bram Moolenaar8c711452005-01-14 21:53:12 +00005851/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005852 * Return a string with the string representation of a variable.
5853 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005854 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005855 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005856 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005857 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005858 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005859 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5860 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005861 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005862 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005863 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005864echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005865 typval_T *tv,
5866 char_u **tofree,
5867 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005868 int copyID,
5869 int echo_style,
5870 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005871 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005872{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005873 static int recurse = 0;
5874 char_u *r = NULL;
5875
Bram Moolenaar33570922005-01-25 22:26:29 +00005876 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005877 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005878 if (!did_echo_string_emsg)
5879 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005880 // Only give this message once for a recursive call to avoid
5881 // flooding the user with errors. And stop iterating over lists
5882 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005883 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005884 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005885 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005886 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005887 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005888 }
5889 ++recurse;
5890
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005891 switch (tv->v_type)
5892 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005893 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005894 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005895 {
5896 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005897 r = tv->vval.v_string;
5898 if (r == NULL)
5899 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005900 }
5901 else
5902 {
5903 *tofree = string_quote(tv->vval.v_string, FALSE);
5904 r = *tofree;
5905 }
5906 break;
5907
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005908 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005909 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005910 char_u buf[MAX_FUNC_NAME_LEN];
5911
5912 if (echo_style)
5913 {
LemonBoya5d35902022-04-29 21:15:02 +01005914 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5915 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005916 buf, MAX_FUNC_NAME_LEN);
5917 if (r == buf)
5918 {
5919 r = vim_strsave(buf);
5920 *tofree = r;
5921 }
5922 else
5923 *tofree = NULL;
5924 }
5925 else
5926 {
5927 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5928 : make_ufunc_name_readable(
5929 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5930 TRUE);
5931 r = *tofree;
5932 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005933 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005934 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005935
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005936 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005937 {
5938 partial_T *pt = tv->vval.v_partial;
5939 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005940 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005941 garray_T ga;
5942 int i;
5943 char_u *tf;
5944
5945 ga_init2(&ga, 1, 100);
5946 ga_concat(&ga, (char_u *)"function(");
5947 if (fname != NULL)
5948 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005949 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005950 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005951 && vim_isupper(fname[1]))
5952 {
5953 ga_concat(&ga, (char_u *)"'g:");
5954 ga_concat(&ga, fname + 1);
5955 }
5956 else
5957 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005958 vim_free(fname);
5959 }
5960 if (pt != NULL && pt->pt_argc > 0)
5961 {
5962 ga_concat(&ga, (char_u *)", [");
5963 for (i = 0; i < pt->pt_argc; ++i)
5964 {
5965 if (i > 0)
5966 ga_concat(&ga, (char_u *)", ");
5967 ga_concat(&ga,
5968 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5969 vim_free(tf);
5970 }
5971 ga_concat(&ga, (char_u *)"]");
5972 }
5973 if (pt != NULL && pt->pt_dict != NULL)
5974 {
5975 typval_T dtv;
5976
5977 ga_concat(&ga, (char_u *)", ");
5978 dtv.v_type = VAR_DICT;
5979 dtv.vval.v_dict = pt->pt_dict;
5980 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5981 vim_free(tf);
5982 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005983 // terminate with ')' and a NUL
5984 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005985
5986 *tofree = ga.ga_data;
5987 r = *tofree;
5988 break;
5989 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005990
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005991 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005992 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005993 break;
5994
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005995 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005996 if (tv->vval.v_list == NULL)
5997 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005998 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005999 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02006000 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006001 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006002 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6003 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006004 {
6005 *tofree = NULL;
6006 r = (char_u *)"[...]";
6007 }
6008 else
6009 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006010 int old_copyID = tv->vval.v_list->lv_copyID;
6011
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006012 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006013 *tofree = list2string(tv, copyID, restore_copyID);
6014 if (restore_copyID)
6015 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006016 r = *tofree;
6017 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006018 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006019
Bram Moolenaar8c711452005-01-14 21:53:12 +00006020 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006021 if (tv->vval.v_dict == NULL)
6022 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006023 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006024 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006025 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006026 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006027 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6028 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006029 {
6030 *tofree = NULL;
6031 r = (char_u *)"{...}";
6032 }
6033 else
6034 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006035 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006036
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006037 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006038 *tofree = dict2string(tv, copyID, restore_copyID);
6039 if (restore_copyID)
6040 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006041 r = *tofree;
6042 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006043 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006044
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006045 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006046 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006047 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006048 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006049 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006050 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006051 break;
6052
Bram Moolenaar835dc632016-02-07 14:27:38 +01006053 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006054 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006055#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00006056 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02006057 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
6058 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006059 if (composite_val)
6060 {
6061 *tofree = string_quote(r, FALSE);
6062 r = *tofree;
6063 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006064#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006066
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006067 case VAR_INSTR:
6068 *tofree = NULL;
6069 r = (char_u *)"instructions";
6070 break;
6071
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006072 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006073 {
6074 class_T *cl = tv->vval.v_class;
6075 size_t len = 6 + (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
6076 r = *tofree = alloc(len);
6077 vim_snprintf((char *)r, len, "class %s",
6078 cl == NULL ? "[unknown]" : (char *)cl->class_name);
6079 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006080 break;
6081
6082 case VAR_OBJECT:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006083 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006084 garray_T ga;
6085 ga_init2(&ga, 1, 50);
6086 ga_concat(&ga, (char_u *)"object of ");
6087 object_T *obj = tv->vval.v_object;
6088 class_T *cl = obj == NULL ? NULL : obj->obj_class;
6089 ga_concat(&ga, cl == NULL ? (char_u *)"[unknown]"
6090 : cl->class_name);
6091 if (cl != NULL)
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006092 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006093 ga_concat(&ga, (char_u *)" {");
6094 for (int i = 0; i < cl->class_obj_member_count; ++i)
6095 {
6096 if (i > 0)
6097 ga_concat(&ga, (char_u *)", ");
Bram Moolenaard505d172022-12-18 21:42:55 +00006098 ocmember_T *m = &cl->class_obj_members[i];
6099 ga_concat(&ga, m->ocm_name);
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006100 ga_concat(&ga, (char_u *)": ");
6101 char_u *tf = NULL;
6102 ga_concat(&ga, echo_string_core(
6103 (typval_T *)(obj + 1) + i,
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006104 &tf, numbuf, copyID, echo_style,
6105 restore_copyID, composite_val));
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006106 vim_free(tf);
6107 }
6108 ga_concat(&ga, (char_u *)"}");
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006109 }
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006110
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006111 *tofree = r = ga.ga_data;
6112 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006113 break;
6114
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006115 case VAR_FLOAT:
6116 *tofree = NULL;
6117 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6118 r = numbuf;
6119 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006120
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006121 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006122 case VAR_SPECIAL:
6123 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006124 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006125 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006126 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006127
Bram Moolenaar8502c702014-06-17 12:51:16 +02006128 if (--recurse == 0)
6129 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006130 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006131}
6132
6133/*
6134 * Return a string with the string representation of a variable.
6135 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6136 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006137 * Does not put quotes around strings, as ":echo" displays values.
6138 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6139 * May return NULL.
6140 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006141 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006142echo_string(
6143 typval_T *tv,
6144 char_u **tofree,
6145 char_u *numbuf,
6146 int copyID)
6147{
6148 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6149}
6150
6151/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006152 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6153 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006154 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006155 */
6156 int
6157buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6158{
6159 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006160 char_u *t;
6161 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006162
6163 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6164 return -1;
6165
6166 if (lnum > buf->b_ml.ml_line_count)
6167 lnum = buf->b_ml.ml_line_count;
6168
6169 str = ml_get_buf(buf, lnum, FALSE);
6170 if (str == NULL)
6171 return -1;
6172
6173 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006174 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006175
Bram Moolenaar91458462021-01-13 20:08:38 +01006176 // count the number of characters
6177 t = str;
6178 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6179 t += mb_ptr2len(t);
6180
6181 // In insert mode, when the cursor is at the end of a non-empty line,
6182 // byteidx points to the NUL character immediately past the end of the
6183 // string. In this case, add one to the character count.
6184 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6185 count++;
6186
6187 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006188}
6189
6190/*
6191 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006192 * byte index. Works only for loaded buffers. Returns -1 on failure.
6193 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006194 */
6195 int
6196buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6197{
6198 char_u *str;
6199 char_u *t;
6200
6201 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6202 return -1;
6203
6204 if (lnum > buf->b_ml.ml_line_count)
6205 lnum = buf->b_ml.ml_line_count;
6206
6207 str = ml_get_buf(buf, lnum, FALSE);
6208 if (str == NULL)
6209 return -1;
6210
6211 // Convert the character offset to a byte offset
6212 t = str;
6213 while (*t != NUL && --charidx > 0)
6214 t += mb_ptr2len(t);
6215
Bram Moolenaar91458462021-01-13 20:08:38 +01006216 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006217}
6218
6219/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006221 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006223 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006224var2fpos(
6225 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006226 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006227 int *fnum, // set to fnum for '0, 'A, etc.
6228 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006230 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006232 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006234 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006235 if (varp->v_type == VAR_LIST)
6236 {
6237 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006238 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006239 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006240 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006241
6242 l = varp->vval.v_list;
6243 if (l == NULL)
6244 return NULL;
6245
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006246 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006247 pos.lnum = list_find_nr(l, 0L, &error);
6248 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006249 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006250 if (charcol)
6251 len = (long)mb_charlen(ml_get(pos.lnum));
6252 else
6253 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006254
Bram Moolenaarec65d772020-08-20 22:29:12 +02006255 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006256 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006257 li = list_find(l, 1L);
6258 if (li != NULL && li->li_tv.v_type == VAR_STRING
6259 && li->li_tv.vval.v_string != NULL
6260 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006261 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006262 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006263 }
6264 else
6265 {
6266 pos.col = list_find_nr(l, 1L, &error);
6267 if (error)
6268 return NULL;
6269 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006270
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006271 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006272 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006273 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006274 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006275
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006276 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006277 pos.coladd = list_find_nr(l, 2L, &error);
6278 if (error)
6279 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006280
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006281 return &pos;
6282 }
6283
Bram Moolenaarc5809432021-03-27 21:23:30 +01006284 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6285 return NULL;
6286
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006287 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006288 if (name == NULL)
6289 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006290
6291 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006292 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006293 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006294 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006295 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006296 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006297 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006298 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006299 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006300 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006301 pos = VIsual;
6302 else
6303 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006304 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006305 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006306 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006308 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006309 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6311 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006312 pos = *pp;
6313 }
6314 if (pos.lnum != 0)
6315 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006316 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006317 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6318 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006320
Bram Moolenaara5525202006-03-02 22:52:09 +00006321 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006322
Bram Moolenaar477933c2007-07-17 14:32:23 +00006323 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006324 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006325 // the "w_valid" flags are not reset when moving the cursor, but they
6326 // do matter for update_topline() and validate_botline().
6327 check_cursor_moved(curwin);
6328
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006329 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006330 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006331 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006332 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006333 // In silent Ex mode topline is zero, but that's not a valid line
6334 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006335 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006336 return &pos;
6337 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006338 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006339 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006340 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006341 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006342 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006343 return &pos;
6344 }
6345 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006346 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006348 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 {
6350 pos.lnum = curbuf->b_ml.ml_line_count;
6351 pos.col = 0;
6352 }
6353 else
6354 {
6355 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006356 if (charcol)
6357 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6358 else
6359 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 }
6361 return &pos;
6362 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006363 if (in_vim9script())
6364 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 return NULL;
6366}
6367
6368/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006369 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006370 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006371 * Note that the column is passed on as-is, the caller may want to decrement
6372 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006373 * If "charcol" is TRUE use the column as the character index instead of the
6374 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006375 * Return FAIL when conversion is not possible, doesn't check the position for
6376 * validity.
6377 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006378 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006379list2fpos(
6380 typval_T *arg,
6381 pos_T *posp,
6382 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006383 colnr_T *curswantp,
6384 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006385{
6386 list_T *l = arg->vval.v_list;
6387 long i = 0;
6388 long n;
6389
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006390 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6391 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006392 if (arg->v_type != VAR_LIST
6393 || l == NULL
6394 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006395 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006396 return FAIL;
6397
6398 if (fnump != NULL)
6399 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006400 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006401 if (n < 0)
6402 return FAIL;
6403 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006404 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006405 *fnump = n;
6406 }
6407
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006408 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006409 if (n < 0)
6410 return FAIL;
6411 posp->lnum = n;
6412
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006413 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006414 if (n < 0)
6415 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006416 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006417 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006418 if (charcol)
6419 {
6420 buf_T *buf;
6421
6422 // Get the text for the specified line in a loaded buffer
6423 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6424 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6425 return FAIL;
6426
Bram Moolenaar79f23442022-10-10 12:42:57 +01006427 n = buf_charidx_to_byteidx(buf,
6428 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006429 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006430 posp->col = n;
6431
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006432 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006433 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006434 posp->coladd = 0;
6435 else
6436 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006437
Bram Moolenaar493c1782014-05-28 14:34:46 +02006438 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006439 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006440
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006441 return OK;
6442}
6443
6444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 * Get the length of an environment variable name.
6446 * Advance "arg" to the first character after the name.
6447 * Return 0 for error.
6448 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006449 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006450get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451{
6452 char_u *p;
6453 int len;
6454
6455 for (p = *arg; vim_isIDc(*p); ++p)
6456 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006457 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 return 0;
6459
6460 len = (int)(p - *arg);
6461 *arg = p;
6462 return len;
6463}
6464
6465/*
6466 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006467 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 * Return 0 if something is wrong.
6469 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006470 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006471get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472{
6473 char_u *p;
6474 int len;
6475
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006476 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006478 {
6479 if (*p == ':')
6480 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006481 // "s:" is start of "s:var", but "n:" is not and can be used in
6482 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006483 len = (int)(p - *arg);
6484 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6485 || len > 1)
6486 break;
6487 }
6488 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006489 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 return 0;
6491
6492 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006493 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494
6495 return len;
6496}
6497
6498/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006499 * Get the length of the name of a variable or function.
6500 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006502 * Return -1 if curly braces expansion failed.
6503 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 * If the name contains 'magic' {}'s, expand them and return the
6505 * expanded name in an allocated string via 'alias' - caller must free.
6506 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006507 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006508get_name_len(
6509 char_u **arg,
6510 char_u **alias,
6511 int evaluate,
6512 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513{
6514 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 char_u *p;
6516 char_u *expr_start;
6517 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006519 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520
6521 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6522 && (*arg)[2] == (int)KE_SNR)
6523 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006524 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 *arg += 3;
6526 return get_id_len(arg) + 3;
6527 }
6528 len = eval_fname_script(*arg);
6529 if (len > 0)
6530 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006531 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 *arg += len;
6533 }
6534
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006536 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006538 p = find_name_end(*arg, &expr_start, &expr_end,
6539 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 if (expr_start != NULL)
6541 {
6542 char_u *temp_string;
6543
6544 if (!evaluate)
6545 {
6546 len += (int)(p - *arg);
6547 *arg = skipwhite(p);
6548 return len;
6549 }
6550
6551 /*
6552 * Include any <SID> etc in the expanded string:
6553 * Thus the -len here.
6554 */
6555 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6556 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006557 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 *alias = temp_string;
6559 *arg = skipwhite(p);
6560 return (int)STRLEN(temp_string);
6561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562
6563 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006564 // Only give an error when there is something, otherwise it will be
6565 // reported at a higher level.
6566 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006567 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568
6569 return len;
6570}
6571
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006572/*
6573 * Find the end of a variable or function name, taking care of magic braces.
6574 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6575 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006576 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006577 * Return a pointer to just after the name. Equal to "arg" if there is no
6578 * valid name.
6579 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006580 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006581find_name_end(
6582 char_u *arg,
6583 char_u **expr_start,
6584 char_u **expr_end,
6585 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006587 int mb_nest = 0;
6588 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006590 int len;
zeertzjqa93d9cd2023-05-02 16:25:47 +01006591 int allow_curly = (flags & FNE_ALLOW_CURLY) || !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006593 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006595 *expr_start = NULL;
6596 *expr_end = NULL;
6597 }
6598
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006599 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006600 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006601 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006602 return arg;
6603
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006604 for (p = arg; *p != NUL
6605 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006606 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006607 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006608 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006609 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006610 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006611 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006612 if (*p == '\'')
6613 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006614 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006615 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006616 ;
6617 if (*p == NUL)
6618 break;
6619 }
6620 else if (*p == '"')
6621 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006622 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006623 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006624 if (*p == '\\' && p[1] != NUL)
6625 ++p;
6626 if (*p == NUL)
6627 break;
6628 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006629 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6630 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006631 // "s:" is start of "s:var", but "n:" is not and can be used in
6632 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006633 len = (int)(p - arg);
6634 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006635 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006636 break;
6637 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006638
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006639 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006641 if (*p == '[')
6642 ++br_nest;
6643 else if (*p == ']')
6644 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006646
zeertzjqa93d9cd2023-05-02 16:25:47 +01006647 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006649 if (*p == '{')
6650 {
6651 mb_nest++;
6652 if (expr_start != NULL && *expr_start == NULL)
6653 *expr_start = p;
6654 }
6655 else if (*p == '}')
6656 {
6657 mb_nest--;
6658 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6659 *expr_end = p;
6660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 }
6663
6664 return p;
6665}
6666
6667/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006668 * Expands out the 'magic' {}'s in a variable/function name.
6669 * Note that this can call itself recursively, to deal with
6670 * constructs like foo{bar}{baz}{bam}
6671 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6672 * "in_start" ^
6673 * "expr_start" ^
6674 * "expr_end" ^
6675 * "in_end" ^
6676 *
6677 * Returns a new allocated string, which the caller must free.
6678 * Returns NULL for failure.
6679 */
6680 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006681make_expanded_name(
6682 char_u *in_start,
6683 char_u *expr_start,
6684 char_u *expr_end,
6685 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006686{
6687 char_u c1;
6688 char_u *retval = NULL;
6689 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006690
6691 if (expr_end == NULL || in_end == NULL)
6692 return NULL;
6693 *expr_start = NUL;
6694 *expr_end = NUL;
6695 c1 = *in_end;
6696 *in_end = NUL;
6697
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006698 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006699 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006700 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006701 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6702 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006703 if (retval != NULL)
6704 {
6705 STRCPY(retval, in_start);
6706 STRCAT(retval, temp_result);
6707 STRCAT(retval, expr_end + 1);
6708 }
6709 }
6710 vim_free(temp_result);
6711
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006712 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006713 *expr_start = '{';
6714 *expr_end = '}';
6715
6716 if (retval != NULL)
6717 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006718 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006719 if (expr_start != NULL)
6720 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006721 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006722 temp_result = make_expanded_name(retval, expr_start,
6723 expr_end, temp_result);
6724 vim_free(retval);
6725 retval = temp_result;
6726 }
6727 }
6728
6729 return retval;
6730}
6731
6732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006734 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006736 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006737eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006739 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006740}
6741
6742/*
6743 * Return TRUE if character "c" can be used as the first character in a
6744 * variable or function name (excluding '{' and '}').
6745 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006746 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006747eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006748{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006749 return ASCII_ISALPHA(c) || c == '_';
6750}
6751
6752/*
6753 * Return TRUE if character "c" can be used as the first character of a
6754 * dictionary key.
6755 */
6756 int
6757eval_isdictc(int c)
6758{
6759 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760}
6761
6762/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006763 * Handle:
6764 * - expr[expr], expr[expr:expr] subscript
6765 * - ".name" lookup
6766 * - function call with Funcref variable: func(expr)
6767 * - method call: var->method()
6768 *
6769 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006770 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006771 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006772 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006773handle_subscript(
6774 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006775 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006776 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006777 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006778 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006779{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006780 int evaluate = evalarg != NULL
6781 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006782 int ret = OK;
6783 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006784 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006785 int getnext;
6786 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006787
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006788 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006789 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006790 // When at the end of the line and ".name" or "->{" or "->X" follows in
6791 // the next line then consume the line break.
6792 p = eval_next_non_blank(*arg, evalarg, &getnext);
6793 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00006794 && ((*p == '.'
6795 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
6796 || rettv->v_type == VAR_CLASS
6797 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02006798 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6799 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6800 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006801 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006802 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006803 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006804 check_white = FALSE;
6805 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006806
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006807 if (rettv->v_type == VAR_ANY)
6808 {
6809 char_u *exp_name;
6810 int cc;
6811 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006812 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006813 type_T *type;
6814
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006815 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006816 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006817 if (**arg != '.')
6818 {
6819 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006820 semsg(_(e_expected_dot_after_name_str),
6821 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006822 ret = FAIL;
6823 break;
6824 }
6825 ++*arg;
6826 if (IS_WHITE_OR_NUL(**arg))
6827 {
6828 if (verbose)
6829 emsg(_(e_no_white_space_allowed_after_dot));
6830 ret = FAIL;
6831 break;
6832 }
6833
6834 // isolate the name
6835 exp_name = *arg;
6836 while (eval_isnamec(**arg))
6837 ++*arg;
6838 cc = **arg;
6839 **arg = NUL;
6840
6841 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01006842 evalarg == NULL ? NULL : evalarg->eval_cctx,
6843 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006844 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006845
6846 if (idx < 0 && ufunc == NULL)
6847 {
6848 ret = FAIL;
6849 break;
6850 }
6851 if (idx >= 0)
6852 {
6853 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6854 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6855
6856 copy_tv(sv->sv_tv, rettv);
6857 }
6858 else
6859 {
6860 rettv->v_type = VAR_FUNC;
6861 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6862 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006863 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006864 }
6865
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006866 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6867 || rettv->v_type == VAR_PARTIAL))
6868 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006869 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006870 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6871 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006872
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006873 // Stop the expression evaluation when immediately aborting on
6874 // error, or when an interrupt occurred or an exception was thrown
6875 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006876 if (aborting())
6877 {
6878 if (ret == OK)
6879 clear_tv(rettv);
6880 ret = FAIL;
6881 }
6882 dict_unref(selfdict);
6883 selfdict = NULL;
6884 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006885 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006886 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006887 if (in_vim9script())
6888 *arg = skipwhite(p + 2);
6889 else
6890 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00006891 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006892 {
zeertzjqc481ad32023-03-11 16:18:51 +00006893 emsg(_(e_no_white_space_allowed_before_parenthesis));
6894 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006895 }
zeertzjqc481ad32023-03-11 16:18:51 +00006896 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
6897 // expr->{lambda}() or expr->(lambda)()
6898 ret = eval_lambda(arg, rettv, evalarg, verbose);
6899 else
6900 // expr->name()
6901 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02006902 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006903 // "." is ".name" lookup when we found a dict or when evaluating and
6904 // scriptversion is at least 2, where string concatenation is "..".
6905 else if (**arg == '['
6906 || (**arg == '.' && (rettv->v_type == VAR_DICT
6907 || (!evaluate
6908 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006909 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006910 {
6911 dict_unref(selfdict);
6912 if (rettv->v_type == VAR_DICT)
6913 {
6914 selfdict = rettv->vval.v_dict;
6915 if (selfdict != NULL)
6916 ++selfdict->dv_refcount;
6917 }
6918 else
6919 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006920 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006921 {
6922 clear_tv(rettv);
6923 ret = FAIL;
6924 }
6925 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006926 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
6927 || rettv->v_type == VAR_OBJECT))
6928 {
6929 // class member: SomeClass.varname
6930 // class method: SomeClass.SomeMethod()
6931 // class constructor: SomeClass.new()
6932 // object member: someObject.varname
6933 // object method: someObject.SomeMethod()
6934 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
6935 {
6936 clear_tv(rettv);
6937 ret = FAIL;
6938 }
6939 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006940 else
6941 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006942 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006943
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006944 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6945 // Don't do this when "Func" is already a partial that was bound
6946 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006947 if (selfdict != NULL
6948 && (rettv->v_type == VAR_FUNC
6949 || (rettv->v_type == VAR_PARTIAL
6950 && (rettv->vval.v_partial->pt_auto
6951 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006952 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006953
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006954 dict_unref(selfdict);
6955 return ret;
6956}
6957
6958/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006959 * Make a copy of an item.
6960 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006961 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006962 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6963 * reference to an already copied list/dict can be used.
6964 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006965 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006966 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006967item_copy(
6968 typval_T *from,
6969 typval_T *to,
6970 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006971 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006972 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006973{
6974 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006975 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006976
Bram Moolenaar33570922005-01-25 22:26:29 +00006977 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006978 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006979 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006980 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006981 }
6982 ++recurse;
6983
6984 switch (from->v_type)
6985 {
6986 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006987 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006988 case VAR_STRING:
6989 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006990 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006991 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006992 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006993 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006994 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006995 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006996 case VAR_CLASS:
6997 case VAR_OBJECT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006998 copy_tv(from, to);
6999 break;
7000 case VAR_LIST:
7001 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007002 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007003 if (from->vval.v_list == NULL)
7004 to->vval.v_list = NULL;
7005 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7006 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007007 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007008 to->vval.v_list = from->vval.v_list->lv_copylist;
7009 ++to->vval.v_list->lv_refcount;
7010 }
7011 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007012 to->vval.v_list = list_copy(from->vval.v_list,
7013 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007014 if (to->vval.v_list == NULL)
7015 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007016 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007017 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007018 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007019 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007020 case VAR_DICT:
7021 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007022 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007023 if (from->vval.v_dict == NULL)
7024 to->vval.v_dict = NULL;
7025 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7026 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007027 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007028 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7029 ++to->vval.v_dict->dv_refcount;
7030 }
7031 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007032 to->vval.v_dict = dict_copy(from->vval.v_dict,
7033 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007034 if (to->vval.v_dict == NULL)
7035 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007036 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007037 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007038 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007039 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007040 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007041 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007042 }
7043 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007044 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007045}
7046
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007047 void
7048echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7049{
7050 char_u *tofree;
7051 char_u numbuf[NUMBUFLEN];
7052 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7053
7054 if (*atstart)
7055 {
7056 *atstart = FALSE;
7057 // Call msg_start() after eval1(), evaluating the expression
7058 // may cause a message to appear.
7059 if (with_space)
7060 {
7061 // Mark the saved text as finishing the line, so that what
7062 // follows is displayed on a new line when scrolling back
7063 // at the more prompt.
7064 msg_sb_eol();
7065 msg_start();
7066 }
7067 }
7068 else if (with_space)
7069 msg_puts_attr(" ", echo_attr);
7070
7071 if (p != NULL)
7072 for ( ; *p != NUL && !got_int; ++p)
7073 {
7074 if (*p == '\n' || *p == '\r' || *p == TAB)
7075 {
7076 if (*p != TAB && *needclr)
7077 {
7078 // remove any text still there from the command
7079 msg_clr_eos();
7080 *needclr = FALSE;
7081 }
7082 msg_putchar_attr(*p, echo_attr);
7083 }
7084 else
7085 {
7086 if (has_mbyte)
7087 {
7088 int i = (*mb_ptr2len)(p);
7089
7090 (void)msg_outtrans_len_attr(p, i, echo_attr);
7091 p += i - 1;
7092 }
7093 else
7094 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7095 }
7096 }
7097 vim_free(tofree);
7098}
7099
Bram Moolenaare9a41262005-01-15 22:18:47 +00007100/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 * ":echo expr1 ..." print each argument separated with a space, add a
7102 * newline at the end.
7103 * ":echon expr1 ..." print each argument plain.
7104 */
7105 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007106ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107{
7108 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007109 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007110 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 int needclr = TRUE;
7112 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007113 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007114 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007115 evalarg_T evalarg;
7116
Bram Moolenaare707c882020-07-01 13:07:37 +02007117 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118
7119 if (eap->skip)
7120 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007121 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007123 // If eval1() causes an error message the text from the command may
7124 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007125 need_clr_eos = needclr;
7126
Bram Moolenaar68db9962021-05-09 23:19:22 +02007127 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007128 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 {
7130 /*
7131 * Report the invalid expression unless the expression evaluation
7132 * has been cancelled due to an aborting error, an interrupt, or an
7133 * exception.
7134 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007135 if (!aborting() && did_emsg == did_emsg_before
7136 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007137 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007138 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 break;
7140 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007141 need_clr_eos = FALSE;
7142
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007144 {
7145 if (rettv.v_type == VAR_VOID)
7146 {
7147 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7148 break;
7149 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007150 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007151 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007152
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007153 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 arg = skipwhite(arg);
7155 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007156 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007157 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158
7159 if (eap->skip)
7160 --emsg_skip;
7161 else
7162 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007163 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 if (needclr)
7165 msg_clr_eos();
7166 if (eap->cmdidx == CMD_echo)
7167 msg_end();
7168 }
7169}
7170
7171/*
7172 * ":echohl {name}".
7173 */
7174 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007175ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007177 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178}
7179
7180/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007181 * Returns the :echo attribute
7182 */
7183 int
7184get_echo_attr(void)
7185{
7186 return echo_attr;
7187}
7188
7189/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 * ":execute expr1 ..." execute the result of an expression.
7191 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007192 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007194 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 * Each gets spaces around each argument and a newline at the end for
7196 * echo commands
7197 */
7198 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007199ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200{
7201 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007202 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 int ret = OK;
7204 char_u *p;
7205 garray_T ga;
7206 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007207 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208
7209 ga_init2(&ga, 1, 80);
7210
7211 if (eap->skip)
7212 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007213 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007215 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007216 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218
7219 if (!eap->skip)
7220 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007221 char_u buf[NUMBUFLEN];
7222
7223 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007224 {
7225 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7226 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007227 semsg(_(e_using_invalid_value_as_string_str),
7228 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007229 p = NULL;
7230 }
7231 else
7232 p = tv_get_string_buf(&rettv, buf);
7233 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007234 else
7235 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007236 if (p == NULL)
7237 {
7238 clear_tv(&rettv);
7239 ret = FAIL;
7240 break;
7241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 len = (int)STRLEN(p);
7243 if (ga_grow(&ga, len + 2) == FAIL)
7244 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007245 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 ret = FAIL;
7247 break;
7248 }
7249 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 ga.ga_len += len;
7253 }
7254
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007255 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 arg = skipwhite(arg);
7257 }
7258
7259 if (ret != FAIL && ga.ga_data != NULL)
7260 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007261 // use the first line of continuation lines for messages
7262 SOURCING_LNUM = start_lnum;
7263
Bram Moolenaar37fef162022-08-29 18:16:32 +01007264 if (eap->cmdidx == CMD_echomsg
7265 || eap->cmdidx == CMD_echowindow
7266 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007267 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007268 // Mark the already saved text as finishing the line, so that what
7269 // follows is displayed on a new line when scrolling back at the
7270 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007271 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007272 }
7273
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007274 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007275 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007276 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007277 out_flush();
7278 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007279 else if (eap->cmdidx == CMD_echowindow)
7280 {
7281#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007282 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007283#endif
7284 msg_attr(ga.ga_data, echo_attr);
7285#ifdef HAS_MESSAGE_WINDOW
7286 end_echowindow();
7287#endif
7288 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007289 else if (eap->cmdidx == CMD_echoconsole)
7290 {
7291 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7292 ui_write((char_u *)"\r\n", 2, TRUE);
7293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 else if (eap->cmdidx == CMD_echoerr)
7295 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007296 int save_did_emsg = did_emsg;
7297
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007298 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007299 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 if (!force_abort)
7301 did_emsg = save_did_emsg;
7302 }
7303 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007304 {
7305 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7306
7307 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7308 sticky_cmdmod_flags = cmdmod.cmod_flags
7309 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 do_cmdline((char_u *)ga.ga_data,
7311 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007312 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 }
7315
7316 ga_clear(&ga);
7317
7318 if (eap->skip)
7319 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007320 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321}
7322
7323/*
7324 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7325 * "arg" points to the "&" or '+' when called, to "option" when returning.
7326 * Returns NULL when no option name found. Otherwise pointer to the char
7327 * after the option name.
7328 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007329 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007330find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331{
7332 char_u *p = *arg;
7333
7334 ++p;
7335 if (*p == 'g' && p[1] == ':')
7336 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007337 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 p += 2;
7339 }
7340 else if (*p == 'l' && p[1] == ':')
7341 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007342 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 p += 2;
7344 }
7345 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007346 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347
7348 if (!ASCII_ISALPHA(*p))
7349 return NULL;
7350 *arg = p;
7351
7352 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007353 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 else
7355 while (ASCII_ISALPHA(*p))
7356 ++p;
7357 return p;
7358}
7359
7360/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007361 * Display script name where an item was last set.
7362 * Should only be invoked when 'verbose' is non-zero.
7363 */
7364 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007365last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007366{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007367 char_u *p;
7368
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007369 if (script_ctx.sc_sid == 0)
7370 return;
7371
7372 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7373 if (p == NULL)
7374 return;
7375
7376 verbose_enter();
7377 msg_puts(_("\n\tLast set from "));
7378 msg_puts((char *)p);
7379 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007380 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007381 msg_puts(_(line_msg));
7382 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007383 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007384 verbose_leave();
7385 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007386}
7387
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007388#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389
7390/*
7391 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007392 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 * "flags" can be "g" to do a global substitute.
7394 * Returns an allocated string, NULL for error.
7395 */
7396 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007397do_string_sub(
7398 char_u *str,
7399 char_u *pat,
7400 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007401 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007402 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403{
7404 int sublen;
7405 regmatch_T regmatch;
7406 int i;
7407 int do_all;
7408 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007409 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 garray_T ga;
7411 char_u *ret;
7412 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007413 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007415 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007417 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418
7419 ga_init2(&ga, 1, 200);
7420
7421 do_all = (flags[0] == 'g');
7422
7423 regmatch.rm_ic = p_ic;
7424 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7425 if (regmatch.regprog != NULL)
7426 {
7427 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007428 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7430 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007431 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007432 if (regmatch.startp[0] == regmatch.endp[0])
7433 {
7434 if (zero_width == regmatch.startp[0])
7435 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007436 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007437 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007438 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7439 (size_t)i);
7440 ga.ga_len += i;
7441 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007442 continue;
7443 }
7444 zero_width = regmatch.startp[0];
7445 }
7446
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 /*
7448 * Get some space for a temporary buffer to do the substitution
7449 * into. It will contain:
7450 * - The text up to where the match is.
7451 * - The substituted text.
7452 * - The text after the match.
7453 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007454 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007455 if (sublen <= 0)
7456 {
7457 ga_clear(&ga);
7458 break;
7459 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007460 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7462 {
7463 ga_clear(&ga);
7464 break;
7465 }
7466
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007467 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 i = (int)(regmatch.startp[0] - tail);
7469 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007470 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007471 (void)vim_regsub(&regmatch, sub, expr,
7472 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7473 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007475 tail = regmatch.endp[0];
7476 if (*tail == NUL)
7477 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 if (!do_all)
7479 break;
7480 }
7481
7482 if (ga.ga_data != NULL)
7483 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7484
Bram Moolenaar473de612013-06-08 18:19:48 +02007485 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 }
7487
7488 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7489 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007490 if (p_cpo == empty_option)
7491 p_cpo = save_cpo;
7492 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007493 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007494 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007495 // If it's still empty it was changed and restored, need to restore in
7496 // the complicated way.
7497 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007498 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007499 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501
7502 return ret;
7503}