blob: 5d93b7e50c1193c3e057890a4589e9665d6fed53 [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
Bram Moolenaar9510d222022-09-11 15:14:05 +01001034 if (is_scoped_variable(name))
1035 {
1036 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1037 return NULL;
1038 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001039 if (tp == p + 1 && !quiet)
1040 {
1041 semsg(_(e_white_space_required_after_str_str), ":", p);
1042 return NULL;
1043 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001044 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001045 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001046 semsg(_(e_using_type_not_in_script_context_str), p);
1047 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001048 }
1049
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001050 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001051 lp->ll_type = parse_type(&tp,
1052 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1053 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001054 if (lp->ll_type == NULL && !quiet)
1055 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001056 lp->ll_name_end = tp;
1057 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001058 }
1059 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001060 if (lp->ll_name == NULL)
1061 return p;
1062
Bram Moolenaar62aec932022-01-29 21:45:34 +00001063 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001064 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001065 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001066
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001067 if (import != NULL)
1068 {
1069 ufunc_T *ufunc;
1070 type_T *type;
1071
Bram Moolenaar753885b2022-08-24 16:30:36 +01001072 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001073 lp->ll_sid = import->imp_sid;
1074 lp->ll_name = skipwhite(p + 1);
1075 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1076 lp->ll_name_end = p;
1077
1078 // check the item is exported
1079 cc = *p;
1080 *p = NUL;
1081 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001082 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001083 {
1084 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001085 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001086 }
1087 *p = cc;
1088 }
1089 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001090
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001091 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001092 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001093 return p;
1094
Bram Moolenaar4525a572022-02-13 11:57:33 +00001095 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001096 {
1097 // using local variable
1098 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001099 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001100 }
1101 else
1102 {
1103 cc = *p;
1104 *p = NUL;
1105 // When we would write to the variable pass &ht and prevent autoload.
1106 writing = !(flags & GLV_READ_ONLY);
1107 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001108 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001109 if (v == NULL && !quiet)
1110 semsg(_(e_undefined_variable_str), lp->ll_name);
1111 *p = cc;
1112 if (v == NULL)
1113 return NULL;
1114 lp->ll_tv = &v->di_tv;
1115 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001116
Bram Moolenaar4525a572022-02-13 11:57:33 +00001117 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001118 {
1119 if (!quiet)
1120 semsg(_(e_variable_already_declared), lp->ll_name);
1121 return NULL;
1122 }
1123
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001124 /*
1125 * Loop until no more [idx] or .key is following.
1126 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001127 var1.v_type = VAR_UNKNOWN;
1128 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001129 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001130 {
Bram Moolenaarf21d5462022-09-10 12:36:00 +01001131 int r = OK;
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001132
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001133 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1134 {
1135 if (!quiet)
1136 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1137 return NULL;
1138 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001139 if (lp->ll_tv->v_type != VAR_LIST
1140 && lp->ll_tv->v_type != VAR_DICT
1141 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001142 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001143 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001144 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001145 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001146 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001147
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001148 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaare65081d2021-06-27 15:04:05 +02001149 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001150 r = rettv_list_alloc(lp->ll_tv);
Bram Moolenaare65081d2021-06-27 15:04:05 +02001151 else if (lp->ll_tv->v_type == VAR_BLOB
1152 && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001153 r = rettv_blob_alloc(lp->ll_tv);
1154 if (r == FAIL)
1155 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001156
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001157 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001158 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001159 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001160 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001161 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001162 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001163
Bram Moolenaar4525a572022-02-13 11:57:33 +00001164 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001165 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001166 && lp->ll_tv == &v->di_tv
1167 && ht != NULL && ht == get_script_local_ht())
1168 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001169 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001170
1171 // Vim9 script local variable: get the type
1172 if (sv != NULL)
1173 lp->ll_valtype = sv->sv_type;
1174 }
1175
Bram Moolenaar8c711452005-01-14 21:53:12 +00001176 len = -1;
1177 if (*p == '.')
1178 {
1179 key = p + 1;
1180 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1181 ;
1182 if (len == 0)
1183 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001184 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001185 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001186 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001187 }
1188 p = key + len;
1189 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001190 else
1191 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001192 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001193 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001194 if (*p == ':')
1195 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001196 else
1197 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001198 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001199 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001200 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001201 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001202 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001203 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001204 clear_tv(&var1);
1205 return NULL;
1206 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001207 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001208 }
1209
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001210 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001211 if (*p == ':')
1212 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001213 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001214 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001215 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001216 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001217 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001218 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001219 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001220 if (rettv != NULL
1221 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001222 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001223 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001224 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001225 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001226 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001227 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001228 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001229 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001230 }
1231 p = skipwhite(p + 1);
1232 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001233 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001234 else
1235 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001236 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001237 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001238 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001239 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001240 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001241 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001242 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001243 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001244 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001245 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001246 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001247 clear_tv(&var2);
1248 return NULL;
1249 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001250 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001251 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001252 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001253 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001254 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001255
Bram Moolenaar8c711452005-01-14 21:53:12 +00001256 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001257 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001258 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001259 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001260 clear_tv(&var1);
1261 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001262 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001263 }
1264
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001265 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001266 ++p;
1267 }
1268
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001269 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001270 {
1271 if (len == -1)
1272 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001273 // "[key]": get key from "var1"
1274 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001275 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001276 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001277 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001278 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001279 }
1280 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001281 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001282
1283 // a NULL dict is equivalent with an empty dict
1284 if (lp->ll_tv->vval.v_dict == NULL)
1285 {
1286 lp->ll_tv->vval.v_dict = dict_alloc();
1287 if (lp->ll_tv->vval.v_dict == NULL)
1288 {
1289 clear_tv(&var1);
1290 return NULL;
1291 }
1292 ++lp->ll_tv->vval.v_dict->dv_refcount;
1293 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001294 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001295
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001296 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001297
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001298 // When assigning to a scope dictionary check that a function and
1299 // variable name is valid (only variable name unless it is l: or
1300 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001301 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001302 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001303 int prevval;
1304 int wrong;
1305
1306 if (len != -1)
1307 {
1308 prevval = key[len];
1309 key[len] = NUL;
1310 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001311 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001312 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001313 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1314 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001315 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001316 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001317 if (len != -1)
1318 key[len] = prevval;
1319 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001320 {
1321 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001322 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001323 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001324 }
1325
Bram Moolenaar10c65862020-10-08 21:16:42 +02001326 if (lp->ll_valtype != NULL)
1327 // use the type of the member
1328 lp->ll_valtype = lp->ll_valtype->tt_member;
1329
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001330 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001331 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001332 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001333 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001334 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001335 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001336 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001337 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001338 return NULL;
1339 }
1340
Bram Moolenaar31b81602019-02-10 22:14:27 +01001341 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001342 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001343 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001344 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001345 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001346 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001347 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001348 }
1349 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001350 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001351 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001352 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001353 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001354 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001355 p = NULL;
1356 break;
1357 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001358 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001359 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001360 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1361 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001362 {
1363 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001364 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001365 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001366
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001367 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001368 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001369 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001370 else if (lp->ll_tv->v_type == VAR_BLOB)
1371 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001372 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1373
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001374 /*
1375 * Get the number and item for the only or first index of the List.
1376 */
1377 if (empty1)
1378 lp->ll_n1 = 0;
1379 else
1380 // is number or string
1381 lp->ll_n1 = (long)tv_get_number(&var1);
1382 clear_tv(&var1);
1383
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001384 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001385 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001386 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001387 return NULL;
1388 }
1389 if (lp->ll_range && !lp->ll_empty2)
1390 {
1391 lp->ll_n2 = (long)tv_get_number(&var2);
1392 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001393 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1394 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001395 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001396 }
1397 lp->ll_blob = lp->ll_tv->vval.v_blob;
1398 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001399 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001400 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001401 else
1402 {
1403 /*
1404 * Get the number and item for the only or first index of the List.
1405 */
1406 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001407 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001408 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001409 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001410 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001411 clear_tv(&var1);
1412
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001413 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001414 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001415 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1416 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001417 if (lp->ll_li == NULL)
1418 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001419 clear_tv(&var2);
1420 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001421 }
1422
Bram Moolenaar10c65862020-10-08 21:16:42 +02001423 if (lp->ll_valtype != NULL)
1424 // use the type of the member
1425 lp->ll_valtype = lp->ll_valtype->tt_member;
1426
Bram Moolenaar8c711452005-01-14 21:53:12 +00001427 /*
1428 * May need to find the item or absolute index for the second
1429 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001430 * When no index given: "lp->ll_empty2" is TRUE.
1431 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001432 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001433 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001434 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001435 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001436 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001437 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001438 if (check_range_index_two(lp->ll_list,
1439 &lp->ll_n1, lp->ll_li,
1440 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001441 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001442 }
1443
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001444 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001445 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001446 }
1447
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001448 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001449 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001450 return p;
1451}
1452
1453/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001454 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001455 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001456 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001457clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001458{
1459 vim_free(lp->ll_exp_name);
1460 vim_free(lp->ll_newkey);
1461}
1462
1463/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001464 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001465 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001466 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1467 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001468 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001469 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001470set_var_lval(
1471 lval_T *lp,
1472 char_u *endp,
1473 typval_T *rettv,
1474 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001475 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1476 char_u *op,
1477 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001478{
1479 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001480 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001481
1482 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001483 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001484 cc = *endp;
1485 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001486 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1487 return;
1488
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001489 if (lp->ll_blob != NULL)
1490 {
1491 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001492
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001493 if (op != NULL && *op != '=')
1494 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001495 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001496 return;
1497 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001498 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001499 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001500
1501 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1502 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001503 if (lp->ll_empty2)
1504 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1505
Bram Moolenaar68452172021-04-12 21:21:02 +02001506 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1507 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001508 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001509 }
1510 else
1511 {
1512 val = (int)tv_get_number_chk(rettv, &error);
1513 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001514 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001515 }
1516 }
1517 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001518 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001519 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001520
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001521 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1522 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001523 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001524 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001525 *endp = cc;
1526 return;
1527 }
1528
Bram Moolenaarff697e62019-02-12 22:28:33 +01001529 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001530 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001531 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001532 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001533 {
1534 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001535 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1536 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001537 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001538 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001539 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001540 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001541 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001542 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001543 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001544 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001545 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1546 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001547 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001548 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001549 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001550 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001551 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001552 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001553 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001554 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001555 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001556 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001557 else if (lp->ll_range)
1558 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001559 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1560 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001561 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001562 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001563 return;
1564 }
1565
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001566 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1567 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001568 }
1569 else
1570 {
1571 /*
1572 * Assign to a List or Dictionary item.
1573 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001574 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1575 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001576 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001577 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001578 return;
1579 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001580
1581 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001582 && check_typval_arg_type(lp->ll_valtype, rettv,
1583 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001584 return;
1585
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001586 if (lp->ll_newkey != NULL)
1587 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001588 if (op != NULL && *op != '=')
1589 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001590 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001591 return;
1592 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001593 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1594 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001595 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001596
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001597 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001598 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001599 if (di == NULL)
1600 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001601 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1602 {
1603 vim_free(di);
1604 return;
1605 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001606 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001607 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001608 else if (op != NULL && *op != '=')
1609 {
1610 tv_op(lp->ll_tv, rettv, op);
1611 return;
1612 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001613 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001614 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001615
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001616 /*
1617 * Assign the value to the variable or list item.
1618 */
1619 if (copy)
1620 copy_tv(rettv, lp->ll_tv);
1621 else
1622 {
1623 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001624 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001625 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001626 }
1627 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001628}
1629
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001630/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001631 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1632 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001633 * Returns OK or FAIL.
1634 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001635 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001636tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001637{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001638 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001639 char_u numbuf[NUMBUFLEN];
1640 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001641 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001642
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001643 // Can't do anything with a Funcref or Dict on the right.
1644 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001645 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001646 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1647 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001648 {
1649 switch (tv1->v_type)
1650 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001651 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001652 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001653 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001654 case VAR_DICT:
1655 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001656 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001657 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001658 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001659 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001660 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001661 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001662 break;
1663
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001664 case VAR_BLOB:
1665 if (*op != '+' || tv2->v_type != VAR_BLOB)
1666 break;
1667 // BLOB += BLOB
1668 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1669 {
1670 blob_T *b1 = tv1->vval.v_blob;
1671 blob_T *b2 = tv2->vval.v_blob;
1672 int i, len = blob_len(b2);
1673 for (i = 0; i < len; i++)
1674 ga_append(&b1->bv_ga, blob_get(b2, i));
1675 }
1676 return OK;
1677
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001678 case VAR_LIST:
1679 if (*op != '+' || tv2->v_type != VAR_LIST)
1680 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001681 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001682 if (tv2->vval.v_list != NULL)
1683 {
1684 if (tv1->vval.v_list == NULL)
1685 {
1686 tv1->vval.v_list = tv2->vval.v_list;
1687 ++tv1->vval.v_list->lv_refcount;
1688 }
1689 else
1690 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1691 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001692 return OK;
1693
1694 case VAR_NUMBER:
1695 case VAR_STRING:
1696 if (tv2->v_type == VAR_LIST)
1697 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001698 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001699 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001700 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001701 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001702#ifdef FEAT_FLOAT
1703 if (tv2->v_type == VAR_FLOAT)
1704 {
1705 float_T f = n;
1706
Bram Moolenaarff697e62019-02-12 22:28:33 +01001707 if (*op == '%')
1708 break;
1709 switch (*op)
1710 {
1711 case '+': f += tv2->vval.v_float; break;
1712 case '-': f -= tv2->vval.v_float; break;
1713 case '*': f *= tv2->vval.v_float; break;
1714 case '/': f /= tv2->vval.v_float; break;
1715 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001716 clear_tv(tv1);
1717 tv1->v_type = VAR_FLOAT;
1718 tv1->vval.v_float = f;
1719 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001720 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001721#endif
1722 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001723 switch (*op)
1724 {
1725 case '+': n += tv_get_number(tv2); break;
1726 case '-': n -= tv_get_number(tv2); break;
1727 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001728 case '/': n = num_divide(n, tv_get_number(tv2),
1729 &failed); break;
1730 case '%': n = num_modulus(n, tv_get_number(tv2),
1731 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001732 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001733 clear_tv(tv1);
1734 tv1->v_type = VAR_NUMBER;
1735 tv1->vval.v_number = n;
1736 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001737 }
1738 else
1739 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001740 if (tv2->v_type == VAR_FLOAT)
1741 break;
1742
Bram Moolenaarff697e62019-02-12 22:28:33 +01001743 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001744 s = tv_get_string(tv1);
1745 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001746 clear_tv(tv1);
1747 tv1->v_type = VAR_STRING;
1748 tv1->vval.v_string = s;
1749 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001750 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001751
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001752 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001753#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001754 {
1755 float_T f;
1756
Bram Moolenaarff697e62019-02-12 22:28:33 +01001757 if (*op == '%' || *op == '.'
1758 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001759 && tv2->v_type != VAR_NUMBER
1760 && tv2->v_type != VAR_STRING))
1761 break;
1762 if (tv2->v_type == VAR_FLOAT)
1763 f = tv2->vval.v_float;
1764 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001765 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001766 switch (*op)
1767 {
1768 case '+': tv1->vval.v_float += f; break;
1769 case '-': tv1->vval.v_float -= f; break;
1770 case '*': tv1->vval.v_float *= f; break;
1771 case '/': tv1->vval.v_float /= f; break;
1772 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001773 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001774#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001775 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001776 }
1777 }
1778
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001779 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001780 return FAIL;
1781}
1782
1783/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001784 * Evaluate the expression used in a ":for var in expr" command.
1785 * "arg" points to "var".
1786 * Set "*errp" to TRUE for an error, FALSE otherwise;
1787 * Return a pointer that holds the info. Null when there is an error.
1788 */
1789 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001790eval_for_line(
1791 char_u *arg,
1792 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001793 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001794 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001795{
Bram Moolenaar33570922005-01-25 22:26:29 +00001796 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001797 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001798 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001799 typval_T tv;
1800 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001801 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001802
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001803 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001804
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001805 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001806 if (fi == NULL)
1807 return NULL;
1808
Bram Moolenaar404557e2021-07-05 21:41:48 +02001809 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1810 &fi->fi_semicolon, FALSE);
1811 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001812 return fi;
1813
Bram Moolenaar404557e2021-07-05 21:41:48 +02001814 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001815 if (expr[0] != 'i' || expr[1] != 'n'
1816 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001817 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001818 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1819 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1820 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001821 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001822 return fi;
1823 }
1824
1825 if (skip)
1826 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001827 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1828 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001829 {
1830 *errp = FALSE;
1831 if (!skip)
1832 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001833 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001834 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001835 l = tv.vval.v_list;
1836 if (l == NULL)
1837 {
1838 // a null list is like an empty list: do nothing
1839 clear_tv(&tv);
1840 }
1841 else
1842 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001844 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001845
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001846 // No need to increment the refcount, it's already set for
1847 // the list being used in "tv".
1848 fi->fi_list = l;
1849 list_add_watch(l, &fi->fi_lw);
1850 fi->fi_lw.lw_item = l->lv_first;
1851 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001852 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001853 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001854 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001855 fi->fi_bi = 0;
1856 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001857 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001858 typval_T btv;
1859
1860 // Make a copy, so that the iteration still works when the
1861 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001862 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001863 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001864 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001865 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001866 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001867 else if (tv.v_type == VAR_STRING)
1868 {
1869 fi->fi_byte_idx = 0;
1870 fi->fi_string = tv.vval.v_string;
1871 tv.vval.v_string = NULL;
1872 if (fi->fi_string == NULL)
1873 fi->fi_string = vim_strsave((char_u *)"");
1874 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001875 else
1876 {
Sean Dewar80d73952021-08-04 19:25:54 +02001877 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001878 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001879 }
1880 }
1881 }
1882 if (skip)
1883 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001884 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001885
1886 return fi;
1887}
1888
1889/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001890 * Used when looping over a :for line, skip the "in expr" part.
1891 */
1892 void
1893skip_for_lines(void *fi_void, evalarg_T *evalarg)
1894{
1895 forinfo_T *fi = (forinfo_T *)fi_void;
1896 int i;
1897
1898 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01001899 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001900}
1901
1902/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001903 * Use the first item in a ":for" list. Advance to the next.
1904 * Assign the values to the variable (list). "arg" points to the first one.
1905 * Return TRUE when a valid item was found, FALSE when at end of list or
1906 * something wrong.
1907 */
1908 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001909next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001910{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001911 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001913 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001914 ? (ASSIGN_FINAL
1915 // first round: error if variable exists
1916 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1917 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001918 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001919 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001920 int skip_assign = in_vim9script() && arg[0] == '_'
1921 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001923 if (fi->fi_blob != NULL)
1924 {
1925 typval_T tv;
1926
1927 if (fi->fi_bi >= blob_len(fi->fi_blob))
1928 return FALSE;
1929 tv.v_type = VAR_NUMBER;
1930 tv.v_lock = VAR_FIXED;
1931 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1932 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001933 if (skip_assign)
1934 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001935 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001936 fi->fi_varcount, flag, NULL) == OK;
1937 }
1938
1939 if (fi->fi_string != NULL)
1940 {
1941 typval_T tv;
1942 int len;
1943
1944 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1945 if (len == 0)
1946 return FALSE;
1947 tv.v_type = VAR_STRING;
1948 tv.v_lock = VAR_FIXED;
1949 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1950 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001951 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001952 if (skip_assign)
1953 result = TRUE;
1954 else
1955 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001956 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001957 vim_free(tv.vval.v_string);
1958 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001959 }
1960
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001961 item = fi->fi_lw.lw_item;
1962 if (item == NULL)
1963 result = FALSE;
1964 else
1965 {
1966 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001967 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001968 if (skip_assign)
1969 result = TRUE;
1970 else
1971 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001972 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973 }
1974 return result;
1975}
1976
1977/*
1978 * Free the structure used to store info used by ":for".
1979 */
1980 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001981free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001982{
Bram Moolenaar33570922005-01-25 22:26:29 +00001983 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001984
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001985 if (fi == NULL)
1986 return;
1987 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001988 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001990 list_unref(fi->fi_list);
1991 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001992 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001993 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001994 else
1995 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001996 vim_free(fi);
1997}
1998
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002000set_context_for_expression(
2001 expand_T *xp,
2002 char_u *arg,
2003 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002005 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002007 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002009 if (cmdidx == CMD_let || cmdidx == CMD_var
2010 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002011 {
2012 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002013 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002014 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002015 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002016 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002017 {
2018 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002019 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002020 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002021 break;
2022 }
2023 return;
2024 }
2025 }
2026 else
2027 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2028 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 while ((xp->xp_pattern = vim_strpbrk(arg,
2030 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2031 {
2032 c = *xp->xp_pattern;
2033 if (c == '&')
2034 {
2035 c = xp->xp_pattern[1];
2036 if (c == '&')
2037 {
2038 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002039 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 }
2041 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002042 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002044 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2045 xp->xp_pattern += 2;
2046
2047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 }
2049 else if (c == '$')
2050 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002051 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 xp->xp_context = EXPAND_ENV_VARS;
2053 }
2054 else if (c == '=')
2055 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002056 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 xp->xp_context = EXPAND_EXPRESSION;
2058 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002059 else if (c == '#'
2060 && xp->xp_context == EXPAND_EXPRESSION)
2061 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002062 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002063 break;
2064 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002065 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 && xp->xp_context == EXPAND_FUNCTIONS
2067 && vim_strchr(xp->xp_pattern, '(') == NULL)
2068 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002069 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 break;
2071 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002072 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002074 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 {
2076 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2077 if (c == '\\' && xp->xp_pattern[1] != NUL)
2078 ++xp->xp_pattern;
2079 xp->xp_context = EXPAND_NOTHING;
2080 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002081 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002083 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2085 /* skip */ ;
2086 xp->xp_context = EXPAND_NOTHING;
2087 }
2088 else if (c == '|')
2089 {
2090 if (xp->xp_pattern[1] == '|')
2091 {
2092 ++xp->xp_pattern;
2093 xp->xp_context = EXPAND_EXPRESSION;
2094 }
2095 else
2096 xp->xp_context = EXPAND_COMMANDS;
2097 }
2098 else
2099 xp->xp_context = EXPAND_EXPRESSION;
2100 }
2101 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002102 // Doesn't look like something valid, expand as an expression
2103 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002104 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 arg = xp->xp_pattern;
2106 if (*arg != NUL)
2107 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2108 /* skip */ ;
2109 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002110
2111 // ":exe one two" completes "two"
2112 if ((cmdidx == CMD_execute
2113 || cmdidx == CMD_echo
2114 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002115 || cmdidx == CMD_echomsg
2116 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002117 && xp->xp_context == EXPAND_EXPRESSION)
2118 {
2119 for (;;)
2120 {
2121 char_u *n = skiptowhite(arg);
2122
2123 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2124 break;
2125 arg = skipwhite(n);
2126 }
2127 }
2128
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 xp->xp_pattern = arg;
2130}
2131
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002133 * Return TRUE if "pat" matches "text".
2134 * Does not use 'cpo' and always uses 'magic'.
2135 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002136 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002137pattern_match(char_u *pat, char_u *text, int ic)
2138{
2139 int matches = FALSE;
2140 char_u *save_cpo;
2141 regmatch_T regmatch;
2142
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002143 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002144 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002145 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002146 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2147 if (regmatch.regprog != NULL)
2148 {
2149 regmatch.rm_ic = ic;
2150 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2151 vim_regfree(regmatch.regprog);
2152 }
2153 p_cpo = save_cpo;
2154 return matches;
2155}
2156
2157/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002158 * Handle a name followed by "(". Both for just "name(arg)" and for
2159 * "expr->name(arg)".
2160 * Returns OK or FAIL.
2161 */
2162 static int
2163eval_func(
2164 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002165 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002166 char_u *name,
2167 int name_len,
2168 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002169 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002170 typval_T *basetv) // "expr" for "expr->name(arg)"
2171{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002172 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002173 char_u *s = name;
2174 int len = name_len;
2175 partial_T *partial;
2176 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002177 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002178 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002179
2180 if (!evaluate)
2181 check_vars(s, len);
2182
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002183 // If "s" is the name of a variable of type VAR_FUNC
2184 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002185 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002186 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002187
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002188 // Need to make a copy, in case evaluating the arguments makes
2189 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002190 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002191 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002192 ret = FAIL;
2193 else
2194 {
2195 funcexe_T funcexe;
2196
2197 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002198 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002199 funcexe.fe_firstline = curwin->w_cursor.lnum;
2200 funcexe.fe_lastline = curwin->w_cursor.lnum;
2201 funcexe.fe_evaluate = evaluate;
2202 funcexe.fe_partial = partial;
2203 funcexe.fe_basetv = basetv;
2204 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002205 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002206 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002207 }
2208 vim_free(s);
2209
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002210 // If evaluate is FALSE rettv->v_type was not set in
2211 // get_func_tv, but it's needed in handle_subscript() to parse
2212 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002213 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2214 {
2215 rettv->vval.v_string = NULL;
2216 rettv->v_type = VAR_FUNC;
2217 }
2218
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002219 // Stop the expression evaluation when immediately
2220 // aborting on error, or when an interrupt occurred or
2221 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002222 if (evaluate && aborting())
2223 {
2224 if (ret == OK)
2225 clear_tv(rettv);
2226 ret = FAIL;
2227 }
2228 return ret;
2229}
2230
2231/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002232 * After a NL, skip over empty lines and comment-only lines.
2233 */
2234 static char_u *
2235newline_skip_comments(char_u *arg)
2236{
2237 char_u *p = arg + 1;
2238
2239 for (;;)
2240 {
2241 p = skipwhite(p);
2242
2243 if (*p == NUL)
2244 break;
2245 if (vim9_comment_start(p))
2246 {
2247 char_u *nl = vim_strchr(p, NL);
2248
2249 if (nl == NULL)
2250 break;
2251 p = nl;
2252 }
2253 if (*p != NL)
2254 break;
2255 ++p; // skip another NL
2256 }
2257 return p;
2258}
2259
2260/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002261 * Get the next line source line without advancing. But do skip over comment
2262 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002263 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002264 */
2265 static char_u *
2266getline_peek_skip_comments(evalarg_T *evalarg)
2267{
2268 for (;;)
2269 {
2270 char_u *next = getline_peek(evalarg->eval_getline,
2271 evalarg->eval_cookie);
2272 char_u *p;
2273
2274 if (next == NULL)
2275 break;
2276 p = skipwhite(next);
2277 if (*p != NUL && !vim9_comment_start(p))
2278 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002279 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002280 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002281 }
2282 return NULL;
2283}
2284
2285/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002286 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2287 * comment) and there is a next line, return the next line (skipping blanks)
2288 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002289 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2290 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002291 * "arg" must point somewhere inside a line, not at the start.
2292 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002293 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002294eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2295{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002296 char_u *p = skipwhite(arg);
2297
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002298 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002299 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002300 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002301 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2302 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002303 && (*p == NUL || *p == NL
2304 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002305 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002306 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002307
Bram Moolenaare442d592022-05-05 12:20:28 +01002308 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002309 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002310 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002311 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002312 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002313 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002314
Bram Moolenaar9d489562020-07-30 20:08:50 +02002315 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002316 {
2317 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002318 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002319 }
2320 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002321 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002322}
2323
2324/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002325 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002326 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002327 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002328 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002329eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002330{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002331 garray_T *gap = &evalarg->eval_ga;
2332 char_u *line;
2333
Bram Moolenaar39be4982022-05-06 21:51:50 +01002334 if (arg != NULL)
2335 {
2336 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002337 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002338 // Truncate before a trailing comment, so that concatenating the lines
2339 // won't turn the rest into a comment.
2340 if (*skipwhite(arg) == '#')
2341 *arg = NUL;
2342 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002343
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002344 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002345 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2346 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002347 else
2348 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002349 if (line == NULL)
2350 return NULL;
2351
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002352 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002353 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2354 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002355 char_u *p = skipwhite(line);
2356
2357 // Going to concatenate the lines after parsing. For an empty or
2358 // comment line use an empty string.
2359 if (*p == NUL || vim9_comment_start(p))
2360 {
2361 vim_free(line);
2362 line = vim_strsave((char_u *)"");
2363 }
2364
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002365 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2366 ++gap->ga_len;
2367 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002368 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002369 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002370 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002371 evalarg->eval_tofree = line;
2372 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002373
2374 // Advanced to the next line, "arg" no longer points into the previous
2375 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002376 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002377 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002378}
2379
Bram Moolenaare6b53242020-07-01 17:28:33 +02002380/*
2381 * Call eval_next_non_blank() and get the next line if needed.
2382 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002383 char_u *
2384skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2385{
2386 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002387 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002388
Bram Moolenaar962d7212020-07-04 14:15:00 +02002389 if (evalarg == NULL)
2390 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002391 eval_next_non_blank(p, evalarg, &getnext);
2392 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002393 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002394 return p;
2395}
2396
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002397/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002399 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2401 */
2402
2403/*
2404 * Handle zero level expression.
2405 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002406 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002407 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002408 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 * Return OK or FAIL.
2410 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002411 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002412eval0(
2413 char_u *arg,
2414 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002415 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002416 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002418 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2419}
2420
2421/*
2422 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2423 * expression and don't check what comes after the expression.
2424 */
2425 int
2426eval0_retarg(
2427 char_u *arg,
2428 typval_T *rettv,
2429 exarg_T *eap,
2430 evalarg_T *evalarg,
2431 char_u **retarg)
2432{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 int ret;
2434 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002435 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002436 int did_emsg_before = did_emsg;
2437 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002438 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002439 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002440 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441
2442 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002443 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002444
Bram Moolenaar79481362022-06-27 20:15:10 +01002445 if (ret != FAIL)
2446 {
2447 expr_end = p;
2448 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002449
Bram Moolenaar79481362022-06-27 20:15:10 +01002450 // In Vim9 script a command block is not split at NL characters for
2451 // commands using an expression argument. Skip over a '#' comment to
2452 // check for a following NL. Require white space before the '#'.
2453 if (in_vim9script() && p > expr_end && retarg == NULL)
2454 while (*p == '#')
2455 {
2456 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002457
Bram Moolenaar79481362022-06-27 20:15:10 +01002458 if (nl == NULL)
2459 break;
2460 p = skipwhite(nl + 1);
2461 if (eap != NULL && *p != NUL)
2462 eap->nextcmd = p;
2463 check_for_end = FALSE;
2464 }
2465
2466 if (check_for_end)
2467 end_error = !ends_excmd2(arg, p);
2468 }
2469
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002470 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 {
2472 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002473 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 /*
2475 * Report the invalid expression unless the expression evaluation has
2476 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002477 * exception, or we already gave a more specific error.
2478 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002480 if (!aborting()
2481 && did_emsg == did_emsg_before
2482 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002483 && (flags & EVAL_CONSTANT) == 0
2484 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002485 {
2486 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002487 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002488 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002489 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002490 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002491
2492 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002493 // a next command to avoid more errors, unless "|" is following, which
2494 // could only be a command separator.
Bram Moolenaar79481362022-06-27 20:15:10 +01002495 if (eap != NULL && p != NULL
2496 && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
Bram Moolenaar8143a532020-12-13 20:26:29 +01002497 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002498 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002500
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002501 if (retarg != NULL)
2502 *retarg = p;
2503 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002504 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002505
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 return ret;
2507}
2508
2509/*
2510 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002511 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002512 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 *
2514 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002515 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002517 * Note: "rettv.v_lock" is not set.
2518 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 * Return OK or FAIL.
2520 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002521 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002522eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002524 char_u *p;
2525 int getnext;
2526
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002527 CLEAR_POINTER(rettv);
2528
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 /*
2530 * Get the first variable.
2531 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002532 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 return FAIL;
2534
Bram Moolenaar793648f2020-06-26 21:28:25 +02002535 p = eval_next_non_blank(*arg, evalarg, &getnext);
2536 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002538 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002539 int result;
2540 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002541 evalarg_T *evalarg_used = evalarg;
2542 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002543 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002544 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002545 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002546
2547 if (evalarg == NULL)
2548 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002549 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002550 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002551 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002552 orig_flags = evalarg_used->eval_flags;
2553 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002554
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002555 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002556 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002557 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002558 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002559 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002560 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002561 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002562 clear_tv(rettv);
2563 return FAIL;
2564 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002565 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002566 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002567
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002569 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002571 int error = FALSE;
2572
Bram Moolenaar13106602020-10-04 16:06:05 +02002573 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002574 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002575 else if (vim9script)
2576 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002577 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002579 if (error || !op_falsy || !result)
2580 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002581 if (error)
2582 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 }
2584
2585 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002586 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002588 if (op_falsy)
2589 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002590 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002591 {
mityu4ac198c2021-05-28 17:52:40 +02002592 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002593 clear_tv(rettv);
2594 return FAIL;
2595 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002596 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002597 evalarg_used->eval_flags = (op_falsy ? !result : result)
2598 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2599 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002600 {
2601 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002603 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002604 if (!op_falsy || !result)
2605 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002607 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002609 /*
2610 * Check for the ":".
2611 */
2612 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2613 if (*p != ':')
2614 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002615 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002616 if (evaluate && result)
2617 clear_tv(rettv);
2618 evalarg_used->eval_flags = orig_flags;
2619 return FAIL;
2620 }
2621 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002622 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002623 else
2624 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002625 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002626 {
2627 error_white_both(p, 1);
2628 clear_tv(rettv);
2629 evalarg_used->eval_flags = orig_flags;
2630 return FAIL;
2631 }
2632 *arg = p;
2633 }
2634
2635 /*
2636 * Get the third variable. Recursive!
2637 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002638 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002639 {
mityu4ac198c2021-05-28 17:52:40 +02002640 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002641 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002642 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002643 return FAIL;
2644 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002645 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2646 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002647 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002648 if (eval1(arg, &var2, evalarg_used) == FAIL)
2649 {
2650 if (evaluate && result)
2651 clear_tv(rettv);
2652 evalarg_used->eval_flags = orig_flags;
2653 return FAIL;
2654 }
2655 if (evaluate && !result)
2656 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002658
2659 if (evalarg == NULL)
2660 clear_evalarg(&local_evalarg, NULL);
2661 else
2662 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 }
2664
2665 return OK;
2666}
2667
2668/*
2669 * Handle first level expression:
2670 * expr2 || expr2 || expr2 logical OR
2671 *
2672 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002673 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 *
2675 * Return OK or FAIL.
2676 */
2677 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002678eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002680 char_u *p;
2681 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682
2683 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002684 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002686 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 return FAIL;
2688
2689 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002690 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002692 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002693 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002695 evalarg_T *evalarg_used = evalarg;
2696 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002697 int evaluate;
2698 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002699 long result = FALSE;
2700 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002701 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002702 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002703
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002704 if (evalarg == NULL)
2705 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002706 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002707 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002708 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002709 orig_flags = evalarg_used->eval_flags;
2710 evaluate = orig_flags & EVAL_EVALUATE;
2711 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002712 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002713 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002714 result = tv_get_bool_chk(rettv, &error);
2715 else if (tv_get_number_chk(rettv, &error) != 0)
2716 result = TRUE;
2717 clear_tv(rettv);
2718 if (error)
2719 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 }
2721
2722 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002723 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002725 while (p[0] == '|' && p[1] == '|')
2726 {
2727 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002728 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002729 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002730 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002731 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002732 {
2733 error_white_both(p, 2);
2734 clear_tv(rettv);
2735 return FAIL;
2736 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002737 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002738 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002739
2740 /*
2741 * Get the second variable.
2742 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002743 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002744 {
mityu4ac198c2021-05-28 17:52:40 +02002745 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002746 clear_tv(rettv);
2747 return FAIL;
2748 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002749 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2750 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002751 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002752 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002753 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002754
2755 /*
2756 * Compute the result.
2757 */
2758 if (evaluate && !result)
2759 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002760 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002761 result = tv_get_bool_chk(&var2, &error);
2762 else if (tv_get_number_chk(&var2, &error) != 0)
2763 result = TRUE;
2764 clear_tv(&var2);
2765 if (error)
2766 return FAIL;
2767 }
2768 if (evaluate)
2769 {
2770 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002771 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002772 rettv->v_type = VAR_BOOL;
2773 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002774 }
2775 else
2776 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002777 rettv->v_type = VAR_NUMBER;
2778 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002779 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002780 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002781
2782 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002784
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002785 if (evalarg == NULL)
2786 clear_evalarg(&local_evalarg, NULL);
2787 else
2788 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 }
2790
2791 return OK;
2792}
2793
2794/*
2795 * Handle second level expression:
2796 * expr3 && expr3 && expr3 logical AND
2797 *
2798 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002799 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 *
2801 * Return OK or FAIL.
2802 */
2803 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002804eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002806 char_u *p;
2807 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808
2809 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002810 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002812 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 return FAIL;
2814
2815 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002816 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002818 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002819 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002821 evalarg_T *evalarg_used = evalarg;
2822 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002823 int orig_flags;
2824 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002825 long result = TRUE;
2826 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002827 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002828 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002829
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002830 if (evalarg == NULL)
2831 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002832 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002833 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002834 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002835 orig_flags = evalarg_used->eval_flags;
2836 evaluate = orig_flags & EVAL_EVALUATE;
2837 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002838 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002839 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002840 result = tv_get_bool_chk(rettv, &error);
2841 else if (tv_get_number_chk(rettv, &error) == 0)
2842 result = FALSE;
2843 clear_tv(rettv);
2844 if (error)
2845 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 }
2847
2848 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002849 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002851 while (p[0] == '&' && p[1] == '&')
2852 {
2853 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002854 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002855 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002856 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002857 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002858 {
2859 error_white_both(p, 2);
2860 clear_tv(rettv);
2861 return FAIL;
2862 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002863 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002864 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002865
2866 /*
2867 * Get the second variable.
2868 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002869 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002870 {
mityu4ac198c2021-05-28 17:52:40 +02002871 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002872 clear_tv(rettv);
2873 return FAIL;
2874 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002875 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2876 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002877 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002878 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002879 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002880 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002881
2882 /*
2883 * Compute the result.
2884 */
2885 if (evaluate && result)
2886 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002887 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002888 result = tv_get_bool_chk(&var2, &error);
2889 else if (tv_get_number_chk(&var2, &error) == 0)
2890 result = FALSE;
2891 clear_tv(&var2);
2892 if (error)
2893 return FAIL;
2894 }
2895 if (evaluate)
2896 {
2897 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002898 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002899 rettv->v_type = VAR_BOOL;
2900 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002901 }
2902 else
2903 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002904 rettv->v_type = VAR_NUMBER;
2905 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002906 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002907 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002908
2909 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002911
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002912 if (evalarg == NULL)
2913 clear_evalarg(&local_evalarg, NULL);
2914 else
2915 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 }
2917
2918 return OK;
2919}
2920
2921/*
2922 * Handle third level expression:
2923 * var1 == var2
2924 * var1 =~ var2
2925 * var1 != var2
2926 * var1 !~ var2
2927 * var1 > var2
2928 * var1 >= var2
2929 * var1 < var2
2930 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002931 * var1 is var2
2932 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 *
2934 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002935 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 *
2937 * Return OK or FAIL.
2938 */
2939 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002940eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002943 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002944 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002946 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947
2948 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002949 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002951 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 return FAIL;
2953
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002954 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002955
Bram Moolenaar696ba232020-07-29 21:20:41 +02002956 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957
2958 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002959 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002961 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002963 typval_T var2;
2964 int ic;
2965 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002966 int evaluate = evalarg == NULL
2967 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002968 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002969
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002970 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002971 {
Bram Moolenaare442d592022-05-05 12:20:28 +01002972 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002973 p = *arg;
2974 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002975 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2976 {
mityu4ac198c2021-05-28 17:52:40 +02002977 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002978 clear_tv(rettv);
2979 return FAIL;
2980 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002981
Bram Moolenaar696ba232020-07-29 21:20:41 +02002982 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2983 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002984 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002985 clear_tv(rettv);
2986 return FAIL;
2987 }
2988
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002989 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 if (p[len] == '?')
2991 {
2992 ic = TRUE;
2993 ++len;
2994 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002995 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 else if (p[len] == '#')
2997 {
2998 ic = FALSE;
2999 ++len;
3000 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003001 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003003 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004
3005 /*
3006 * Get the second variable.
3007 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003008 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3009 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003010 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003011 clear_tv(rettv);
3012 return FAIL;
3013 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003014 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003015 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003017 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 return FAIL;
3019 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003020 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003021 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003022 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003023
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003024 // use the line of the comparison for messages
3025 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003026 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003027 {
3028 ret = FAIL;
3029 clear_tv(rettv);
3030 }
3031 else
3032 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003033 clear_tv(&var2);
3034 return ret;
3035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 }
3037
3038 return OK;
3039}
3040
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003041/*
3042 * Make a copy of blob "tv1" and append blob "tv2".
3043 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 void
3045eval_addblob(typval_T *tv1, typval_T *tv2)
3046{
3047 blob_T *b1 = tv1->vval.v_blob;
3048 blob_T *b2 = tv2->vval.v_blob;
3049 blob_T *b = blob_alloc();
3050 int i;
3051
3052 if (b != NULL)
3053 {
3054 for (i = 0; i < blob_len(b1); i++)
3055 ga_append(&b->bv_ga, blob_get(b1, i));
3056 for (i = 0; i < blob_len(b2); i++)
3057 ga_append(&b->bv_ga, blob_get(b2, i));
3058
3059 clear_tv(tv1);
3060 rettv_blob_set(tv1, b);
3061 }
3062}
3063
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003064/*
3065 * Make a copy of list "tv1" and append list "tv2".
3066 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 int
3068eval_addlist(typval_T *tv1, typval_T *tv2)
3069{
3070 typval_T var3;
3071
3072 // concatenate Lists
3073 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3074 {
3075 clear_tv(tv1);
3076 clear_tv(tv2);
3077 return FAIL;
3078 }
3079 clear_tv(tv1);
3080 *tv1 = var3;
3081 return OK;
3082}
3083
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003085 * Handle the bitwise left/right shift operator expression:
3086 * var1 << var2
3087 * var1 >> var2
3088 *
3089 * "arg" must point to the first non-white of the expression.
3090 * "arg" is advanced to just after the recognized expression.
3091 *
3092 * Return OK or FAIL.
3093 */
3094 static int
3095eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3096{
3097 /*
3098 * Get the first expression.
3099 */
3100 if (eval6(arg, rettv, evalarg) == FAIL)
3101 return FAIL;
3102
3103 /*
3104 * Repeat computing, until no '<<' or '>>' is following.
3105 */
3106 for (;;)
3107 {
3108 char_u *p;
3109 int getnext;
3110 exprtype_T type;
3111 int evaluate;
3112 typval_T var2;
3113 int vim9script;
3114
3115 p = eval_next_non_blank(*arg, evalarg, &getnext);
3116 if (p[0] == '<' && p[1] == '<')
3117 type = EXPR_LSHIFT;
3118 else if (p[0] == '>' && p[1] == '>')
3119 type = EXPR_RSHIFT;
3120 else
3121 return OK;
3122
3123 // Handle a bitwise left or right shift operator
3124 if (rettv->v_type != VAR_NUMBER)
3125 {
3126 // left operand should be a number
3127 emsg(_(e_bitshift_ops_must_be_number));
3128 clear_tv(rettv);
3129 return FAIL;
3130 }
3131
3132 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3133 vim9script = in_vim9script();
3134 if (getnext)
3135 {
3136 *arg = eval_next_line(*arg, evalarg);
3137 p = *arg;
3138 }
3139 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3140 {
3141 error_white_both(*arg, 2);
3142 clear_tv(rettv);
3143 return FAIL;
3144 }
3145
3146 /*
3147 * Get the second variable.
3148 */
3149 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3150 {
3151 error_white_both(p, 2);
3152 clear_tv(rettv);
3153 return FAIL;
3154 }
3155 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3156 if (eval6(arg, &var2, evalarg) == FAIL)
3157 {
3158 clear_tv(rettv);
3159 return FAIL;
3160 }
3161
3162 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3163 {
3164 // right operand should be a positive number
3165 if (var2.v_type != VAR_NUMBER)
3166 emsg(_(e_bitshift_ops_must_be_number));
3167 else
3168 emsg(_(e_bitshift_ops_must_be_postive));
3169 clear_tv(rettv);
3170 clear_tv(&var2);
3171 return FAIL;
3172 }
3173
3174 if (evaluate)
3175 {
3176 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3177 // shifting more bits than we have always results in zero
3178 rettv->vval.v_number = 0;
3179 else if (type == EXPR_LSHIFT)
3180 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003181 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003182 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003183 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003184 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003185 }
3186
3187 clear_tv(&var2);
3188 }
3189
3190 return OK;
3191}
3192
3193/*
3194 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003195 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003197 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003198 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 *
3200 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003201 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 *
3203 * Return OK or FAIL.
3204 */
3205 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003206eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003209 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003211 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 return FAIL;
3213
3214 /*
3215 * Repeat computing, until no '+', '-' or '.' is following.
3216 */
3217 for (;;)
3218 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003219 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003220 int getnext;
3221 char_u *p;
3222 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003223 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003224 int concat;
3225 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003226 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003227
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003228 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003229 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003230 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003231 p = eval_next_non_blank(*arg, evalarg, &getnext);
3232 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003233 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003234 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3235 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003237 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3238 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003239
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003240 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003241 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003242 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003243 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003244 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003245 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003246 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003247 {
mityu4ac198c2021-05-28 17:52:40 +02003248 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003249 clear_tv(rettv);
3250 return FAIL;
3251 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003252 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003253 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003254 if ((op != '+' || (rettv->v_type != VAR_LIST
3255 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003256#ifdef FEAT_FLOAT
3257 && (op == '.' || rettv->v_type != VAR_FLOAT)
3258#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003259 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003260 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003261 int error = FALSE;
3262
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003263 // For "list + ...", an illegal use of the first operand as
3264 // a number cannot be determined before evaluating the 2nd
3265 // operand: if this is also a list, all is ok.
3266 // For "something . ...", "something - ..." or "non-list + ...",
3267 // we know that the first operand needs to be a string or number
3268 // without evaluating the 2nd operand. So check before to avoid
3269 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003270 if (op != '.')
3271 tv_get_number_chk(rettv, &error);
3272 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003273 {
3274 clear_tv(rettv);
3275 return FAIL;
3276 }
3277 }
3278
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 /*
3280 * Get the second variable.
3281 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003282 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003283 {
mityu89dcb4d2021-05-28 13:50:17 +02003284 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003285 clear_tv(rettv);
3286 return FAIL;
3287 }
3288 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003289 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003291 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 return FAIL;
3293 }
3294
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003295 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 {
3297 /*
3298 * Compute the result.
3299 */
3300 if (op == '.')
3301 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003302 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3303 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003304 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003305
Bram Moolenaar2e086612020-08-25 22:37:48 +02003306 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003307 || var2.v_type == VAR_CHANNEL
3308 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003309 semsg(_(e_using_invalid_value_as_string_str),
3310 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003311#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003312 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003313 {
3314 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3315 var2.vval.v_float);
3316 s2 = buf2;
3317 }
3318#endif
3319 else
3320 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003321 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003322 {
3323 clear_tv(rettv);
3324 clear_tv(&var2);
3325 return FAIL;
3326 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003327 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003328 clear_tv(rettv);
3329 rettv->v_type = VAR_STRING;
3330 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003332 else if (op == '+' && rettv->v_type == VAR_BLOB
3333 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003334 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003335 else if (op == '+' && rettv->v_type == VAR_LIST
3336 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003337 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003338 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003339 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 else
3342 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003343 int error = FALSE;
3344 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003345#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003346 float_T f1 = 0, f2 = 0;
3347
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003348 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003349 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003350 f1 = rettv->vval.v_float;
3351 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003352 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003353 else
3354#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003355 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003356 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003357 if (error)
3358 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003359 // This can only happen for "list + non-list" or
3360 // "blob + non-blob". For "non-list + ..." or
3361 // "something - ...", we returned before evaluating the
3362 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003363 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003364 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003365 return FAIL;
3366 }
3367#ifdef FEAT_FLOAT
3368 if (var2.v_type == VAR_FLOAT)
3369 f1 = n1;
3370#endif
3371 }
3372#ifdef FEAT_FLOAT
3373 if (var2.v_type == VAR_FLOAT)
3374 {
3375 f2 = var2.vval.v_float;
3376 n2 = 0;
3377 }
3378 else
3379#endif
3380 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003381 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003382 if (error)
3383 {
3384 clear_tv(rettv);
3385 clear_tv(&var2);
3386 return FAIL;
3387 }
3388#ifdef FEAT_FLOAT
3389 if (rettv->v_type == VAR_FLOAT)
3390 f2 = n2;
3391#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003392 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003393 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003394
3395#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003396 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003397 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3398 {
3399 if (op == '+')
3400 f1 = f1 + f2;
3401 else
3402 f1 = f1 - f2;
3403 rettv->v_type = VAR_FLOAT;
3404 rettv->vval.v_float = f1;
3405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003407#endif
3408 {
3409 if (op == '+')
3410 n1 = n1 + n2;
3411 else
3412 n1 = n1 - n2;
3413 rettv->v_type = VAR_NUMBER;
3414 rettv->vval.v_number = n1;
3415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003417 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 }
3419 }
3420 return OK;
3421}
3422
3423/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003424 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 * * number multiplication
3426 * / number division
3427 * % number modulo
3428 *
3429 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003430 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 *
3432 * Return OK or FAIL.
3433 */
3434 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003435eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003436 char_u **arg,
3437 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003438 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003439 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003441#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003442 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444
3445 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003446 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003448 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 return FAIL;
3450
3451 /*
3452 * Repeat computing, until no '*', '/' or '%' is following.
3453 */
3454 for (;;)
3455 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003456 int evaluate;
3457 int getnext;
3458 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003459 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003460 int op;
3461 varnumber_T n1, n2;
3462#ifdef FEAT_FLOAT
3463 float_T f1, f2;
3464#endif
3465 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003466
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003467 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003468 p = eval_next_non_blank(*arg, evalarg, &getnext);
3469 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003470 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003472
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003473 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003474 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003475 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003476 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003477 {
3478 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3479 {
mityu4ac198c2021-05-28 17:52:40 +02003480 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003481 clear_tv(rettv);
3482 return FAIL;
3483 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003484 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003487#ifdef FEAT_FLOAT
3488 f1 = 0;
3489 f2 = 0;
3490#endif
3491 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003492 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003494#ifdef FEAT_FLOAT
3495 if (rettv->v_type == VAR_FLOAT)
3496 {
3497 f1 = rettv->vval.v_float;
3498 use_float = TRUE;
3499 n1 = 0;
3500 }
3501 else
3502#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003503 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003504 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003505 if (error)
3506 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 }
3508 else
3509 n1 = 0;
3510
3511 /*
3512 * Get the second variable.
3513 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003514 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3515 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003516 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003517 clear_tv(rettv);
3518 return FAIL;
3519 }
3520 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003521 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 return FAIL;
3523
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003524 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003526#ifdef FEAT_FLOAT
3527 if (var2.v_type == VAR_FLOAT)
3528 {
3529 if (!use_float)
3530 {
3531 f1 = n1;
3532 use_float = TRUE;
3533 }
3534 f2 = var2.vval.v_float;
3535 n2 = 0;
3536 }
3537 else
3538#endif
3539 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003540 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003541 clear_tv(&var2);
3542 if (error)
3543 return FAIL;
3544#ifdef FEAT_FLOAT
3545 if (use_float)
3546 f2 = n2;
3547#endif
3548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549
3550 /*
3551 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003552 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003554#ifdef FEAT_FLOAT
3555 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003557 if (op == '*')
3558 f1 = f1 * f2;
3559 else if (op == '/')
3560 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003561# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003562 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003563 if (f2 == 0.0)
3564 {
3565 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003566 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003567 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003568 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003569 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003570 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003571 }
3572 else
3573 f1 = f1 / f2;
3574# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003575 // We rely on the floating point library to handle divide
3576 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003577 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003578# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003581 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003582 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003583 return FAIL;
3584 }
3585 rettv->v_type = VAR_FLOAT;
3586 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 }
3588 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003591 int failed = FALSE;
3592
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003593 if (op == '*')
3594 n1 = n1 * n2;
3595 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003596 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003598 n1 = num_modulus(n1, n2, &failed);
3599 if (failed)
3600 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003601
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003602 rettv->v_type = VAR_NUMBER;
3603 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 }
3606 }
3607
3608 return OK;
3609}
3610
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003611/*
3612 * Handle a type cast before a base level expression.
3613 * "arg" must point to the first non-white of the expression.
3614 * "arg" is advanced to just after the recognized expression.
3615 * Return OK or FAIL.
3616 */
3617 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003618eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003619 char_u **arg,
3620 typval_T *rettv,
3621 evalarg_T *evalarg,
3622 int want_string) // after "." operator
3623{
3624 type_T *want_type = NULL;
3625 garray_T type_list; // list of pointers to allocated types
3626 int res;
3627 int evaluate = evalarg == NULL ? 0
3628 : (evalarg->eval_flags & EVAL_EVALUATE);
3629
3630 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003631 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3632 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003633 {
3634 ++*arg;
3635 ga_init2(&type_list, sizeof(type_T *), 10);
3636 want_type = parse_type(arg, &type_list, TRUE);
3637 if (want_type == NULL && (evaluate || **arg != '>'))
3638 {
3639 clear_type_list(&type_list);
3640 return FAIL;
3641 }
3642
3643 if (**arg != '>')
3644 {
3645 if (*skipwhite(*arg) == '>')
3646 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3647 else
3648 emsg(_(e_missing_gt));
3649 clear_type_list(&type_list);
3650 return FAIL;
3651 }
3652 ++*arg;
3653 *arg = skipwhite_and_linebreak(*arg, evalarg);
3654 }
3655
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003656 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003657
3658 if (want_type != NULL && evaluate)
3659 {
3660 if (res == OK)
3661 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003662 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3663 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003664
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003665 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003666 {
3667 if (want_type == &t_bool && actual != &t_bool
3668 && (actual->tt_flags & TTFLAG_BOOL_OK))
3669 {
3670 int n = tv2bool(rettv);
3671
3672 // can use "0" and "1" for boolean in some places
3673 clear_tv(rettv);
3674 rettv->v_type = VAR_BOOL;
3675 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3676 }
3677 else
3678 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003679 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003680
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003681 where.wt_variable = TRUE;
3682 res = check_type(want_type, actual, TRUE, where);
3683 }
3684 }
3685 }
3686 clear_type_list(&type_list);
3687 }
3688
3689 return res;
3690}
3691
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003692 int
3693eval_leader(char_u **arg, int vim9)
3694{
3695 char_u *s = *arg;
3696 char_u *p = *arg;
3697
3698 while (*p == '!' || *p == '-' || *p == '+')
3699 {
3700 char_u *n = skipwhite(p + 1);
3701
3702 // ++, --, -+ and +- are not accepted in Vim9 script
3703 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3704 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003705 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003706 return FAIL;
3707 }
3708 p = n;
3709 }
3710 *arg = p;
3711 return OK;
3712}
3713
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003715 * Check for a predefined value "true", "false" and "null.*".
3716 * Return OK when recognized.
3717 */
3718 int
3719handle_predefined(char_u *s, int len, typval_T *rettv)
3720{
3721 switch (len)
3722 {
3723 case 4: if (STRNCMP(s, "true", 4) == 0)
3724 {
3725 rettv->v_type = VAR_BOOL;
3726 rettv->vval.v_number = VVAL_TRUE;
3727 return OK;
3728 }
3729 if (STRNCMP(s, "null", 4) == 0)
3730 {
3731 rettv->v_type = VAR_SPECIAL;
3732 rettv->vval.v_number = VVAL_NULL;
3733 return OK;
3734 }
3735 break;
3736 case 5: if (STRNCMP(s, "false", 5) == 0)
3737 {
3738 rettv->v_type = VAR_BOOL;
3739 rettv->vval.v_number = VVAL_FALSE;
3740 return OK;
3741 }
3742 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003743 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3744 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003745#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003746 rettv->v_type = VAR_JOB;
3747 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003748#else
3749 rettv->v_type = VAR_SPECIAL;
3750 rettv->vval.v_number = VVAL_NULL;
3751#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003752 return OK;
3753 }
3754 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003755 case 9:
3756 if (STRNCMP(s, "null_", 5) != 0)
3757 break;
3758 if (STRNCMP(s + 5, "list", 4) == 0)
3759 {
3760 rettv->v_type = VAR_LIST;
3761 rettv->vval.v_list = NULL;
3762 return OK;
3763 }
3764 if (STRNCMP(s + 5, "dict", 4) == 0)
3765 {
3766 rettv->v_type = VAR_DICT;
3767 rettv->vval.v_dict = NULL;
3768 return OK;
3769 }
3770 if (STRNCMP(s + 5, "blob", 4) == 0)
3771 {
3772 rettv->v_type = VAR_BLOB;
3773 rettv->vval.v_blob = NULL;
3774 return OK;
3775 }
3776 break;
3777 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3778 {
3779 rettv->v_type = VAR_STRING;
3780 rettv->vval.v_string = NULL;
3781 return OK;
3782 }
3783 break;
3784 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003785 if (STRNCMP(s, "null_channel", 12) == 0)
3786 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003787#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003788 rettv->v_type = VAR_CHANNEL;
3789 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003790#else
3791 rettv->v_type = VAR_SPECIAL;
3792 rettv->vval.v_number = VVAL_NULL;
3793#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003794 return OK;
3795 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003796 if (STRNCMP(s, "null_partial", 12) == 0)
3797 {
3798 rettv->v_type = VAR_PARTIAL;
3799 rettv->vval.v_partial = NULL;
3800 return OK;
3801 }
3802 break;
3803 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3804 {
3805 rettv->v_type = VAR_FUNC;
3806 rettv->vval.v_string = NULL;
3807 return OK;
3808 }
3809 break;
3810 }
3811 return FAIL;
3812}
3813
3814/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 * Handle sixth level expression:
3816 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003817 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003818 * "string" string constant
3819 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 * &option-name option value
3821 * @r register contents
3822 * identifier variable value
3823 * function() function call
3824 * $VAR environment variable
3825 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003826 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003827 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003828 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003829 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 *
3831 * Also handle:
3832 * ! in front logical NOT
3833 * - in front unary minus
3834 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003835 * trailing [] subscript in String or List
3836 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003837 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 *
3839 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003840 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 *
3842 * Return OK or FAIL.
3843 */
3844 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003845eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003846 char_u **arg,
3847 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003848 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003851 int evaluate = evalarg != NULL
3852 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 int len;
3854 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003855 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 char_u *start_leader, *end_leader;
3857 int ret = OK;
3858 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003859 static int recurse = 0;
3860 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861
3862 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003863 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003864 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003866 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867
3868 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003869 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 */
3871 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003872 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003873 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 end_leader = *arg;
3875
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003876 if (**arg == '.' && (!isdigit(*(*arg + 1))
3877#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003878 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003879#endif
3880 ))
3881 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003882 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003883 ++*arg;
3884 return FAIL;
3885 }
3886
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003887 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003888 // and crash. With MSVC the stack is smaller.
3889 if (recurse ==
3890#ifdef _MSC_VER
3891 300
3892#else
3893 1000
3894#endif
3895 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003896 {
3897 semsg(_(e_expression_too_recursive_str), *arg);
3898 return FAIL;
3899 }
3900 ++recurse;
3901
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 switch (**arg)
3903 {
3904 /*
3905 * Number constant.
3906 */
3907 case '0':
3908 case '1':
3909 case '2':
3910 case '3':
3911 case '4':
3912 case '5':
3913 case '6':
3914 case '7':
3915 case '8':
3916 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003917 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003918
3919 // Apply prefixed "-" and "+" now. Matters especially when
3920 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003921 if (ret == OK && evaluate && end_leader > start_leader
3922 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003923 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 break;
3925
3926 /*
3927 * String constant: "string".
3928 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003929 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 break;
3931
3932 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003933 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003935 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003936 break;
3937
3938 /*
3939 * List: [expr, expr]
3940 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003941 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 break;
3943
3944 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003945 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003946 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003947 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003948 {
3949 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3950 }
3951 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003952 {
3953 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003954 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003955 }
3956 else
3957 ret = NOTDONE;
3958 break;
3959
3960 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003961 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003962 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003963 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003964 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003965 ret = NOTDONE;
3966 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00003967 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003968 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003969 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003970 break;
3971
3972 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003973 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003975 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 break;
3977
3978 /*
3979 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01003980 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 */
LemonBoy2eaef102022-05-06 13:14:50 +01003982 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
3983 ret = eval_interp_string(arg, rettv, evaluate);
3984 else
3985 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 break;
3987
3988 /*
3989 * Register contents: @r.
3990 */
3991 case '@': ++*arg;
3992 if (evaluate)
3993 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003994 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003995 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003996 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003997 emsg_invreg(**arg);
3998 else
3999 {
4000 rettv->v_type = VAR_STRING;
4001 rettv->vval.v_string = get_reg_contents(**arg,
4002 GREG_EXPR_SRC);
4003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 }
4005 if (**arg != NUL)
4006 ++*arg;
4007 break;
4008
4009 /*
4010 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004011 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004013 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004014 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004015 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004016 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004017 if (ret == OK && evaluate)
4018 {
4019 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4020
Bram Moolenaara9931532021-06-12 15:58:16 +02004021 // Compile it here to get the return type. The return
4022 // type is optional, when it's missing use t_unknown.
4023 // This is recognized in compile_return().
4024 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4025 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004026 if (compile_def_function(ufunc, FALSE,
4027 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004028 {
4029 clear_tv(rettv);
4030 ret = FAIL;
4031 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004032 }
4033 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004034 if (ret == NOTDONE)
4035 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004036 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004037 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004038
Bram Moolenaar9215f012020-06-27 21:18:00 +02004039 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004040 if (**arg == ')')
4041 ++*arg;
4042 else if (ret == OK)
4043 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004044 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004045 clear_tv(rettv);
4046 ret = FAIL;
4047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 }
4049 break;
4050
Bram Moolenaar8c711452005-01-14 21:53:12 +00004051 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 break;
4053 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004054
4055 if (ret == NOTDONE)
4056 {
4057 /*
4058 * Must be a variable or function name.
4059 * Can also be a curly-braces kind of name: {expr}.
4060 */
4061 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004062 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004063 if (alias != NULL)
4064 s = alias;
4065
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004066 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004067 ret = FAIL;
4068 else
4069 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004070 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4071
Bram Moolenaar4525a572022-02-13 11:57:33 +00004072 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004073 {
4074 emsg(_(e_cannot_use_underscore_here));
4075 ret = FAIL;
4076 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004077 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004078 && s[0] == 's' && s[1] == ':')
4079 {
4080 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4081 ret = FAIL;
4082 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004083 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004084 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004085 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004086 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004087 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004088 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02004089 else if (flags & EVAL_CONSTANT)
4090 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004091 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004092 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004093 // get the value of "true", "false", etc. or a variable
4094 ret = FAIL;
4095 if (vim9script)
4096 ret = handle_predefined(s, len, rettv);
4097 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004098 {
4099 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004100 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004101 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004102 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004103 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004104 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004105 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004106 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004107 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004108 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004109 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004110 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004111 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004112 }
4113
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004114 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4115 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004116 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004117 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118
4119 /*
4120 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4121 */
4122 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004123 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004124
4125 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004126 return ret;
4127}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004128
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004129/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004130 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004131 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004132 * Adjusts "end_leaderp" until it is at "start_leader".
4133 */
4134 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004135eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004136 typval_T *rettv,
4137 int numeric_only,
4138 char_u *start_leader,
4139 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004140{
4141 char_u *end_leader = *end_leaderp;
4142 int ret = OK;
4143 int error = FALSE;
4144 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004145 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004146 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004147#ifdef FEAT_FLOAT
4148 float_T f = 0.0;
4149
4150 if (rettv->v_type == VAR_FLOAT)
4151 f = rettv->vval.v_float;
4152 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004153#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004154 {
4155 while (VIM_ISWHITE(end_leader[-1]))
4156 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004157 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004158 val = tv2bool(rettv);
4159 else
4160 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004161 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004162 if (error)
4163 {
4164 clear_tv(rettv);
4165 ret = FAIL;
4166 }
4167 else
4168 {
4169 while (end_leader > start_leader)
4170 {
4171 --end_leader;
4172 if (*end_leader == '!')
4173 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004174 if (numeric_only)
4175 {
4176 ++end_leader;
4177 break;
4178 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004179#ifdef FEAT_FLOAT
4180 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004181 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004182 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004183 {
4184 rettv->v_type = VAR_BOOL;
4185 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4186 }
4187 else
4188 f = !f;
4189 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004190 else
4191#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004192 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004193 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004194 type = VAR_BOOL;
4195 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004196 }
4197 else if (*end_leader == '-')
4198 {
4199#ifdef FEAT_FLOAT
4200 if (rettv->v_type == VAR_FLOAT)
4201 f = -f;
4202 else
4203#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004204 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004205 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004206 type = VAR_NUMBER;
4207 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004208 }
4209 }
4210#ifdef FEAT_FLOAT
4211 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004213 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004214 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004216 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004217#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004218 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004219 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004220 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004221 rettv->v_type = type;
4222 else
4223 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004224 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004227 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 return ret;
4229}
4230
4231/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004232 * Call the function referred to in "rettv".
4233 */
4234 static int
4235call_func_rettv(
4236 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004237 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004238 typval_T *rettv,
4239 int evaluate,
4240 dict_T *selfdict,
4241 typval_T *basetv)
4242{
4243 partial_T *pt = NULL;
4244 funcexe_T funcexe;
4245 typval_T functv;
4246 char_u *s;
4247 int ret;
4248
4249 // need to copy the funcref so that we can clear rettv
4250 if (evaluate)
4251 {
4252 functv = *rettv;
4253 rettv->v_type = VAR_UNKNOWN;
4254
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004255 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004256 if (functv.v_type == VAR_PARTIAL)
4257 {
4258 pt = functv.vval.v_partial;
4259 s = partial_name(pt);
4260 }
4261 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004262 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004263 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004264 if (s == NULL || *s == NUL)
4265 {
4266 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004267 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004268 goto theend;
4269 }
4270 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004271 }
4272 else
4273 s = (char_u *)"";
4274
Bram Moolenaara80faa82020-04-12 19:37:17 +02004275 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004276 funcexe.fe_firstline = curwin->w_cursor.lnum;
4277 funcexe.fe_lastline = curwin->w_cursor.lnum;
4278 funcexe.fe_evaluate = evaluate;
4279 funcexe.fe_partial = pt;
4280 funcexe.fe_selfdict = selfdict;
4281 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004282 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004283
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004284theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004285 // Clear the funcref afterwards, so that deleting it while
4286 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004287 if (evaluate)
4288 clear_tv(&functv);
4289
4290 return ret;
4291}
4292
4293/*
4294 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004295 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004296 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4297 */
4298 static int
4299eval_lambda(
4300 char_u **arg,
4301 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004302 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004303 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004304{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004305 int evaluate = evalarg != NULL
4306 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004307 typval_T base = *rettv;
4308 int ret;
4309
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004310 rettv->v_type = VAR_UNKNOWN;
4311
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004312 if (**arg == '{')
4313 {
4314 // ->{lambda}()
4315 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4316 }
4317 else
4318 {
4319 // ->(lambda)()
4320 ++*arg;
4321 ret = eval1(arg, rettv, evalarg);
4322 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004323 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004324 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004325 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004326 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004327 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004328 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4329 && rettv->v_type != VAR_PARTIAL)
4330 {
4331 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4332 return FAIL;
4333 }
4334 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004335 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004336 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004337 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004338
4339 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004340 {
4341 if (verbose)
4342 {
4343 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004344 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004345 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004346 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004347 }
4348 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004349 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004350 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004351 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004352 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004353
4354 // Clear the funcref afterwards, so that deleting it while
4355 // evaluating the arguments is possible (see test55).
4356 if (evaluate)
4357 clear_tv(&base);
4358
4359 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004360}
4361
4362/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004363 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004364 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004365 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4366 */
4367 static int
4368eval_method(
4369 char_u **arg,
4370 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004371 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004372 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004373{
4374 char_u *name;
4375 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004376 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004377 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004378 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004379 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004380 int evaluate = evalarg != NULL
4381 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004382
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004383 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004384
Bram Moolenaarac92e252019-08-03 21:58:38 +02004385 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004386 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004387 if (alias != NULL)
4388 name = alias;
4389
4390 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004391 {
4392 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004393 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004394 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004395 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004396 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004397 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004398 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004399
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004400 // If there is no "(" immediately following, but there is further on,
4401 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4402 // Does not handle anything where "(" is part of the expression.
4403 *arg = skipwhite(*arg);
4404
4405 if (**arg != '(' && alias == NULL
4406 && (paren = vim_strchr(*arg, '(')) != NULL)
4407 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004408 char_u *deref;
4409
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004410 *arg = name;
4411 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004412 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4413 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004414 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004415 *arg = name + len;
4416 ret = FAIL;
4417 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004418 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004419 {
4420 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004421 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004422 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004423 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004424 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004425
4426 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004427 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004428 *arg = skipwhite(*arg);
4429
4430 if (**arg != '(')
4431 {
4432 if (verbose)
4433 semsg(_(e_missing_parenthesis_str), name);
4434 ret = FAIL;
4435 }
4436 else if (VIM_ISWHITE((*arg)[-1]))
4437 {
4438 if (verbose)
4439 emsg(_(e_no_white_space_allowed_before_parenthesis));
4440 ret = FAIL;
4441 }
4442 else
4443 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004444 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004445 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004446 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004447
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004448 // Clear the funcref afterwards, so that deleting it while
4449 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004450 if (evaluate)
4451 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004452 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004453
Bram Moolenaarac92e252019-08-03 21:58:38 +02004454 return ret;
4455}
4456
4457/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004458 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4459 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004460 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4461 */
4462 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004463eval_index(
4464 char_u **arg,
4465 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004466 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004467 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004468{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004469 int evaluate = evalarg != NULL
4470 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004471 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004472 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004473 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004474 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004475 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004476 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004477
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004478 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4479 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004480
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004481 init_tv(&var1);
4482 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004483 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004484 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004485 /*
4486 * dict.name
4487 */
4488 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004489 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004490 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004491 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004492 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004493 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004494 }
4495 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004496 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004497 /*
4498 * something[idx]
4499 *
4500 * Get the (first) variable from inside the [].
4501 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004502 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004503 if (**arg == ':')
4504 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004505 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004506 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004507 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004508 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004509 semsg(_(e_white_space_required_before_and_after_str_at_str),
4510 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004511 clear_tv(&var1);
4512 return FAIL;
4513 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004514 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004515 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004516 int error = FALSE;
4517
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004518#ifdef FEAT_FLOAT
4519 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004520 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004521 && var1.v_type == VAR_FLOAT)
4522 {
4523 var1.vval.v_string = typval_tostring(&var1, TRUE);
4524 var1.v_type = VAR_STRING;
4525 }
4526#endif
Bram Moolenaar4525a572022-02-13 11:57:33 +00004527 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004528 tv_get_number_chk(&var1, &error);
4529 else
4530 error = tv_get_string_chk(&var1) == NULL;
4531 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004532 {
4533 // not a number or string
4534 clear_tv(&var1);
4535 return FAIL;
4536 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004537 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004538
4539 /*
4540 * Get the second variable from inside the [:].
4541 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004542 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004543 if (**arg == ':')
4544 {
4545 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004546 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004547 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004548 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004549 semsg(_(e_white_space_required_before_and_after_str_at_str),
4550 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004551 if (!empty1)
4552 clear_tv(&var1);
4553 return FAIL;
4554 }
4555 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004556 if (**arg == ']')
4557 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004558 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004559 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004560 if (!empty1)
4561 clear_tv(&var1);
4562 return FAIL;
4563 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004564 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004565 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004566 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004567 if (!empty1)
4568 clear_tv(&var1);
4569 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004570 return FAIL;
4571 }
4572 }
4573
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004574 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004575 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004576 if (**arg != ']')
4577 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004578 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004579 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004580 clear_tv(&var1);
4581 if (range)
4582 clear_tv(&var2);
4583 return FAIL;
4584 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004585 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004586 }
4587
4588 if (evaluate)
4589 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004590 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004591 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004592 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004593
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004594 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004595 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004596 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004597 clear_tv(&var2);
4598 return res;
4599 }
4600 return OK;
4601}
4602
4603/*
4604 * Check if "rettv" can have an [index] or [sli:ce]
4605 */
4606 int
4607check_can_index(typval_T *rettv, int evaluate, int verbose)
4608{
4609 switch (rettv->v_type)
4610 {
4611 case VAR_FUNC:
4612 case VAR_PARTIAL:
4613 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004614 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004615 return FAIL;
4616 case VAR_FLOAT:
4617#ifdef FEAT_FLOAT
4618 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004619 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004620 return FAIL;
4621#endif
4622 case VAR_BOOL:
4623 case VAR_SPECIAL:
4624 case VAR_JOB:
4625 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004626 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004627 if (verbose)
4628 emsg(_(e_cannot_index_special_variable));
4629 return FAIL;
4630 case VAR_UNKNOWN:
4631 case VAR_ANY:
4632 case VAR_VOID:
4633 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004634 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004635 emsg(_(e_cannot_index_special_variable));
4636 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004637 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004638 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004639
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004640 case VAR_STRING:
4641 case VAR_LIST:
4642 case VAR_DICT:
4643 case VAR_BLOB:
4644 break;
4645 case VAR_NUMBER:
4646 if (in_vim9script())
4647 emsg(_(e_cannot_index_number));
4648 break;
4649 }
4650 return OK;
4651}
4652
4653/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004654 * slice() function
4655 */
4656 void
4657f_slice(typval_T *argvars, typval_T *rettv)
4658{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004659 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004660 && ((argvars[0].v_type != VAR_STRING
4661 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004662 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004663 && check_for_list_arg(argvars, 0) == FAIL)
4664 || check_for_number_arg(argvars, 1) == FAIL
4665 || check_for_opt_number_arg(argvars, 2) == FAIL))
4666 return;
4667
Bram Moolenaar6601b622021-01-13 21:47:15 +01004668 if (check_can_index(argvars, TRUE, FALSE) == OK)
4669 {
4670 copy_tv(argvars, rettv);
4671 eval_index_inner(rettv, TRUE, argvars + 1,
4672 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4673 TRUE, NULL, 0, FALSE);
4674 }
4675}
4676
4677/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004678 * Apply index or range to "rettv".
4679 * "var1" is the first index, NULL for [:expr].
4680 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004681 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4682 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004683 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4684 */
4685 int
4686eval_index_inner(
4687 typval_T *rettv,
4688 int is_range,
4689 typval_T *var1,
4690 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004691 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004692 char_u *key,
4693 int keylen,
4694 int verbose)
4695{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004696 varnumber_T n1, n2 = 0;
4697 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004698
4699 n1 = 0;
4700 if (var1 != NULL && rettv->v_type != VAR_DICT)
4701 n1 = tv_get_number(var1);
4702
4703 if (is_range)
4704 {
4705 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004706 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004707 if (verbose)
4708 emsg(_(e_cannot_slice_dictionary));
4709 return FAIL;
4710 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004711 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004712 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004713 else
4714 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004715 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004716
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004717 switch (rettv->v_type)
4718 {
4719 case VAR_UNKNOWN:
4720 case VAR_ANY:
4721 case VAR_VOID:
4722 case VAR_FUNC:
4723 case VAR_PARTIAL:
4724 case VAR_FLOAT:
4725 case VAR_BOOL:
4726 case VAR_SPECIAL:
4727 case VAR_JOB:
4728 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004729 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004730 break; // not evaluating, skipping over subscript
4731
4732 case VAR_NUMBER:
4733 case VAR_STRING:
4734 {
4735 char_u *s = tv_get_string(rettv);
4736
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004737 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004738 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004739 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004740 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004741 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004742 else
4743 s = char_from_string(s, n1);
4744 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004745 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004746 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004747 // The resulting variable is a substring. If the indexes
4748 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004749 if (n1 < 0)
4750 {
4751 n1 = len + n1;
4752 if (n1 < 0)
4753 n1 = 0;
4754 }
4755 if (n2 < 0)
4756 n2 = len + n2;
4757 else if (n2 >= len)
4758 n2 = len;
4759 if (n1 >= len || n2 < 0 || n1 > n2)
4760 s = NULL;
4761 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004762 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004763 }
4764 else
4765 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004766 // The resulting variable is a string of a single
4767 // character. If the index is too big or negative the
4768 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004769 if (n1 >= len || n1 < 0)
4770 s = NULL;
4771 else
4772 s = vim_strnsave(s + n1, 1);
4773 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004774 clear_tv(rettv);
4775 rettv->v_type = VAR_STRING;
4776 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004777 }
4778 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004779
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004780 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004781 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4782 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004783 break;
4784
4785 case VAR_LIST:
4786 if (var1 == NULL)
4787 n1 = 0;
4788 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004789 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004790 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004791 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004792 return FAIL;
4793 break;
4794
4795 case VAR_DICT:
4796 {
4797 dictitem_T *item;
4798 typval_T tmp;
4799
4800 if (key == NULL)
4801 {
4802 key = tv_get_string_chk(var1);
4803 if (key == NULL)
4804 return FAIL;
4805 }
4806
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004807 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004808
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004809 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004810 {
4811 if (verbose)
4812 {
4813 if (keylen > 0)
4814 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004815 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004816 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004817 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004818 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004819
4820 copy_tv(&item->di_tv, &tmp);
4821 clear_tv(rettv);
4822 *rettv = tmp;
4823 }
4824 break;
4825 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004826 return OK;
4827}
4828
4829/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004830 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004831 */
4832 char_u *
4833partial_name(partial_T *pt)
4834{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004835 if (pt != NULL)
4836 {
4837 if (pt->pt_name != NULL)
4838 return pt->pt_name;
4839 if (pt->pt_func != NULL)
4840 return pt->pt_func->uf_name;
4841 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004842 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004843}
4844
Bram Moolenaarddecc252016-04-06 22:59:37 +02004845 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004846partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004847{
4848 int i;
4849
4850 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004851 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004852 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004853 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004854 if (pt->pt_name != NULL)
4855 {
4856 func_unref(pt->pt_name);
4857 vim_free(pt->pt_name);
4858 }
4859 else
4860 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004861
Bram Moolenaar54656012021-06-09 20:50:46 +02004862 // "out_up" is no longer used, decrement refcount on partial that owns it.
4863 partial_unref(pt->pt_outer.out_up_partial);
4864
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004865 // Using pt_outer from another partial.
4866 partial_unref(pt->pt_outer_partial);
4867
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004868 // Decrease the reference count for the context of a closure. If down
4869 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004870 if (pt->pt_funcstack != NULL)
4871 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004872 --pt->pt_funcstack->fs_refcount;
4873 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004874 }
4875
Bram Moolenaarddecc252016-04-06 22:59:37 +02004876 vim_free(pt);
4877}
4878
4879/*
4880 * Unreference a closure: decrement the reference count and free it when it
4881 * becomes zero.
4882 */
4883 void
4884partial_unref(partial_T *pt)
4885{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004886 if (pt != NULL)
4887 {
4888 if (--pt->pt_refcount <= 0)
4889 partial_free(pt);
4890
4891 // If the reference count goes down to one, the funcstack may be the
4892 // only reference and can be freed if no other partials reference it.
4893 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4894 funcstack_check_refcount(pt->pt_funcstack);
4895 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004896}
4897
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004898/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004899 * Return the next (unique) copy ID.
4900 * Used for serializing nested structures.
4901 */
4902 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004903get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004904{
4905 current_copyID += COPYID_INC;
4906 return current_copyID;
4907}
4908
4909/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004910 * Garbage collection for lists and dictionaries.
4911 *
4912 * We use reference counts to be able to free most items right away when they
4913 * are no longer used. But for composite items it's possible that it becomes
4914 * unused while the reference count is > 0: When there is a recursive
4915 * reference. Example:
4916 * :let l = [1, 2, 3]
4917 * :let d = {9: l}
4918 * :let l[1] = d
4919 *
4920 * Since this is quite unusual we handle this with garbage collection: every
4921 * once in a while find out which lists and dicts are not referenced from any
4922 * variable.
4923 *
4924 * Here is a good reference text about garbage collection (refers to Python
4925 * but it applies to all reference-counting mechanisms):
4926 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004927 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004928
4929/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004930 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004931 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004932 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004933 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004934 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004935garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004936{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004937 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004938 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004939 buf_T *buf;
4940 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004941 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004942 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004943
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004944 if (!testing)
4945 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004946 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004947 want_garbage_collect = FALSE;
4948 may_garbage_collect = FALSE;
4949 garbage_collect_at_exit = FALSE;
4950 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004951
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004952 // The execution stack can grow big, limit the size.
4953 if (exestack.ga_maxlen - exestack.ga_len > 500)
4954 {
4955 size_t new_len;
4956 char_u *pp;
4957 int n;
4958
4959 // Keep 150% of the current size, with a minimum of the growth size.
4960 n = exestack.ga_len / 2;
4961 if (n < exestack.ga_growsize)
4962 n = exestack.ga_growsize;
4963
4964 // Don't make it bigger though.
4965 if (exestack.ga_len + n < exestack.ga_maxlen)
4966 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004967 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004968 pp = vim_realloc(exestack.ga_data, new_len);
4969 if (pp == NULL)
4970 return FAIL;
4971 exestack.ga_maxlen = exestack.ga_len + n;
4972 exestack.ga_data = pp;
4973 }
4974 }
4975
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004976 // We advance by two because we add one for items referenced through
4977 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004978 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004979
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004980 /*
4981 * 1. Go through all accessible variables and mark all lists and dicts
4982 * with copyID.
4983 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004984
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004985 // Don't free variables in the previous_funccal list unless they are only
4986 // referenced through previous_funccal. This must be first, because if
4987 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004988 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004989
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004990 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004991 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004992
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004993 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004994 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004995 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4996 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004997
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004998 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004999 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005000 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5001 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005002 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005003 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5004 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005005#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005006 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005007 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5008 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005009 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005010 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005011 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5012 NULL, NULL);
5013#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005014
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005015 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005016 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005017 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5018 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005019 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005020 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005021
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005022 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005023 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005024
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005025 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005026 abort = abort || set_ref_in_functions(copyID);
5027
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005028 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005029 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005030
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005031 // funcstacks keep variables for closures
5032 abort = abort || set_ref_in_funcstacks(copyID);
5033
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005034 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005035 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005036
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005037 // callbacks in buffers
5038 abort = abort || set_ref_in_buffers(copyID);
5039
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005040 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5041 abort = abort || set_ref_in_insexpand_funcs(copyID);
5042
5043 // 'operatorfunc' callback
5044 abort = abort || set_ref_in_opfunc(copyID);
5045
5046 // 'tagfunc' callback
5047 abort = abort || set_ref_in_tagfunc(copyID);
5048
5049 // 'imactivatefunc' and 'imstatusfunc' callbacks
5050 abort = abort || set_ref_in_im_funcs(copyID);
5051
Bram Moolenaar1dced572012-04-05 16:54:08 +02005052#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005053 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005054#endif
5055
Bram Moolenaardb913952012-06-29 12:54:53 +02005056#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005057 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005058#endif
5059
5060#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005061 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005062#endif
5063
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005064#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005065 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005066 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005067#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005068#ifdef FEAT_NETBEANS_INTG
5069 abort = abort || set_ref_in_nb_channel(copyID);
5070#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005071
Bram Moolenaare3188e22016-05-31 21:13:04 +02005072#ifdef FEAT_TIMERS
5073 abort = abort || set_ref_in_timer(copyID);
5074#endif
5075
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005076#ifdef FEAT_QUICKFIX
5077 abort = abort || set_ref_in_quickfix(copyID);
5078#endif
5079
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005080#ifdef FEAT_TERMINAL
5081 abort = abort || set_ref_in_term(copyID);
5082#endif
5083
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005084#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005085 abort = abort || set_ref_in_popups(copyID);
5086#endif
5087
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005088 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005089 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005090 /*
5091 * 2. Free lists and dictionaries that are not referenced.
5092 */
5093 did_free = free_unref_items(copyID);
5094
5095 /*
5096 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005097 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005098 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005099 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005100 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005101 else if (p_verbose > 0)
5102 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005103 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005104 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005105
5106 return did_free;
5107}
5108
5109/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005110 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005111 */
5112 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005113free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005114{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005115 int did_free = FALSE;
5116
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005117 // Let all "free" functions know that we are here. This means no
5118 // dictionaries, lists, channels or jobs are to be freed, because we will
5119 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005120 in_free_unref_items = TRUE;
5121
5122 /*
5123 * PASS 1: free the contents of the items. We don't free the items
5124 * themselves yet, so that it is possible to decrement refcount counters
5125 */
5126
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005127 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005128 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005129
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005130 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005131 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005132
5133#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005134 // Go through the list of jobs and free items without the copyID. This
5135 // must happen before doing channels, because jobs refer to channels, but
5136 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005137 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5138
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005139 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005140 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5141#endif
5142
5143 /*
5144 * PASS 2: free the items themselves.
5145 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005146 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005147 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005148
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005149#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005150 // Go through the list of jobs and free items without the copyID. This
5151 // must happen before doing channels, because jobs refer to channels, but
5152 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005153 free_unused_jobs(copyID, COPYID_MASK);
5154
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005155 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005156 free_unused_channels(copyID, COPYID_MASK);
5157#endif
5158
5159 in_free_unref_items = FALSE;
5160
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005161 return did_free;
5162}
5163
5164/*
5165 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005166 * "list_stack" is used to add lists to be marked. Can be NULL.
5167 *
5168 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005169 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005170 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005171set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005172{
5173 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005174 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005175 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005176 hashtab_T *cur_ht;
5177 ht_stack_T *ht_stack = NULL;
5178 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005179
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005180 cur_ht = ht;
5181 for (;;)
5182 {
5183 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005184 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005185 // Mark each item in the hashtab. If the item contains a hashtab
5186 // it is added to ht_stack, if it contains a list it is added to
5187 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005188 todo = (int)cur_ht->ht_used;
5189 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5190 if (!HASHITEM_EMPTY(hi))
5191 {
5192 --todo;
5193 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5194 &ht_stack, list_stack);
5195 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005196 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005197
5198 if (ht_stack == NULL)
5199 break;
5200
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005201 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005202 cur_ht = ht_stack->ht;
5203 tempitem = ht_stack;
5204 ht_stack = ht_stack->prev;
5205 free(tempitem);
5206 }
5207
5208 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005209}
5210
Dominique Pelle748b3082022-01-08 12:41:16 +00005211#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5212 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005213/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005214 * Mark a dict and its items with "copyID".
5215 * Returns TRUE if setting references failed somehow.
5216 */
5217 int
5218set_ref_in_dict(dict_T *d, int copyID)
5219{
5220 if (d != NULL && d->dv_copyID != copyID)
5221 {
5222 d->dv_copyID = copyID;
5223 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5224 }
5225 return FALSE;
5226}
Dominique Pelle748b3082022-01-08 12:41:16 +00005227#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005228
5229/*
5230 * Mark a list and its items with "copyID".
5231 * Returns TRUE if setting references failed somehow.
5232 */
5233 int
5234set_ref_in_list(list_T *ll, int copyID)
5235{
5236 if (ll != NULL && ll->lv_copyID != copyID)
5237 {
5238 ll->lv_copyID = copyID;
5239 return set_ref_in_list_items(ll, copyID, NULL);
5240 }
5241 return FALSE;
5242}
5243
5244/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005245 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005246 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5247 *
5248 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005249 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005250 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005251set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005252{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005253 listitem_T *li;
5254 int abort = FALSE;
5255 list_T *cur_l;
5256 list_stack_T *list_stack = NULL;
5257 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005258
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005259 cur_l = l;
5260 for (;;)
5261 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005262 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005263 // Mark each item in the list. If the item contains a hashtab
5264 // it is added to ht_stack, if it contains a list it is added to
5265 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005266 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5267 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5268 ht_stack, &list_stack);
5269 if (list_stack == NULL)
5270 break;
5271
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005272 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005273 cur_l = list_stack->list;
5274 tempitem = list_stack;
5275 list_stack = list_stack->prev;
5276 free(tempitem);
5277 }
5278
5279 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005280}
5281
5282/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005283 * Mark the partial in callback 'cb' with "copyID".
5284 */
5285 int
5286set_ref_in_callback(callback_T *cb, int copyID)
5287{
5288 typval_T tv;
5289
5290 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5291 return FALSE;
5292
5293 tv.v_type = VAR_PARTIAL;
5294 tv.vval.v_partial = cb->cb_partial;
5295 return set_ref_in_item(&tv, copyID, NULL, NULL);
5296}
5297
5298/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005299 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005300 * "list_stack" is used to add lists to be marked. Can be NULL.
5301 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5302 *
5303 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005304 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005305 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005306set_ref_in_item(
5307 typval_T *tv,
5308 int copyID,
5309 ht_stack_T **ht_stack,
5310 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005311{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005312 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005313
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005314 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005315 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005316 dict_T *dd = tv->vval.v_dict;
5317
Bram Moolenaara03f2332016-02-06 18:09:59 +01005318 if (dd != NULL && dd->dv_copyID != copyID)
5319 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005320 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005321 dd->dv_copyID = copyID;
5322 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005323 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005324 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5325 }
5326 else
5327 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005328 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5329
Bram Moolenaara03f2332016-02-06 18:09:59 +01005330 if (newitem == NULL)
5331 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005332 else
5333 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005334 newitem->ht = &dd->dv_hashtab;
5335 newitem->prev = *ht_stack;
5336 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005337 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005338 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005339 }
5340 }
5341 else if (tv->v_type == VAR_LIST)
5342 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005343 list_T *ll = tv->vval.v_list;
5344
Bram Moolenaara03f2332016-02-06 18:09:59 +01005345 if (ll != NULL && ll->lv_copyID != copyID)
5346 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005347 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005348 ll->lv_copyID = copyID;
5349 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005350 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005351 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005352 }
5353 else
5354 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005355 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5356
Bram Moolenaara03f2332016-02-06 18:09:59 +01005357 if (newitem == NULL)
5358 abort = TRUE;
5359 else
5360 {
5361 newitem->list = ll;
5362 newitem->prev = *list_stack;
5363 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005364 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005365 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005366 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005367 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005368 else if (tv->v_type == VAR_FUNC)
5369 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005370 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005371 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005372 else if (tv->v_type == VAR_PARTIAL)
5373 {
5374 partial_T *pt = tv->vval.v_partial;
5375 int i;
5376
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005377 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005378 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005379 // Didn't see this partial yet.
5380 pt->pt_copyID = copyID;
5381
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005382 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005383
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005384 if (pt->pt_dict != NULL)
5385 {
5386 typval_T dtv;
5387
5388 dtv.v_type = VAR_DICT;
5389 dtv.vval.v_dict = pt->pt_dict;
5390 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5391 }
5392
5393 for (i = 0; i < pt->pt_argc; ++i)
5394 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5395 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005396 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005397 }
5398 }
5399#ifdef FEAT_JOB_CHANNEL
5400 else if (tv->v_type == VAR_JOB)
5401 {
5402 job_T *job = tv->vval.v_job;
5403 typval_T dtv;
5404
5405 if (job != NULL && job->jv_copyID != copyID)
5406 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005407 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005408 if (job->jv_channel != NULL)
5409 {
5410 dtv.v_type = VAR_CHANNEL;
5411 dtv.vval.v_channel = job->jv_channel;
5412 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5413 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005414 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005415 {
5416 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005417 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005418 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5419 }
5420 }
5421 }
5422 else if (tv->v_type == VAR_CHANNEL)
5423 {
5424 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005425 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005426 typval_T dtv;
5427 jsonq_T *jq;
5428 cbq_T *cq;
5429
5430 if (ch != NULL && ch->ch_copyID != copyID)
5431 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005432 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005433 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005434 {
5435 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5436 jq = jq->jq_next)
5437 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5438 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5439 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005440 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005441 {
5442 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005443 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005444 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5445 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005446 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005447 {
5448 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005449 dtv.vval.v_partial =
5450 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005451 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5452 }
5453 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005454 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005455 {
5456 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005457 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005458 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5459 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005460 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005461 {
5462 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005463 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005464 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5465 }
5466 }
5467 }
5468#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005469 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005470}
5471
Bram Moolenaar8c711452005-01-14 21:53:12 +00005472/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005473 * Return a string with the string representation of a variable.
5474 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005475 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005476 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005477 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005478 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005479 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005480 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5481 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005482 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005483 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005484 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005485echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005486 typval_T *tv,
5487 char_u **tofree,
5488 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005489 int copyID,
5490 int echo_style,
5491 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005492 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005494 static int recurse = 0;
5495 char_u *r = NULL;
5496
Bram Moolenaar33570922005-01-25 22:26:29 +00005497 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005498 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005499 if (!did_echo_string_emsg)
5500 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005501 // Only give this message once for a recursive call to avoid
5502 // flooding the user with errors. And stop iterating over lists
5503 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005504 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005505 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005506 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005507 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005508 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005509 }
5510 ++recurse;
5511
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005512 switch (tv->v_type)
5513 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005514 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005515 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005516 {
5517 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005518 r = tv->vval.v_string;
5519 if (r == NULL)
5520 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005521 }
5522 else
5523 {
5524 *tofree = string_quote(tv->vval.v_string, FALSE);
5525 r = *tofree;
5526 }
5527 break;
5528
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005529 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005530 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005531 char_u buf[MAX_FUNC_NAME_LEN];
5532
5533 if (echo_style)
5534 {
LemonBoya5d35902022-04-29 21:15:02 +01005535 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5536 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005537 buf, MAX_FUNC_NAME_LEN);
5538 if (r == buf)
5539 {
5540 r = vim_strsave(buf);
5541 *tofree = r;
5542 }
5543 else
5544 *tofree = NULL;
5545 }
5546 else
5547 {
5548 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5549 : make_ufunc_name_readable(
5550 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5551 TRUE);
5552 r = *tofree;
5553 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005554 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005555 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005556
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005557 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005558 {
5559 partial_T *pt = tv->vval.v_partial;
5560 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005561 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005562 garray_T ga;
5563 int i;
5564 char_u *tf;
5565
5566 ga_init2(&ga, 1, 100);
5567 ga_concat(&ga, (char_u *)"function(");
5568 if (fname != NULL)
5569 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005570 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005571 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005572 && vim_isupper(fname[1]))
5573 {
5574 ga_concat(&ga, (char_u *)"'g:");
5575 ga_concat(&ga, fname + 1);
5576 }
5577 else
5578 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005579 vim_free(fname);
5580 }
5581 if (pt != NULL && pt->pt_argc > 0)
5582 {
5583 ga_concat(&ga, (char_u *)", [");
5584 for (i = 0; i < pt->pt_argc; ++i)
5585 {
5586 if (i > 0)
5587 ga_concat(&ga, (char_u *)", ");
5588 ga_concat(&ga,
5589 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5590 vim_free(tf);
5591 }
5592 ga_concat(&ga, (char_u *)"]");
5593 }
5594 if (pt != NULL && pt->pt_dict != NULL)
5595 {
5596 typval_T dtv;
5597
5598 ga_concat(&ga, (char_u *)", ");
5599 dtv.v_type = VAR_DICT;
5600 dtv.vval.v_dict = pt->pt_dict;
5601 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5602 vim_free(tf);
5603 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005604 // terminate with ')' and a NUL
5605 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005606
5607 *tofree = ga.ga_data;
5608 r = *tofree;
5609 break;
5610 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005611
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005612 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005613 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005614 break;
5615
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005616 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005617 if (tv->vval.v_list == NULL)
5618 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005619 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005620 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005621 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005622 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005623 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5624 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005625 {
5626 *tofree = NULL;
5627 r = (char_u *)"[...]";
5628 }
5629 else
5630 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005631 int old_copyID = tv->vval.v_list->lv_copyID;
5632
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005633 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005634 *tofree = list2string(tv, copyID, restore_copyID);
5635 if (restore_copyID)
5636 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005637 r = *tofree;
5638 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005639 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005640
Bram Moolenaar8c711452005-01-14 21:53:12 +00005641 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005642 if (tv->vval.v_dict == NULL)
5643 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005644 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005645 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005646 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005647 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005648 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5649 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005650 {
5651 *tofree = NULL;
5652 r = (char_u *)"{...}";
5653 }
5654 else
5655 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005656 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005657
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005658 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005659 *tofree = dict2string(tv, copyID, restore_copyID);
5660 if (restore_copyID)
5661 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005662 r = *tofree;
5663 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005664 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005665
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005666 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005667 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005668 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005669 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005670 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005671 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005672 break;
5673
Bram Moolenaar835dc632016-02-07 14:27:38 +01005674 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005675 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005676#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005677 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005678 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5679 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005680 if (composite_val)
5681 {
5682 *tofree = string_quote(r, FALSE);
5683 r = *tofree;
5684 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005685#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005686 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005687
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005688 case VAR_INSTR:
5689 *tofree = NULL;
5690 r = (char_u *)"instructions";
5691 break;
5692
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005693 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005694#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005695 *tofree = NULL;
5696 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5697 r = numbuf;
5698 break;
5699#endif
5700
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005701 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005702 case VAR_SPECIAL:
5703 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005704 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005705 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005706 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005707
Bram Moolenaar8502c702014-06-17 12:51:16 +02005708 if (--recurse == 0)
5709 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005710 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005711}
5712
5713/*
5714 * Return a string with the string representation of a variable.
5715 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5716 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005717 * Does not put quotes around strings, as ":echo" displays values.
5718 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5719 * May return NULL.
5720 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005721 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005722echo_string(
5723 typval_T *tv,
5724 char_u **tofree,
5725 char_u *numbuf,
5726 int copyID)
5727{
5728 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5729}
5730
5731/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005732 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5733 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005734 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005735 */
5736 int
5737buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5738{
5739 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005740 char_u *t;
5741 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005742
5743 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5744 return -1;
5745
5746 if (lnum > buf->b_ml.ml_line_count)
5747 lnum = buf->b_ml.ml_line_count;
5748
5749 str = ml_get_buf(buf, lnum, FALSE);
5750 if (str == NULL)
5751 return -1;
5752
5753 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005754 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005755
Bram Moolenaar91458462021-01-13 20:08:38 +01005756 // count the number of characters
5757 t = str;
5758 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5759 t += mb_ptr2len(t);
5760
5761 // In insert mode, when the cursor is at the end of a non-empty line,
5762 // byteidx points to the NUL character immediately past the end of the
5763 // string. In this case, add one to the character count.
5764 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5765 count++;
5766
5767 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005768}
5769
5770/*
5771 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005772 * byte index. Works only for loaded buffers. Returns -1 on failure.
5773 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005774 */
5775 int
5776buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5777{
5778 char_u *str;
5779 char_u *t;
5780
5781 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5782 return -1;
5783
5784 if (lnum > buf->b_ml.ml_line_count)
5785 lnum = buf->b_ml.ml_line_count;
5786
5787 str = ml_get_buf(buf, lnum, FALSE);
5788 if (str == NULL)
5789 return -1;
5790
5791 // Convert the character offset to a byte offset
5792 t = str;
5793 while (*t != NUL && --charidx > 0)
5794 t += mb_ptr2len(t);
5795
Bram Moolenaar91458462021-01-13 20:08:38 +01005796 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005797}
5798
5799/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005801 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005803 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005804var2fpos(
5805 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005806 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005807 int *fnum, // set to fnum for '0, 'A, etc.
5808 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005810 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005812 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005814 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005815 if (varp->v_type == VAR_LIST)
5816 {
5817 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005818 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005819 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005820 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005821
5822 l = varp->vval.v_list;
5823 if (l == NULL)
5824 return NULL;
5825
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005826 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005827 pos.lnum = list_find_nr(l, 0L, &error);
5828 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005829 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005830 if (charcol)
5831 len = (long)mb_charlen(ml_get(pos.lnum));
5832 else
5833 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005834
Bram Moolenaarec65d772020-08-20 22:29:12 +02005835 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005836 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005837 li = list_find(l, 1L);
5838 if (li != NULL && li->li_tv.v_type == VAR_STRING
5839 && li->li_tv.vval.v_string != NULL
5840 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005841 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005842 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005843 }
5844 else
5845 {
5846 pos.col = list_find_nr(l, 1L, &error);
5847 if (error)
5848 return NULL;
5849 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005850
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005851 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005852 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005853 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005854 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005855
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005856 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005857 pos.coladd = list_find_nr(l, 2L, &error);
5858 if (error)
5859 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005860
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005861 return &pos;
5862 }
5863
Bram Moolenaarc5809432021-03-27 21:23:30 +01005864 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5865 return NULL;
5866
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005867 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005868 if (name == NULL)
5869 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005870
5871 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005872 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005873 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005874 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005875 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005876 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005877 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005878 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005879 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005880 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005881 pos = VIsual;
5882 else
5883 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005884 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005885 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005886 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005888 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005889 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5891 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005892 pos = *pp;
5893 }
5894 if (pos.lnum != 0)
5895 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005896 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005897 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5898 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005900
Bram Moolenaara5525202006-03-02 22:52:09 +00005901 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005902
Bram Moolenaar477933c2007-07-17 14:32:23 +00005903 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005904 {
5905 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005906 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005907 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005908 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005909 // In silent Ex mode topline is zero, but that's not a valid line
5910 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005911 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005912 return &pos;
5913 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005914 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005915 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005916 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005917 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005918 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005919 return &pos;
5920 }
5921 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005922 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005924 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 {
5926 pos.lnum = curbuf->b_ml.ml_line_count;
5927 pos.col = 0;
5928 }
5929 else
5930 {
5931 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005932 if (charcol)
5933 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5934 else
5935 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 }
5937 return &pos;
5938 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005939 if (in_vim9script())
5940 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 return NULL;
5942}
5943
5944/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005945 * Convert list in "arg" into a position and optional file number.
5946 * When "fnump" is NULL there is no file number, only 3 items.
5947 * Note that the column is passed on as-is, the caller may want to decrement
5948 * it to use 1 for the first column.
5949 * Return FAIL when conversion is not possible, doesn't check the position for
5950 * validity.
5951 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005952 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005953list2fpos(
5954 typval_T *arg,
5955 pos_T *posp,
5956 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005957 colnr_T *curswantp,
5958 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005959{
5960 list_T *l = arg->vval.v_list;
5961 long i = 0;
5962 long n;
5963
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005964 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5965 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005966 if (arg->v_type != VAR_LIST
5967 || l == NULL
5968 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005969 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005970 return FAIL;
5971
5972 if (fnump != NULL)
5973 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005974 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005975 if (n < 0)
5976 return FAIL;
5977 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005978 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005979 *fnump = n;
5980 }
5981
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005982 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005983 if (n < 0)
5984 return FAIL;
5985 posp->lnum = n;
5986
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005987 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005988 if (n < 0)
5989 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005990 // If character position is specified, then convert to byte position
5991 if (charcol)
5992 {
5993 buf_T *buf;
5994
5995 // Get the text for the specified line in a loaded buffer
5996 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5997 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5998 return FAIL;
5999
Bram Moolenaar91458462021-01-13 20:08:38 +01006000 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006001 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006002 posp->col = n;
6003
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006004 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006005 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006006 posp->coladd = 0;
6007 else
6008 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006009
Bram Moolenaar493c1782014-05-28 14:34:46 +02006010 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006011 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006012
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006013 return OK;
6014}
6015
6016/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 * Get the length of an environment variable name.
6018 * Advance "arg" to the first character after the name.
6019 * Return 0 for error.
6020 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006021 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006022get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023{
6024 char_u *p;
6025 int len;
6026
6027 for (p = *arg; vim_isIDc(*p); ++p)
6028 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006029 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 return 0;
6031
6032 len = (int)(p - *arg);
6033 *arg = p;
6034 return len;
6035}
6036
6037/*
6038 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006039 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 * Return 0 if something is wrong.
6041 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006042 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006043get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044{
6045 char_u *p;
6046 int len;
6047
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006048 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006050 {
6051 if (*p == ':')
6052 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006053 // "s:" is start of "s:var", but "n:" is not and can be used in
6054 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006055 len = (int)(p - *arg);
6056 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6057 || len > 1)
6058 break;
6059 }
6060 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006061 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 return 0;
6063
6064 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006065 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066
6067 return len;
6068}
6069
6070/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006071 * Get the length of the name of a variable or function.
6072 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006074 * Return -1 if curly braces expansion failed.
6075 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 * If the name contains 'magic' {}'s, expand them and return the
6077 * expanded name in an allocated string via 'alias' - caller must free.
6078 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006079 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006080get_name_len(
6081 char_u **arg,
6082 char_u **alias,
6083 int evaluate,
6084 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085{
6086 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 char_u *p;
6088 char_u *expr_start;
6089 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006091 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092
6093 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6094 && (*arg)[2] == (int)KE_SNR)
6095 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006096 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 *arg += 3;
6098 return get_id_len(arg) + 3;
6099 }
6100 len = eval_fname_script(*arg);
6101 if (len > 0)
6102 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006103 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 *arg += len;
6105 }
6106
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006108 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006110 p = find_name_end(*arg, &expr_start, &expr_end,
6111 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 if (expr_start != NULL)
6113 {
6114 char_u *temp_string;
6115
6116 if (!evaluate)
6117 {
6118 len += (int)(p - *arg);
6119 *arg = skipwhite(p);
6120 return len;
6121 }
6122
6123 /*
6124 * Include any <SID> etc in the expanded string:
6125 * Thus the -len here.
6126 */
6127 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6128 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006129 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 *alias = temp_string;
6131 *arg = skipwhite(p);
6132 return (int)STRLEN(temp_string);
6133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134
6135 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006136 // Only give an error when there is something, otherwise it will be
6137 // reported at a higher level.
6138 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006139 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140
6141 return len;
6142}
6143
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006144/*
6145 * Find the end of a variable or function name, taking care of magic braces.
6146 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6147 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006148 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006149 * Return a pointer to just after the name. Equal to "arg" if there is no
6150 * valid name.
6151 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006152 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006153find_name_end(
6154 char_u *arg,
6155 char_u **expr_start,
6156 char_u **expr_end,
6157 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006159 int mb_nest = 0;
6160 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006162 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02006163 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006165 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006167 *expr_start = NULL;
6168 *expr_end = NULL;
6169 }
6170
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006171 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006172 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
6173 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006174 return arg;
6175
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006176 for (p = arg; *p != NUL
6177 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006178 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006179 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006180 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006181 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006182 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006183 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006184 if (*p == '\'')
6185 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006186 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006187 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006188 ;
6189 if (*p == NUL)
6190 break;
6191 }
6192 else if (*p == '"')
6193 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006194 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006195 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006196 if (*p == '\\' && p[1] != NUL)
6197 ++p;
6198 if (*p == NUL)
6199 break;
6200 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006201 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6202 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006203 // "s:" is start of "s:var", but "n:" is not and can be used in
6204 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006205 len = (int)(p - arg);
6206 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006207 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006208 break;
6209 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006210
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006211 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006213 if (*p == '[')
6214 ++br_nest;
6215 else if (*p == ']')
6216 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006218
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006219 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006221 if (*p == '{')
6222 {
6223 mb_nest++;
6224 if (expr_start != NULL && *expr_start == NULL)
6225 *expr_start = p;
6226 }
6227 else if (*p == '}')
6228 {
6229 mb_nest--;
6230 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6231 *expr_end = p;
6232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 }
6235
6236 return p;
6237}
6238
6239/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006240 * Expands out the 'magic' {}'s in a variable/function name.
6241 * Note that this can call itself recursively, to deal with
6242 * constructs like foo{bar}{baz}{bam}
6243 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6244 * "in_start" ^
6245 * "expr_start" ^
6246 * "expr_end" ^
6247 * "in_end" ^
6248 *
6249 * Returns a new allocated string, which the caller must free.
6250 * Returns NULL for failure.
6251 */
6252 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006253make_expanded_name(
6254 char_u *in_start,
6255 char_u *expr_start,
6256 char_u *expr_end,
6257 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006258{
6259 char_u c1;
6260 char_u *retval = NULL;
6261 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006262
6263 if (expr_end == NULL || in_end == NULL)
6264 return NULL;
6265 *expr_start = NUL;
6266 *expr_end = NUL;
6267 c1 = *in_end;
6268 *in_end = NUL;
6269
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006270 temp_result = eval_to_string(expr_start + 1, FALSE);
6271 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006272 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006273 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6274 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006275 if (retval != NULL)
6276 {
6277 STRCPY(retval, in_start);
6278 STRCAT(retval, temp_result);
6279 STRCAT(retval, expr_end + 1);
6280 }
6281 }
6282 vim_free(temp_result);
6283
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006284 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006285 *expr_start = '{';
6286 *expr_end = '}';
6287
6288 if (retval != NULL)
6289 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006290 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006291 if (expr_start != NULL)
6292 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006293 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006294 temp_result = make_expanded_name(retval, expr_start,
6295 expr_end, temp_result);
6296 vim_free(retval);
6297 retval = temp_result;
6298 }
6299 }
6300
6301 return retval;
6302}
6303
6304/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006306 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006308 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006309eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006311 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006312}
6313
6314/*
6315 * Return TRUE if character "c" can be used as the first character in a
6316 * variable or function name (excluding '{' and '}').
6317 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006318 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006319eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006320{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006321 return ASCII_ISALPHA(c) || c == '_';
6322}
6323
6324/*
6325 * Return TRUE if character "c" can be used as the first character of a
6326 * dictionary key.
6327 */
6328 int
6329eval_isdictc(int c)
6330{
6331 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332}
6333
6334/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006335 * Handle:
6336 * - expr[expr], expr[expr:expr] subscript
6337 * - ".name" lookup
6338 * - function call with Funcref variable: func(expr)
6339 * - method call: var->method()
6340 *
6341 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006342 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006343 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006344 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006345handle_subscript(
6346 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006347 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006348 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006349 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006350 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006351{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006352 int evaluate = evalarg != NULL
6353 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006354 int ret = OK;
6355 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006356 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006357 int getnext;
6358 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006359
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006360 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006361 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006362 // When at the end of the line and ".name" or "->{" or "->X" follows in
6363 // the next line then consume the line break.
6364 p = eval_next_non_blank(*arg, evalarg, &getnext);
6365 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006366 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006367 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6368 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6369 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006370 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006371 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006372 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006373 check_white = FALSE;
6374 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006375
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006376 if (rettv->v_type == VAR_ANY)
6377 {
6378 char_u *exp_name;
6379 int cc;
6380 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006381 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006382 type_T *type;
6383
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006384 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006385 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006386 if (**arg != '.')
6387 {
6388 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006389 semsg(_(e_expected_dot_after_name_str),
6390 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006391 ret = FAIL;
6392 break;
6393 }
6394 ++*arg;
6395 if (IS_WHITE_OR_NUL(**arg))
6396 {
6397 if (verbose)
6398 emsg(_(e_no_white_space_allowed_after_dot));
6399 ret = FAIL;
6400 break;
6401 }
6402
6403 // isolate the name
6404 exp_name = *arg;
6405 while (eval_isnamec(**arg))
6406 ++*arg;
6407 cc = **arg;
6408 **arg = NUL;
6409
6410 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00006411 evalarg->eval_cctx, evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006412 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006413
6414 if (idx < 0 && ufunc == NULL)
6415 {
6416 ret = FAIL;
6417 break;
6418 }
6419 if (idx >= 0)
6420 {
6421 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6422 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6423
6424 copy_tv(sv->sv_tv, rettv);
6425 }
6426 else
6427 {
6428 rettv->v_type = VAR_FUNC;
6429 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6430 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006431 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006432 }
6433
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006434 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6435 || rettv->v_type == VAR_PARTIAL))
6436 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006437 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006438 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6439 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006440
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006441 // Stop the expression evaluation when immediately aborting on
6442 // error, or when an interrupt occurred or an exception was thrown
6443 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006444 if (aborting())
6445 {
6446 if (ret == OK)
6447 clear_tv(rettv);
6448 ret = FAIL;
6449 }
6450 dict_unref(selfdict);
6451 selfdict = NULL;
6452 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006453 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006454 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006455 if (in_vim9script())
6456 *arg = skipwhite(p + 2);
6457 else
6458 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006459 if (ret == OK)
6460 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006461 if (VIM_ISWHITE(**arg))
6462 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006463 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006464 ret = FAIL;
6465 }
6466 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006467 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006468 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006469 else
6470 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006471 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006472 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006473 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006474 // "." is ".name" lookup when we found a dict or when evaluating and
6475 // scriptversion is at least 2, where string concatenation is "..".
6476 else if (**arg == '['
6477 || (**arg == '.' && (rettv->v_type == VAR_DICT
6478 || (!evaluate
6479 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006480 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006481 {
6482 dict_unref(selfdict);
6483 if (rettv->v_type == VAR_DICT)
6484 {
6485 selfdict = rettv->vval.v_dict;
6486 if (selfdict != NULL)
6487 ++selfdict->dv_refcount;
6488 }
6489 else
6490 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006491 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006492 {
6493 clear_tv(rettv);
6494 ret = FAIL;
6495 }
6496 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006497 else
6498 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006499 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006500
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006501 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6502 // Don't do this when "Func" is already a partial that was bound
6503 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006504 if (selfdict != NULL
6505 && (rettv->v_type == VAR_FUNC
6506 || (rettv->v_type == VAR_PARTIAL
6507 && (rettv->vval.v_partial->pt_auto
6508 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006509 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006510
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006511 dict_unref(selfdict);
6512 return ret;
6513}
6514
6515/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006516 * Make a copy of an item.
6517 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006518 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006519 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6520 * reference to an already copied list/dict can be used.
6521 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006522 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006523 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006524item_copy(
6525 typval_T *from,
6526 typval_T *to,
6527 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006528 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006529 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006530{
6531 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006532 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006533
Bram Moolenaar33570922005-01-25 22:26:29 +00006534 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006535 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006536 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006537 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006538 }
6539 ++recurse;
6540
6541 switch (from->v_type)
6542 {
6543 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006544 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006545 case VAR_STRING:
6546 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006547 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006548 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006549 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006550 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006551 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006552 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006553 copy_tv(from, to);
6554 break;
6555 case VAR_LIST:
6556 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006557 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006558 if (from->vval.v_list == NULL)
6559 to->vval.v_list = NULL;
6560 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6561 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006562 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006563 to->vval.v_list = from->vval.v_list->lv_copylist;
6564 ++to->vval.v_list->lv_refcount;
6565 }
6566 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006567 to->vval.v_list = list_copy(from->vval.v_list,
6568 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006569 if (to->vval.v_list == NULL)
6570 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006571 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006572 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006573 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006574 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006575 case VAR_DICT:
6576 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006577 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006578 if (from->vval.v_dict == NULL)
6579 to->vval.v_dict = NULL;
6580 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6581 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006582 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006583 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6584 ++to->vval.v_dict->dv_refcount;
6585 }
6586 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006587 to->vval.v_dict = dict_copy(from->vval.v_dict,
6588 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006589 if (to->vval.v_dict == NULL)
6590 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006591 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006592 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006593 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006594 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006595 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006596 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006597 }
6598 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006599 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006600}
6601
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006602 void
6603echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6604{
6605 char_u *tofree;
6606 char_u numbuf[NUMBUFLEN];
6607 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6608
6609 if (*atstart)
6610 {
6611 *atstart = FALSE;
6612 // Call msg_start() after eval1(), evaluating the expression
6613 // may cause a message to appear.
6614 if (with_space)
6615 {
6616 // Mark the saved text as finishing the line, so that what
6617 // follows is displayed on a new line when scrolling back
6618 // at the more prompt.
6619 msg_sb_eol();
6620 msg_start();
6621 }
6622 }
6623 else if (with_space)
6624 msg_puts_attr(" ", echo_attr);
6625
6626 if (p != NULL)
6627 for ( ; *p != NUL && !got_int; ++p)
6628 {
6629 if (*p == '\n' || *p == '\r' || *p == TAB)
6630 {
6631 if (*p != TAB && *needclr)
6632 {
6633 // remove any text still there from the command
6634 msg_clr_eos();
6635 *needclr = FALSE;
6636 }
6637 msg_putchar_attr(*p, echo_attr);
6638 }
6639 else
6640 {
6641 if (has_mbyte)
6642 {
6643 int i = (*mb_ptr2len)(p);
6644
6645 (void)msg_outtrans_len_attr(p, i, echo_attr);
6646 p += i - 1;
6647 }
6648 else
6649 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6650 }
6651 }
6652 vim_free(tofree);
6653}
6654
Bram Moolenaare9a41262005-01-15 22:18:47 +00006655/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 * ":echo expr1 ..." print each argument separated with a space, add a
6657 * newline at the end.
6658 * ":echon expr1 ..." print each argument plain.
6659 */
6660 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006661ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662{
6663 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006664 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006665 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 int needclr = TRUE;
6667 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006668 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006669 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006670 evalarg_T evalarg;
6671
Bram Moolenaare707c882020-07-01 13:07:37 +02006672 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673
6674 if (eap->skip)
6675 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006676 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006678 // If eval1() causes an error message the text from the command may
6679 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006680 need_clr_eos = needclr;
6681
Bram Moolenaar68db9962021-05-09 23:19:22 +02006682 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006683 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 {
6685 /*
6686 * Report the invalid expression unless the expression evaluation
6687 * has been cancelled due to an aborting error, an interrupt, or an
6688 * exception.
6689 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006690 if (!aborting() && did_emsg == did_emsg_before
6691 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006692 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006693 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 break;
6695 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006696 need_clr_eos = FALSE;
6697
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006699 {
6700 if (rettv.v_type == VAR_VOID)
6701 {
6702 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6703 break;
6704 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006705 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006706 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006707
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006708 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 arg = skipwhite(arg);
6710 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006711 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006712 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713
6714 if (eap->skip)
6715 --emsg_skip;
6716 else
6717 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006718 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 if (needclr)
6720 msg_clr_eos();
6721 if (eap->cmdidx == CMD_echo)
6722 msg_end();
6723 }
6724}
6725
6726/*
6727 * ":echohl {name}".
6728 */
6729 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006730ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006732 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733}
6734
6735/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006736 * Returns the :echo attribute
6737 */
6738 int
6739get_echo_attr(void)
6740{
6741 return echo_attr;
6742}
6743
6744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 * ":execute expr1 ..." execute the result of an expression.
6746 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01006747 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006749 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 * Each gets spaces around each argument and a newline at the end for
6751 * echo commands
6752 */
6753 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006754ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755{
6756 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006757 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 int ret = OK;
6759 char_u *p;
6760 garray_T ga;
6761 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006762 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763
6764 ga_init2(&ga, 1, 80);
6765
6766 if (eap->skip)
6767 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006768 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006770 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006771 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773
6774 if (!eap->skip)
6775 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006776 char_u buf[NUMBUFLEN];
6777
6778 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006779 {
6780 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6781 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006782 semsg(_(e_using_invalid_value_as_string_str),
6783 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006784 p = NULL;
6785 }
6786 else
6787 p = tv_get_string_buf(&rettv, buf);
6788 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006789 else
6790 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006791 if (p == NULL)
6792 {
6793 clear_tv(&rettv);
6794 ret = FAIL;
6795 break;
6796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 len = (int)STRLEN(p);
6798 if (ga_grow(&ga, len + 2) == FAIL)
6799 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006800 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 ret = FAIL;
6802 break;
6803 }
6804 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 ga.ga_len += len;
6808 }
6809
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006810 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 arg = skipwhite(arg);
6812 }
6813
6814 if (ret != FAIL && ga.ga_data != NULL)
6815 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006816 // use the first line of continuation lines for messages
6817 SOURCING_LNUM = start_lnum;
6818
Bram Moolenaar37fef162022-08-29 18:16:32 +01006819 if (eap->cmdidx == CMD_echomsg
6820 || eap->cmdidx == CMD_echowindow
6821 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006822 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006823 // Mark the already saved text as finishing the line, so that what
6824 // follows is displayed on a new line when scrolling back at the
6825 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006826 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006827 }
6828
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006829 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006830 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006831 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006832 out_flush();
6833 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006834 else if (eap->cmdidx == CMD_echowindow)
6835 {
6836#ifdef HAS_MESSAGE_WINDOW
6837 start_echowindow();
6838#endif
6839 msg_attr(ga.ga_data, echo_attr);
6840#ifdef HAS_MESSAGE_WINDOW
6841 end_echowindow();
6842#endif
6843 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006844 else if (eap->cmdidx == CMD_echoconsole)
6845 {
6846 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6847 ui_write((char_u *)"\r\n", 2, TRUE);
6848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 else if (eap->cmdidx == CMD_echoerr)
6850 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006851 int save_did_emsg = did_emsg;
6852
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006853 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006854 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 if (!force_abort)
6856 did_emsg = save_did_emsg;
6857 }
6858 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006859 {
6860 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6861
6862 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6863 sticky_cmdmod_flags = cmdmod.cmod_flags
6864 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 do_cmdline((char_u *)ga.ga_data,
6866 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006867 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 }
6870
6871 ga_clear(&ga);
6872
6873 if (eap->skip)
6874 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02006875 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876}
6877
6878/*
6879 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6880 * "arg" points to the "&" or '+' when called, to "option" when returning.
6881 * Returns NULL when no option name found. Otherwise pointer to the char
6882 * after the option name.
6883 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006884 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006885find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886{
6887 char_u *p = *arg;
6888
6889 ++p;
6890 if (*p == 'g' && p[1] == ':')
6891 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006892 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 p += 2;
6894 }
6895 else if (*p == 'l' && p[1] == ':')
6896 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006897 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 p += 2;
6899 }
6900 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006901 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902
6903 if (!ASCII_ISALPHA(*p))
6904 return NULL;
6905 *arg = p;
6906
6907 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006908 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 else
6910 while (ASCII_ISALPHA(*p))
6911 ++p;
6912 return p;
6913}
6914
6915/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006916 * Display script name where an item was last set.
6917 * Should only be invoked when 'verbose' is non-zero.
6918 */
6919 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006920last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006921{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006922 char_u *p;
6923
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006924 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006925 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006926 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006927 if (p != NULL)
6928 {
6929 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006930 msg_puts(_("\n\tLast set from "));
6931 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006932 if (script_ctx.sc_lnum > 0)
6933 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006934 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006935 msg_outnum((long)script_ctx.sc_lnum);
6936 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006937 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006938 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006939 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006940 }
6941}
6942
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006943#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944
6945/*
6946 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006947 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 * "flags" can be "g" to do a global substitute.
6949 * Returns an allocated string, NULL for error.
6950 */
6951 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006952do_string_sub(
6953 char_u *str,
6954 char_u *pat,
6955 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006956 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006957 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958{
6959 int sublen;
6960 regmatch_T regmatch;
6961 int i;
6962 int do_all;
6963 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006964 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 garray_T ga;
6966 char_u *ret;
6967 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006968 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006970 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006972 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973
6974 ga_init2(&ga, 1, 200);
6975
6976 do_all = (flags[0] == 'g');
6977
6978 regmatch.rm_ic = p_ic;
6979 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6980 if (regmatch.regprog != NULL)
6981 {
6982 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006983 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6985 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006986 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006987 if (regmatch.startp[0] == regmatch.endp[0])
6988 {
6989 if (zero_width == regmatch.startp[0])
6990 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006991 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006992 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006993 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6994 (size_t)i);
6995 ga.ga_len += i;
6996 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006997 continue;
6998 }
6999 zero_width = regmatch.startp[0];
7000 }
7001
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 /*
7003 * Get some space for a temporary buffer to do the substitution
7004 * into. It will contain:
7005 * - The text up to where the match is.
7006 * - The substituted text.
7007 * - The text after the match.
7008 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007009 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaare90c8532014-11-05 16:03:44 +01007010 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7012 {
7013 ga_clear(&ga);
7014 break;
7015 }
7016
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007017 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 i = (int)(regmatch.startp[0] - tail);
7019 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007020 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007021 (void)vim_regsub(&regmatch, sub, expr,
7022 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7023 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007025 tail = regmatch.endp[0];
7026 if (*tail == NUL)
7027 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 if (!do_all)
7029 break;
7030 }
7031
7032 if (ga.ga_data != NULL)
7033 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7034
Bram Moolenaar473de612013-06-08 18:19:48 +02007035 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 }
7037
7038 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7039 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007040 if (p_cpo == empty_option)
7041 p_cpo = save_cpo;
7042 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007043 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007044 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007045 // If it's still empty it was changed and restored, need to restore in
7046 // the complicated way.
7047 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007048 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007049 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051
7052 return ret;
7053}