blob: f208ab100961555e8f9426cc0a76092d9beb5b28 [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 Moolenaar071d4272004-06-13 20:20:40 +000032/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000033 * Info used by a ":for" loop.
34 */
Bram Moolenaar33570922005-01-25 22:26:29 +000035typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000036{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010037 int fi_semicolon; // TRUE if ending in '; var]'
38 int fi_varcount; // nr of variables in the list
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020039 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010040 listwatch_T fi_lw; // keep an eye on the item used.
41 list_T *fi_list; // list being used
42 int fi_bi; // index of blob
43 blob_T *fi_blob; // blob being used
Bram Moolenaar74e54fc2021-03-26 20:41:29 +010044 char_u *fi_string; // copy of string being used
45 int fi_byte_idx; // byte index in fi_string
Bram Moolenaar33570922005-01-25 22:26:29 +000046} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000047
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020048static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
49static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010052static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020053static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010054static int eval8(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
55static int eval9(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
56static int eval9_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000057
Bram Moolenaar48e697e2016-01-23 22:17:30 +010058static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020059static 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 +020060
Bram Moolenaare21c1582019-03-02 11:57:09 +010061/*
62 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010063 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010064 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020065 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010066num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010067{
68 varnumber_T result;
69
Bram Moolenaar99880f92021-01-20 21:23:14 +010070 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010071 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010072 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010073 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010074 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010075 if (failed != NULL)
76 *failed = TRUE;
77 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010078 if (n1 == 0)
79 result = VARNUM_MIN; // similar to NaN
80 else if (n1 < 0)
81 result = -VARNUM_MAX;
82 else
83 result = VARNUM_MAX;
84 }
85 else
86 result = n1 / n2;
87
88 return result;
89}
90
91/*
92 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010093 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010094 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020095 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010096num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010097{
Bram Moolenaar99880f92021-01-20 21:23:14 +010098 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010099 {
Bram Moolenaar99880f92021-01-20 21:23:14 +0100100 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +0100101 if (failed != NULL)
102 *failed = TRUE;
103 }
Bram Moolenaare21c1582019-03-02 11:57:09 +0100104 return (n2 == 0) ? 0 : (n1 % n2);
105}
106
Bram Moolenaar33570922005-01-25 22:26:29 +0000107/*
108 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000109 */
110 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100111eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000112{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200113 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200114 func_init();
Bram Moolenaara7043832005-01-21 11:56:39 +0000115}
116
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000117#if defined(EXITFREE) || defined(PROTO)
118 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100119eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000120{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200121 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100122 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200123 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000124
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200125 // autoloaded script names
126 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000127
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200128 // unreferenced lists and dicts
129 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200130
131 // functions not garbage collected
132 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000133}
134#endif
135
Bram Moolenaare6b53242020-07-01 17:28:33 +0200136 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200137fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
138{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100139 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200140 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200141 if (eap != NULL)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200142 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200143 evalarg->eval_cstack = eap->cstack;
Bram Moolenaara7583c42022-05-07 21:14:05 +0100144 if (sourcing_a_script(eap) || eap->getline == get_list_line)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200145 {
146 evalarg->eval_getline = eap->getline;
147 evalarg->eval_cookie = eap->cookie;
148 }
Bram Moolenaar37c83712020-06-30 21:18:36 +0200149 }
150}
151
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152/*
153 * Top level evaluation function, returning a boolean.
154 * Sets "error" to TRUE if there was an error.
155 * Return TRUE or FALSE.
156 */
157 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100158eval_to_bool(
159 char_u *arg,
160 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200161 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100162 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163{
Bram Moolenaar33570922005-01-25 22:26:29 +0000164 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200165 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200166 evalarg_T evalarg;
167
Bram Moolenaar37c83712020-06-30 21:18:36 +0200168 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169
170 if (skip)
171 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200172 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 else
175 {
176 *error = FALSE;
177 if (!skip)
178 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200179 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200180 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200181 else
182 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000183 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 }
185 }
186 if (skip)
187 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200188 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200190 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191}
192
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100193/*
194 * Call eval1() and give an error message if not done at a lower level.
195 */
196 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200197eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100198{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100199 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100200 int ret;
201 int did_emsg_before = did_emsg;
202 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200203 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100204
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200205 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
206
207 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100208 if (ret == FAIL)
209 {
210 // Report the invalid expression unless the expression evaluation has
211 // been cancelled due to an aborting error, an interrupt, or an
212 // exception, or we already gave a more specific error.
213 // Also check called_emsg for when using assert_fails().
214 if (!aborting() && did_emsg == did_emsg_before
215 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200216 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100217 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200218 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100219 return ret;
220}
221
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100222/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200223 * Return whether a typval is a valid expression to pass to eval_expr_typval()
224 * or eval_expr_to_bool(). An empty string returns FALSE;
225 */
226 int
227eval_expr_valid_arg(typval_T *tv)
228{
229 return tv->v_type != VAR_UNKNOWN
230 && (tv->v_type != VAR_STRING
231 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
232}
233
234/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100235 * Evaluate an expression, which can be a function, partial or string.
236 * Pass arguments "argv[argc]".
237 * Return the result in "rettv" and OK or FAIL.
238 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200239 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100240eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
241{
242 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100243 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200244 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100245
246 if (expr->v_type == VAR_FUNC)
247 {
248 s = expr->vval.v_string;
249 if (s == NULL || *s == NUL)
250 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200251 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000252 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200253 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100254 return FAIL;
255 }
256 else if (expr->v_type == VAR_PARTIAL)
257 {
258 partial_T *partial = expr->vval.v_partial;
259
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200260 if (partial == NULL)
261 return FAIL;
262
Bram Moolenaar822ba242020-05-24 23:00:18 +0200263 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200264 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100265 {
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100266 funccall_T *fc = create_funccal(partial->pt_func, rettv);
267 int r;
268
269 if (fc == NULL)
270 return FAIL;
271
Bram Moolenaarc9c967d2022-09-07 16:48:46 +0100272 // Shortcut to call a compiled function without overhead.
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100273 r = call_def_function(partial->pt_func, argc, argv,
274 DEF_USE_PT_ARGV, partial, fc, rettv);
275 remove_funccal();
276 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277 return FAIL;
278 }
279 else
280 {
281 s = partial_name(partial);
282 if (s == NULL || *s == NUL)
283 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200284 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000285 funcexe.fe_evaluate = TRUE;
286 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100287 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
288 return FAIL;
289 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100290 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200291 else if (expr->v_type == VAR_INSTR)
292 {
293 return exe_typval_instr(expr, rettv);
294 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100295 else
296 {
Bram Moolenaarfc4c4482022-01-26 21:17:04 +0000297 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
Bram Moolenaar48570482017-10-30 21:48:41 +0100298 if (s == NULL)
299 return FAIL;
300 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200301 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100302 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200303 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100304 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200305 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200306 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100307 return FAIL;
308 }
309 }
310 return OK;
311}
312
313/*
314 * Like eval_to_bool() but using a typval_T instead of a string.
315 * Works for string, funcref and partial.
316 */
317 int
318eval_expr_to_bool(typval_T *expr, int *error)
319{
320 typval_T rettv;
321 int res;
322
323 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
324 {
325 *error = TRUE;
326 return FALSE;
327 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200328 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100329 clear_tv(&rettv);
330 return res;
331}
332
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333/*
334 * Top level evaluation function, returning a string. If "skip" is TRUE,
335 * only parsing to "nextcmd" is done, without reporting errors. Return
336 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
337 */
338 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100339eval_to_string_skip(
340 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200341 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100342 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343{
Bram Moolenaar33570922005-01-25 22:26:29 +0000344 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200346 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347
Bram Moolenaar37c83712020-06-30 21:18:36 +0200348 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 if (skip)
350 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200351 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 retval = NULL;
353 else
354 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100355 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000356 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 }
358 if (skip)
359 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200360 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361
362 return retval;
363}
364
365/*
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100366 * Initialize "evalarg" for use.
367 */
368 void
369init_evalarg(evalarg_T *evalarg)
370{
371 CLEAR_POINTER(evalarg);
372 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
373}
374
375/*
376 * If "evalarg->eval_tofree" is not NULL free it later.
377 * Caller is expected to overwrite "evalarg->eval_tofree" next.
378 */
379 static void
380free_eval_tofree_later(evalarg_T *evalarg)
381{
382 if (evalarg->eval_tofree != NULL)
383 {
384 if (ga_grow(&evalarg->eval_tofree_ga, 1) == OK)
385 ((char_u **)evalarg->eval_tofree_ga.ga_data)
386 [evalarg->eval_tofree_ga.ga_len++]
387 = evalarg->eval_tofree;
388 else
389 vim_free(evalarg->eval_tofree);
390 }
391}
392
393/*
394 * After using "evalarg" filled from "eap": free the memory.
395 */
396 void
397clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
398{
399 if (evalarg != NULL)
400 {
401 if (evalarg->eval_tofree != NULL)
402 {
403 if (eap != NULL)
404 {
405 // We may need to keep the original command line, e.g. for
406 // ":let" it has the variable names. But we may also need the
407 // new one, "nextcmd" points into it. Keep both.
408 vim_free(eap->cmdline_tofree);
409 eap->cmdline_tofree = *eap->cmdlinep;
410 *eap->cmdlinep = evalarg->eval_tofree;
411 }
412 else
413 vim_free(evalarg->eval_tofree);
414 evalarg->eval_tofree = NULL;
415 }
416
417 ga_clear_strings(&evalarg->eval_tofree_ga);
418 VIM_CLEAR(evalarg->eval_tofree_lambda);
419 }
420}
421
422/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000423 * Skip over an expression at "*pp".
424 * Return FAIL for an error, OK otherwise.
425 */
426 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200427skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000428{
Bram Moolenaar33570922005-01-25 22:26:29 +0000429 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000430
431 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200432 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000433}
434
435/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200436 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200437 * If in Vim9 script and line breaks are encountered, the lines are
438 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200439 * "arg" is advanced to just after the expression.
440 * "start" is set to the start of the expression, "end" to just after the end.
441 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200442 * Return FAIL for an error, OK otherwise.
443 */
444 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200445skip_expr_concatenate(
446 char_u **arg,
447 char_u **start,
448 char_u **end,
449 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200450{
451 typval_T rettv;
452 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200453 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200454 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200455 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200456 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200457 int evaluate = evalarg == NULL
458 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200459
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200460 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200461 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200462 {
463 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200464 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200465 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200466 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200467 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200468 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200469 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200470
471 // Don't evaluate the expression.
472 if (evalarg != NULL)
473 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200474 *arg = skipwhite(*arg);
475 res = eval1(arg, &rettv, evalarg);
476 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200477 if (evalarg != NULL)
478 evalarg->eval_flags = save_flags;
479
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200480 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200481 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200482 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200483 if (evalarg->eval_ga.ga_len == 1)
484 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200485 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200486 ga_clear(gap);
487 gap->ga_itemsize = 0;
488 }
489 else
490 {
491 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200492 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200493
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200494 // Line breaks encountered, concatenate all the lines.
495 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200496 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200497
498 // free the lines only when using getsourceline()
499 if (evalarg->eval_cookie != NULL)
500 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200501 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200502 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200503 // Do not free the last line, "arg" points into it, free it
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100504 // later. Also free "eval_tofree" later if needed.
505 free_eval_tofree_later(evalarg);
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200506 evalarg->eval_tofree =
507 ((char_u **)gap->ga_data)[gap->ga_len - 1];
508 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200509 ga_clear_strings(gap);
510 }
511 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200512 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200513 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200514
515 // free lines that were explicitly marked for freeing
516 ga_clear_strings(freegap);
517 }
518
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200519 gap->ga_itemsize = 0;
520 if (p == NULL)
521 return FAIL;
522 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200523 vim_free(evalarg->eval_tofree_lambda);
524 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200525 // Compute "end" relative to the end.
526 *end = *start + STRLEN(*start) - endoff;
527 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200528 }
529
530 return res;
531}
532
533/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100534 * Convert "tv" to a string.
535 * When "convert" is TRUE convert a List into a sequence of lines and convert
536 * a Float to a String.
537 * Returns an allocated string (NULL when out of memory).
538 */
539 char_u *
540typval2string(typval_T *tv, int convert)
541{
542 garray_T ga;
543 char_u *retval;
544#ifdef FEAT_FLOAT
545 char_u numbuf[NUMBUFLEN];
546#endif
547
548 if (convert && tv->v_type == VAR_LIST)
549 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000550 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100551 if (tv->vval.v_list != NULL)
552 {
553 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
554 if (tv->vval.v_list->lv_len > 0)
555 ga_append(&ga, NL);
556 }
557 ga_append(&ga, NUL);
558 retval = (char_u *)ga.ga_data;
559 }
560#ifdef FEAT_FLOAT
561 else if (convert && tv->v_type == VAR_FLOAT)
562 {
563 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
564 retval = vim_strsave(numbuf);
565 }
566#endif
567 else
568 retval = vim_strsave(tv_get_string(tv));
569 return retval;
570}
571
572/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200573 * Top level evaluation function, returning a string. Does not handle line
574 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000575 * When "convert" is TRUE convert a List into a sequence of lines and convert
576 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 * Return pointer to allocated memory, or NULL for failure.
578 */
579 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100580eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100581 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100582 int convert,
583 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584{
Bram Moolenaar33570922005-01-25 22:26:29 +0000585 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100587 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100589 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
590 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 retval = NULL;
592 else
593 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100594 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000595 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100597 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598
599 return retval;
600}
601
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100602 char_u *
603eval_to_string(
604 char_u *arg,
605 int convert)
606{
607 return eval_to_string_eap(arg, convert, NULL);
608}
609
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000611 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100612 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200613 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 */
615 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100616eval_to_string_safe(
617 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000618 int use_sandbox,
619 int keep_script_version)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620{
621 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200622 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200623 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200624 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625
Bram Moolenaar9530b582022-01-22 13:39:08 +0000626 if (!keep_script_version)
627 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200628 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000629 if (use_sandbox)
630 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100631 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200632 may_garbage_collect = FALSE;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200633 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000634 if (use_sandbox)
635 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100636 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200637 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200638 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200639 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 return retval;
641}
642
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643/*
644 * Top level evaluation function, returning a number.
645 * Evaluates "expr" silently.
646 * Returns -1 for an error.
647 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200648 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100649eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650{
Bram Moolenaar33570922005-01-25 22:26:29 +0000651 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200652 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000653 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654
655 ++emsg_off;
656
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200657 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 retval = -1;
659 else
660 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100661 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000662 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 }
664 --emsg_off;
665
666 return retval;
667}
668
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000669/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000670 * Top level evaluation function.
671 * Returns an allocated typval_T with the result.
672 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000673 */
674 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200675eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000676{
677 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200678 evalarg_T evalarg;
679
680 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000681
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200682 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200683 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100684 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000685
Bram Moolenaar37c83712020-06-30 21:18:36 +0200686 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000687 return tv;
688}
689
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000691 * "*arg" points to what can be a function name in the form of "import.Name" or
692 * "Funcref". Return the name of the function. Set "tofree" to something that
693 * was allocated.
694 * If "verbose" is FALSE no errors are given.
695 * Return NULL for any failure.
696 */
697 static char_u *
698deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000699 char_u **arg,
700 char_u **tofree,
701 evalarg_T *evalarg,
702 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000703{
704 typval_T ref;
705 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100706 int save_flags = 0;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000707
708 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100709 if (evalarg != NULL)
710 {
711 // need to evaluate this to get an import, like in "a.Func"
712 save_flags = evalarg->eval_flags;
713 evalarg->eval_flags |= EVAL_EVALUATE;
714 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100715 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100716 {
717 dictitem_T *v;
718
719 // If <SID>VarName was used it would not be found, try another way.
720 v = find_var_also_in_script(name, NULL, FALSE);
721 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100722 {
723 name = NULL;
724 goto theend;
725 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100726 copy_tv(&v->di_tv, &ref);
727 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000728 if (*skipwhite(*arg) != NUL)
729 {
730 if (verbose)
731 semsg(_(e_trailing_characters_str), *arg);
732 name = NULL;
733 }
734 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
735 {
736 name = ref.vval.v_string;
737 ref.vval.v_string = NULL;
738 *tofree = name;
739 }
740 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
741 {
742 if (ref.vval.v_partial->pt_argc > 0
743 || ref.vval.v_partial->pt_dict != NULL)
744 {
745 if (verbose)
746 emsg(_(e_cannot_use_partial_here));
747 name = NULL;
748 }
749 else
750 {
751 name = vim_strsave(partial_name(ref.vval.v_partial));
752 *tofree = name;
753 }
754 }
755 else
756 {
757 if (verbose)
758 semsg(_(e_not_callable_type_str), name);
759 name = NULL;
760 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100761
762theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000763 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100764 if (evalarg != NULL)
765 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000766 return name;
767}
768
769/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100770 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200771 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
772 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000773 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200775 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100776call_vim_function(
777 char_u *func,
778 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200779 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200780 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000782 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200783 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000784 char_u *arg;
785 char_u *name;
786 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000787 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100789 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200790 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000791 funcexe.fe_firstline = curwin->w_cursor.lnum;
792 funcexe.fe_lastline = curwin->w_cursor.lnum;
793 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000794
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000795 // The name might be "import.Func" or "Funcref". We don't know, we need to
796 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000797 // autoload script has errors. Guess that when there is a dot in the name
798 // showing errors is the right choice.
799 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000800 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000801 if (ignore_errors)
802 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000803 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000804 if (ignore_errors)
805 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000806 if (name == NULL)
807 name = func;
808
809 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
810
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000811 if (ret == FAIL)
812 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000813 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000814
815 return ret;
816}
817
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100818/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100819 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000820 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
821 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000822 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000823 */
824 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100825call_func_retstr(
826 char_u *func,
827 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200828 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000829{
830 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000831 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000832
Bram Moolenaarded27a12018-08-01 19:06:03 +0200833 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000834 return NULL;
835
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100836 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000837 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 return retval;
839}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000840
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000841/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100842 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000843 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000844 * Returns NULL when there is something wrong.
Bram Moolenaar55e93662022-09-10 13:52:26 +0100845 * Gives an error when the returned value is not a list.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000846 */
847 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100848call_func_retlist(
849 char_u *func,
850 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200851 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000852{
853 typval_T rettv;
854
Bram Moolenaarded27a12018-08-01 19:06:03 +0200855 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000856 return NULL;
857
858 if (rettv.v_type != VAR_LIST)
859 {
Bram Moolenaar55e93662022-09-10 13:52:26 +0100860 semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
861 vartype_name(rettv.v_type));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000862 clear_tv(&rettv);
863 return NULL;
864 }
865
866 return rettv.vval.v_list;
867}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868
Bram Moolenaare70dd112022-01-21 16:31:11 +0000869#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100871 * Evaluate "arg", which is 'foldexpr'.
872 * Note: caller must set "curwin" to match "arg".
873 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
874 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 */
876 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000877eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000879 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000880 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200881 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000883 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000884 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
885 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886
Bram Moolenaare70dd112022-01-21 16:31:11 +0000887 arg = wp->w_p_fde;
888 current_sctx = wp->w_p_script_ctx[WV_FDE];
889
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000891 if (use_sandbox)
892 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100893 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200895 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 retval = 0;
897 else
898 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100899 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000900 if (tv.v_type == VAR_NUMBER)
901 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000902 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 retval = 0;
904 else
905 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100906 // If the result is a string, check if there is a non-digit before
907 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000908 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 if (!VIM_ISDIGIT(*s) && *s != '-')
910 *cp = *s++;
911 retval = atol((char *)s);
912 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000913 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 }
915 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000916 if (use_sandbox)
917 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100918 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200919 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000920 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200922 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923}
924#endif
925
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000927 * Get an lval: variable, Dict item or List item that can be assigned a value
928 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
929 * "name.key", "name.key[expr]" etc.
930 * Indexing only works if "name" is an existing List or Dictionary.
931 * "name" points to the start of the name.
932 * If "rettv" is not NULL it points to the value to be assigned.
933 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
934 * wrong; must end in space or cmd separator.
935 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100936 * flags:
937 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100938 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100939 * GLV_NO_AUTOLOAD: do not use script autoloading
940 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000941 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000942 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000943 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000944 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200945 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100946get_lval(
947 char_u *name,
948 typval_T *rettv,
949 lval_T *lp,
950 int unlet,
951 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100952 int flags, // GLV_ values
953 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000954{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000955 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000956 char_u *expr_start, *expr_end;
957 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000958 dictitem_T *v;
959 typval_T var1;
960 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000961 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000962 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000963 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100964 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100965 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100966 int writing;
Bram Moolenaar4525a572022-02-13 11:57:33 +0000967 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000968
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100969 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200970 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000971
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100972 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000973 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100974 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000975 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200976 lp->ll_name_end = find_name_end(name, NULL, NULL,
977 FNE_INCL_BR | fne_flags);
978 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000979 }
980
Bram Moolenaara749a422022-02-12 19:52:25 +0000981 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +0000982 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +0000983 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
984 {
985 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
986 return NULL;
987 }
988
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100989 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000990 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100991 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000992 if (expr_start != NULL)
993 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100994 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100995 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000996 && *p != '[' && *p != '.')
997 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000998 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000999 return NULL;
1000 }
1001
1002 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1003 if (lp->ll_exp_name == NULL)
1004 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001005 // Report an invalid expression in braces, unless the
1006 // expression evaluation has been cancelled due to an
1007 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001008 if (!aborting() && !quiet)
1009 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001010 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001011 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001012 return NULL;
1013 }
1014 }
1015 lp->ll_name = lp->ll_exp_name;
1016 }
1017 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001018 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001019 lp->ll_name = name;
1020
Bram Moolenaar4525a572022-02-13 11:57:33 +00001021 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001022 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001023 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001024 // However, "g:[key]" is indexing a dictionary.
1025 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001026 {
1027 --p;
1028 lp->ll_name_end = p;
1029 }
1030 if (*p == ':')
1031 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001032 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001033
1034 if (tp == p + 1 && !quiet)
1035 {
1036 semsg(_(e_white_space_required_after_str_str), ":", p);
1037 return NULL;
1038 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001039
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001040 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001041 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001042 semsg(_(e_using_type_not_in_script_context_str), p);
1043 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001044 }
1045
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001046 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001047 lp->ll_type = parse_type(&tp,
1048 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1049 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001050 if (lp->ll_type == NULL && !quiet)
1051 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001052 lp->ll_name_end = tp;
1053 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001054 }
1055 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001056 if (lp->ll_name == NULL)
1057 return p;
1058
Bram Moolenaar62aec932022-01-29 21:45:34 +00001059 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001060 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001061 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001062
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001063 if (import != NULL)
1064 {
1065 ufunc_T *ufunc;
1066 type_T *type;
1067
Bram Moolenaar753885b2022-08-24 16:30:36 +01001068 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001069 lp->ll_sid = import->imp_sid;
1070 lp->ll_name = skipwhite(p + 1);
1071 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1072 lp->ll_name_end = p;
1073
1074 // check the item is exported
1075 cc = *p;
1076 *p = NUL;
1077 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001078 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001079 {
1080 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001081 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001082 }
1083 *p = cc;
1084 }
1085 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001086
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001087 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001088 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001089 return p;
1090
Bram Moolenaar4525a572022-02-13 11:57:33 +00001091 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001092 {
1093 // using local variable
1094 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001095 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001096 }
1097 else
1098 {
1099 cc = *p;
1100 *p = NUL;
1101 // When we would write to the variable pass &ht and prevent autoload.
1102 writing = !(flags & GLV_READ_ONLY);
1103 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001104 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001105 if (v == NULL && !quiet)
1106 semsg(_(e_undefined_variable_str), lp->ll_name);
1107 *p = cc;
1108 if (v == NULL)
1109 return NULL;
1110 lp->ll_tv = &v->di_tv;
1111 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001112
Bram Moolenaar4525a572022-02-13 11:57:33 +00001113 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001114 {
1115 if (!quiet)
1116 semsg(_(e_variable_already_declared), lp->ll_name);
1117 return NULL;
1118 }
1119
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001120 /*
1121 * Loop until no more [idx] or .key is following.
1122 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001123 var1.v_type = VAR_UNKNOWN;
1124 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001125 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001126 {
Bram Moolenaarf21d5462022-09-10 12:36:00 +01001127 int r = OK;
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001128
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001129 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1130 {
1131 if (!quiet)
1132 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1133 return NULL;
1134 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001135 if (lp->ll_tv->v_type != VAR_LIST
1136 && lp->ll_tv->v_type != VAR_DICT
1137 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001138 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001139 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001140 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001141 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001142 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001143
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001144 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaare65081d2021-06-27 15:04:05 +02001145 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001146 r = rettv_list_alloc(lp->ll_tv);
Bram Moolenaare65081d2021-06-27 15:04:05 +02001147 else if (lp->ll_tv->v_type == VAR_BLOB
1148 && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001149 r = rettv_blob_alloc(lp->ll_tv);
1150 if (r == FAIL)
1151 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001152
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001153 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001154 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001155 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001156 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001157 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001158 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001159
Bram Moolenaar4525a572022-02-13 11:57:33 +00001160 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001161 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001162 && lp->ll_tv == &v->di_tv
1163 && ht != NULL && ht == get_script_local_ht())
1164 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001165 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001166
1167 // Vim9 script local variable: get the type
1168 if (sv != NULL)
1169 lp->ll_valtype = sv->sv_type;
1170 }
1171
Bram Moolenaar8c711452005-01-14 21:53:12 +00001172 len = -1;
1173 if (*p == '.')
1174 {
1175 key = p + 1;
1176 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1177 ;
1178 if (len == 0)
1179 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001180 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001181 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001182 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001183 }
1184 p = key + len;
1185 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001186 else
1187 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001188 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001189 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001190 if (*p == ':')
1191 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001192 else
1193 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001194 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001195 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001196 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001197 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001198 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001199 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001200 clear_tv(&var1);
1201 return NULL;
1202 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001203 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001204 }
1205
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001206 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001207 if (*p == ':')
1208 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001209 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001210 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001211 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001212 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001213 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001214 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001215 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001216 if (rettv != NULL
1217 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001218 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001219 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001220 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001221 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001222 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001223 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001224 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001225 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001226 }
1227 p = skipwhite(p + 1);
1228 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001229 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001230 else
1231 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001232 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001233 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001234 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001235 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001236 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001237 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001238 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001239 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001240 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001241 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001242 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001243 clear_tv(&var2);
1244 return NULL;
1245 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001246 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001247 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001248 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001249 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001250 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001251
Bram Moolenaar8c711452005-01-14 21:53:12 +00001252 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001253 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001254 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001255 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001256 clear_tv(&var1);
1257 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001258 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001259 }
1260
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001261 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001262 ++p;
1263 }
1264
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001265 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001266 {
1267 if (len == -1)
1268 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001269 // "[key]": get key from "var1"
1270 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001271 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001272 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001273 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001274 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001275 }
1276 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001277 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001278
1279 // a NULL dict is equivalent with an empty dict
1280 if (lp->ll_tv->vval.v_dict == NULL)
1281 {
1282 lp->ll_tv->vval.v_dict = dict_alloc();
1283 if (lp->ll_tv->vval.v_dict == NULL)
1284 {
1285 clear_tv(&var1);
1286 return NULL;
1287 }
1288 ++lp->ll_tv->vval.v_dict->dv_refcount;
1289 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001290 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001291
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001292 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001293
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001294 // When assigning to a scope dictionary check that a function and
1295 // variable name is valid (only variable name unless it is l: or
1296 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001297 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001298 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001299 int prevval;
1300 int wrong;
1301
1302 if (len != -1)
1303 {
1304 prevval = key[len];
1305 key[len] = NUL;
1306 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001307 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001308 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001309 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1310 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001311 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001312 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001313 if (len != -1)
1314 key[len] = prevval;
1315 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001316 {
1317 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001318 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001319 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001320 }
1321
Bram Moolenaar10c65862020-10-08 21:16:42 +02001322 if (lp->ll_valtype != NULL)
1323 // use the type of the member
1324 lp->ll_valtype = lp->ll_valtype->tt_member;
1325
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001326 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001327 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001328 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001329 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001330 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001331 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001332 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001333 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001334 return NULL;
1335 }
1336
Bram Moolenaar31b81602019-02-10 22:14:27 +01001337 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001338 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001339 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001340 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001341 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001342 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001343 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001344 }
1345 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001346 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001347 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001348 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001349 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001350 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001351 p = NULL;
1352 break;
1353 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001354 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001355 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001356 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1357 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001358 {
1359 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001360 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001361 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001362
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001363 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001364 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001365 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001366 else if (lp->ll_tv->v_type == VAR_BLOB)
1367 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001368 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1369
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001370 /*
1371 * Get the number and item for the only or first index of the List.
1372 */
1373 if (empty1)
1374 lp->ll_n1 = 0;
1375 else
1376 // is number or string
1377 lp->ll_n1 = (long)tv_get_number(&var1);
1378 clear_tv(&var1);
1379
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001380 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001381 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001382 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001383 return NULL;
1384 }
1385 if (lp->ll_range && !lp->ll_empty2)
1386 {
1387 lp->ll_n2 = (long)tv_get_number(&var2);
1388 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001389 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1390 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001391 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001392 }
1393 lp->ll_blob = lp->ll_tv->vval.v_blob;
1394 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001395 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001396 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001397 else
1398 {
1399 /*
1400 * Get the number and item for the only or first index of the List.
1401 */
1402 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001403 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001404 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001405 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001406 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001407 clear_tv(&var1);
1408
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001409 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001410 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001411 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1412 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001413 if (lp->ll_li == NULL)
1414 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001415 clear_tv(&var2);
1416 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001417 }
1418
Bram Moolenaar10c65862020-10-08 21:16:42 +02001419 if (lp->ll_valtype != NULL)
1420 // use the type of the member
1421 lp->ll_valtype = lp->ll_valtype->tt_member;
1422
Bram Moolenaar8c711452005-01-14 21:53:12 +00001423 /*
1424 * May need to find the item or absolute index for the second
1425 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001426 * When no index given: "lp->ll_empty2" is TRUE.
1427 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001428 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001429 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001430 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001431 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001432 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001433 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001434 if (check_range_index_two(lp->ll_list,
1435 &lp->ll_n1, lp->ll_li,
1436 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001437 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001438 }
1439
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001440 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001441 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001442 }
1443
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001444 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001445 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001446 return p;
1447}
1448
1449/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001450 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001451 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001452 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001453clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001454{
1455 vim_free(lp->ll_exp_name);
1456 vim_free(lp->ll_newkey);
1457}
1458
1459/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001460 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001461 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001462 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1463 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001464 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001465 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001466set_var_lval(
1467 lval_T *lp,
1468 char_u *endp,
1469 typval_T *rettv,
1470 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001471 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1472 char_u *op,
1473 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001474{
1475 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001476 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001477
1478 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001479 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001480 cc = *endp;
1481 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001482 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1483 return;
1484
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001485 if (lp->ll_blob != NULL)
1486 {
1487 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001488
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001489 if (op != NULL && *op != '=')
1490 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001491 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001492 return;
1493 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001494 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001495 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001496
1497 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1498 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001499 if (lp->ll_empty2)
1500 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1501
Bram Moolenaar68452172021-04-12 21:21:02 +02001502 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1503 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001504 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001505 }
1506 else
1507 {
1508 val = (int)tv_get_number_chk(rettv, &error);
1509 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001510 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001511 }
1512 }
1513 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001514 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001515 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001516
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001517 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1518 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001519 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001520 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001521 *endp = cc;
1522 return;
1523 }
1524
Bram Moolenaarff697e62019-02-12 22:28:33 +01001525 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001526 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001527 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001528 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001529 {
1530 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001531 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1532 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001533 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001534 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001535 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001536 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001537 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001538 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001539 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001540 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001541 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1542 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001543 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001544 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001545 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001546 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001547 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001548 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001549 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001550 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001551 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001552 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001553 else if (lp->ll_range)
1554 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001555 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1556 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001557 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001558 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001559 return;
1560 }
1561
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001562 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1563 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001564 }
1565 else
1566 {
1567 /*
1568 * Assign to a List or Dictionary item.
1569 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001570 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1571 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001572 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001573 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001574 return;
1575 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001576
1577 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001578 && check_typval_arg_type(lp->ll_valtype, rettv,
1579 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001580 return;
1581
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001582 if (lp->ll_newkey != NULL)
1583 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001584 if (op != NULL && *op != '=')
1585 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001586 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001587 return;
1588 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001589 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1590 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001591 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001592
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001593 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001594 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001595 if (di == NULL)
1596 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001597 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1598 {
1599 vim_free(di);
1600 return;
1601 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001602 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001603 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001604 else if (op != NULL && *op != '=')
1605 {
1606 tv_op(lp->ll_tv, rettv, op);
1607 return;
1608 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001609 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001610 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001611
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001612 /*
1613 * Assign the value to the variable or list item.
1614 */
1615 if (copy)
1616 copy_tv(rettv, lp->ll_tv);
1617 else
1618 {
1619 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001620 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001621 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001622 }
1623 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001624}
1625
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001626/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001627 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1628 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001629 * Returns OK or FAIL.
1630 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001631 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001632tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001633{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001634 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001635 char_u numbuf[NUMBUFLEN];
1636 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001637 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001638
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001639 // Can't do anything with a Funcref or Dict on the right.
1640 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001641 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001642 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1643 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001644 {
1645 switch (tv1->v_type)
1646 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001647 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001648 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001649 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001650 case VAR_DICT:
1651 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001652 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001653 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001654 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001655 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001656 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001657 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001658 break;
1659
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001660 case VAR_BLOB:
1661 if (*op != '+' || tv2->v_type != VAR_BLOB)
1662 break;
1663 // BLOB += BLOB
1664 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1665 {
1666 blob_T *b1 = tv1->vval.v_blob;
1667 blob_T *b2 = tv2->vval.v_blob;
1668 int i, len = blob_len(b2);
1669 for (i = 0; i < len; i++)
1670 ga_append(&b1->bv_ga, blob_get(b2, i));
1671 }
1672 return OK;
1673
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001674 case VAR_LIST:
1675 if (*op != '+' || tv2->v_type != VAR_LIST)
1676 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001677 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001678 if (tv2->vval.v_list != NULL)
1679 {
1680 if (tv1->vval.v_list == NULL)
1681 {
1682 tv1->vval.v_list = tv2->vval.v_list;
1683 ++tv1->vval.v_list->lv_refcount;
1684 }
1685 else
1686 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1687 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001688 return OK;
1689
1690 case VAR_NUMBER:
1691 case VAR_STRING:
1692 if (tv2->v_type == VAR_LIST)
1693 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001694 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001695 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001696 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001697 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001698#ifdef FEAT_FLOAT
1699 if (tv2->v_type == VAR_FLOAT)
1700 {
1701 float_T f = n;
1702
Bram Moolenaarff697e62019-02-12 22:28:33 +01001703 if (*op == '%')
1704 break;
1705 switch (*op)
1706 {
1707 case '+': f += tv2->vval.v_float; break;
1708 case '-': f -= tv2->vval.v_float; break;
1709 case '*': f *= tv2->vval.v_float; break;
1710 case '/': f /= tv2->vval.v_float; break;
1711 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001712 clear_tv(tv1);
1713 tv1->v_type = VAR_FLOAT;
1714 tv1->vval.v_float = f;
1715 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001716 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001717#endif
1718 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001719 switch (*op)
1720 {
1721 case '+': n += tv_get_number(tv2); break;
1722 case '-': n -= tv_get_number(tv2); break;
1723 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001724 case '/': n = num_divide(n, tv_get_number(tv2),
1725 &failed); break;
1726 case '%': n = num_modulus(n, tv_get_number(tv2),
1727 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001728 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001729 clear_tv(tv1);
1730 tv1->v_type = VAR_NUMBER;
1731 tv1->vval.v_number = n;
1732 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001733 }
1734 else
1735 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001736 if (tv2->v_type == VAR_FLOAT)
1737 break;
1738
Bram Moolenaarff697e62019-02-12 22:28:33 +01001739 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001740 s = tv_get_string(tv1);
1741 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001742 clear_tv(tv1);
1743 tv1->v_type = VAR_STRING;
1744 tv1->vval.v_string = s;
1745 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001746 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001747
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001748 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001749#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001750 {
1751 float_T f;
1752
Bram Moolenaarff697e62019-02-12 22:28:33 +01001753 if (*op == '%' || *op == '.'
1754 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001755 && tv2->v_type != VAR_NUMBER
1756 && tv2->v_type != VAR_STRING))
1757 break;
1758 if (tv2->v_type == VAR_FLOAT)
1759 f = tv2->vval.v_float;
1760 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001761 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001762 switch (*op)
1763 {
1764 case '+': tv1->vval.v_float += f; break;
1765 case '-': tv1->vval.v_float -= f; break;
1766 case '*': tv1->vval.v_float *= f; break;
1767 case '/': tv1->vval.v_float /= f; break;
1768 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001769 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001770#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001771 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001772 }
1773 }
1774
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001775 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001776 return FAIL;
1777}
1778
1779/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001780 * Evaluate the expression used in a ":for var in expr" command.
1781 * "arg" points to "var".
1782 * Set "*errp" to TRUE for an error, FALSE otherwise;
1783 * Return a pointer that holds the info. Null when there is an error.
1784 */
1785 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001786eval_for_line(
1787 char_u *arg,
1788 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001789 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001790 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001791{
Bram Moolenaar33570922005-01-25 22:26:29 +00001792 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001793 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001794 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001795 typval_T tv;
1796 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001797 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001798
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001799 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001800
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001801 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001802 if (fi == NULL)
1803 return NULL;
1804
Bram Moolenaar404557e2021-07-05 21:41:48 +02001805 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1806 &fi->fi_semicolon, FALSE);
1807 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001808 return fi;
1809
Bram Moolenaar404557e2021-07-05 21:41:48 +02001810 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001811 if (expr[0] != 'i' || expr[1] != 'n'
1812 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001813 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001814 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1815 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1816 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001817 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001818 return fi;
1819 }
1820
1821 if (skip)
1822 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001823 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1824 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001825 {
1826 *errp = FALSE;
1827 if (!skip)
1828 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001829 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001830 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001831 l = tv.vval.v_list;
1832 if (l == NULL)
1833 {
1834 // a null list is like an empty list: do nothing
1835 clear_tv(&tv);
1836 }
1837 else
1838 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001840 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001841
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001842 // No need to increment the refcount, it's already set for
1843 // the list being used in "tv".
1844 fi->fi_list = l;
1845 list_add_watch(l, &fi->fi_lw);
1846 fi->fi_lw.lw_item = l->lv_first;
1847 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001848 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001849 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001850 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001851 fi->fi_bi = 0;
1852 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001853 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001854 typval_T btv;
1855
1856 // Make a copy, so that the iteration still works when the
1857 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001858 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001859 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001860 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001861 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001862 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001863 else if (tv.v_type == VAR_STRING)
1864 {
1865 fi->fi_byte_idx = 0;
1866 fi->fi_string = tv.vval.v_string;
1867 tv.vval.v_string = NULL;
1868 if (fi->fi_string == NULL)
1869 fi->fi_string = vim_strsave((char_u *)"");
1870 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001871 else
1872 {
Sean Dewar80d73952021-08-04 19:25:54 +02001873 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001874 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001875 }
1876 }
1877 }
1878 if (skip)
1879 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001880 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001881
1882 return fi;
1883}
1884
1885/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001886 * Used when looping over a :for line, skip the "in expr" part.
1887 */
1888 void
1889skip_for_lines(void *fi_void, evalarg_T *evalarg)
1890{
1891 forinfo_T *fi = (forinfo_T *)fi_void;
1892 int i;
1893
1894 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01001895 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001896}
1897
1898/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001899 * Use the first item in a ":for" list. Advance to the next.
1900 * Assign the values to the variable (list). "arg" points to the first one.
1901 * Return TRUE when a valid item was found, FALSE when at end of list or
1902 * something wrong.
1903 */
1904 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001905next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001907 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001908 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001909 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001910 ? (ASSIGN_FINAL
1911 // first round: error if variable exists
1912 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1913 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001914 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001915 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001916 int skip_assign = in_vim9script() && arg[0] == '_'
1917 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001918
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001919 if (fi->fi_blob != NULL)
1920 {
1921 typval_T tv;
1922
1923 if (fi->fi_bi >= blob_len(fi->fi_blob))
1924 return FALSE;
1925 tv.v_type = VAR_NUMBER;
1926 tv.v_lock = VAR_FIXED;
1927 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1928 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001929 if (skip_assign)
1930 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001931 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001932 fi->fi_varcount, flag, NULL) == OK;
1933 }
1934
1935 if (fi->fi_string != NULL)
1936 {
1937 typval_T tv;
1938 int len;
1939
1940 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1941 if (len == 0)
1942 return FALSE;
1943 tv.v_type = VAR_STRING;
1944 tv.v_lock = VAR_FIXED;
1945 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1946 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001947 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001948 if (skip_assign)
1949 result = TRUE;
1950 else
1951 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001952 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001953 vim_free(tv.vval.v_string);
1954 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001955 }
1956
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957 item = fi->fi_lw.lw_item;
1958 if (item == NULL)
1959 result = FALSE;
1960 else
1961 {
1962 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001963 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001964 if (skip_assign)
1965 result = TRUE;
1966 else
1967 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001968 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001969 }
1970 return result;
1971}
1972
1973/*
1974 * Free the structure used to store info used by ":for".
1975 */
1976 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001977free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001978{
Bram Moolenaar33570922005-01-25 22:26:29 +00001979 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001980
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001981 if (fi == NULL)
1982 return;
1983 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001984 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001985 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001986 list_unref(fi->fi_list);
1987 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001988 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001989 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001990 else
1991 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001992 vim_free(fi);
1993}
1994
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001996set_context_for_expression(
1997 expand_T *xp,
1998 char_u *arg,
1999 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002001 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002003 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002005 if (cmdidx == CMD_let || cmdidx == CMD_var
2006 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002007 {
2008 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002009 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002010 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002011 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002012 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002013 {
2014 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002015 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002016 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002017 break;
2018 }
2019 return;
2020 }
2021 }
2022 else
2023 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2024 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 while ((xp->xp_pattern = vim_strpbrk(arg,
2026 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2027 {
2028 c = *xp->xp_pattern;
2029 if (c == '&')
2030 {
2031 c = xp->xp_pattern[1];
2032 if (c == '&')
2033 {
2034 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002035 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 }
2037 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002038 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002040 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2041 xp->xp_pattern += 2;
2042
2043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 }
2045 else if (c == '$')
2046 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002047 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 xp->xp_context = EXPAND_ENV_VARS;
2049 }
2050 else if (c == '=')
2051 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002052 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 xp->xp_context = EXPAND_EXPRESSION;
2054 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002055 else if (c == '#'
2056 && xp->xp_context == EXPAND_EXPRESSION)
2057 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002058 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002059 break;
2060 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002061 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 && xp->xp_context == EXPAND_FUNCTIONS
2063 && vim_strchr(xp->xp_pattern, '(') == NULL)
2064 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002065 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 break;
2067 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002068 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002070 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 {
2072 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2073 if (c == '\\' && xp->xp_pattern[1] != NUL)
2074 ++xp->xp_pattern;
2075 xp->xp_context = EXPAND_NOTHING;
2076 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002077 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002079 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2081 /* skip */ ;
2082 xp->xp_context = EXPAND_NOTHING;
2083 }
2084 else if (c == '|')
2085 {
2086 if (xp->xp_pattern[1] == '|')
2087 {
2088 ++xp->xp_pattern;
2089 xp->xp_context = EXPAND_EXPRESSION;
2090 }
2091 else
2092 xp->xp_context = EXPAND_COMMANDS;
2093 }
2094 else
2095 xp->xp_context = EXPAND_EXPRESSION;
2096 }
2097 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002098 // Doesn't look like something valid, expand as an expression
2099 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002100 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 arg = xp->xp_pattern;
2102 if (*arg != NUL)
2103 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2104 /* skip */ ;
2105 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002106
2107 // ":exe one two" completes "two"
2108 if ((cmdidx == CMD_execute
2109 || cmdidx == CMD_echo
2110 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002111 || cmdidx == CMD_echomsg
2112 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002113 && xp->xp_context == EXPAND_EXPRESSION)
2114 {
2115 for (;;)
2116 {
2117 char_u *n = skiptowhite(arg);
2118
2119 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2120 break;
2121 arg = skipwhite(n);
2122 }
2123 }
2124
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 xp->xp_pattern = arg;
2126}
2127
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002129 * Return TRUE if "pat" matches "text".
2130 * Does not use 'cpo' and always uses 'magic'.
2131 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002132 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002133pattern_match(char_u *pat, char_u *text, int ic)
2134{
2135 int matches = FALSE;
2136 char_u *save_cpo;
2137 regmatch_T regmatch;
2138
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002139 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002140 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002141 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002142 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2143 if (regmatch.regprog != NULL)
2144 {
2145 regmatch.rm_ic = ic;
2146 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2147 vim_regfree(regmatch.regprog);
2148 }
2149 p_cpo = save_cpo;
2150 return matches;
2151}
2152
2153/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002154 * Handle a name followed by "(". Both for just "name(arg)" and for
2155 * "expr->name(arg)".
2156 * Returns OK or FAIL.
2157 */
2158 static int
2159eval_func(
2160 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002161 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002162 char_u *name,
2163 int name_len,
2164 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002165 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002166 typval_T *basetv) // "expr" for "expr->name(arg)"
2167{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002168 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002169 char_u *s = name;
2170 int len = name_len;
2171 partial_T *partial;
2172 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002173 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002174 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002175
2176 if (!evaluate)
2177 check_vars(s, len);
2178
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002179 // If "s" is the name of a variable of type VAR_FUNC
2180 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002181 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002182 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002183
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002184 // Need to make a copy, in case evaluating the arguments makes
2185 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002186 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002187 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002188 ret = FAIL;
2189 else
2190 {
2191 funcexe_T funcexe;
2192
2193 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002194 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002195 funcexe.fe_firstline = curwin->w_cursor.lnum;
2196 funcexe.fe_lastline = curwin->w_cursor.lnum;
2197 funcexe.fe_evaluate = evaluate;
2198 funcexe.fe_partial = partial;
2199 funcexe.fe_basetv = basetv;
2200 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002201 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002202 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002203 }
2204 vim_free(s);
2205
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002206 // If evaluate is FALSE rettv->v_type was not set in
2207 // get_func_tv, but it's needed in handle_subscript() to parse
2208 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002209 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2210 {
2211 rettv->vval.v_string = NULL;
2212 rettv->v_type = VAR_FUNC;
2213 }
2214
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002215 // Stop the expression evaluation when immediately
2216 // aborting on error, or when an interrupt occurred or
2217 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002218 if (evaluate && aborting())
2219 {
2220 if (ret == OK)
2221 clear_tv(rettv);
2222 ret = FAIL;
2223 }
2224 return ret;
2225}
2226
2227/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002228 * After a NL, skip over empty lines and comment-only lines.
2229 */
2230 static char_u *
2231newline_skip_comments(char_u *arg)
2232{
2233 char_u *p = arg + 1;
2234
2235 for (;;)
2236 {
2237 p = skipwhite(p);
2238
2239 if (*p == NUL)
2240 break;
2241 if (vim9_comment_start(p))
2242 {
2243 char_u *nl = vim_strchr(p, NL);
2244
2245 if (nl == NULL)
2246 break;
2247 p = nl;
2248 }
2249 if (*p != NL)
2250 break;
2251 ++p; // skip another NL
2252 }
2253 return p;
2254}
2255
2256/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002257 * Get the next line source line without advancing. But do skip over comment
2258 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002259 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002260 */
2261 static char_u *
2262getline_peek_skip_comments(evalarg_T *evalarg)
2263{
2264 for (;;)
2265 {
2266 char_u *next = getline_peek(evalarg->eval_getline,
2267 evalarg->eval_cookie);
2268 char_u *p;
2269
2270 if (next == NULL)
2271 break;
2272 p = skipwhite(next);
2273 if (*p != NUL && !vim9_comment_start(p))
2274 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002275 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002276 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002277 }
2278 return NULL;
2279}
2280
2281/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002282 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2283 * comment) and there is a next line, return the next line (skipping blanks)
2284 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002285 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2286 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002287 * "arg" must point somewhere inside a line, not at the start.
2288 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002289 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002290eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2291{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002292 char_u *p = skipwhite(arg);
2293
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002294 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002295 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002296 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002297 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2298 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002299 && (*p == NUL || *p == NL
2300 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002301 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002302 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002303
Bram Moolenaare442d592022-05-05 12:20:28 +01002304 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002305 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002306 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002307 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002308 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002309 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002310
Bram Moolenaar9d489562020-07-30 20:08:50 +02002311 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002312 {
2313 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002314 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002315 }
2316 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002317 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002318}
2319
2320/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002321 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002322 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002323 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002324 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002325eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002326{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002327 garray_T *gap = &evalarg->eval_ga;
2328 char_u *line;
2329
Bram Moolenaar39be4982022-05-06 21:51:50 +01002330 if (arg != NULL)
2331 {
2332 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002333 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002334 // Truncate before a trailing comment, so that concatenating the lines
2335 // won't turn the rest into a comment.
2336 if (*skipwhite(arg) == '#')
2337 *arg = NUL;
2338 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002339
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002340 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002341 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2342 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002343 else
2344 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002345 if (line == NULL)
2346 return NULL;
2347
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002348 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002349 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2350 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002351 char_u *p = skipwhite(line);
2352
2353 // Going to concatenate the lines after parsing. For an empty or
2354 // comment line use an empty string.
2355 if (*p == NUL || vim9_comment_start(p))
2356 {
2357 vim_free(line);
2358 line = vim_strsave((char_u *)"");
2359 }
2360
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002361 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2362 ++gap->ga_len;
2363 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002364 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002365 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002366 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002367 evalarg->eval_tofree = line;
2368 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002369
2370 // Advanced to the next line, "arg" no longer points into the previous
2371 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002372 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002373 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002374}
2375
Bram Moolenaare6b53242020-07-01 17:28:33 +02002376/*
2377 * Call eval_next_non_blank() and get the next line if needed.
2378 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002379 char_u *
2380skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2381{
2382 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002383 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002384
Bram Moolenaar962d7212020-07-04 14:15:00 +02002385 if (evalarg == NULL)
2386 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002387 eval_next_non_blank(p, evalarg, &getnext);
2388 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002389 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002390 return p;
2391}
2392
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002393/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002395 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2397 */
2398
2399/*
2400 * Handle zero level expression.
2401 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002402 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002403 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002404 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 * Return OK or FAIL.
2406 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002407 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002408eval0(
2409 char_u *arg,
2410 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002411 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002412 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002414 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2415}
2416
2417/*
2418 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2419 * expression and don't check what comes after the expression.
2420 */
2421 int
2422eval0_retarg(
2423 char_u *arg,
2424 typval_T *rettv,
2425 exarg_T *eap,
2426 evalarg_T *evalarg,
2427 char_u **retarg)
2428{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 int ret;
2430 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002431 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002432 int did_emsg_before = did_emsg;
2433 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002434 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002435 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002436 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437
2438 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002439 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002440
Bram Moolenaar79481362022-06-27 20:15:10 +01002441 if (ret != FAIL)
2442 {
2443 expr_end = p;
2444 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002445
Bram Moolenaar79481362022-06-27 20:15:10 +01002446 // In Vim9 script a command block is not split at NL characters for
2447 // commands using an expression argument. Skip over a '#' comment to
2448 // check for a following NL. Require white space before the '#'.
2449 if (in_vim9script() && p > expr_end && retarg == NULL)
2450 while (*p == '#')
2451 {
2452 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002453
Bram Moolenaar79481362022-06-27 20:15:10 +01002454 if (nl == NULL)
2455 break;
2456 p = skipwhite(nl + 1);
2457 if (eap != NULL && *p != NUL)
2458 eap->nextcmd = p;
2459 check_for_end = FALSE;
2460 }
2461
2462 if (check_for_end)
2463 end_error = !ends_excmd2(arg, p);
2464 }
2465
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002466 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 {
2468 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002469 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 /*
2471 * Report the invalid expression unless the expression evaluation has
2472 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002473 * exception, or we already gave a more specific error.
2474 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002476 if (!aborting()
2477 && did_emsg == did_emsg_before
2478 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002479 && (flags & EVAL_CONSTANT) == 0
2480 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002481 {
2482 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002483 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002484 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002485 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002486 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002487
2488 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002489 // a next command to avoid more errors, unless "|" is following, which
2490 // could only be a command separator.
Bram Moolenaar79481362022-06-27 20:15:10 +01002491 if (eap != NULL && p != NULL
2492 && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
Bram Moolenaar8143a532020-12-13 20:26:29 +01002493 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002494 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002496
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002497 if (retarg != NULL)
2498 *retarg = p;
2499 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002500 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002501
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 return ret;
2503}
2504
2505/*
2506 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002507 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002508 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 *
2510 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002511 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002513 * Note: "rettv.v_lock" is not set.
2514 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 * Return OK or FAIL.
2516 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002517 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002518eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002520 char_u *p;
2521 int getnext;
2522
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002523 CLEAR_POINTER(rettv);
2524
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 /*
2526 * Get the first variable.
2527 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002528 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 return FAIL;
2530
Bram Moolenaar793648f2020-06-26 21:28:25 +02002531 p = eval_next_non_blank(*arg, evalarg, &getnext);
2532 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002534 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002535 int result;
2536 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002537 evalarg_T *evalarg_used = evalarg;
2538 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002539 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002540 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002541 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002542
2543 if (evalarg == NULL)
2544 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002545 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002546 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002547 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002548 orig_flags = evalarg_used->eval_flags;
2549 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002550
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002551 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002552 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002553 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002554 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002555 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002556 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002557 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002558 clear_tv(rettv);
2559 return FAIL;
2560 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002561 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002562 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002563
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002565 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002567 int error = FALSE;
2568
Bram Moolenaar13106602020-10-04 16:06:05 +02002569 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002570 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002571 else if (vim9script)
2572 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002573 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002575 if (error || !op_falsy || !result)
2576 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002577 if (error)
2578 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 }
2580
2581 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002582 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002584 if (op_falsy)
2585 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002586 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002587 {
mityu4ac198c2021-05-28 17:52:40 +02002588 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002589 clear_tv(rettv);
2590 return FAIL;
2591 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002592 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002593 evalarg_used->eval_flags = (op_falsy ? !result : result)
2594 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2595 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002596 {
2597 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002599 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002600 if (!op_falsy || !result)
2601 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002603 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002605 /*
2606 * Check for the ":".
2607 */
2608 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2609 if (*p != ':')
2610 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002611 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002612 if (evaluate && result)
2613 clear_tv(rettv);
2614 evalarg_used->eval_flags = orig_flags;
2615 return FAIL;
2616 }
2617 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002618 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002619 else
2620 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002621 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002622 {
2623 error_white_both(p, 1);
2624 clear_tv(rettv);
2625 evalarg_used->eval_flags = orig_flags;
2626 return FAIL;
2627 }
2628 *arg = p;
2629 }
2630
2631 /*
2632 * Get the third variable. Recursive!
2633 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002634 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002635 {
mityu4ac198c2021-05-28 17:52:40 +02002636 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002637 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002638 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002639 return FAIL;
2640 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002641 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2642 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002643 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002644 if (eval1(arg, &var2, evalarg_used) == FAIL)
2645 {
2646 if (evaluate && result)
2647 clear_tv(rettv);
2648 evalarg_used->eval_flags = orig_flags;
2649 return FAIL;
2650 }
2651 if (evaluate && !result)
2652 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002654
2655 if (evalarg == NULL)
2656 clear_evalarg(&local_evalarg, NULL);
2657 else
2658 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 }
2660
2661 return OK;
2662}
2663
2664/*
2665 * Handle first level expression:
2666 * expr2 || expr2 || expr2 logical OR
2667 *
2668 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002669 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 *
2671 * Return OK or FAIL.
2672 */
2673 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002674eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002676 char_u *p;
2677 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678
2679 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002680 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002682 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 return FAIL;
2684
2685 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002686 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002688 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002689 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002691 evalarg_T *evalarg_used = evalarg;
2692 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002693 int evaluate;
2694 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002695 long result = FALSE;
2696 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002697 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002698 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002699
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002700 if (evalarg == NULL)
2701 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002702 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002703 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002704 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002705 orig_flags = evalarg_used->eval_flags;
2706 evaluate = orig_flags & EVAL_EVALUATE;
2707 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002708 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002709 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002710 result = tv_get_bool_chk(rettv, &error);
2711 else if (tv_get_number_chk(rettv, &error) != 0)
2712 result = TRUE;
2713 clear_tv(rettv);
2714 if (error)
2715 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 }
2717
2718 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002719 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002721 while (p[0] == '|' && p[1] == '|')
2722 {
2723 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002724 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002725 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002726 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002727 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002728 {
2729 error_white_both(p, 2);
2730 clear_tv(rettv);
2731 return FAIL;
2732 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002733 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002734 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002735
2736 /*
2737 * Get the second variable.
2738 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002739 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002740 {
mityu4ac198c2021-05-28 17:52:40 +02002741 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002742 clear_tv(rettv);
2743 return FAIL;
2744 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002745 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2746 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002747 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002748 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002749 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002750
2751 /*
2752 * Compute the result.
2753 */
2754 if (evaluate && !result)
2755 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002756 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002757 result = tv_get_bool_chk(&var2, &error);
2758 else if (tv_get_number_chk(&var2, &error) != 0)
2759 result = TRUE;
2760 clear_tv(&var2);
2761 if (error)
2762 return FAIL;
2763 }
2764 if (evaluate)
2765 {
2766 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002767 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002768 rettv->v_type = VAR_BOOL;
2769 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002770 }
2771 else
2772 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002773 rettv->v_type = VAR_NUMBER;
2774 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002775 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002776 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002777
2778 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002780
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002781 if (evalarg == NULL)
2782 clear_evalarg(&local_evalarg, NULL);
2783 else
2784 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 }
2786
2787 return OK;
2788}
2789
2790/*
2791 * Handle second level expression:
2792 * expr3 && expr3 && expr3 logical AND
2793 *
2794 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002795 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 *
2797 * Return OK or FAIL.
2798 */
2799 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002800eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002802 char_u *p;
2803 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804
2805 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002806 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002808 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 return FAIL;
2810
2811 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002812 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002814 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002815 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002817 evalarg_T *evalarg_used = evalarg;
2818 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002819 int orig_flags;
2820 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002821 long result = TRUE;
2822 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002823 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002824 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002825
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002826 if (evalarg == NULL)
2827 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002828 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002829 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002830 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002831 orig_flags = evalarg_used->eval_flags;
2832 evaluate = orig_flags & EVAL_EVALUATE;
2833 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002834 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002835 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002836 result = tv_get_bool_chk(rettv, &error);
2837 else if (tv_get_number_chk(rettv, &error) == 0)
2838 result = FALSE;
2839 clear_tv(rettv);
2840 if (error)
2841 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 }
2843
2844 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002845 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002847 while (p[0] == '&' && p[1] == '&')
2848 {
2849 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002850 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002851 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002852 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002853 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002854 {
2855 error_white_both(p, 2);
2856 clear_tv(rettv);
2857 return FAIL;
2858 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002859 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002860 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002861
2862 /*
2863 * Get the second variable.
2864 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002865 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002866 {
mityu4ac198c2021-05-28 17:52:40 +02002867 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002868 clear_tv(rettv);
2869 return FAIL;
2870 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002871 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2872 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002873 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002874 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002875 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002876 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002877
2878 /*
2879 * Compute the result.
2880 */
2881 if (evaluate && result)
2882 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002883 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002884 result = tv_get_bool_chk(&var2, &error);
2885 else if (tv_get_number_chk(&var2, &error) == 0)
2886 result = FALSE;
2887 clear_tv(&var2);
2888 if (error)
2889 return FAIL;
2890 }
2891 if (evaluate)
2892 {
2893 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002894 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002895 rettv->v_type = VAR_BOOL;
2896 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002897 }
2898 else
2899 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002900 rettv->v_type = VAR_NUMBER;
2901 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002902 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002903 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002904
2905 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002907
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002908 if (evalarg == NULL)
2909 clear_evalarg(&local_evalarg, NULL);
2910 else
2911 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 }
2913
2914 return OK;
2915}
2916
2917/*
2918 * Handle third level expression:
2919 * var1 == var2
2920 * var1 =~ var2
2921 * var1 != var2
2922 * var1 !~ var2
2923 * var1 > var2
2924 * var1 >= var2
2925 * var1 < var2
2926 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002927 * var1 is var2
2928 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 *
2930 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002931 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 *
2933 * Return OK or FAIL.
2934 */
2935 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002936eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002939 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002940 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002942 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943
2944 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002945 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002947 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 return FAIL;
2949
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002950 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002951
Bram Moolenaar696ba232020-07-29 21:20:41 +02002952 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953
2954 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002955 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002957 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002959 typval_T var2;
2960 int ic;
2961 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002962 int evaluate = evalarg == NULL
2963 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002964 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002965
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002966 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002967 {
Bram Moolenaare442d592022-05-05 12:20:28 +01002968 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002969 p = *arg;
2970 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002971 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2972 {
mityu4ac198c2021-05-28 17:52:40 +02002973 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002974 clear_tv(rettv);
2975 return FAIL;
2976 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002977
Bram Moolenaar696ba232020-07-29 21:20:41 +02002978 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2979 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002980 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002981 clear_tv(rettv);
2982 return FAIL;
2983 }
2984
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002985 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 if (p[len] == '?')
2987 {
2988 ic = TRUE;
2989 ++len;
2990 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002991 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 else if (p[len] == '#')
2993 {
2994 ic = FALSE;
2995 ++len;
2996 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002997 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002999 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000
3001 /*
3002 * Get the second variable.
3003 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003004 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3005 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003006 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003007 clear_tv(rettv);
3008 return FAIL;
3009 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003010 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003011 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003013 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 return FAIL;
3015 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003016 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003017 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003018 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003019
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003020 // use the line of the comparison for messages
3021 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003022 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003023 {
3024 ret = FAIL;
3025 clear_tv(rettv);
3026 }
3027 else
3028 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003029 clear_tv(&var2);
3030 return ret;
3031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 }
3033
3034 return OK;
3035}
3036
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003037/*
3038 * Make a copy of blob "tv1" and append blob "tv2".
3039 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040 void
3041eval_addblob(typval_T *tv1, typval_T *tv2)
3042{
3043 blob_T *b1 = tv1->vval.v_blob;
3044 blob_T *b2 = tv2->vval.v_blob;
3045 blob_T *b = blob_alloc();
3046 int i;
3047
3048 if (b != NULL)
3049 {
3050 for (i = 0; i < blob_len(b1); i++)
3051 ga_append(&b->bv_ga, blob_get(b1, i));
3052 for (i = 0; i < blob_len(b2); i++)
3053 ga_append(&b->bv_ga, blob_get(b2, i));
3054
3055 clear_tv(tv1);
3056 rettv_blob_set(tv1, b);
3057 }
3058}
3059
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003060/*
3061 * Make a copy of list "tv1" and append list "tv2".
3062 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003063 int
3064eval_addlist(typval_T *tv1, typval_T *tv2)
3065{
3066 typval_T var3;
3067
3068 // concatenate Lists
3069 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3070 {
3071 clear_tv(tv1);
3072 clear_tv(tv2);
3073 return FAIL;
3074 }
3075 clear_tv(tv1);
3076 *tv1 = var3;
3077 return OK;
3078}
3079
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003081 * Handle the bitwise left/right shift operator expression:
3082 * var1 << var2
3083 * var1 >> var2
3084 *
3085 * "arg" must point to the first non-white of the expression.
3086 * "arg" is advanced to just after the recognized expression.
3087 *
3088 * Return OK or FAIL.
3089 */
3090 static int
3091eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3092{
3093 /*
3094 * Get the first expression.
3095 */
3096 if (eval6(arg, rettv, evalarg) == FAIL)
3097 return FAIL;
3098
3099 /*
3100 * Repeat computing, until no '<<' or '>>' is following.
3101 */
3102 for (;;)
3103 {
3104 char_u *p;
3105 int getnext;
3106 exprtype_T type;
3107 int evaluate;
3108 typval_T var2;
3109 int vim9script;
3110
3111 p = eval_next_non_blank(*arg, evalarg, &getnext);
3112 if (p[0] == '<' && p[1] == '<')
3113 type = EXPR_LSHIFT;
3114 else if (p[0] == '>' && p[1] == '>')
3115 type = EXPR_RSHIFT;
3116 else
3117 return OK;
3118
3119 // Handle a bitwise left or right shift operator
3120 if (rettv->v_type != VAR_NUMBER)
3121 {
3122 // left operand should be a number
3123 emsg(_(e_bitshift_ops_must_be_number));
3124 clear_tv(rettv);
3125 return FAIL;
3126 }
3127
3128 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3129 vim9script = in_vim9script();
3130 if (getnext)
3131 {
3132 *arg = eval_next_line(*arg, evalarg);
3133 p = *arg;
3134 }
3135 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3136 {
3137 error_white_both(*arg, 2);
3138 clear_tv(rettv);
3139 return FAIL;
3140 }
3141
3142 /*
3143 * Get the second variable.
3144 */
3145 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3146 {
3147 error_white_both(p, 2);
3148 clear_tv(rettv);
3149 return FAIL;
3150 }
3151 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3152 if (eval6(arg, &var2, evalarg) == FAIL)
3153 {
3154 clear_tv(rettv);
3155 return FAIL;
3156 }
3157
3158 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3159 {
3160 // right operand should be a positive number
3161 if (var2.v_type != VAR_NUMBER)
3162 emsg(_(e_bitshift_ops_must_be_number));
3163 else
3164 emsg(_(e_bitshift_ops_must_be_postive));
3165 clear_tv(rettv);
3166 clear_tv(&var2);
3167 return FAIL;
3168 }
3169
3170 if (evaluate)
3171 {
3172 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3173 // shifting more bits than we have always results in zero
3174 rettv->vval.v_number = 0;
3175 else if (type == EXPR_LSHIFT)
3176 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003177 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003178 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003179 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003180 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003181 }
3182
3183 clear_tv(&var2);
3184 }
3185
3186 return OK;
3187}
3188
3189/*
3190 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003191 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003193 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003194 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 *
3196 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003197 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 *
3199 * Return OK or FAIL.
3200 */
3201 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003202eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003205 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003207 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 return FAIL;
3209
3210 /*
3211 * Repeat computing, until no '+', '-' or '.' is following.
3212 */
3213 for (;;)
3214 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003215 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003216 int getnext;
3217 char_u *p;
3218 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003219 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003220 int concat;
3221 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003222 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003223
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003224 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003225 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003226 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003227 p = eval_next_non_blank(*arg, evalarg, &getnext);
3228 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003229 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003230 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3231 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003233 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3234 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003235
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003236 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003237 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003238 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003239 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003240 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003241 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003242 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003243 {
mityu4ac198c2021-05-28 17:52:40 +02003244 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003245 clear_tv(rettv);
3246 return FAIL;
3247 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003248 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003249 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003250 if ((op != '+' || (rettv->v_type != VAR_LIST
3251 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003252#ifdef FEAT_FLOAT
3253 && (op == '.' || rettv->v_type != VAR_FLOAT)
3254#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003255 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003256 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003257 int error = FALSE;
3258
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003259 // For "list + ...", an illegal use of the first operand as
3260 // a number cannot be determined before evaluating the 2nd
3261 // operand: if this is also a list, all is ok.
3262 // For "something . ...", "something - ..." or "non-list + ...",
3263 // we know that the first operand needs to be a string or number
3264 // without evaluating the 2nd operand. So check before to avoid
3265 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003266 if (op != '.')
3267 tv_get_number_chk(rettv, &error);
3268 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003269 {
3270 clear_tv(rettv);
3271 return FAIL;
3272 }
3273 }
3274
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 /*
3276 * Get the second variable.
3277 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003278 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003279 {
mityu89dcb4d2021-05-28 13:50:17 +02003280 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003281 clear_tv(rettv);
3282 return FAIL;
3283 }
3284 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003285 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003287 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 return FAIL;
3289 }
3290
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003291 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 {
3293 /*
3294 * Compute the result.
3295 */
3296 if (op == '.')
3297 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003298 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3299 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003300 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003301
Bram Moolenaar2e086612020-08-25 22:37:48 +02003302 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003303 || var2.v_type == VAR_CHANNEL
3304 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003305 semsg(_(e_using_invalid_value_as_string_str),
3306 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003307#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003308 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003309 {
3310 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3311 var2.vval.v_float);
3312 s2 = buf2;
3313 }
3314#endif
3315 else
3316 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003317 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003318 {
3319 clear_tv(rettv);
3320 clear_tv(&var2);
3321 return FAIL;
3322 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003323 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003324 clear_tv(rettv);
3325 rettv->v_type = VAR_STRING;
3326 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003328 else if (op == '+' && rettv->v_type == VAR_BLOB
3329 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003330 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003331 else if (op == '+' && rettv->v_type == VAR_LIST
3332 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003333 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003334 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003335 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 else
3338 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003339 int error = FALSE;
3340 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003341#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003342 float_T f1 = 0, f2 = 0;
3343
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003344 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003345 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003346 f1 = rettv->vval.v_float;
3347 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003348 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003349 else
3350#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003351 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003352 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003353 if (error)
3354 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003355 // This can only happen for "list + non-list" or
3356 // "blob + non-blob". For "non-list + ..." or
3357 // "something - ...", we returned before evaluating the
3358 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003359 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003360 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003361 return FAIL;
3362 }
3363#ifdef FEAT_FLOAT
3364 if (var2.v_type == VAR_FLOAT)
3365 f1 = n1;
3366#endif
3367 }
3368#ifdef FEAT_FLOAT
3369 if (var2.v_type == VAR_FLOAT)
3370 {
3371 f2 = var2.vval.v_float;
3372 n2 = 0;
3373 }
3374 else
3375#endif
3376 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003377 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003378 if (error)
3379 {
3380 clear_tv(rettv);
3381 clear_tv(&var2);
3382 return FAIL;
3383 }
3384#ifdef FEAT_FLOAT
3385 if (rettv->v_type == VAR_FLOAT)
3386 f2 = n2;
3387#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003388 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003389 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003390
3391#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003392 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003393 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3394 {
3395 if (op == '+')
3396 f1 = f1 + f2;
3397 else
3398 f1 = f1 - f2;
3399 rettv->v_type = VAR_FLOAT;
3400 rettv->vval.v_float = f1;
3401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003403#endif
3404 {
3405 if (op == '+')
3406 n1 = n1 + n2;
3407 else
3408 n1 = n1 - n2;
3409 rettv->v_type = VAR_NUMBER;
3410 rettv->vval.v_number = n1;
3411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003413 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 }
3415 }
3416 return OK;
3417}
3418
3419/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003420 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 * * number multiplication
3422 * / number division
3423 * % number modulo
3424 *
3425 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003426 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 *
3428 * Return OK or FAIL.
3429 */
3430 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003431eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003432 char_u **arg,
3433 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003434 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003435 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003437#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003438 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440
3441 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003442 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003444 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 return FAIL;
3446
3447 /*
3448 * Repeat computing, until no '*', '/' or '%' is following.
3449 */
3450 for (;;)
3451 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003452 int evaluate;
3453 int getnext;
3454 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003455 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003456 int op;
3457 varnumber_T n1, n2;
3458#ifdef FEAT_FLOAT
3459 float_T f1, f2;
3460#endif
3461 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003462
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003463 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003464 p = eval_next_non_blank(*arg, evalarg, &getnext);
3465 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003466 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003468
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003469 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003470 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003471 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003472 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003473 {
3474 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3475 {
mityu4ac198c2021-05-28 17:52:40 +02003476 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003477 clear_tv(rettv);
3478 return FAIL;
3479 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003480 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003483#ifdef FEAT_FLOAT
3484 f1 = 0;
3485 f2 = 0;
3486#endif
3487 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003488 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003490#ifdef FEAT_FLOAT
3491 if (rettv->v_type == VAR_FLOAT)
3492 {
3493 f1 = rettv->vval.v_float;
3494 use_float = TRUE;
3495 n1 = 0;
3496 }
3497 else
3498#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003499 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003500 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003501 if (error)
3502 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 }
3504 else
3505 n1 = 0;
3506
3507 /*
3508 * Get the second variable.
3509 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003510 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3511 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003512 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003513 clear_tv(rettv);
3514 return FAIL;
3515 }
3516 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003517 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 return FAIL;
3519
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003520 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003522#ifdef FEAT_FLOAT
3523 if (var2.v_type == VAR_FLOAT)
3524 {
3525 if (!use_float)
3526 {
3527 f1 = n1;
3528 use_float = TRUE;
3529 }
3530 f2 = var2.vval.v_float;
3531 n2 = 0;
3532 }
3533 else
3534#endif
3535 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003536 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003537 clear_tv(&var2);
3538 if (error)
3539 return FAIL;
3540#ifdef FEAT_FLOAT
3541 if (use_float)
3542 f2 = n2;
3543#endif
3544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545
3546 /*
3547 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003548 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003550#ifdef FEAT_FLOAT
3551 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003553 if (op == '*')
3554 f1 = f1 * f2;
3555 else if (op == '/')
3556 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003557# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003558 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003559 if (f2 == 0.0)
3560 {
3561 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003562 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003563 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003564 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003565 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003566 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003567 }
3568 else
3569 f1 = f1 / f2;
3570# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003571 // We rely on the floating point library to handle divide
3572 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003573 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003574# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003577 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003578 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003579 return FAIL;
3580 }
3581 rettv->v_type = VAR_FLOAT;
3582 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 }
3584 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003587 int failed = FALSE;
3588
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003589 if (op == '*')
3590 n1 = n1 * n2;
3591 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003592 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003594 n1 = num_modulus(n1, n2, &failed);
3595 if (failed)
3596 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003597
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003598 rettv->v_type = VAR_NUMBER;
3599 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 }
3602 }
3603
3604 return OK;
3605}
3606
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003607/*
3608 * Handle a type cast before a base level expression.
3609 * "arg" must point to the first non-white of the expression.
3610 * "arg" is advanced to just after the recognized expression.
3611 * Return OK or FAIL.
3612 */
3613 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003614eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003615 char_u **arg,
3616 typval_T *rettv,
3617 evalarg_T *evalarg,
3618 int want_string) // after "." operator
3619{
3620 type_T *want_type = NULL;
3621 garray_T type_list; // list of pointers to allocated types
3622 int res;
3623 int evaluate = evalarg == NULL ? 0
3624 : (evalarg->eval_flags & EVAL_EVALUATE);
3625
3626 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003627 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3628 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003629 {
3630 ++*arg;
3631 ga_init2(&type_list, sizeof(type_T *), 10);
3632 want_type = parse_type(arg, &type_list, TRUE);
3633 if (want_type == NULL && (evaluate || **arg != '>'))
3634 {
3635 clear_type_list(&type_list);
3636 return FAIL;
3637 }
3638
3639 if (**arg != '>')
3640 {
3641 if (*skipwhite(*arg) == '>')
3642 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3643 else
3644 emsg(_(e_missing_gt));
3645 clear_type_list(&type_list);
3646 return FAIL;
3647 }
3648 ++*arg;
3649 *arg = skipwhite_and_linebreak(*arg, evalarg);
3650 }
3651
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003652 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003653
3654 if (want_type != NULL && evaluate)
3655 {
3656 if (res == OK)
3657 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003658 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3659 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003660
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003661 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003662 {
3663 if (want_type == &t_bool && actual != &t_bool
3664 && (actual->tt_flags & TTFLAG_BOOL_OK))
3665 {
3666 int n = tv2bool(rettv);
3667
3668 // can use "0" and "1" for boolean in some places
3669 clear_tv(rettv);
3670 rettv->v_type = VAR_BOOL;
3671 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3672 }
3673 else
3674 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003675 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003676
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003677 where.wt_variable = TRUE;
3678 res = check_type(want_type, actual, TRUE, where);
3679 }
3680 }
3681 }
3682 clear_type_list(&type_list);
3683 }
3684
3685 return res;
3686}
3687
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003688 int
3689eval_leader(char_u **arg, int vim9)
3690{
3691 char_u *s = *arg;
3692 char_u *p = *arg;
3693
3694 while (*p == '!' || *p == '-' || *p == '+')
3695 {
3696 char_u *n = skipwhite(p + 1);
3697
3698 // ++, --, -+ and +- are not accepted in Vim9 script
3699 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3700 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003701 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003702 return FAIL;
3703 }
3704 p = n;
3705 }
3706 *arg = p;
3707 return OK;
3708}
3709
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003711 * Check for a predefined value "true", "false" and "null.*".
3712 * Return OK when recognized.
3713 */
3714 int
3715handle_predefined(char_u *s, int len, typval_T *rettv)
3716{
3717 switch (len)
3718 {
3719 case 4: if (STRNCMP(s, "true", 4) == 0)
3720 {
3721 rettv->v_type = VAR_BOOL;
3722 rettv->vval.v_number = VVAL_TRUE;
3723 return OK;
3724 }
3725 if (STRNCMP(s, "null", 4) == 0)
3726 {
3727 rettv->v_type = VAR_SPECIAL;
3728 rettv->vval.v_number = VVAL_NULL;
3729 return OK;
3730 }
3731 break;
3732 case 5: if (STRNCMP(s, "false", 5) == 0)
3733 {
3734 rettv->v_type = VAR_BOOL;
3735 rettv->vval.v_number = VVAL_FALSE;
3736 return OK;
3737 }
3738 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003739 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3740 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003741#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003742 rettv->v_type = VAR_JOB;
3743 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003744#else
3745 rettv->v_type = VAR_SPECIAL;
3746 rettv->vval.v_number = VVAL_NULL;
3747#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003748 return OK;
3749 }
3750 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003751 case 9:
3752 if (STRNCMP(s, "null_", 5) != 0)
3753 break;
3754 if (STRNCMP(s + 5, "list", 4) == 0)
3755 {
3756 rettv->v_type = VAR_LIST;
3757 rettv->vval.v_list = NULL;
3758 return OK;
3759 }
3760 if (STRNCMP(s + 5, "dict", 4) == 0)
3761 {
3762 rettv->v_type = VAR_DICT;
3763 rettv->vval.v_dict = NULL;
3764 return OK;
3765 }
3766 if (STRNCMP(s + 5, "blob", 4) == 0)
3767 {
3768 rettv->v_type = VAR_BLOB;
3769 rettv->vval.v_blob = NULL;
3770 return OK;
3771 }
3772 break;
3773 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3774 {
3775 rettv->v_type = VAR_STRING;
3776 rettv->vval.v_string = NULL;
3777 return OK;
3778 }
3779 break;
3780 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003781 if (STRNCMP(s, "null_channel", 12) == 0)
3782 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003783#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003784 rettv->v_type = VAR_CHANNEL;
3785 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003786#else
3787 rettv->v_type = VAR_SPECIAL;
3788 rettv->vval.v_number = VVAL_NULL;
3789#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003790 return OK;
3791 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003792 if (STRNCMP(s, "null_partial", 12) == 0)
3793 {
3794 rettv->v_type = VAR_PARTIAL;
3795 rettv->vval.v_partial = NULL;
3796 return OK;
3797 }
3798 break;
3799 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3800 {
3801 rettv->v_type = VAR_FUNC;
3802 rettv->vval.v_string = NULL;
3803 return OK;
3804 }
3805 break;
3806 }
3807 return FAIL;
3808}
3809
3810/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 * Handle sixth level expression:
3812 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003813 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003814 * "string" string constant
3815 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 * &option-name option value
3817 * @r register contents
3818 * identifier variable value
3819 * function() function call
3820 * $VAR environment variable
3821 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003822 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003824 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003825 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 *
3827 * Also handle:
3828 * ! in front logical NOT
3829 * - in front unary minus
3830 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003831 * trailing [] subscript in String or List
3832 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003833 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 *
3835 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003836 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 *
3838 * Return OK or FAIL.
3839 */
3840 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003841eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003842 char_u **arg,
3843 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003844 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003845 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003847 int evaluate = evalarg != NULL
3848 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 int len;
3850 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003851 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 char_u *start_leader, *end_leader;
3853 int ret = OK;
3854 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003855 static int recurse = 0;
3856 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857
3858 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003859 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003860 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003862 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863
3864 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003865 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 */
3867 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003868 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003869 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 end_leader = *arg;
3871
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003872 if (**arg == '.' && (!isdigit(*(*arg + 1))
3873#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003874 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003875#endif
3876 ))
3877 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003878 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003879 ++*arg;
3880 return FAIL;
3881 }
3882
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003883 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003884 // and crash. With MSVC the stack is smaller.
3885 if (recurse ==
3886#ifdef _MSC_VER
3887 300
3888#else
3889 1000
3890#endif
3891 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003892 {
3893 semsg(_(e_expression_too_recursive_str), *arg);
3894 return FAIL;
3895 }
3896 ++recurse;
3897
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 switch (**arg)
3899 {
3900 /*
3901 * Number constant.
3902 */
3903 case '0':
3904 case '1':
3905 case '2':
3906 case '3':
3907 case '4':
3908 case '5':
3909 case '6':
3910 case '7':
3911 case '8':
3912 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003913 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003914
3915 // Apply prefixed "-" and "+" now. Matters especially when
3916 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003917 if (ret == OK && evaluate && end_leader > start_leader
3918 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003919 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 break;
3921
3922 /*
3923 * String constant: "string".
3924 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003925 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 break;
3927
3928 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003929 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003931 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003932 break;
3933
3934 /*
3935 * List: [expr, expr]
3936 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003937 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 break;
3939
3940 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003941 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003942 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003943 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003944 {
3945 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3946 }
3947 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003948 {
3949 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003950 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003951 }
3952 else
3953 ret = NOTDONE;
3954 break;
3955
3956 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003957 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003958 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003959 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003960 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003961 ret = NOTDONE;
3962 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00003963 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003964 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003965 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003966 break;
3967
3968 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003969 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003971 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 break;
3973
3974 /*
3975 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01003976 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 */
LemonBoy2eaef102022-05-06 13:14:50 +01003978 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
3979 ret = eval_interp_string(arg, rettv, evaluate);
3980 else
3981 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 break;
3983
3984 /*
3985 * Register contents: @r.
3986 */
3987 case '@': ++*arg;
3988 if (evaluate)
3989 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003990 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003991 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003992 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003993 emsg_invreg(**arg);
3994 else
3995 {
3996 rettv->v_type = VAR_STRING;
3997 rettv->vval.v_string = get_reg_contents(**arg,
3998 GREG_EXPR_SRC);
3999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 }
4001 if (**arg != NUL)
4002 ++*arg;
4003 break;
4004
4005 /*
4006 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004007 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004009 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004010 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004011 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004012 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004013 if (ret == OK && evaluate)
4014 {
4015 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4016
Bram Moolenaara9931532021-06-12 15:58:16 +02004017 // Compile it here to get the return type. The return
4018 // type is optional, when it's missing use t_unknown.
4019 // This is recognized in compile_return().
4020 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4021 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004022 if (compile_def_function(ufunc, FALSE,
4023 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004024 {
4025 clear_tv(rettv);
4026 ret = FAIL;
4027 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004028 }
4029 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004030 if (ret == NOTDONE)
4031 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004032 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004033 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004034
Bram Moolenaar9215f012020-06-27 21:18:00 +02004035 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004036 if (**arg == ')')
4037 ++*arg;
4038 else if (ret == OK)
4039 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004040 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004041 clear_tv(rettv);
4042 ret = FAIL;
4043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 }
4045 break;
4046
Bram Moolenaar8c711452005-01-14 21:53:12 +00004047 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 break;
4049 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004050
4051 if (ret == NOTDONE)
4052 {
4053 /*
4054 * Must be a variable or function name.
4055 * Can also be a curly-braces kind of name: {expr}.
4056 */
4057 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004058 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004059 if (alias != NULL)
4060 s = alias;
4061
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004062 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004063 ret = FAIL;
4064 else
4065 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004066 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4067
Bram Moolenaar4525a572022-02-13 11:57:33 +00004068 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004069 {
4070 emsg(_(e_cannot_use_underscore_here));
4071 ret = FAIL;
4072 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004073 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004074 && s[0] == 's' && s[1] == ':')
4075 {
4076 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4077 ret = FAIL;
4078 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004079 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004080 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004081 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004082 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004083 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004084 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02004085 else if (flags & EVAL_CONSTANT)
4086 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004087 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004088 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004089 // get the value of "true", "false", etc. or a variable
4090 ret = FAIL;
4091 if (vim9script)
4092 ret = handle_predefined(s, len, rettv);
4093 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004094 {
4095 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004096 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004097 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004098 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004099 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004100 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004101 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004102 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004103 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004104 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004105 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004106 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004107 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004108 }
4109
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004110 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4111 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004112 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004113 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114
4115 /*
4116 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4117 */
4118 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004119 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004120
4121 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004122 return ret;
4123}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004124
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004125/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004126 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004127 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004128 * Adjusts "end_leaderp" until it is at "start_leader".
4129 */
4130 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004131eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004132 typval_T *rettv,
4133 int numeric_only,
4134 char_u *start_leader,
4135 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004136{
4137 char_u *end_leader = *end_leaderp;
4138 int ret = OK;
4139 int error = FALSE;
4140 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004141 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004142 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004143#ifdef FEAT_FLOAT
4144 float_T f = 0.0;
4145
4146 if (rettv->v_type == VAR_FLOAT)
4147 f = rettv->vval.v_float;
4148 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004149#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004150 {
4151 while (VIM_ISWHITE(end_leader[-1]))
4152 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004153 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004154 val = tv2bool(rettv);
4155 else
4156 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004157 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004158 if (error)
4159 {
4160 clear_tv(rettv);
4161 ret = FAIL;
4162 }
4163 else
4164 {
4165 while (end_leader > start_leader)
4166 {
4167 --end_leader;
4168 if (*end_leader == '!')
4169 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004170 if (numeric_only)
4171 {
4172 ++end_leader;
4173 break;
4174 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004175#ifdef FEAT_FLOAT
4176 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004177 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004178 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004179 {
4180 rettv->v_type = VAR_BOOL;
4181 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4182 }
4183 else
4184 f = !f;
4185 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004186 else
4187#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004188 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004189 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004190 type = VAR_BOOL;
4191 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004192 }
4193 else if (*end_leader == '-')
4194 {
4195#ifdef FEAT_FLOAT
4196 if (rettv->v_type == VAR_FLOAT)
4197 f = -f;
4198 else
4199#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004200 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004201 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004202 type = VAR_NUMBER;
4203 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004204 }
4205 }
4206#ifdef FEAT_FLOAT
4207 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004209 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004210 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004212 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004213#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004214 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004215 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004216 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004217 rettv->v_type = type;
4218 else
4219 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004220 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004223 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 return ret;
4225}
4226
4227/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004228 * Call the function referred to in "rettv".
4229 */
4230 static int
4231call_func_rettv(
4232 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004233 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004234 typval_T *rettv,
4235 int evaluate,
4236 dict_T *selfdict,
4237 typval_T *basetv)
4238{
4239 partial_T *pt = NULL;
4240 funcexe_T funcexe;
4241 typval_T functv;
4242 char_u *s;
4243 int ret;
4244
4245 // need to copy the funcref so that we can clear rettv
4246 if (evaluate)
4247 {
4248 functv = *rettv;
4249 rettv->v_type = VAR_UNKNOWN;
4250
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004251 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004252 if (functv.v_type == VAR_PARTIAL)
4253 {
4254 pt = functv.vval.v_partial;
4255 s = partial_name(pt);
4256 }
4257 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004258 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004259 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004260 if (s == NULL || *s == NUL)
4261 {
4262 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004263 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004264 goto theend;
4265 }
4266 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004267 }
4268 else
4269 s = (char_u *)"";
4270
Bram Moolenaara80faa82020-04-12 19:37:17 +02004271 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004272 funcexe.fe_firstline = curwin->w_cursor.lnum;
4273 funcexe.fe_lastline = curwin->w_cursor.lnum;
4274 funcexe.fe_evaluate = evaluate;
4275 funcexe.fe_partial = pt;
4276 funcexe.fe_selfdict = selfdict;
4277 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004278 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004279
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004280theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004281 // Clear the funcref afterwards, so that deleting it while
4282 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004283 if (evaluate)
4284 clear_tv(&functv);
4285
4286 return ret;
4287}
4288
4289/*
4290 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004291 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004292 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4293 */
4294 static int
4295eval_lambda(
4296 char_u **arg,
4297 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004298 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004299 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004300{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004301 int evaluate = evalarg != NULL
4302 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004303 typval_T base = *rettv;
4304 int ret;
4305
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004306 rettv->v_type = VAR_UNKNOWN;
4307
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004308 if (**arg == '{')
4309 {
4310 // ->{lambda}()
4311 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4312 }
4313 else
4314 {
4315 // ->(lambda)()
4316 ++*arg;
4317 ret = eval1(arg, rettv, evalarg);
4318 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004319 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004320 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004321 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004322 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004323 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004324 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4325 && rettv->v_type != VAR_PARTIAL)
4326 {
4327 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4328 return FAIL;
4329 }
4330 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004331 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004332 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004333 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004334
4335 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004336 {
4337 if (verbose)
4338 {
4339 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004340 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004341 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004342 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004343 }
4344 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004345 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004346 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004347 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004348 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004349
4350 // Clear the funcref afterwards, so that deleting it while
4351 // evaluating the arguments is possible (see test55).
4352 if (evaluate)
4353 clear_tv(&base);
4354
4355 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004356}
4357
4358/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004359 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004360 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004361 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4362 */
4363 static int
4364eval_method(
4365 char_u **arg,
4366 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004367 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004368 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004369{
4370 char_u *name;
4371 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004372 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004373 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004374 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004375 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004376 int evaluate = evalarg != NULL
4377 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004378
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004379 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004380
Bram Moolenaarac92e252019-08-03 21:58:38 +02004381 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004382 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004383 if (alias != NULL)
4384 name = alias;
4385
4386 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004387 {
4388 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004389 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004390 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004391 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004392 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004393 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004394 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004395
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004396 // If there is no "(" immediately following, but there is further on,
4397 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4398 // Does not handle anything where "(" is part of the expression.
4399 *arg = skipwhite(*arg);
4400
4401 if (**arg != '(' && alias == NULL
4402 && (paren = vim_strchr(*arg, '(')) != NULL)
4403 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004404 char_u *deref;
4405
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004406 *arg = name;
4407 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004408 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4409 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004410 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004411 *arg = name + len;
4412 ret = FAIL;
4413 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004414 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004415 {
4416 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004417 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004418 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004419 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004420 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004421
4422 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004423 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004424 *arg = skipwhite(*arg);
4425
4426 if (**arg != '(')
4427 {
4428 if (verbose)
4429 semsg(_(e_missing_parenthesis_str), name);
4430 ret = FAIL;
4431 }
4432 else if (VIM_ISWHITE((*arg)[-1]))
4433 {
4434 if (verbose)
4435 emsg(_(e_no_white_space_allowed_before_parenthesis));
4436 ret = FAIL;
4437 }
4438 else
4439 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004440 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004441 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004442 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004443
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004444 // Clear the funcref afterwards, so that deleting it while
4445 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004446 if (evaluate)
4447 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004448 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004449
Bram Moolenaarac92e252019-08-03 21:58:38 +02004450 return ret;
4451}
4452
4453/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004454 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4455 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004456 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4457 */
4458 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004459eval_index(
4460 char_u **arg,
4461 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004462 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004463 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004464{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004465 int evaluate = evalarg != NULL
4466 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004467 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004468 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004469 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004470 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004471 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004472 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004473
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004474 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4475 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004476
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004477 init_tv(&var1);
4478 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004479 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004480 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004481 /*
4482 * dict.name
4483 */
4484 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004485 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004486 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004487 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004488 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004489 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004490 }
4491 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004492 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004493 /*
4494 * something[idx]
4495 *
4496 * Get the (first) variable from inside the [].
4497 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004498 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004499 if (**arg == ':')
4500 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004501 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004502 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004503 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004504 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004505 semsg(_(e_white_space_required_before_and_after_str_at_str),
4506 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004507 clear_tv(&var1);
4508 return FAIL;
4509 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004510 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004511 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004512 int error = FALSE;
4513
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004514#ifdef FEAT_FLOAT
4515 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004516 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004517 && var1.v_type == VAR_FLOAT)
4518 {
4519 var1.vval.v_string = typval_tostring(&var1, TRUE);
4520 var1.v_type = VAR_STRING;
4521 }
4522#endif
Bram Moolenaar4525a572022-02-13 11:57:33 +00004523 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004524 tv_get_number_chk(&var1, &error);
4525 else
4526 error = tv_get_string_chk(&var1) == NULL;
4527 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004528 {
4529 // not a number or string
4530 clear_tv(&var1);
4531 return FAIL;
4532 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004533 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004534
4535 /*
4536 * Get the second variable from inside the [:].
4537 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004538 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004539 if (**arg == ':')
4540 {
4541 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004542 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004543 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004544 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004545 semsg(_(e_white_space_required_before_and_after_str_at_str),
4546 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004547 if (!empty1)
4548 clear_tv(&var1);
4549 return FAIL;
4550 }
4551 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004552 if (**arg == ']')
4553 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004554 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004555 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004556 if (!empty1)
4557 clear_tv(&var1);
4558 return FAIL;
4559 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004560 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004561 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004562 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004563 if (!empty1)
4564 clear_tv(&var1);
4565 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004566 return FAIL;
4567 }
4568 }
4569
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004570 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004571 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004572 if (**arg != ']')
4573 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004574 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004575 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004576 clear_tv(&var1);
4577 if (range)
4578 clear_tv(&var2);
4579 return FAIL;
4580 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004581 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004582 }
4583
4584 if (evaluate)
4585 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004586 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004587 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004588 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004589
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004590 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004591 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004592 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004593 clear_tv(&var2);
4594 return res;
4595 }
4596 return OK;
4597}
4598
4599/*
4600 * Check if "rettv" can have an [index] or [sli:ce]
4601 */
4602 int
4603check_can_index(typval_T *rettv, int evaluate, int verbose)
4604{
4605 switch (rettv->v_type)
4606 {
4607 case VAR_FUNC:
4608 case VAR_PARTIAL:
4609 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004610 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004611 return FAIL;
4612 case VAR_FLOAT:
4613#ifdef FEAT_FLOAT
4614 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004615 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004616 return FAIL;
4617#endif
4618 case VAR_BOOL:
4619 case VAR_SPECIAL:
4620 case VAR_JOB:
4621 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004622 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004623 if (verbose)
4624 emsg(_(e_cannot_index_special_variable));
4625 return FAIL;
4626 case VAR_UNKNOWN:
4627 case VAR_ANY:
4628 case VAR_VOID:
4629 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004630 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004631 emsg(_(e_cannot_index_special_variable));
4632 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004633 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004634 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004635
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004636 case VAR_STRING:
4637 case VAR_LIST:
4638 case VAR_DICT:
4639 case VAR_BLOB:
4640 break;
4641 case VAR_NUMBER:
4642 if (in_vim9script())
4643 emsg(_(e_cannot_index_number));
4644 break;
4645 }
4646 return OK;
4647}
4648
4649/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004650 * slice() function
4651 */
4652 void
4653f_slice(typval_T *argvars, typval_T *rettv)
4654{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004655 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004656 && ((argvars[0].v_type != VAR_STRING
4657 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004658 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004659 && check_for_list_arg(argvars, 0) == FAIL)
4660 || check_for_number_arg(argvars, 1) == FAIL
4661 || check_for_opt_number_arg(argvars, 2) == FAIL))
4662 return;
4663
Bram Moolenaar6601b622021-01-13 21:47:15 +01004664 if (check_can_index(argvars, TRUE, FALSE) == OK)
4665 {
4666 copy_tv(argvars, rettv);
4667 eval_index_inner(rettv, TRUE, argvars + 1,
4668 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4669 TRUE, NULL, 0, FALSE);
4670 }
4671}
4672
4673/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004674 * Apply index or range to "rettv".
4675 * "var1" is the first index, NULL for [:expr].
4676 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004677 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4678 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004679 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4680 */
4681 int
4682eval_index_inner(
4683 typval_T *rettv,
4684 int is_range,
4685 typval_T *var1,
4686 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004687 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004688 char_u *key,
4689 int keylen,
4690 int verbose)
4691{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004692 varnumber_T n1, n2 = 0;
4693 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004694
4695 n1 = 0;
4696 if (var1 != NULL && rettv->v_type != VAR_DICT)
4697 n1 = tv_get_number(var1);
4698
4699 if (is_range)
4700 {
4701 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004702 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004703 if (verbose)
4704 emsg(_(e_cannot_slice_dictionary));
4705 return FAIL;
4706 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004707 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004708 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004709 else
4710 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004711 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004712
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004713 switch (rettv->v_type)
4714 {
4715 case VAR_UNKNOWN:
4716 case VAR_ANY:
4717 case VAR_VOID:
4718 case VAR_FUNC:
4719 case VAR_PARTIAL:
4720 case VAR_FLOAT:
4721 case VAR_BOOL:
4722 case VAR_SPECIAL:
4723 case VAR_JOB:
4724 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004725 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004726 break; // not evaluating, skipping over subscript
4727
4728 case VAR_NUMBER:
4729 case VAR_STRING:
4730 {
4731 char_u *s = tv_get_string(rettv);
4732
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004733 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004734 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004735 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004736 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004737 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004738 else
4739 s = char_from_string(s, n1);
4740 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004741 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004742 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004743 // The resulting variable is a substring. If the indexes
4744 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004745 if (n1 < 0)
4746 {
4747 n1 = len + n1;
4748 if (n1 < 0)
4749 n1 = 0;
4750 }
4751 if (n2 < 0)
4752 n2 = len + n2;
4753 else if (n2 >= len)
4754 n2 = len;
4755 if (n1 >= len || n2 < 0 || n1 > n2)
4756 s = NULL;
4757 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004758 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004759 }
4760 else
4761 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004762 // The resulting variable is a string of a single
4763 // character. If the index is too big or negative the
4764 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004765 if (n1 >= len || n1 < 0)
4766 s = NULL;
4767 else
4768 s = vim_strnsave(s + n1, 1);
4769 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004770 clear_tv(rettv);
4771 rettv->v_type = VAR_STRING;
4772 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004773 }
4774 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004775
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004776 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004777 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4778 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004779 break;
4780
4781 case VAR_LIST:
4782 if (var1 == NULL)
4783 n1 = 0;
4784 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004785 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004786 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004787 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004788 return FAIL;
4789 break;
4790
4791 case VAR_DICT:
4792 {
4793 dictitem_T *item;
4794 typval_T tmp;
4795
4796 if (key == NULL)
4797 {
4798 key = tv_get_string_chk(var1);
4799 if (key == NULL)
4800 return FAIL;
4801 }
4802
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004803 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004804
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004805 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004806 {
4807 if (verbose)
4808 {
4809 if (keylen > 0)
4810 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004811 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004812 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004813 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004814 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004815
4816 copy_tv(&item->di_tv, &tmp);
4817 clear_tv(rettv);
4818 *rettv = tmp;
4819 }
4820 break;
4821 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004822 return OK;
4823}
4824
4825/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004826 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004827 */
4828 char_u *
4829partial_name(partial_T *pt)
4830{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004831 if (pt != NULL)
4832 {
4833 if (pt->pt_name != NULL)
4834 return pt->pt_name;
4835 if (pt->pt_func != NULL)
4836 return pt->pt_func->uf_name;
4837 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004838 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004839}
4840
Bram Moolenaarddecc252016-04-06 22:59:37 +02004841 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004842partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004843{
4844 int i;
4845
4846 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004847 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004848 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004849 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004850 if (pt->pt_name != NULL)
4851 {
4852 func_unref(pt->pt_name);
4853 vim_free(pt->pt_name);
4854 }
4855 else
4856 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004857
Bram Moolenaar54656012021-06-09 20:50:46 +02004858 // "out_up" is no longer used, decrement refcount on partial that owns it.
4859 partial_unref(pt->pt_outer.out_up_partial);
4860
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004861 // Using pt_outer from another partial.
4862 partial_unref(pt->pt_outer_partial);
4863
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004864 // Decrease the reference count for the context of a closure. If down
4865 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004866 if (pt->pt_funcstack != NULL)
4867 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004868 --pt->pt_funcstack->fs_refcount;
4869 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004870 }
4871
Bram Moolenaarddecc252016-04-06 22:59:37 +02004872 vim_free(pt);
4873}
4874
4875/*
4876 * Unreference a closure: decrement the reference count and free it when it
4877 * becomes zero.
4878 */
4879 void
4880partial_unref(partial_T *pt)
4881{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004882 if (pt != NULL)
4883 {
4884 if (--pt->pt_refcount <= 0)
4885 partial_free(pt);
4886
4887 // If the reference count goes down to one, the funcstack may be the
4888 // only reference and can be freed if no other partials reference it.
4889 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4890 funcstack_check_refcount(pt->pt_funcstack);
4891 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004892}
4893
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004894/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004895 * Return the next (unique) copy ID.
4896 * Used for serializing nested structures.
4897 */
4898 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004899get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004900{
4901 current_copyID += COPYID_INC;
4902 return current_copyID;
4903}
4904
4905/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004906 * Garbage collection for lists and dictionaries.
4907 *
4908 * We use reference counts to be able to free most items right away when they
4909 * are no longer used. But for composite items it's possible that it becomes
4910 * unused while the reference count is > 0: When there is a recursive
4911 * reference. Example:
4912 * :let l = [1, 2, 3]
4913 * :let d = {9: l}
4914 * :let l[1] = d
4915 *
4916 * Since this is quite unusual we handle this with garbage collection: every
4917 * once in a while find out which lists and dicts are not referenced from any
4918 * variable.
4919 *
4920 * Here is a good reference text about garbage collection (refers to Python
4921 * but it applies to all reference-counting mechanisms):
4922 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004923 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004924
4925/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004926 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004927 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004928 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004929 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004930 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004931garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004932{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004933 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004934 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004935 buf_T *buf;
4936 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004937 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004938 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004939
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004940 if (!testing)
4941 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004942 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004943 want_garbage_collect = FALSE;
4944 may_garbage_collect = FALSE;
4945 garbage_collect_at_exit = FALSE;
4946 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004947
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004948 // The execution stack can grow big, limit the size.
4949 if (exestack.ga_maxlen - exestack.ga_len > 500)
4950 {
4951 size_t new_len;
4952 char_u *pp;
4953 int n;
4954
4955 // Keep 150% of the current size, with a minimum of the growth size.
4956 n = exestack.ga_len / 2;
4957 if (n < exestack.ga_growsize)
4958 n = exestack.ga_growsize;
4959
4960 // Don't make it bigger though.
4961 if (exestack.ga_len + n < exestack.ga_maxlen)
4962 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004963 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004964 pp = vim_realloc(exestack.ga_data, new_len);
4965 if (pp == NULL)
4966 return FAIL;
4967 exestack.ga_maxlen = exestack.ga_len + n;
4968 exestack.ga_data = pp;
4969 }
4970 }
4971
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004972 // We advance by two because we add one for items referenced through
4973 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004974 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004975
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004976 /*
4977 * 1. Go through all accessible variables and mark all lists and dicts
4978 * with copyID.
4979 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004980
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004981 // Don't free variables in the previous_funccal list unless they are only
4982 // referenced through previous_funccal. This must be first, because if
4983 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004984 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004985
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004986 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004987 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004988
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004989 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004990 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004991 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4992 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004993
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004994 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004995 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004996 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4997 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004998 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004999 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5000 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005001#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005002 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005003 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5004 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005005 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005006 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005007 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5008 NULL, NULL);
5009#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005010
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005011 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005012 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005013 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5014 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005015 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005016 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005017
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005018 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005019 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005020
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005021 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005022 abort = abort || set_ref_in_functions(copyID);
5023
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005024 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005025 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005026
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005027 // funcstacks keep variables for closures
5028 abort = abort || set_ref_in_funcstacks(copyID);
5029
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005030 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005031 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005032
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005033 // callbacks in buffers
5034 abort = abort || set_ref_in_buffers(copyID);
5035
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005036 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5037 abort = abort || set_ref_in_insexpand_funcs(copyID);
5038
5039 // 'operatorfunc' callback
5040 abort = abort || set_ref_in_opfunc(copyID);
5041
5042 // 'tagfunc' callback
5043 abort = abort || set_ref_in_tagfunc(copyID);
5044
5045 // 'imactivatefunc' and 'imstatusfunc' callbacks
5046 abort = abort || set_ref_in_im_funcs(copyID);
5047
Bram Moolenaar1dced572012-04-05 16:54:08 +02005048#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005049 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005050#endif
5051
Bram Moolenaardb913952012-06-29 12:54:53 +02005052#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005053 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005054#endif
5055
5056#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005057 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005058#endif
5059
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005060#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005061 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005062 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005063#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005064#ifdef FEAT_NETBEANS_INTG
5065 abort = abort || set_ref_in_nb_channel(copyID);
5066#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005067
Bram Moolenaare3188e22016-05-31 21:13:04 +02005068#ifdef FEAT_TIMERS
5069 abort = abort || set_ref_in_timer(copyID);
5070#endif
5071
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005072#ifdef FEAT_QUICKFIX
5073 abort = abort || set_ref_in_quickfix(copyID);
5074#endif
5075
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005076#ifdef FEAT_TERMINAL
5077 abort = abort || set_ref_in_term(copyID);
5078#endif
5079
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005080#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005081 abort = abort || set_ref_in_popups(copyID);
5082#endif
5083
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005084 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005085 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005086 /*
5087 * 2. Free lists and dictionaries that are not referenced.
5088 */
5089 did_free = free_unref_items(copyID);
5090
5091 /*
5092 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005093 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005094 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005095 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005096 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005097 else if (p_verbose > 0)
5098 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005099 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005100 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005101
5102 return did_free;
5103}
5104
5105/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005106 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005107 */
5108 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005109free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005110{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005111 int did_free = FALSE;
5112
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005113 // Let all "free" functions know that we are here. This means no
5114 // dictionaries, lists, channels or jobs are to be freed, because we will
5115 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005116 in_free_unref_items = TRUE;
5117
5118 /*
5119 * PASS 1: free the contents of the items. We don't free the items
5120 * themselves yet, so that it is possible to decrement refcount counters
5121 */
5122
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005123 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005124 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005125
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005126 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005127 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005128
5129#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005130 // Go through the list of jobs and free items without the copyID. This
5131 // must happen before doing channels, because jobs refer to channels, but
5132 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005133 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5134
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005135 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005136 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5137#endif
5138
5139 /*
5140 * PASS 2: free the items themselves.
5141 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005142 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005143 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005144
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005145#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005146 // Go through the list of jobs and free items without the copyID. This
5147 // must happen before doing channels, because jobs refer to channels, but
5148 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005149 free_unused_jobs(copyID, COPYID_MASK);
5150
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005151 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005152 free_unused_channels(copyID, COPYID_MASK);
5153#endif
5154
5155 in_free_unref_items = FALSE;
5156
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005157 return did_free;
5158}
5159
5160/*
5161 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005162 * "list_stack" is used to add lists to be marked. Can be NULL.
5163 *
5164 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005165 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005166 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005167set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005168{
5169 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005170 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005171 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005172 hashtab_T *cur_ht;
5173 ht_stack_T *ht_stack = NULL;
5174 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005175
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005176 cur_ht = ht;
5177 for (;;)
5178 {
5179 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005180 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005181 // Mark each item in the hashtab. If the item contains a hashtab
5182 // it is added to ht_stack, if it contains a list it is added to
5183 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005184 todo = (int)cur_ht->ht_used;
5185 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5186 if (!HASHITEM_EMPTY(hi))
5187 {
5188 --todo;
5189 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5190 &ht_stack, list_stack);
5191 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005192 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005193
5194 if (ht_stack == NULL)
5195 break;
5196
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005197 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005198 cur_ht = ht_stack->ht;
5199 tempitem = ht_stack;
5200 ht_stack = ht_stack->prev;
5201 free(tempitem);
5202 }
5203
5204 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005205}
5206
Dominique Pelle748b3082022-01-08 12:41:16 +00005207#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5208 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005209/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005210 * Mark a dict and its items with "copyID".
5211 * Returns TRUE if setting references failed somehow.
5212 */
5213 int
5214set_ref_in_dict(dict_T *d, int copyID)
5215{
5216 if (d != NULL && d->dv_copyID != copyID)
5217 {
5218 d->dv_copyID = copyID;
5219 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5220 }
5221 return FALSE;
5222}
Dominique Pelle748b3082022-01-08 12:41:16 +00005223#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005224
5225/*
5226 * Mark a list and its items with "copyID".
5227 * Returns TRUE if setting references failed somehow.
5228 */
5229 int
5230set_ref_in_list(list_T *ll, int copyID)
5231{
5232 if (ll != NULL && ll->lv_copyID != copyID)
5233 {
5234 ll->lv_copyID = copyID;
5235 return set_ref_in_list_items(ll, copyID, NULL);
5236 }
5237 return FALSE;
5238}
5239
5240/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005241 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005242 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5243 *
5244 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005245 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005246 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005247set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005248{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005249 listitem_T *li;
5250 int abort = FALSE;
5251 list_T *cur_l;
5252 list_stack_T *list_stack = NULL;
5253 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005254
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005255 cur_l = l;
5256 for (;;)
5257 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005258 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005259 // Mark each item in the list. If the item contains a hashtab
5260 // it is added to ht_stack, if it contains a list it is added to
5261 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005262 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5263 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5264 ht_stack, &list_stack);
5265 if (list_stack == NULL)
5266 break;
5267
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005268 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005269 cur_l = list_stack->list;
5270 tempitem = list_stack;
5271 list_stack = list_stack->prev;
5272 free(tempitem);
5273 }
5274
5275 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005276}
5277
5278/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005279 * Mark the partial in callback 'cb' with "copyID".
5280 */
5281 int
5282set_ref_in_callback(callback_T *cb, int copyID)
5283{
5284 typval_T tv;
5285
5286 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5287 return FALSE;
5288
5289 tv.v_type = VAR_PARTIAL;
5290 tv.vval.v_partial = cb->cb_partial;
5291 return set_ref_in_item(&tv, copyID, NULL, NULL);
5292}
5293
5294/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005295 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005296 * "list_stack" is used to add lists to be marked. Can be NULL.
5297 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5298 *
5299 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005300 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005301 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005302set_ref_in_item(
5303 typval_T *tv,
5304 int copyID,
5305 ht_stack_T **ht_stack,
5306 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005307{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005308 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005309
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005310 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005311 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005312 dict_T *dd = tv->vval.v_dict;
5313
Bram Moolenaara03f2332016-02-06 18:09:59 +01005314 if (dd != NULL && dd->dv_copyID != copyID)
5315 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005316 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005317 dd->dv_copyID = copyID;
5318 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005319 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005320 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5321 }
5322 else
5323 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005324 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5325
Bram Moolenaara03f2332016-02-06 18:09:59 +01005326 if (newitem == NULL)
5327 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005328 else
5329 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005330 newitem->ht = &dd->dv_hashtab;
5331 newitem->prev = *ht_stack;
5332 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005333 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005334 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005335 }
5336 }
5337 else if (tv->v_type == VAR_LIST)
5338 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005339 list_T *ll = tv->vval.v_list;
5340
Bram Moolenaara03f2332016-02-06 18:09:59 +01005341 if (ll != NULL && ll->lv_copyID != copyID)
5342 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005343 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005344 ll->lv_copyID = copyID;
5345 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005346 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005347 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005348 }
5349 else
5350 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005351 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5352
Bram Moolenaara03f2332016-02-06 18:09:59 +01005353 if (newitem == NULL)
5354 abort = TRUE;
5355 else
5356 {
5357 newitem->list = ll;
5358 newitem->prev = *list_stack;
5359 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005360 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005361 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005362 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005363 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005364 else if (tv->v_type == VAR_FUNC)
5365 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005366 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005367 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005368 else if (tv->v_type == VAR_PARTIAL)
5369 {
5370 partial_T *pt = tv->vval.v_partial;
5371 int i;
5372
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005373 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005374 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005375 // Didn't see this partial yet.
5376 pt->pt_copyID = copyID;
5377
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005378 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005379
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005380 if (pt->pt_dict != NULL)
5381 {
5382 typval_T dtv;
5383
5384 dtv.v_type = VAR_DICT;
5385 dtv.vval.v_dict = pt->pt_dict;
5386 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5387 }
5388
5389 for (i = 0; i < pt->pt_argc; ++i)
5390 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5391 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005392 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005393 }
5394 }
5395#ifdef FEAT_JOB_CHANNEL
5396 else if (tv->v_type == VAR_JOB)
5397 {
5398 job_T *job = tv->vval.v_job;
5399 typval_T dtv;
5400
5401 if (job != NULL && job->jv_copyID != copyID)
5402 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005403 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005404 if (job->jv_channel != NULL)
5405 {
5406 dtv.v_type = VAR_CHANNEL;
5407 dtv.vval.v_channel = job->jv_channel;
5408 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5409 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005410 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005411 {
5412 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005413 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005414 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5415 }
5416 }
5417 }
5418 else if (tv->v_type == VAR_CHANNEL)
5419 {
5420 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005421 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005422 typval_T dtv;
5423 jsonq_T *jq;
5424 cbq_T *cq;
5425
5426 if (ch != NULL && ch->ch_copyID != copyID)
5427 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005428 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005429 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005430 {
5431 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5432 jq = jq->jq_next)
5433 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5434 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5435 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005436 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005437 {
5438 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005439 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005440 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5441 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005442 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005443 {
5444 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005445 dtv.vval.v_partial =
5446 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005447 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5448 }
5449 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005450 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005451 {
5452 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005453 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005454 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5455 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005456 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005457 {
5458 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005459 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005460 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5461 }
5462 }
5463 }
5464#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005465 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005466}
5467
Bram Moolenaar8c711452005-01-14 21:53:12 +00005468/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005469 * Return a string with the string representation of a variable.
5470 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005471 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005472 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005473 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005474 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005475 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005476 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5477 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005478 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005479 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005480 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005481echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005482 typval_T *tv,
5483 char_u **tofree,
5484 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005485 int copyID,
5486 int echo_style,
5487 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005488 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005490 static int recurse = 0;
5491 char_u *r = NULL;
5492
Bram Moolenaar33570922005-01-25 22:26:29 +00005493 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005494 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005495 if (!did_echo_string_emsg)
5496 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005497 // Only give this message once for a recursive call to avoid
5498 // flooding the user with errors. And stop iterating over lists
5499 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005500 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005501 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005502 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005503 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005504 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005505 }
5506 ++recurse;
5507
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005508 switch (tv->v_type)
5509 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005510 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005511 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005512 {
5513 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005514 r = tv->vval.v_string;
5515 if (r == NULL)
5516 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005517 }
5518 else
5519 {
5520 *tofree = string_quote(tv->vval.v_string, FALSE);
5521 r = *tofree;
5522 }
5523 break;
5524
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005525 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005526 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005527 char_u buf[MAX_FUNC_NAME_LEN];
5528
5529 if (echo_style)
5530 {
LemonBoya5d35902022-04-29 21:15:02 +01005531 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5532 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005533 buf, MAX_FUNC_NAME_LEN);
5534 if (r == buf)
5535 {
5536 r = vim_strsave(buf);
5537 *tofree = r;
5538 }
5539 else
5540 *tofree = NULL;
5541 }
5542 else
5543 {
5544 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5545 : make_ufunc_name_readable(
5546 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5547 TRUE);
5548 r = *tofree;
5549 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005550 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005551 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005552
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005553 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005554 {
5555 partial_T *pt = tv->vval.v_partial;
5556 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005557 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005558 garray_T ga;
5559 int i;
5560 char_u *tf;
5561
5562 ga_init2(&ga, 1, 100);
5563 ga_concat(&ga, (char_u *)"function(");
5564 if (fname != NULL)
5565 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005566 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005567 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005568 && vim_isupper(fname[1]))
5569 {
5570 ga_concat(&ga, (char_u *)"'g:");
5571 ga_concat(&ga, fname + 1);
5572 }
5573 else
5574 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005575 vim_free(fname);
5576 }
5577 if (pt != NULL && pt->pt_argc > 0)
5578 {
5579 ga_concat(&ga, (char_u *)", [");
5580 for (i = 0; i < pt->pt_argc; ++i)
5581 {
5582 if (i > 0)
5583 ga_concat(&ga, (char_u *)", ");
5584 ga_concat(&ga,
5585 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5586 vim_free(tf);
5587 }
5588 ga_concat(&ga, (char_u *)"]");
5589 }
5590 if (pt != NULL && pt->pt_dict != NULL)
5591 {
5592 typval_T dtv;
5593
5594 ga_concat(&ga, (char_u *)", ");
5595 dtv.v_type = VAR_DICT;
5596 dtv.vval.v_dict = pt->pt_dict;
5597 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5598 vim_free(tf);
5599 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005600 // terminate with ')' and a NUL
5601 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005602
5603 *tofree = ga.ga_data;
5604 r = *tofree;
5605 break;
5606 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005607
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005608 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005609 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005610 break;
5611
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005612 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005613 if (tv->vval.v_list == NULL)
5614 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005615 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005616 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005617 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005618 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005619 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5620 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005621 {
5622 *tofree = NULL;
5623 r = (char_u *)"[...]";
5624 }
5625 else
5626 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005627 int old_copyID = tv->vval.v_list->lv_copyID;
5628
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005629 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005630 *tofree = list2string(tv, copyID, restore_copyID);
5631 if (restore_copyID)
5632 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005633 r = *tofree;
5634 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005635 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005636
Bram Moolenaar8c711452005-01-14 21:53:12 +00005637 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005638 if (tv->vval.v_dict == NULL)
5639 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005640 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005641 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005642 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005643 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005644 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5645 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005646 {
5647 *tofree = NULL;
5648 r = (char_u *)"{...}";
5649 }
5650 else
5651 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005652 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005653
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005654 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005655 *tofree = dict2string(tv, copyID, restore_copyID);
5656 if (restore_copyID)
5657 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005658 r = *tofree;
5659 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005660 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005661
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005662 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005663 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005664 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005665 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005666 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005667 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005668 break;
5669
Bram Moolenaar835dc632016-02-07 14:27:38 +01005670 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005671 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005672#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005673 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005674 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5675 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005676 if (composite_val)
5677 {
5678 *tofree = string_quote(r, FALSE);
5679 r = *tofree;
5680 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005681#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005682 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005683
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005684 case VAR_INSTR:
5685 *tofree = NULL;
5686 r = (char_u *)"instructions";
5687 break;
5688
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005689 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005690#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005691 *tofree = NULL;
5692 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5693 r = numbuf;
5694 break;
5695#endif
5696
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005697 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005698 case VAR_SPECIAL:
5699 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005700 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005701 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005702 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005703
Bram Moolenaar8502c702014-06-17 12:51:16 +02005704 if (--recurse == 0)
5705 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005706 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005707}
5708
5709/*
5710 * Return a string with the string representation of a variable.
5711 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5712 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005713 * Does not put quotes around strings, as ":echo" displays values.
5714 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5715 * May return NULL.
5716 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005717 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005718echo_string(
5719 typval_T *tv,
5720 char_u **tofree,
5721 char_u *numbuf,
5722 int copyID)
5723{
5724 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5725}
5726
5727/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005728 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5729 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005730 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005731 */
5732 int
5733buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5734{
5735 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005736 char_u *t;
5737 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005738
5739 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5740 return -1;
5741
5742 if (lnum > buf->b_ml.ml_line_count)
5743 lnum = buf->b_ml.ml_line_count;
5744
5745 str = ml_get_buf(buf, lnum, FALSE);
5746 if (str == NULL)
5747 return -1;
5748
5749 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005750 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005751
Bram Moolenaar91458462021-01-13 20:08:38 +01005752 // count the number of characters
5753 t = str;
5754 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5755 t += mb_ptr2len(t);
5756
5757 // In insert mode, when the cursor is at the end of a non-empty line,
5758 // byteidx points to the NUL character immediately past the end of the
5759 // string. In this case, add one to the character count.
5760 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5761 count++;
5762
5763 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005764}
5765
5766/*
5767 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005768 * byte index. Works only for loaded buffers. Returns -1 on failure.
5769 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005770 */
5771 int
5772buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5773{
5774 char_u *str;
5775 char_u *t;
5776
5777 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5778 return -1;
5779
5780 if (lnum > buf->b_ml.ml_line_count)
5781 lnum = buf->b_ml.ml_line_count;
5782
5783 str = ml_get_buf(buf, lnum, FALSE);
5784 if (str == NULL)
5785 return -1;
5786
5787 // Convert the character offset to a byte offset
5788 t = str;
5789 while (*t != NUL && --charidx > 0)
5790 t += mb_ptr2len(t);
5791
Bram Moolenaar91458462021-01-13 20:08:38 +01005792 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005793}
5794
5795/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005797 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005799 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005800var2fpos(
5801 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005802 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005803 int *fnum, // set to fnum for '0, 'A, etc.
5804 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005806 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005808 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005810 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005811 if (varp->v_type == VAR_LIST)
5812 {
5813 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005814 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005815 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005816 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005817
5818 l = varp->vval.v_list;
5819 if (l == NULL)
5820 return NULL;
5821
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005822 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005823 pos.lnum = list_find_nr(l, 0L, &error);
5824 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005825 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005826 if (charcol)
5827 len = (long)mb_charlen(ml_get(pos.lnum));
5828 else
5829 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005830
Bram Moolenaarec65d772020-08-20 22:29:12 +02005831 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005832 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005833 li = list_find(l, 1L);
5834 if (li != NULL && li->li_tv.v_type == VAR_STRING
5835 && li->li_tv.vval.v_string != NULL
5836 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005837 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005838 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005839 }
5840 else
5841 {
5842 pos.col = list_find_nr(l, 1L, &error);
5843 if (error)
5844 return NULL;
5845 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005846
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005847 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005848 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005849 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005850 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005851
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005852 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005853 pos.coladd = list_find_nr(l, 2L, &error);
5854 if (error)
5855 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005856
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005857 return &pos;
5858 }
5859
Bram Moolenaarc5809432021-03-27 21:23:30 +01005860 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5861 return NULL;
5862
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005863 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005864 if (name == NULL)
5865 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005866
5867 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005868 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005869 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005870 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005871 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005872 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005873 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005874 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005875 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005876 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005877 pos = VIsual;
5878 else
5879 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005880 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005881 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005882 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005884 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005885 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5887 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005888 pos = *pp;
5889 }
5890 if (pos.lnum != 0)
5891 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005892 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005893 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5894 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005896
Bram Moolenaara5525202006-03-02 22:52:09 +00005897 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005898
Bram Moolenaar477933c2007-07-17 14:32:23 +00005899 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005900 {
5901 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005902 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005903 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005904 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005905 // In silent Ex mode topline is zero, but that's not a valid line
5906 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005907 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005908 return &pos;
5909 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005910 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005911 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005912 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005913 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005914 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005915 return &pos;
5916 }
5917 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005918 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005920 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 {
5922 pos.lnum = curbuf->b_ml.ml_line_count;
5923 pos.col = 0;
5924 }
5925 else
5926 {
5927 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005928 if (charcol)
5929 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5930 else
5931 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 }
5933 return &pos;
5934 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005935 if (in_vim9script())
5936 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 return NULL;
5938}
5939
5940/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005941 * Convert list in "arg" into a position and optional file number.
5942 * When "fnump" is NULL there is no file number, only 3 items.
5943 * Note that the column is passed on as-is, the caller may want to decrement
5944 * it to use 1 for the first column.
5945 * Return FAIL when conversion is not possible, doesn't check the position for
5946 * validity.
5947 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005948 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005949list2fpos(
5950 typval_T *arg,
5951 pos_T *posp,
5952 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005953 colnr_T *curswantp,
5954 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005955{
5956 list_T *l = arg->vval.v_list;
5957 long i = 0;
5958 long n;
5959
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005960 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5961 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005962 if (arg->v_type != VAR_LIST
5963 || l == NULL
5964 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005965 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005966 return FAIL;
5967
5968 if (fnump != NULL)
5969 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005970 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005971 if (n < 0)
5972 return FAIL;
5973 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005974 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005975 *fnump = n;
5976 }
5977
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005978 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005979 if (n < 0)
5980 return FAIL;
5981 posp->lnum = n;
5982
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005983 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005984 if (n < 0)
5985 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005986 // If character position is specified, then convert to byte position
5987 if (charcol)
5988 {
5989 buf_T *buf;
5990
5991 // Get the text for the specified line in a loaded buffer
5992 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5993 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5994 return FAIL;
5995
Bram Moolenaar91458462021-01-13 20:08:38 +01005996 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005997 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005998 posp->col = n;
5999
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006000 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006001 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006002 posp->coladd = 0;
6003 else
6004 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006005
Bram Moolenaar493c1782014-05-28 14:34:46 +02006006 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006007 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006008
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006009 return OK;
6010}
6011
6012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 * Get the length of an environment variable name.
6014 * Advance "arg" to the first character after the name.
6015 * Return 0 for error.
6016 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006017 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006018get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019{
6020 char_u *p;
6021 int len;
6022
6023 for (p = *arg; vim_isIDc(*p); ++p)
6024 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006025 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 return 0;
6027
6028 len = (int)(p - *arg);
6029 *arg = p;
6030 return len;
6031}
6032
6033/*
6034 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006035 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 * Return 0 if something is wrong.
6037 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006038 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006039get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040{
6041 char_u *p;
6042 int len;
6043
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006044 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006046 {
6047 if (*p == ':')
6048 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006049 // "s:" is start of "s:var", but "n:" is not and can be used in
6050 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006051 len = (int)(p - *arg);
6052 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6053 || len > 1)
6054 break;
6055 }
6056 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006057 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 return 0;
6059
6060 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006061 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062
6063 return len;
6064}
6065
6066/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006067 * Get the length of the name of a variable or function.
6068 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006070 * Return -1 if curly braces expansion failed.
6071 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 * If the name contains 'magic' {}'s, expand them and return the
6073 * expanded name in an allocated string via 'alias' - caller must free.
6074 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006075 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006076get_name_len(
6077 char_u **arg,
6078 char_u **alias,
6079 int evaluate,
6080 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081{
6082 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 char_u *p;
6084 char_u *expr_start;
6085 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006087 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088
6089 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6090 && (*arg)[2] == (int)KE_SNR)
6091 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006092 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 *arg += 3;
6094 return get_id_len(arg) + 3;
6095 }
6096 len = eval_fname_script(*arg);
6097 if (len > 0)
6098 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006099 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 *arg += len;
6101 }
6102
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006104 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006106 p = find_name_end(*arg, &expr_start, &expr_end,
6107 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 if (expr_start != NULL)
6109 {
6110 char_u *temp_string;
6111
6112 if (!evaluate)
6113 {
6114 len += (int)(p - *arg);
6115 *arg = skipwhite(p);
6116 return len;
6117 }
6118
6119 /*
6120 * Include any <SID> etc in the expanded string:
6121 * Thus the -len here.
6122 */
6123 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6124 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006125 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 *alias = temp_string;
6127 *arg = skipwhite(p);
6128 return (int)STRLEN(temp_string);
6129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130
6131 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006132 // Only give an error when there is something, otherwise it will be
6133 // reported at a higher level.
6134 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006135 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136
6137 return len;
6138}
6139
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006140/*
6141 * Find the end of a variable or function name, taking care of magic braces.
6142 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6143 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006144 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006145 * Return a pointer to just after the name. Equal to "arg" if there is no
6146 * valid name.
6147 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006148 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006149find_name_end(
6150 char_u *arg,
6151 char_u **expr_start,
6152 char_u **expr_end,
6153 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006155 int mb_nest = 0;
6156 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006158 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02006159 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006161 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006163 *expr_start = NULL;
6164 *expr_end = NULL;
6165 }
6166
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006167 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006168 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
6169 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006170 return arg;
6171
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006172 for (p = arg; *p != NUL
6173 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006174 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006175 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006176 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006177 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006178 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006179 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006180 if (*p == '\'')
6181 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006182 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006183 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006184 ;
6185 if (*p == NUL)
6186 break;
6187 }
6188 else if (*p == '"')
6189 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006190 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006191 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006192 if (*p == '\\' && p[1] != NUL)
6193 ++p;
6194 if (*p == NUL)
6195 break;
6196 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006197 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6198 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006199 // "s:" is start of "s:var", but "n:" is not and can be used in
6200 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006201 len = (int)(p - arg);
6202 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006203 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006204 break;
6205 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006206
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006207 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006209 if (*p == '[')
6210 ++br_nest;
6211 else if (*p == ']')
6212 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006214
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006215 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006217 if (*p == '{')
6218 {
6219 mb_nest++;
6220 if (expr_start != NULL && *expr_start == NULL)
6221 *expr_start = p;
6222 }
6223 else if (*p == '}')
6224 {
6225 mb_nest--;
6226 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6227 *expr_end = p;
6228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 }
6231
6232 return p;
6233}
6234
6235/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006236 * Expands out the 'magic' {}'s in a variable/function name.
6237 * Note that this can call itself recursively, to deal with
6238 * constructs like foo{bar}{baz}{bam}
6239 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6240 * "in_start" ^
6241 * "expr_start" ^
6242 * "expr_end" ^
6243 * "in_end" ^
6244 *
6245 * Returns a new allocated string, which the caller must free.
6246 * Returns NULL for failure.
6247 */
6248 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006249make_expanded_name(
6250 char_u *in_start,
6251 char_u *expr_start,
6252 char_u *expr_end,
6253 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006254{
6255 char_u c1;
6256 char_u *retval = NULL;
6257 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006258
6259 if (expr_end == NULL || in_end == NULL)
6260 return NULL;
6261 *expr_start = NUL;
6262 *expr_end = NUL;
6263 c1 = *in_end;
6264 *in_end = NUL;
6265
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006266 temp_result = eval_to_string(expr_start + 1, FALSE);
6267 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006268 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006269 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6270 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006271 if (retval != NULL)
6272 {
6273 STRCPY(retval, in_start);
6274 STRCAT(retval, temp_result);
6275 STRCAT(retval, expr_end + 1);
6276 }
6277 }
6278 vim_free(temp_result);
6279
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006280 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006281 *expr_start = '{';
6282 *expr_end = '}';
6283
6284 if (retval != NULL)
6285 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006286 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006287 if (expr_start != NULL)
6288 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006289 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006290 temp_result = make_expanded_name(retval, expr_start,
6291 expr_end, temp_result);
6292 vim_free(retval);
6293 retval = temp_result;
6294 }
6295 }
6296
6297 return retval;
6298}
6299
6300/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006302 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006304 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006305eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006307 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006308}
6309
6310/*
6311 * Return TRUE if character "c" can be used as the first character in a
6312 * variable or function name (excluding '{' and '}').
6313 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006314 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006315eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006316{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006317 return ASCII_ISALPHA(c) || c == '_';
6318}
6319
6320/*
6321 * Return TRUE if character "c" can be used as the first character of a
6322 * dictionary key.
6323 */
6324 int
6325eval_isdictc(int c)
6326{
6327 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328}
6329
6330/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006331 * Handle:
6332 * - expr[expr], expr[expr:expr] subscript
6333 * - ".name" lookup
6334 * - function call with Funcref variable: func(expr)
6335 * - method call: var->method()
6336 *
6337 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006338 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006339 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006340 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006341handle_subscript(
6342 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006343 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006344 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006345 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006346 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006347{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006348 int evaluate = evalarg != NULL
6349 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006350 int ret = OK;
6351 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006352 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006353 int getnext;
6354 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006355
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006356 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006357 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006358 // When at the end of the line and ".name" or "->{" or "->X" follows in
6359 // the next line then consume the line break.
6360 p = eval_next_non_blank(*arg, evalarg, &getnext);
6361 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006362 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006363 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6364 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6365 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006366 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006367 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006368 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006369 check_white = FALSE;
6370 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006371
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006372 if (rettv->v_type == VAR_ANY)
6373 {
6374 char_u *exp_name;
6375 int cc;
6376 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006377 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006378 type_T *type;
6379
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006380 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006381 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006382 if (**arg != '.')
6383 {
6384 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006385 semsg(_(e_expected_dot_after_name_str),
6386 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006387 ret = FAIL;
6388 break;
6389 }
6390 ++*arg;
6391 if (IS_WHITE_OR_NUL(**arg))
6392 {
6393 if (verbose)
6394 emsg(_(e_no_white_space_allowed_after_dot));
6395 ret = FAIL;
6396 break;
6397 }
6398
6399 // isolate the name
6400 exp_name = *arg;
6401 while (eval_isnamec(**arg))
6402 ++*arg;
6403 cc = **arg;
6404 **arg = NUL;
6405
6406 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00006407 evalarg->eval_cctx, evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006408 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006409
6410 if (idx < 0 && ufunc == NULL)
6411 {
6412 ret = FAIL;
6413 break;
6414 }
6415 if (idx >= 0)
6416 {
6417 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6418 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6419
6420 copy_tv(sv->sv_tv, rettv);
6421 }
6422 else
6423 {
6424 rettv->v_type = VAR_FUNC;
6425 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6426 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006427 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006428 }
6429
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006430 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6431 || rettv->v_type == VAR_PARTIAL))
6432 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006433 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006434 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6435 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006436
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006437 // Stop the expression evaluation when immediately aborting on
6438 // error, or when an interrupt occurred or an exception was thrown
6439 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006440 if (aborting())
6441 {
6442 if (ret == OK)
6443 clear_tv(rettv);
6444 ret = FAIL;
6445 }
6446 dict_unref(selfdict);
6447 selfdict = NULL;
6448 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006449 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006450 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006451 if (in_vim9script())
6452 *arg = skipwhite(p + 2);
6453 else
6454 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006455 if (ret == OK)
6456 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006457 if (VIM_ISWHITE(**arg))
6458 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006459 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006460 ret = FAIL;
6461 }
6462 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006463 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006464 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006465 else
6466 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006467 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006468 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006469 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006470 // "." is ".name" lookup when we found a dict or when evaluating and
6471 // scriptversion is at least 2, where string concatenation is "..".
6472 else if (**arg == '['
6473 || (**arg == '.' && (rettv->v_type == VAR_DICT
6474 || (!evaluate
6475 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006476 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006477 {
6478 dict_unref(selfdict);
6479 if (rettv->v_type == VAR_DICT)
6480 {
6481 selfdict = rettv->vval.v_dict;
6482 if (selfdict != NULL)
6483 ++selfdict->dv_refcount;
6484 }
6485 else
6486 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006487 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006488 {
6489 clear_tv(rettv);
6490 ret = FAIL;
6491 }
6492 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006493 else
6494 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006495 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006496
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006497 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6498 // Don't do this when "Func" is already a partial that was bound
6499 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006500 if (selfdict != NULL
6501 && (rettv->v_type == VAR_FUNC
6502 || (rettv->v_type == VAR_PARTIAL
6503 && (rettv->vval.v_partial->pt_auto
6504 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006505 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006506
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006507 dict_unref(selfdict);
6508 return ret;
6509}
6510
6511/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006512 * Make a copy of an item.
6513 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006514 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006515 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6516 * reference to an already copied list/dict can be used.
6517 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006518 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006519 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006520item_copy(
6521 typval_T *from,
6522 typval_T *to,
6523 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006524 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006525 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006526{
6527 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006528 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006529
Bram Moolenaar33570922005-01-25 22:26:29 +00006530 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006531 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006532 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006533 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006534 }
6535 ++recurse;
6536
6537 switch (from->v_type)
6538 {
6539 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006540 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006541 case VAR_STRING:
6542 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006543 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006544 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006545 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006546 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006547 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006548 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006549 copy_tv(from, to);
6550 break;
6551 case VAR_LIST:
6552 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006553 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006554 if (from->vval.v_list == NULL)
6555 to->vval.v_list = NULL;
6556 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6557 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006558 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006559 to->vval.v_list = from->vval.v_list->lv_copylist;
6560 ++to->vval.v_list->lv_refcount;
6561 }
6562 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006563 to->vval.v_list = list_copy(from->vval.v_list,
6564 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006565 if (to->vval.v_list == NULL)
6566 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006567 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006568 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006569 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006570 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006571 case VAR_DICT:
6572 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006573 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006574 if (from->vval.v_dict == NULL)
6575 to->vval.v_dict = NULL;
6576 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6577 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006578 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006579 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6580 ++to->vval.v_dict->dv_refcount;
6581 }
6582 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006583 to->vval.v_dict = dict_copy(from->vval.v_dict,
6584 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006585 if (to->vval.v_dict == NULL)
6586 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006587 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006588 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006589 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006590 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006591 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006592 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006593 }
6594 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006595 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006596}
6597
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006598 void
6599echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6600{
6601 char_u *tofree;
6602 char_u numbuf[NUMBUFLEN];
6603 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6604
6605 if (*atstart)
6606 {
6607 *atstart = FALSE;
6608 // Call msg_start() after eval1(), evaluating the expression
6609 // may cause a message to appear.
6610 if (with_space)
6611 {
6612 // Mark the saved text as finishing the line, so that what
6613 // follows is displayed on a new line when scrolling back
6614 // at the more prompt.
6615 msg_sb_eol();
6616 msg_start();
6617 }
6618 }
6619 else if (with_space)
6620 msg_puts_attr(" ", echo_attr);
6621
6622 if (p != NULL)
6623 for ( ; *p != NUL && !got_int; ++p)
6624 {
6625 if (*p == '\n' || *p == '\r' || *p == TAB)
6626 {
6627 if (*p != TAB && *needclr)
6628 {
6629 // remove any text still there from the command
6630 msg_clr_eos();
6631 *needclr = FALSE;
6632 }
6633 msg_putchar_attr(*p, echo_attr);
6634 }
6635 else
6636 {
6637 if (has_mbyte)
6638 {
6639 int i = (*mb_ptr2len)(p);
6640
6641 (void)msg_outtrans_len_attr(p, i, echo_attr);
6642 p += i - 1;
6643 }
6644 else
6645 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6646 }
6647 }
6648 vim_free(tofree);
6649}
6650
Bram Moolenaare9a41262005-01-15 22:18:47 +00006651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 * ":echo expr1 ..." print each argument separated with a space, add a
6653 * newline at the end.
6654 * ":echon expr1 ..." print each argument plain.
6655 */
6656 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006657ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658{
6659 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006660 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006661 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 int needclr = TRUE;
6663 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006664 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006665 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006666 evalarg_T evalarg;
6667
Bram Moolenaare707c882020-07-01 13:07:37 +02006668 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669
6670 if (eap->skip)
6671 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006672 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006674 // If eval1() causes an error message the text from the command may
6675 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006676 need_clr_eos = needclr;
6677
Bram Moolenaar68db9962021-05-09 23:19:22 +02006678 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006679 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 {
6681 /*
6682 * Report the invalid expression unless the expression evaluation
6683 * has been cancelled due to an aborting error, an interrupt, or an
6684 * exception.
6685 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006686 if (!aborting() && did_emsg == did_emsg_before
6687 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006688 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006689 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 break;
6691 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006692 need_clr_eos = FALSE;
6693
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006695 {
6696 if (rettv.v_type == VAR_VOID)
6697 {
6698 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6699 break;
6700 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006701 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006702 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006703
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006704 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 arg = skipwhite(arg);
6706 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006707 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006708 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709
6710 if (eap->skip)
6711 --emsg_skip;
6712 else
6713 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006714 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 if (needclr)
6716 msg_clr_eos();
6717 if (eap->cmdidx == CMD_echo)
6718 msg_end();
6719 }
6720}
6721
6722/*
6723 * ":echohl {name}".
6724 */
6725 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006726ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006728 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729}
6730
6731/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006732 * Returns the :echo attribute
6733 */
6734 int
6735get_echo_attr(void)
6736{
6737 return echo_attr;
6738}
6739
6740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 * ":execute expr1 ..." execute the result of an expression.
6742 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01006743 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006745 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 * Each gets spaces around each argument and a newline at the end for
6747 * echo commands
6748 */
6749 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006750ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751{
6752 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006753 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 int ret = OK;
6755 char_u *p;
6756 garray_T ga;
6757 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006758 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759
6760 ga_init2(&ga, 1, 80);
6761
6762 if (eap->skip)
6763 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006764 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006766 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006767 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769
6770 if (!eap->skip)
6771 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006772 char_u buf[NUMBUFLEN];
6773
6774 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006775 {
6776 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6777 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006778 semsg(_(e_using_invalid_value_as_string_str),
6779 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006780 p = NULL;
6781 }
6782 else
6783 p = tv_get_string_buf(&rettv, buf);
6784 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006785 else
6786 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006787 if (p == NULL)
6788 {
6789 clear_tv(&rettv);
6790 ret = FAIL;
6791 break;
6792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 len = (int)STRLEN(p);
6794 if (ga_grow(&ga, len + 2) == FAIL)
6795 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006796 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 ret = FAIL;
6798 break;
6799 }
6800 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 ga.ga_len += len;
6804 }
6805
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006806 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 arg = skipwhite(arg);
6808 }
6809
6810 if (ret != FAIL && ga.ga_data != NULL)
6811 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006812 // use the first line of continuation lines for messages
6813 SOURCING_LNUM = start_lnum;
6814
Bram Moolenaar37fef162022-08-29 18:16:32 +01006815 if (eap->cmdidx == CMD_echomsg
6816 || eap->cmdidx == CMD_echowindow
6817 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006818 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006819 // Mark the already saved text as finishing the line, so that what
6820 // follows is displayed on a new line when scrolling back at the
6821 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006822 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006823 }
6824
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006825 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006826 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006827 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006828 out_flush();
6829 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006830 else if (eap->cmdidx == CMD_echowindow)
6831 {
6832#ifdef HAS_MESSAGE_WINDOW
6833 start_echowindow();
6834#endif
6835 msg_attr(ga.ga_data, echo_attr);
6836#ifdef HAS_MESSAGE_WINDOW
6837 end_echowindow();
6838#endif
6839 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006840 else if (eap->cmdidx == CMD_echoconsole)
6841 {
6842 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6843 ui_write((char_u *)"\r\n", 2, TRUE);
6844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 else if (eap->cmdidx == CMD_echoerr)
6846 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006847 int save_did_emsg = did_emsg;
6848
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006849 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006850 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 if (!force_abort)
6852 did_emsg = save_did_emsg;
6853 }
6854 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006855 {
6856 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6857
6858 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6859 sticky_cmdmod_flags = cmdmod.cmod_flags
6860 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 do_cmdline((char_u *)ga.ga_data,
6862 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006863 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 }
6866
6867 ga_clear(&ga);
6868
6869 if (eap->skip)
6870 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02006871 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872}
6873
6874/*
6875 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6876 * "arg" points to the "&" or '+' when called, to "option" when returning.
6877 * Returns NULL when no option name found. Otherwise pointer to the char
6878 * after the option name.
6879 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006880 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006881find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882{
6883 char_u *p = *arg;
6884
6885 ++p;
6886 if (*p == 'g' && p[1] == ':')
6887 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006888 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 p += 2;
6890 }
6891 else if (*p == 'l' && p[1] == ':')
6892 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006893 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 p += 2;
6895 }
6896 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006897 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898
6899 if (!ASCII_ISALPHA(*p))
6900 return NULL;
6901 *arg = p;
6902
6903 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006904 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 else
6906 while (ASCII_ISALPHA(*p))
6907 ++p;
6908 return p;
6909}
6910
6911/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006912 * Display script name where an item was last set.
6913 * Should only be invoked when 'verbose' is non-zero.
6914 */
6915 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006916last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006917{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006918 char_u *p;
6919
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006920 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006921 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006922 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006923 if (p != NULL)
6924 {
6925 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006926 msg_puts(_("\n\tLast set from "));
6927 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006928 if (script_ctx.sc_lnum > 0)
6929 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006930 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006931 msg_outnum((long)script_ctx.sc_lnum);
6932 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006933 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006934 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006935 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006936 }
6937}
6938
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006939#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940
6941/*
6942 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006943 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 * "flags" can be "g" to do a global substitute.
6945 * Returns an allocated string, NULL for error.
6946 */
6947 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006948do_string_sub(
6949 char_u *str,
6950 char_u *pat,
6951 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006952 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006953 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954{
6955 int sublen;
6956 regmatch_T regmatch;
6957 int i;
6958 int do_all;
6959 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006960 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 garray_T ga;
6962 char_u *ret;
6963 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006964 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006966 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006968 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969
6970 ga_init2(&ga, 1, 200);
6971
6972 do_all = (flags[0] == 'g');
6973
6974 regmatch.rm_ic = p_ic;
6975 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6976 if (regmatch.regprog != NULL)
6977 {
6978 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006979 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6981 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006982 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006983 if (regmatch.startp[0] == regmatch.endp[0])
6984 {
6985 if (zero_width == regmatch.startp[0])
6986 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006987 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006988 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006989 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6990 (size_t)i);
6991 ga.ga_len += i;
6992 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006993 continue;
6994 }
6995 zero_width = regmatch.startp[0];
6996 }
6997
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 /*
6999 * Get some space for a temporary buffer to do the substitution
7000 * into. It will contain:
7001 * - The text up to where the match is.
7002 * - The substituted text.
7003 * - The text after the match.
7004 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007005 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaare90c8532014-11-05 16:03:44 +01007006 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7008 {
7009 ga_clear(&ga);
7010 break;
7011 }
7012
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007013 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 i = (int)(regmatch.startp[0] - tail);
7015 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007016 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007017 (void)vim_regsub(&regmatch, sub, expr,
7018 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7019 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007021 tail = regmatch.endp[0];
7022 if (*tail == NUL)
7023 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 if (!do_all)
7025 break;
7026 }
7027
7028 if (ga.ga_data != NULL)
7029 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7030
Bram Moolenaar473de612013-06-08 18:19:48 +02007031 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 }
7033
7034 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7035 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007036 if (p_cpo == empty_option)
7037 p_cpo = save_cpo;
7038 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007039 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007040 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007041 // If it's still empty it was changed and restored, need to restore in
7042 // the complicated way.
7043 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007044 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007045 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047
7048 return ret;
7049}