blob: db8b4e6c982cb7d836908cd6acc683b4f93589dc [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 Moolenaard8e9bb22005-07-09 21:14:46 +0000845 */
846 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100847call_func_retlist(
848 char_u *func,
849 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200850 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000851{
852 typval_T rettv;
853
Bram Moolenaarded27a12018-08-01 19:06:03 +0200854 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000855 return NULL;
856
857 if (rettv.v_type != VAR_LIST)
858 {
859 clear_tv(&rettv);
860 return NULL;
861 }
862
863 return rettv.vval.v_list;
864}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
Bram Moolenaare70dd112022-01-21 16:31:11 +0000866#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100868 * Evaluate "arg", which is 'foldexpr'.
869 * Note: caller must set "curwin" to match "arg".
870 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
871 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 */
873 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000874eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000876 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000877 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200878 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000880 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000881 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
882 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883
Bram Moolenaare70dd112022-01-21 16:31:11 +0000884 arg = wp->w_p_fde;
885 current_sctx = wp->w_p_script_ctx[WV_FDE];
886
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000888 if (use_sandbox)
889 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100890 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200892 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 retval = 0;
894 else
895 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100896 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000897 if (tv.v_type == VAR_NUMBER)
898 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000899 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 retval = 0;
901 else
902 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100903 // If the result is a string, check if there is a non-digit before
904 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000905 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 if (!VIM_ISDIGIT(*s) && *s != '-')
907 *cp = *s++;
908 retval = atol((char *)s);
909 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000910 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 }
912 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000913 if (use_sandbox)
914 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100915 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200916 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000917 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200919 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920}
921#endif
922
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000924 * Get an lval: variable, Dict item or List item that can be assigned a value
925 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
926 * "name.key", "name.key[expr]" etc.
927 * Indexing only works if "name" is an existing List or Dictionary.
928 * "name" points to the start of the name.
929 * If "rettv" is not NULL it points to the value to be assigned.
930 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
931 * wrong; must end in space or cmd separator.
932 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100933 * flags:
934 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100935 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100936 * GLV_NO_AUTOLOAD: do not use script autoloading
937 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000938 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000939 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000940 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000941 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200942 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100943get_lval(
944 char_u *name,
945 typval_T *rettv,
946 lval_T *lp,
947 int unlet,
948 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100949 int flags, // GLV_ values
950 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000951{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000952 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000953 char_u *expr_start, *expr_end;
954 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000955 dictitem_T *v;
956 typval_T var1;
957 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000958 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000959 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000960 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100961 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100962 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100963 int writing;
Bram Moolenaar4525a572022-02-13 11:57:33 +0000964 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000965
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100966 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200967 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000968
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100969 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000970 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100971 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000972 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200973 lp->ll_name_end = find_name_end(name, NULL, NULL,
974 FNE_INCL_BR | fne_flags);
975 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000976 }
977
Bram Moolenaara749a422022-02-12 19:52:25 +0000978 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +0000979 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +0000980 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
981 {
982 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
983 return NULL;
984 }
985
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100986 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000987 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100988 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000989 if (expr_start != NULL)
990 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100991 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100992 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000993 && *p != '[' && *p != '.')
994 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000995 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000996 return NULL;
997 }
998
999 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1000 if (lp->ll_exp_name == NULL)
1001 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001002 // Report an invalid expression in braces, unless the
1003 // expression evaluation has been cancelled due to an
1004 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001005 if (!aborting() && !quiet)
1006 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001007 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001008 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001009 return NULL;
1010 }
1011 }
1012 lp->ll_name = lp->ll_exp_name;
1013 }
1014 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001015 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001016 lp->ll_name = name;
1017
Bram Moolenaar4525a572022-02-13 11:57:33 +00001018 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001019 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001020 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001021 // However, "g:[key]" is indexing a dictionary.
1022 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001023 {
1024 --p;
1025 lp->ll_name_end = p;
1026 }
1027 if (*p == ':')
1028 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001029 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001030
1031 if (tp == p + 1 && !quiet)
1032 {
1033 semsg(_(e_white_space_required_after_str_str), ":", p);
1034 return NULL;
1035 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001036
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001037 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001038 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001039 semsg(_(e_using_type_not_in_script_context_str), p);
1040 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001041 }
1042
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001043 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001044 lp->ll_type = parse_type(&tp,
1045 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1046 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001047 if (lp->ll_type == NULL && !quiet)
1048 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001049 lp->ll_name_end = tp;
1050 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001051 }
1052 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001053 if (lp->ll_name == NULL)
1054 return p;
1055
Bram Moolenaar62aec932022-01-29 21:45:34 +00001056 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001057 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001058 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001059
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001060 if (import != NULL)
1061 {
1062 ufunc_T *ufunc;
1063 type_T *type;
1064
Bram Moolenaar753885b2022-08-24 16:30:36 +01001065 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001066 lp->ll_sid = import->imp_sid;
1067 lp->ll_name = skipwhite(p + 1);
1068 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1069 lp->ll_name_end = p;
1070
1071 // check the item is exported
1072 cc = *p;
1073 *p = NUL;
1074 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001075 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001076 {
1077 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001078 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001079 }
1080 *p = cc;
1081 }
1082 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001083
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001084 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001085 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001086 return p;
1087
Bram Moolenaar4525a572022-02-13 11:57:33 +00001088 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001089 {
1090 // using local variable
1091 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001092 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001093 }
1094 else
1095 {
1096 cc = *p;
1097 *p = NUL;
1098 // When we would write to the variable pass &ht and prevent autoload.
1099 writing = !(flags & GLV_READ_ONLY);
1100 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001101 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001102 if (v == NULL && !quiet)
1103 semsg(_(e_undefined_variable_str), lp->ll_name);
1104 *p = cc;
1105 if (v == NULL)
1106 return NULL;
1107 lp->ll_tv = &v->di_tv;
1108 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001109
Bram Moolenaar4525a572022-02-13 11:57:33 +00001110 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001111 {
1112 if (!quiet)
1113 semsg(_(e_variable_already_declared), lp->ll_name);
1114 return NULL;
1115 }
1116
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001117 /*
1118 * Loop until no more [idx] or .key is following.
1119 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001120 var1.v_type = VAR_UNKNOWN;
1121 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001122 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001123 {
Bram Moolenaarf21d5462022-09-10 12:36:00 +01001124 int r = OK;
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001125
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001126 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1127 {
1128 if (!quiet)
1129 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1130 return NULL;
1131 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001132 if (lp->ll_tv->v_type != VAR_LIST
1133 && lp->ll_tv->v_type != VAR_DICT
1134 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001135 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001136 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001137 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001138 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001139 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001140
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001141 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaare65081d2021-06-27 15:04:05 +02001142 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001143 r = rettv_list_alloc(lp->ll_tv);
Bram Moolenaare65081d2021-06-27 15:04:05 +02001144 else if (lp->ll_tv->v_type == VAR_BLOB
1145 && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001146 r = rettv_blob_alloc(lp->ll_tv);
1147 if (r == FAIL)
1148 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001149
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001150 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001151 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001152 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001153 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001154 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001155 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001156
Bram Moolenaar4525a572022-02-13 11:57:33 +00001157 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001158 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001159 && lp->ll_tv == &v->di_tv
1160 && ht != NULL && ht == get_script_local_ht())
1161 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001162 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001163
1164 // Vim9 script local variable: get the type
1165 if (sv != NULL)
1166 lp->ll_valtype = sv->sv_type;
1167 }
1168
Bram Moolenaar8c711452005-01-14 21:53:12 +00001169 len = -1;
1170 if (*p == '.')
1171 {
1172 key = p + 1;
1173 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1174 ;
1175 if (len == 0)
1176 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001177 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001178 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001179 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001180 }
1181 p = key + len;
1182 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001183 else
1184 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001185 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001186 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001187 if (*p == ':')
1188 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001189 else
1190 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001191 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001192 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001193 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001194 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001195 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001196 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001197 clear_tv(&var1);
1198 return NULL;
1199 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001200 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001201 }
1202
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001203 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001204 if (*p == ':')
1205 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001206 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001207 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001208 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001209 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001210 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001211 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001212 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001213 if (rettv != NULL
1214 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001215 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001216 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001217 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001218 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001219 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001220 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001221 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001222 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001223 }
1224 p = skipwhite(p + 1);
1225 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001226 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001227 else
1228 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001229 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001230 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001231 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001232 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001233 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001234 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001235 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001236 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001237 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001238 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001239 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001240 clear_tv(&var2);
1241 return NULL;
1242 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001243 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001244 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001245 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001246 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001247 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001248
Bram Moolenaar8c711452005-01-14 21:53:12 +00001249 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001250 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001251 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001252 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001253 clear_tv(&var1);
1254 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001255 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001256 }
1257
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001258 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001259 ++p;
1260 }
1261
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001262 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001263 {
1264 if (len == -1)
1265 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001266 // "[key]": get key from "var1"
1267 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001268 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001269 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001270 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001271 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001272 }
1273 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001274 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001275
1276 // a NULL dict is equivalent with an empty dict
1277 if (lp->ll_tv->vval.v_dict == NULL)
1278 {
1279 lp->ll_tv->vval.v_dict = dict_alloc();
1280 if (lp->ll_tv->vval.v_dict == NULL)
1281 {
1282 clear_tv(&var1);
1283 return NULL;
1284 }
1285 ++lp->ll_tv->vval.v_dict->dv_refcount;
1286 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001287 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001288
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001289 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001290
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001291 // When assigning to a scope dictionary check that a function and
1292 // variable name is valid (only variable name unless it is l: or
1293 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001294 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001295 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001296 int prevval;
1297 int wrong;
1298
1299 if (len != -1)
1300 {
1301 prevval = key[len];
1302 key[len] = NUL;
1303 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001304 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001305 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001306 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1307 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001308 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001309 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001310 if (len != -1)
1311 key[len] = prevval;
1312 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001313 {
1314 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001315 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001316 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001317 }
1318
Bram Moolenaar10c65862020-10-08 21:16:42 +02001319 if (lp->ll_valtype != NULL)
1320 // use the type of the member
1321 lp->ll_valtype = lp->ll_valtype->tt_member;
1322
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001323 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001324 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001325 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001326 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001327 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001328 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001329 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001330 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001331 return NULL;
1332 }
1333
Bram Moolenaar31b81602019-02-10 22:14:27 +01001334 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001335 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001336 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001337 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001338 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001339 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001340 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001341 }
1342 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001343 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001344 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001345 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001346 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001347 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001348 p = NULL;
1349 break;
1350 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001351 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001352 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001353 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1354 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001355 {
1356 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001357 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001358 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001359
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001360 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001361 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001362 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001363 else if (lp->ll_tv->v_type == VAR_BLOB)
1364 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001365 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1366
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001367 /*
1368 * Get the number and item for the only or first index of the List.
1369 */
1370 if (empty1)
1371 lp->ll_n1 = 0;
1372 else
1373 // is number or string
1374 lp->ll_n1 = (long)tv_get_number(&var1);
1375 clear_tv(&var1);
1376
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001377 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001378 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001379 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001380 return NULL;
1381 }
1382 if (lp->ll_range && !lp->ll_empty2)
1383 {
1384 lp->ll_n2 = (long)tv_get_number(&var2);
1385 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001386 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1387 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001388 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001389 }
1390 lp->ll_blob = lp->ll_tv->vval.v_blob;
1391 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001392 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001393 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001394 else
1395 {
1396 /*
1397 * Get the number and item for the only or first index of the List.
1398 */
1399 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001400 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001401 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001402 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001403 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001404 clear_tv(&var1);
1405
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001406 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001407 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001408 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1409 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001410 if (lp->ll_li == NULL)
1411 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001412 clear_tv(&var2);
1413 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001414 }
1415
Bram Moolenaar10c65862020-10-08 21:16:42 +02001416 if (lp->ll_valtype != NULL)
1417 // use the type of the member
1418 lp->ll_valtype = lp->ll_valtype->tt_member;
1419
Bram Moolenaar8c711452005-01-14 21:53:12 +00001420 /*
1421 * May need to find the item or absolute index for the second
1422 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001423 * When no index given: "lp->ll_empty2" is TRUE.
1424 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001425 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001426 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001427 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001428 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001429 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001430 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001431 if (check_range_index_two(lp->ll_list,
1432 &lp->ll_n1, lp->ll_li,
1433 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001434 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001435 }
1436
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001437 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001438 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001439 }
1440
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001441 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001442 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001443 return p;
1444}
1445
1446/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001447 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001448 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001449 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001450clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001451{
1452 vim_free(lp->ll_exp_name);
1453 vim_free(lp->ll_newkey);
1454}
1455
1456/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001457 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001458 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001459 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1460 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001461 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001462 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001463set_var_lval(
1464 lval_T *lp,
1465 char_u *endp,
1466 typval_T *rettv,
1467 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001468 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1469 char_u *op,
1470 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001471{
1472 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001473 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001474
1475 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001476 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001477 cc = *endp;
1478 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001479 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1480 return;
1481
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001482 if (lp->ll_blob != NULL)
1483 {
1484 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001485
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001486 if (op != NULL && *op != '=')
1487 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001488 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001489 return;
1490 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001491 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001492 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001493
1494 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1495 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001496 if (lp->ll_empty2)
1497 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1498
Bram Moolenaar68452172021-04-12 21:21:02 +02001499 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1500 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001501 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001502 }
1503 else
1504 {
1505 val = (int)tv_get_number_chk(rettv, &error);
1506 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001507 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001508 }
1509 }
1510 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001511 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001512 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001513
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001514 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1515 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001516 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001517 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001518 *endp = cc;
1519 return;
1520 }
1521
Bram Moolenaarff697e62019-02-12 22:28:33 +01001522 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001523 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001524 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001525 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001526 {
1527 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001528 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1529 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001530 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001531 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001532 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001533 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001534 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001535 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001536 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001537 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001538 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1539 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001540 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001541 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001542 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001543 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001544 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001545 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001546 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001547 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001548 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001549 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001550 else if (lp->ll_range)
1551 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001552 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1553 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001554 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001555 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001556 return;
1557 }
1558
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001559 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1560 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001561 }
1562 else
1563 {
1564 /*
1565 * Assign to a List or Dictionary item.
1566 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001567 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1568 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001569 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001570 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001571 return;
1572 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001573
1574 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001575 && check_typval_arg_type(lp->ll_valtype, rettv,
1576 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001577 return;
1578
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001579 if (lp->ll_newkey != NULL)
1580 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001581 if (op != NULL && *op != '=')
1582 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001583 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001584 return;
1585 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001586 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1587 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001588 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001589
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001590 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001591 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001592 if (di == NULL)
1593 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001594 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1595 {
1596 vim_free(di);
1597 return;
1598 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001599 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001600 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001601 else if (op != NULL && *op != '=')
1602 {
1603 tv_op(lp->ll_tv, rettv, op);
1604 return;
1605 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001606 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001607 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001608
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001609 /*
1610 * Assign the value to the variable or list item.
1611 */
1612 if (copy)
1613 copy_tv(rettv, lp->ll_tv);
1614 else
1615 {
1616 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001617 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001618 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001619 }
1620 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001621}
1622
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001623/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001624 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1625 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001626 * Returns OK or FAIL.
1627 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001628 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001629tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001630{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001631 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001632 char_u numbuf[NUMBUFLEN];
1633 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001634 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001635
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001636 // Can't do anything with a Funcref or Dict on the right.
1637 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001638 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001639 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1640 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001641 {
1642 switch (tv1->v_type)
1643 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001644 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001645 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001646 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001647 case VAR_DICT:
1648 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001649 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001650 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001651 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001652 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001653 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001654 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001655 break;
1656
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001657 case VAR_BLOB:
1658 if (*op != '+' || tv2->v_type != VAR_BLOB)
1659 break;
1660 // BLOB += BLOB
1661 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1662 {
1663 blob_T *b1 = tv1->vval.v_blob;
1664 blob_T *b2 = tv2->vval.v_blob;
1665 int i, len = blob_len(b2);
1666 for (i = 0; i < len; i++)
1667 ga_append(&b1->bv_ga, blob_get(b2, i));
1668 }
1669 return OK;
1670
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001671 case VAR_LIST:
1672 if (*op != '+' || tv2->v_type != VAR_LIST)
1673 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001674 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001675 if (tv2->vval.v_list != NULL)
1676 {
1677 if (tv1->vval.v_list == NULL)
1678 {
1679 tv1->vval.v_list = tv2->vval.v_list;
1680 ++tv1->vval.v_list->lv_refcount;
1681 }
1682 else
1683 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1684 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001685 return OK;
1686
1687 case VAR_NUMBER:
1688 case VAR_STRING:
1689 if (tv2->v_type == VAR_LIST)
1690 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001691 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001692 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001693 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001694 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001695#ifdef FEAT_FLOAT
1696 if (tv2->v_type == VAR_FLOAT)
1697 {
1698 float_T f = n;
1699
Bram Moolenaarff697e62019-02-12 22:28:33 +01001700 if (*op == '%')
1701 break;
1702 switch (*op)
1703 {
1704 case '+': f += tv2->vval.v_float; break;
1705 case '-': f -= tv2->vval.v_float; break;
1706 case '*': f *= tv2->vval.v_float; break;
1707 case '/': f /= tv2->vval.v_float; break;
1708 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001709 clear_tv(tv1);
1710 tv1->v_type = VAR_FLOAT;
1711 tv1->vval.v_float = f;
1712 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001713 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001714#endif
1715 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001716 switch (*op)
1717 {
1718 case '+': n += tv_get_number(tv2); break;
1719 case '-': n -= tv_get_number(tv2); break;
1720 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001721 case '/': n = num_divide(n, tv_get_number(tv2),
1722 &failed); break;
1723 case '%': n = num_modulus(n, tv_get_number(tv2),
1724 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001725 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001726 clear_tv(tv1);
1727 tv1->v_type = VAR_NUMBER;
1728 tv1->vval.v_number = n;
1729 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001730 }
1731 else
1732 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001733 if (tv2->v_type == VAR_FLOAT)
1734 break;
1735
Bram Moolenaarff697e62019-02-12 22:28:33 +01001736 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001737 s = tv_get_string(tv1);
1738 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001739 clear_tv(tv1);
1740 tv1->v_type = VAR_STRING;
1741 tv1->vval.v_string = s;
1742 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001743 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001744
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001745 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001746#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001747 {
1748 float_T f;
1749
Bram Moolenaarff697e62019-02-12 22:28:33 +01001750 if (*op == '%' || *op == '.'
1751 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001752 && tv2->v_type != VAR_NUMBER
1753 && tv2->v_type != VAR_STRING))
1754 break;
1755 if (tv2->v_type == VAR_FLOAT)
1756 f = tv2->vval.v_float;
1757 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001758 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001759 switch (*op)
1760 {
1761 case '+': tv1->vval.v_float += f; break;
1762 case '-': tv1->vval.v_float -= f; break;
1763 case '*': tv1->vval.v_float *= f; break;
1764 case '/': tv1->vval.v_float /= f; break;
1765 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001766 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001767#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001768 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001769 }
1770 }
1771
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001772 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001773 return FAIL;
1774}
1775
1776/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001777 * Evaluate the expression used in a ":for var in expr" command.
1778 * "arg" points to "var".
1779 * Set "*errp" to TRUE for an error, FALSE otherwise;
1780 * Return a pointer that holds the info. Null when there is an error.
1781 */
1782 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001783eval_for_line(
1784 char_u *arg,
1785 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001786 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001787 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001788{
Bram Moolenaar33570922005-01-25 22:26:29 +00001789 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001790 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001791 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001792 typval_T tv;
1793 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001794 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001795
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001796 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001797
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001798 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001799 if (fi == NULL)
1800 return NULL;
1801
Bram Moolenaar404557e2021-07-05 21:41:48 +02001802 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1803 &fi->fi_semicolon, FALSE);
1804 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001805 return fi;
1806
Bram Moolenaar404557e2021-07-05 21:41:48 +02001807 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001808 if (expr[0] != 'i' || expr[1] != 'n'
1809 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001810 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001811 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1812 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1813 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001814 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001815 return fi;
1816 }
1817
1818 if (skip)
1819 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001820 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1821 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001822 {
1823 *errp = FALSE;
1824 if (!skip)
1825 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001826 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001827 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001828 l = tv.vval.v_list;
1829 if (l == NULL)
1830 {
1831 // a null list is like an empty list: do nothing
1832 clear_tv(&tv);
1833 }
1834 else
1835 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001836 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001837 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001838
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001839 // No need to increment the refcount, it's already set for
1840 // the list being used in "tv".
1841 fi->fi_list = l;
1842 list_add_watch(l, &fi->fi_lw);
1843 fi->fi_lw.lw_item = l->lv_first;
1844 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001845 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001846 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001847 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001848 fi->fi_bi = 0;
1849 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001850 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001851 typval_T btv;
1852
1853 // Make a copy, so that the iteration still works when the
1854 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001855 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001856 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001857 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001858 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001859 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001860 else if (tv.v_type == VAR_STRING)
1861 {
1862 fi->fi_byte_idx = 0;
1863 fi->fi_string = tv.vval.v_string;
1864 tv.vval.v_string = NULL;
1865 if (fi->fi_string == NULL)
1866 fi->fi_string = vim_strsave((char_u *)"");
1867 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001868 else
1869 {
Sean Dewar80d73952021-08-04 19:25:54 +02001870 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001871 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001872 }
1873 }
1874 }
1875 if (skip)
1876 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001877 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001878
1879 return fi;
1880}
1881
1882/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001883 * Used when looping over a :for line, skip the "in expr" part.
1884 */
1885 void
1886skip_for_lines(void *fi_void, evalarg_T *evalarg)
1887{
1888 forinfo_T *fi = (forinfo_T *)fi_void;
1889 int i;
1890
1891 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01001892 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001893}
1894
1895/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001896 * Use the first item in a ":for" list. Advance to the next.
1897 * Assign the values to the variable (list). "arg" points to the first one.
1898 * Return TRUE when a valid item was found, FALSE when at end of list or
1899 * something wrong.
1900 */
1901 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001902next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001903{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001904 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001905 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001906 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001907 ? (ASSIGN_FINAL
1908 // first round: error if variable exists
1909 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1910 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001911 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001912 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001913 int skip_assign = in_vim9script() && arg[0] == '_'
1914 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001915
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001916 if (fi->fi_blob != NULL)
1917 {
1918 typval_T tv;
1919
1920 if (fi->fi_bi >= blob_len(fi->fi_blob))
1921 return FALSE;
1922 tv.v_type = VAR_NUMBER;
1923 tv.v_lock = VAR_FIXED;
1924 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1925 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001926 if (skip_assign)
1927 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001928 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001929 fi->fi_varcount, flag, NULL) == OK;
1930 }
1931
1932 if (fi->fi_string != NULL)
1933 {
1934 typval_T tv;
1935 int len;
1936
1937 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1938 if (len == 0)
1939 return FALSE;
1940 tv.v_type = VAR_STRING;
1941 tv.v_lock = VAR_FIXED;
1942 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1943 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001944 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001945 if (skip_assign)
1946 result = TRUE;
1947 else
1948 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001949 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001950 vim_free(tv.vval.v_string);
1951 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001952 }
1953
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001954 item = fi->fi_lw.lw_item;
1955 if (item == NULL)
1956 result = FALSE;
1957 else
1958 {
1959 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001960 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001961 if (skip_assign)
1962 result = TRUE;
1963 else
1964 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001965 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 }
1967 return result;
1968}
1969
1970/*
1971 * Free the structure used to store info used by ":for".
1972 */
1973 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001974free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001975{
Bram Moolenaar33570922005-01-25 22:26:29 +00001976 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001977
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001978 if (fi == NULL)
1979 return;
1980 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001981 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001982 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001983 list_unref(fi->fi_list);
1984 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001985 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001986 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001987 else
1988 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989 vim_free(fi);
1990}
1991
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001993set_context_for_expression(
1994 expand_T *xp,
1995 char_u *arg,
1996 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001998 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002000 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002002 if (cmdidx == CMD_let || cmdidx == CMD_var
2003 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002004 {
2005 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002006 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002007 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002008 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002009 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002010 {
2011 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002012 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002013 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002014 break;
2015 }
2016 return;
2017 }
2018 }
2019 else
2020 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2021 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 while ((xp->xp_pattern = vim_strpbrk(arg,
2023 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2024 {
2025 c = *xp->xp_pattern;
2026 if (c == '&')
2027 {
2028 c = xp->xp_pattern[1];
2029 if (c == '&')
2030 {
2031 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002032 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 }
2034 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002035 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002037 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2038 xp->xp_pattern += 2;
2039
2040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 }
2042 else if (c == '$')
2043 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002044 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 xp->xp_context = EXPAND_ENV_VARS;
2046 }
2047 else if (c == '=')
2048 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002049 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 xp->xp_context = EXPAND_EXPRESSION;
2051 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002052 else if (c == '#'
2053 && xp->xp_context == EXPAND_EXPRESSION)
2054 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002055 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002056 break;
2057 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002058 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 && xp->xp_context == EXPAND_FUNCTIONS
2060 && vim_strchr(xp->xp_pattern, '(') == NULL)
2061 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002062 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 break;
2064 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002065 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002067 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 {
2069 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2070 if (c == '\\' && xp->xp_pattern[1] != NUL)
2071 ++xp->xp_pattern;
2072 xp->xp_context = EXPAND_NOTHING;
2073 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002074 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002076 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2078 /* skip */ ;
2079 xp->xp_context = EXPAND_NOTHING;
2080 }
2081 else if (c == '|')
2082 {
2083 if (xp->xp_pattern[1] == '|')
2084 {
2085 ++xp->xp_pattern;
2086 xp->xp_context = EXPAND_EXPRESSION;
2087 }
2088 else
2089 xp->xp_context = EXPAND_COMMANDS;
2090 }
2091 else
2092 xp->xp_context = EXPAND_EXPRESSION;
2093 }
2094 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002095 // Doesn't look like something valid, expand as an expression
2096 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002097 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 arg = xp->xp_pattern;
2099 if (*arg != NUL)
2100 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2101 /* skip */ ;
2102 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002103
2104 // ":exe one two" completes "two"
2105 if ((cmdidx == CMD_execute
2106 || cmdidx == CMD_echo
2107 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002108 || cmdidx == CMD_echomsg
2109 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002110 && xp->xp_context == EXPAND_EXPRESSION)
2111 {
2112 for (;;)
2113 {
2114 char_u *n = skiptowhite(arg);
2115
2116 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2117 break;
2118 arg = skipwhite(n);
2119 }
2120 }
2121
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 xp->xp_pattern = arg;
2123}
2124
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002126 * Return TRUE if "pat" matches "text".
2127 * Does not use 'cpo' and always uses 'magic'.
2128 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002129 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002130pattern_match(char_u *pat, char_u *text, int ic)
2131{
2132 int matches = FALSE;
2133 char_u *save_cpo;
2134 regmatch_T regmatch;
2135
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002136 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002137 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002138 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002139 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2140 if (regmatch.regprog != NULL)
2141 {
2142 regmatch.rm_ic = ic;
2143 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2144 vim_regfree(regmatch.regprog);
2145 }
2146 p_cpo = save_cpo;
2147 return matches;
2148}
2149
2150/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002151 * Handle a name followed by "(". Both for just "name(arg)" and for
2152 * "expr->name(arg)".
2153 * Returns OK or FAIL.
2154 */
2155 static int
2156eval_func(
2157 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002158 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002159 char_u *name,
2160 int name_len,
2161 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002162 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002163 typval_T *basetv) // "expr" for "expr->name(arg)"
2164{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002165 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002166 char_u *s = name;
2167 int len = name_len;
2168 partial_T *partial;
2169 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002170 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002171 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002172
2173 if (!evaluate)
2174 check_vars(s, len);
2175
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002176 // If "s" is the name of a variable of type VAR_FUNC
2177 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002178 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002179 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002180
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002181 // Need to make a copy, in case evaluating the arguments makes
2182 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002183 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002184 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002185 ret = FAIL;
2186 else
2187 {
2188 funcexe_T funcexe;
2189
2190 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002191 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002192 funcexe.fe_firstline = curwin->w_cursor.lnum;
2193 funcexe.fe_lastline = curwin->w_cursor.lnum;
2194 funcexe.fe_evaluate = evaluate;
2195 funcexe.fe_partial = partial;
2196 funcexe.fe_basetv = basetv;
2197 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002198 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002199 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002200 }
2201 vim_free(s);
2202
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002203 // If evaluate is FALSE rettv->v_type was not set in
2204 // get_func_tv, but it's needed in handle_subscript() to parse
2205 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002206 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2207 {
2208 rettv->vval.v_string = NULL;
2209 rettv->v_type = VAR_FUNC;
2210 }
2211
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002212 // Stop the expression evaluation when immediately
2213 // aborting on error, or when an interrupt occurred or
2214 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002215 if (evaluate && aborting())
2216 {
2217 if (ret == OK)
2218 clear_tv(rettv);
2219 ret = FAIL;
2220 }
2221 return ret;
2222}
2223
2224/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002225 * After a NL, skip over empty lines and comment-only lines.
2226 */
2227 static char_u *
2228newline_skip_comments(char_u *arg)
2229{
2230 char_u *p = arg + 1;
2231
2232 for (;;)
2233 {
2234 p = skipwhite(p);
2235
2236 if (*p == NUL)
2237 break;
2238 if (vim9_comment_start(p))
2239 {
2240 char_u *nl = vim_strchr(p, NL);
2241
2242 if (nl == NULL)
2243 break;
2244 p = nl;
2245 }
2246 if (*p != NL)
2247 break;
2248 ++p; // skip another NL
2249 }
2250 return p;
2251}
2252
2253/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002254 * Get the next line source line without advancing. But do skip over comment
2255 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002256 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002257 */
2258 static char_u *
2259getline_peek_skip_comments(evalarg_T *evalarg)
2260{
2261 for (;;)
2262 {
2263 char_u *next = getline_peek(evalarg->eval_getline,
2264 evalarg->eval_cookie);
2265 char_u *p;
2266
2267 if (next == NULL)
2268 break;
2269 p = skipwhite(next);
2270 if (*p != NUL && !vim9_comment_start(p))
2271 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002272 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002273 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002274 }
2275 return NULL;
2276}
2277
2278/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002279 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2280 * comment) and there is a next line, return the next line (skipping blanks)
2281 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002282 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2283 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002284 * "arg" must point somewhere inside a line, not at the start.
2285 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002286 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002287eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2288{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002289 char_u *p = skipwhite(arg);
2290
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002291 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002292 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002293 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002294 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2295 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002296 && (*p == NUL || *p == NL
2297 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002298 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002299 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002300
Bram Moolenaare442d592022-05-05 12:20:28 +01002301 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002302 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002303 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002304 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002305 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002306 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002307
Bram Moolenaar9d489562020-07-30 20:08:50 +02002308 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002309 {
2310 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002311 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002312 }
2313 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002314 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002315}
2316
2317/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002318 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002319 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002320 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002321 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002322eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002323{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002324 garray_T *gap = &evalarg->eval_ga;
2325 char_u *line;
2326
Bram Moolenaar39be4982022-05-06 21:51:50 +01002327 if (arg != NULL)
2328 {
2329 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002330 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002331 // Truncate before a trailing comment, so that concatenating the lines
2332 // won't turn the rest into a comment.
2333 if (*skipwhite(arg) == '#')
2334 *arg = NUL;
2335 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002336
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002337 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002338 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2339 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002340 else
2341 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002342 if (line == NULL)
2343 return NULL;
2344
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002345 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002346 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2347 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002348 char_u *p = skipwhite(line);
2349
2350 // Going to concatenate the lines after parsing. For an empty or
2351 // comment line use an empty string.
2352 if (*p == NUL || vim9_comment_start(p))
2353 {
2354 vim_free(line);
2355 line = vim_strsave((char_u *)"");
2356 }
2357
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002358 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2359 ++gap->ga_len;
2360 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002361 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002362 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002363 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002364 evalarg->eval_tofree = line;
2365 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002366
2367 // Advanced to the next line, "arg" no longer points into the previous
2368 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002369 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002370 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002371}
2372
Bram Moolenaare6b53242020-07-01 17:28:33 +02002373/*
2374 * Call eval_next_non_blank() and get the next line if needed.
2375 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002376 char_u *
2377skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2378{
2379 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002380 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002381
Bram Moolenaar962d7212020-07-04 14:15:00 +02002382 if (evalarg == NULL)
2383 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002384 eval_next_non_blank(p, evalarg, &getnext);
2385 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002386 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002387 return p;
2388}
2389
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002390/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002392 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2394 */
2395
2396/*
2397 * Handle zero level expression.
2398 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002399 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002400 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002401 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 * Return OK or FAIL.
2403 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002404 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002405eval0(
2406 char_u *arg,
2407 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002408 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002409 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002411 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2412}
2413
2414/*
2415 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2416 * expression and don't check what comes after the expression.
2417 */
2418 int
2419eval0_retarg(
2420 char_u *arg,
2421 typval_T *rettv,
2422 exarg_T *eap,
2423 evalarg_T *evalarg,
2424 char_u **retarg)
2425{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 int ret;
2427 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002428 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002429 int did_emsg_before = did_emsg;
2430 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002431 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002432 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002433 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434
2435 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002436 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002437
Bram Moolenaar79481362022-06-27 20:15:10 +01002438 if (ret != FAIL)
2439 {
2440 expr_end = p;
2441 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002442
Bram Moolenaar79481362022-06-27 20:15:10 +01002443 // In Vim9 script a command block is not split at NL characters for
2444 // commands using an expression argument. Skip over a '#' comment to
2445 // check for a following NL. Require white space before the '#'.
2446 if (in_vim9script() && p > expr_end && retarg == NULL)
2447 while (*p == '#')
2448 {
2449 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002450
Bram Moolenaar79481362022-06-27 20:15:10 +01002451 if (nl == NULL)
2452 break;
2453 p = skipwhite(nl + 1);
2454 if (eap != NULL && *p != NUL)
2455 eap->nextcmd = p;
2456 check_for_end = FALSE;
2457 }
2458
2459 if (check_for_end)
2460 end_error = !ends_excmd2(arg, p);
2461 }
2462
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002463 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 {
2465 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 /*
2468 * Report the invalid expression unless the expression evaluation has
2469 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002470 * exception, or we already gave a more specific error.
2471 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002473 if (!aborting()
2474 && did_emsg == did_emsg_before
2475 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002476 && (flags & EVAL_CONSTANT) == 0
2477 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002478 {
2479 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002480 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002481 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002482 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002483 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002484
2485 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002486 // a next command to avoid more errors, unless "|" is following, which
2487 // could only be a command separator.
Bram Moolenaar79481362022-06-27 20:15:10 +01002488 if (eap != NULL && p != NULL
2489 && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
Bram Moolenaar8143a532020-12-13 20:26:29 +01002490 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002491 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002493
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002494 if (retarg != NULL)
2495 *retarg = p;
2496 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002497 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002498
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 return ret;
2500}
2501
2502/*
2503 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002504 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002505 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 *
2507 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002508 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002510 * Note: "rettv.v_lock" is not set.
2511 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 * Return OK or FAIL.
2513 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002514 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002515eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002517 char_u *p;
2518 int getnext;
2519
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002520 CLEAR_POINTER(rettv);
2521
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 /*
2523 * Get the first variable.
2524 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002525 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 return FAIL;
2527
Bram Moolenaar793648f2020-06-26 21:28:25 +02002528 p = eval_next_non_blank(*arg, evalarg, &getnext);
2529 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002531 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002532 int result;
2533 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002534 evalarg_T *evalarg_used = evalarg;
2535 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002536 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002537 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002538 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002539
2540 if (evalarg == NULL)
2541 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002542 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002543 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002544 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002545 orig_flags = evalarg_used->eval_flags;
2546 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002547
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002548 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002549 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002550 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002551 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002552 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002553 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002554 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002555 clear_tv(rettv);
2556 return FAIL;
2557 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002558 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002559 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002560
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002562 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002564 int error = FALSE;
2565
Bram Moolenaar13106602020-10-04 16:06:05 +02002566 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002567 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002568 else if (vim9script)
2569 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002570 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002572 if (error || !op_falsy || !result)
2573 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002574 if (error)
2575 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 }
2577
2578 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002579 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002581 if (op_falsy)
2582 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002583 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002584 {
mityu4ac198c2021-05-28 17:52:40 +02002585 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002586 clear_tv(rettv);
2587 return FAIL;
2588 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002589 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002590 evalarg_used->eval_flags = (op_falsy ? !result : result)
2591 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2592 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002593 {
2594 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002596 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002597 if (!op_falsy || !result)
2598 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002600 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002602 /*
2603 * Check for the ":".
2604 */
2605 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2606 if (*p != ':')
2607 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002608 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002609 if (evaluate && result)
2610 clear_tv(rettv);
2611 evalarg_used->eval_flags = orig_flags;
2612 return FAIL;
2613 }
2614 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002615 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002616 else
2617 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002618 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002619 {
2620 error_white_both(p, 1);
2621 clear_tv(rettv);
2622 evalarg_used->eval_flags = orig_flags;
2623 return FAIL;
2624 }
2625 *arg = p;
2626 }
2627
2628 /*
2629 * Get the third variable. Recursive!
2630 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002631 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002632 {
mityu4ac198c2021-05-28 17:52:40 +02002633 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002634 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002635 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002636 return FAIL;
2637 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002638 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2639 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002640 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002641 if (eval1(arg, &var2, evalarg_used) == FAIL)
2642 {
2643 if (evaluate && result)
2644 clear_tv(rettv);
2645 evalarg_used->eval_flags = orig_flags;
2646 return FAIL;
2647 }
2648 if (evaluate && !result)
2649 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002651
2652 if (evalarg == NULL)
2653 clear_evalarg(&local_evalarg, NULL);
2654 else
2655 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 }
2657
2658 return OK;
2659}
2660
2661/*
2662 * Handle first level expression:
2663 * expr2 || expr2 || expr2 logical OR
2664 *
2665 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002666 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 *
2668 * Return OK or FAIL.
2669 */
2670 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002671eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002673 char_u *p;
2674 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675
2676 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002677 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002679 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 return FAIL;
2681
2682 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002683 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002685 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002686 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002688 evalarg_T *evalarg_used = evalarg;
2689 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002690 int evaluate;
2691 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002692 long result = FALSE;
2693 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002694 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002695 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002696
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002697 if (evalarg == NULL)
2698 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002699 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002700 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002701 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002702 orig_flags = evalarg_used->eval_flags;
2703 evaluate = orig_flags & EVAL_EVALUATE;
2704 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002705 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002706 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002707 result = tv_get_bool_chk(rettv, &error);
2708 else if (tv_get_number_chk(rettv, &error) != 0)
2709 result = TRUE;
2710 clear_tv(rettv);
2711 if (error)
2712 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 }
2714
2715 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002716 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002718 while (p[0] == '|' && p[1] == '|')
2719 {
2720 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002721 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002722 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002723 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002724 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002725 {
2726 error_white_both(p, 2);
2727 clear_tv(rettv);
2728 return FAIL;
2729 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002730 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002731 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002732
2733 /*
2734 * Get the second variable.
2735 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002736 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002737 {
mityu4ac198c2021-05-28 17:52:40 +02002738 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002739 clear_tv(rettv);
2740 return FAIL;
2741 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002742 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2743 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002744 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002745 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002746 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002747
2748 /*
2749 * Compute the result.
2750 */
2751 if (evaluate && !result)
2752 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002753 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002754 result = tv_get_bool_chk(&var2, &error);
2755 else if (tv_get_number_chk(&var2, &error) != 0)
2756 result = TRUE;
2757 clear_tv(&var2);
2758 if (error)
2759 return FAIL;
2760 }
2761 if (evaluate)
2762 {
2763 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002764 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002765 rettv->v_type = VAR_BOOL;
2766 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002767 }
2768 else
2769 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002770 rettv->v_type = VAR_NUMBER;
2771 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002772 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002773 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002774
2775 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002777
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002778 if (evalarg == NULL)
2779 clear_evalarg(&local_evalarg, NULL);
2780 else
2781 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 }
2783
2784 return OK;
2785}
2786
2787/*
2788 * Handle second level expression:
2789 * expr3 && expr3 && expr3 logical AND
2790 *
2791 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002792 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 *
2794 * Return OK or FAIL.
2795 */
2796 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002797eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002799 char_u *p;
2800 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801
2802 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002803 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002805 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 return FAIL;
2807
2808 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002809 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002811 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002812 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002814 evalarg_T *evalarg_used = evalarg;
2815 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002816 int orig_flags;
2817 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002818 long result = TRUE;
2819 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002820 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002821 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002822
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002823 if (evalarg == NULL)
2824 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002825 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002826 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002827 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002828 orig_flags = evalarg_used->eval_flags;
2829 evaluate = orig_flags & EVAL_EVALUATE;
2830 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002831 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002832 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002833 result = tv_get_bool_chk(rettv, &error);
2834 else if (tv_get_number_chk(rettv, &error) == 0)
2835 result = FALSE;
2836 clear_tv(rettv);
2837 if (error)
2838 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 }
2840
2841 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002842 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002844 while (p[0] == '&' && p[1] == '&')
2845 {
2846 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002847 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002848 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002849 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002850 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002851 {
2852 error_white_both(p, 2);
2853 clear_tv(rettv);
2854 return FAIL;
2855 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002856 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002857 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002858
2859 /*
2860 * Get the second variable.
2861 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002862 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002863 {
mityu4ac198c2021-05-28 17:52:40 +02002864 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002865 clear_tv(rettv);
2866 return FAIL;
2867 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002868 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2869 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002870 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002871 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002872 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002873 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002874
2875 /*
2876 * Compute the result.
2877 */
2878 if (evaluate && result)
2879 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002880 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002881 result = tv_get_bool_chk(&var2, &error);
2882 else if (tv_get_number_chk(&var2, &error) == 0)
2883 result = FALSE;
2884 clear_tv(&var2);
2885 if (error)
2886 return FAIL;
2887 }
2888 if (evaluate)
2889 {
2890 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002891 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002892 rettv->v_type = VAR_BOOL;
2893 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002894 }
2895 else
2896 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002897 rettv->v_type = VAR_NUMBER;
2898 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002899 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002900 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002901
2902 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002904
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002905 if (evalarg == NULL)
2906 clear_evalarg(&local_evalarg, NULL);
2907 else
2908 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 }
2910
2911 return OK;
2912}
2913
2914/*
2915 * Handle third level expression:
2916 * var1 == var2
2917 * var1 =~ var2
2918 * var1 != var2
2919 * var1 !~ var2
2920 * var1 > var2
2921 * var1 >= var2
2922 * var1 < var2
2923 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002924 * var1 is var2
2925 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 *
2927 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002928 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 *
2930 * Return OK or FAIL.
2931 */
2932 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002933eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002936 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002937 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002939 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940
2941 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002942 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002944 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 return FAIL;
2946
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002947 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002948
Bram Moolenaar696ba232020-07-29 21:20:41 +02002949 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950
2951 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002952 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002954 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002956 typval_T var2;
2957 int ic;
2958 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002959 int evaluate = evalarg == NULL
2960 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002961 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002962
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002963 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002964 {
Bram Moolenaare442d592022-05-05 12:20:28 +01002965 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002966 p = *arg;
2967 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002968 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2969 {
mityu4ac198c2021-05-28 17:52:40 +02002970 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002971 clear_tv(rettv);
2972 return FAIL;
2973 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002974
Bram Moolenaar696ba232020-07-29 21:20:41 +02002975 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2976 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002977 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002978 clear_tv(rettv);
2979 return FAIL;
2980 }
2981
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002982 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 if (p[len] == '?')
2984 {
2985 ic = TRUE;
2986 ++len;
2987 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002988 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 else if (p[len] == '#')
2990 {
2991 ic = FALSE;
2992 ++len;
2993 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002994 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002996 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997
2998 /*
2999 * Get the second variable.
3000 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003001 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3002 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003003 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003004 clear_tv(rettv);
3005 return FAIL;
3006 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003007 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003008 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003010 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 return FAIL;
3012 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003013 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003014 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003015 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003016
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003017 // use the line of the comparison for messages
3018 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003019 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003020 {
3021 ret = FAIL;
3022 clear_tv(rettv);
3023 }
3024 else
3025 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003026 clear_tv(&var2);
3027 return ret;
3028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 }
3030
3031 return OK;
3032}
3033
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003034/*
3035 * Make a copy of blob "tv1" and append blob "tv2".
3036 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 void
3038eval_addblob(typval_T *tv1, typval_T *tv2)
3039{
3040 blob_T *b1 = tv1->vval.v_blob;
3041 blob_T *b2 = tv2->vval.v_blob;
3042 blob_T *b = blob_alloc();
3043 int i;
3044
3045 if (b != NULL)
3046 {
3047 for (i = 0; i < blob_len(b1); i++)
3048 ga_append(&b->bv_ga, blob_get(b1, i));
3049 for (i = 0; i < blob_len(b2); i++)
3050 ga_append(&b->bv_ga, blob_get(b2, i));
3051
3052 clear_tv(tv1);
3053 rettv_blob_set(tv1, b);
3054 }
3055}
3056
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003057/*
3058 * Make a copy of list "tv1" and append list "tv2".
3059 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060 int
3061eval_addlist(typval_T *tv1, typval_T *tv2)
3062{
3063 typval_T var3;
3064
3065 // concatenate Lists
3066 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3067 {
3068 clear_tv(tv1);
3069 clear_tv(tv2);
3070 return FAIL;
3071 }
3072 clear_tv(tv1);
3073 *tv1 = var3;
3074 return OK;
3075}
3076
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003078 * Handle the bitwise left/right shift operator expression:
3079 * var1 << var2
3080 * var1 >> var2
3081 *
3082 * "arg" must point to the first non-white of the expression.
3083 * "arg" is advanced to just after the recognized expression.
3084 *
3085 * Return OK or FAIL.
3086 */
3087 static int
3088eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3089{
3090 /*
3091 * Get the first expression.
3092 */
3093 if (eval6(arg, rettv, evalarg) == FAIL)
3094 return FAIL;
3095
3096 /*
3097 * Repeat computing, until no '<<' or '>>' is following.
3098 */
3099 for (;;)
3100 {
3101 char_u *p;
3102 int getnext;
3103 exprtype_T type;
3104 int evaluate;
3105 typval_T var2;
3106 int vim9script;
3107
3108 p = eval_next_non_blank(*arg, evalarg, &getnext);
3109 if (p[0] == '<' && p[1] == '<')
3110 type = EXPR_LSHIFT;
3111 else if (p[0] == '>' && p[1] == '>')
3112 type = EXPR_RSHIFT;
3113 else
3114 return OK;
3115
3116 // Handle a bitwise left or right shift operator
3117 if (rettv->v_type != VAR_NUMBER)
3118 {
3119 // left operand should be a number
3120 emsg(_(e_bitshift_ops_must_be_number));
3121 clear_tv(rettv);
3122 return FAIL;
3123 }
3124
3125 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3126 vim9script = in_vim9script();
3127 if (getnext)
3128 {
3129 *arg = eval_next_line(*arg, evalarg);
3130 p = *arg;
3131 }
3132 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3133 {
3134 error_white_both(*arg, 2);
3135 clear_tv(rettv);
3136 return FAIL;
3137 }
3138
3139 /*
3140 * Get the second variable.
3141 */
3142 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3143 {
3144 error_white_both(p, 2);
3145 clear_tv(rettv);
3146 return FAIL;
3147 }
3148 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3149 if (eval6(arg, &var2, evalarg) == FAIL)
3150 {
3151 clear_tv(rettv);
3152 return FAIL;
3153 }
3154
3155 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3156 {
3157 // right operand should be a positive number
3158 if (var2.v_type != VAR_NUMBER)
3159 emsg(_(e_bitshift_ops_must_be_number));
3160 else
3161 emsg(_(e_bitshift_ops_must_be_postive));
3162 clear_tv(rettv);
3163 clear_tv(&var2);
3164 return FAIL;
3165 }
3166
3167 if (evaluate)
3168 {
3169 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3170 // shifting more bits than we have always results in zero
3171 rettv->vval.v_number = 0;
3172 else if (type == EXPR_LSHIFT)
3173 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003174 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003175 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003176 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003177 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003178 }
3179
3180 clear_tv(&var2);
3181 }
3182
3183 return OK;
3184}
3185
3186/*
3187 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003188 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003190 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003191 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 *
3193 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003194 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 *
3196 * Return OK or FAIL.
3197 */
3198 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003199eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003202 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003204 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 return FAIL;
3206
3207 /*
3208 * Repeat computing, until no '+', '-' or '.' is following.
3209 */
3210 for (;;)
3211 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003212 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003213 int getnext;
3214 char_u *p;
3215 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003216 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003217 int concat;
3218 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003219 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003220
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003221 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003222 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003223 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003224 p = eval_next_non_blank(*arg, evalarg, &getnext);
3225 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003226 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003227 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3228 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003230 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3231 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003232
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003233 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003234 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003235 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003236 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003237 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003238 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003239 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003240 {
mityu4ac198c2021-05-28 17:52:40 +02003241 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003242 clear_tv(rettv);
3243 return FAIL;
3244 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003245 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003246 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003247 if ((op != '+' || (rettv->v_type != VAR_LIST
3248 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003249#ifdef FEAT_FLOAT
3250 && (op == '.' || rettv->v_type != VAR_FLOAT)
3251#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003252 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003253 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003254 int error = FALSE;
3255
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003256 // For "list + ...", an illegal use of the first operand as
3257 // a number cannot be determined before evaluating the 2nd
3258 // operand: if this is also a list, all is ok.
3259 // For "something . ...", "something - ..." or "non-list + ...",
3260 // we know that the first operand needs to be a string or number
3261 // without evaluating the 2nd operand. So check before to avoid
3262 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003263 if (op != '.')
3264 tv_get_number_chk(rettv, &error);
3265 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003266 {
3267 clear_tv(rettv);
3268 return FAIL;
3269 }
3270 }
3271
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 /*
3273 * Get the second variable.
3274 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003275 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003276 {
mityu89dcb4d2021-05-28 13:50:17 +02003277 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003278 clear_tv(rettv);
3279 return FAIL;
3280 }
3281 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003282 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003284 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 return FAIL;
3286 }
3287
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003288 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 {
3290 /*
3291 * Compute the result.
3292 */
3293 if (op == '.')
3294 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003295 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3296 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003297 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003298
Bram Moolenaar2e086612020-08-25 22:37:48 +02003299 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003300 || var2.v_type == VAR_CHANNEL
3301 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003302 semsg(_(e_using_invalid_value_as_string_str),
3303 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003304#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003305 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003306 {
3307 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3308 var2.vval.v_float);
3309 s2 = buf2;
3310 }
3311#endif
3312 else
3313 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003314 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003315 {
3316 clear_tv(rettv);
3317 clear_tv(&var2);
3318 return FAIL;
3319 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003320 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003321 clear_tv(rettv);
3322 rettv->v_type = VAR_STRING;
3323 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003325 else if (op == '+' && rettv->v_type == VAR_BLOB
3326 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003328 else if (op == '+' && rettv->v_type == VAR_LIST
3329 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003330 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003332 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 else
3335 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003336 int error = FALSE;
3337 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003338#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003339 float_T f1 = 0, f2 = 0;
3340
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003341 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003342 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003343 f1 = rettv->vval.v_float;
3344 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003345 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003346 else
3347#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003348 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003349 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003350 if (error)
3351 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003352 // This can only happen for "list + non-list" or
3353 // "blob + non-blob". For "non-list + ..." or
3354 // "something - ...", we returned before evaluating the
3355 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003356 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003357 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003358 return FAIL;
3359 }
3360#ifdef FEAT_FLOAT
3361 if (var2.v_type == VAR_FLOAT)
3362 f1 = n1;
3363#endif
3364 }
3365#ifdef FEAT_FLOAT
3366 if (var2.v_type == VAR_FLOAT)
3367 {
3368 f2 = var2.vval.v_float;
3369 n2 = 0;
3370 }
3371 else
3372#endif
3373 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003374 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003375 if (error)
3376 {
3377 clear_tv(rettv);
3378 clear_tv(&var2);
3379 return FAIL;
3380 }
3381#ifdef FEAT_FLOAT
3382 if (rettv->v_type == VAR_FLOAT)
3383 f2 = n2;
3384#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003385 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003386 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003387
3388#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003389 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003390 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3391 {
3392 if (op == '+')
3393 f1 = f1 + f2;
3394 else
3395 f1 = f1 - f2;
3396 rettv->v_type = VAR_FLOAT;
3397 rettv->vval.v_float = f1;
3398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003400#endif
3401 {
3402 if (op == '+')
3403 n1 = n1 + n2;
3404 else
3405 n1 = n1 - n2;
3406 rettv->v_type = VAR_NUMBER;
3407 rettv->vval.v_number = n1;
3408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003410 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 }
3412 }
3413 return OK;
3414}
3415
3416/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003417 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 * * number multiplication
3419 * / number division
3420 * % number modulo
3421 *
3422 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003423 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 *
3425 * Return OK or FAIL.
3426 */
3427 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003428eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003429 char_u **arg,
3430 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003431 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003432 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003434#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003435 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437
3438 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003439 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003441 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 return FAIL;
3443
3444 /*
3445 * Repeat computing, until no '*', '/' or '%' is following.
3446 */
3447 for (;;)
3448 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003449 int evaluate;
3450 int getnext;
3451 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003452 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003453 int op;
3454 varnumber_T n1, n2;
3455#ifdef FEAT_FLOAT
3456 float_T f1, f2;
3457#endif
3458 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003459
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003460 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003461 p = eval_next_non_blank(*arg, evalarg, &getnext);
3462 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003463 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003465
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003466 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003467 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003468 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003469 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003470 {
3471 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3472 {
mityu4ac198c2021-05-28 17:52:40 +02003473 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003474 clear_tv(rettv);
3475 return FAIL;
3476 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003477 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003480#ifdef FEAT_FLOAT
3481 f1 = 0;
3482 f2 = 0;
3483#endif
3484 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003485 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003487#ifdef FEAT_FLOAT
3488 if (rettv->v_type == VAR_FLOAT)
3489 {
3490 f1 = rettv->vval.v_float;
3491 use_float = TRUE;
3492 n1 = 0;
3493 }
3494 else
3495#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003496 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003497 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003498 if (error)
3499 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 }
3501 else
3502 n1 = 0;
3503
3504 /*
3505 * Get the second variable.
3506 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003507 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3508 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003509 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003510 clear_tv(rettv);
3511 return FAIL;
3512 }
3513 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003514 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 return FAIL;
3516
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003517 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003519#ifdef FEAT_FLOAT
3520 if (var2.v_type == VAR_FLOAT)
3521 {
3522 if (!use_float)
3523 {
3524 f1 = n1;
3525 use_float = TRUE;
3526 }
3527 f2 = var2.vval.v_float;
3528 n2 = 0;
3529 }
3530 else
3531#endif
3532 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003533 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003534 clear_tv(&var2);
3535 if (error)
3536 return FAIL;
3537#ifdef FEAT_FLOAT
3538 if (use_float)
3539 f2 = n2;
3540#endif
3541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542
3543 /*
3544 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003545 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003547#ifdef FEAT_FLOAT
3548 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003550 if (op == '*')
3551 f1 = f1 * f2;
3552 else if (op == '/')
3553 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003554# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003555 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003556 if (f2 == 0.0)
3557 {
3558 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003559 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003560 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003561 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003562 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003563 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003564 }
3565 else
3566 f1 = f1 / f2;
3567# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003568 // We rely on the floating point library to handle divide
3569 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003570 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003571# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003574 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003575 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003576 return FAIL;
3577 }
3578 rettv->v_type = VAR_FLOAT;
3579 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 }
3581 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003584 int failed = FALSE;
3585
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003586 if (op == '*')
3587 n1 = n1 * n2;
3588 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003589 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003591 n1 = num_modulus(n1, n2, &failed);
3592 if (failed)
3593 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003594
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003595 rettv->v_type = VAR_NUMBER;
3596 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 }
3599 }
3600
3601 return OK;
3602}
3603
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003604/*
3605 * Handle a type cast before a base level expression.
3606 * "arg" must point to the first non-white of the expression.
3607 * "arg" is advanced to just after the recognized expression.
3608 * Return OK or FAIL.
3609 */
3610 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003611eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003612 char_u **arg,
3613 typval_T *rettv,
3614 evalarg_T *evalarg,
3615 int want_string) // after "." operator
3616{
3617 type_T *want_type = NULL;
3618 garray_T type_list; // list of pointers to allocated types
3619 int res;
3620 int evaluate = evalarg == NULL ? 0
3621 : (evalarg->eval_flags & EVAL_EVALUATE);
3622
3623 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003624 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3625 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003626 {
3627 ++*arg;
3628 ga_init2(&type_list, sizeof(type_T *), 10);
3629 want_type = parse_type(arg, &type_list, TRUE);
3630 if (want_type == NULL && (evaluate || **arg != '>'))
3631 {
3632 clear_type_list(&type_list);
3633 return FAIL;
3634 }
3635
3636 if (**arg != '>')
3637 {
3638 if (*skipwhite(*arg) == '>')
3639 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3640 else
3641 emsg(_(e_missing_gt));
3642 clear_type_list(&type_list);
3643 return FAIL;
3644 }
3645 ++*arg;
3646 *arg = skipwhite_and_linebreak(*arg, evalarg);
3647 }
3648
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003649 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003650
3651 if (want_type != NULL && evaluate)
3652 {
3653 if (res == OK)
3654 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003655 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3656 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003657
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003658 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003659 {
3660 if (want_type == &t_bool && actual != &t_bool
3661 && (actual->tt_flags & TTFLAG_BOOL_OK))
3662 {
3663 int n = tv2bool(rettv);
3664
3665 // can use "0" and "1" for boolean in some places
3666 clear_tv(rettv);
3667 rettv->v_type = VAR_BOOL;
3668 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3669 }
3670 else
3671 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003672 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003673
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003674 where.wt_variable = TRUE;
3675 res = check_type(want_type, actual, TRUE, where);
3676 }
3677 }
3678 }
3679 clear_type_list(&type_list);
3680 }
3681
3682 return res;
3683}
3684
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003685 int
3686eval_leader(char_u **arg, int vim9)
3687{
3688 char_u *s = *arg;
3689 char_u *p = *arg;
3690
3691 while (*p == '!' || *p == '-' || *p == '+')
3692 {
3693 char_u *n = skipwhite(p + 1);
3694
3695 // ++, --, -+ and +- are not accepted in Vim9 script
3696 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3697 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003698 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003699 return FAIL;
3700 }
3701 p = n;
3702 }
3703 *arg = p;
3704 return OK;
3705}
3706
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003708 * Check for a predefined value "true", "false" and "null.*".
3709 * Return OK when recognized.
3710 */
3711 int
3712handle_predefined(char_u *s, int len, typval_T *rettv)
3713{
3714 switch (len)
3715 {
3716 case 4: if (STRNCMP(s, "true", 4) == 0)
3717 {
3718 rettv->v_type = VAR_BOOL;
3719 rettv->vval.v_number = VVAL_TRUE;
3720 return OK;
3721 }
3722 if (STRNCMP(s, "null", 4) == 0)
3723 {
3724 rettv->v_type = VAR_SPECIAL;
3725 rettv->vval.v_number = VVAL_NULL;
3726 return OK;
3727 }
3728 break;
3729 case 5: if (STRNCMP(s, "false", 5) == 0)
3730 {
3731 rettv->v_type = VAR_BOOL;
3732 rettv->vval.v_number = VVAL_FALSE;
3733 return OK;
3734 }
3735 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003736 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3737 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003738#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003739 rettv->v_type = VAR_JOB;
3740 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003741#else
3742 rettv->v_type = VAR_SPECIAL;
3743 rettv->vval.v_number = VVAL_NULL;
3744#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003745 return OK;
3746 }
3747 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003748 case 9:
3749 if (STRNCMP(s, "null_", 5) != 0)
3750 break;
3751 if (STRNCMP(s + 5, "list", 4) == 0)
3752 {
3753 rettv->v_type = VAR_LIST;
3754 rettv->vval.v_list = NULL;
3755 return OK;
3756 }
3757 if (STRNCMP(s + 5, "dict", 4) == 0)
3758 {
3759 rettv->v_type = VAR_DICT;
3760 rettv->vval.v_dict = NULL;
3761 return OK;
3762 }
3763 if (STRNCMP(s + 5, "blob", 4) == 0)
3764 {
3765 rettv->v_type = VAR_BLOB;
3766 rettv->vval.v_blob = NULL;
3767 return OK;
3768 }
3769 break;
3770 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3771 {
3772 rettv->v_type = VAR_STRING;
3773 rettv->vval.v_string = NULL;
3774 return OK;
3775 }
3776 break;
3777 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003778 if (STRNCMP(s, "null_channel", 12) == 0)
3779 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003780#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003781 rettv->v_type = VAR_CHANNEL;
3782 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003783#else
3784 rettv->v_type = VAR_SPECIAL;
3785 rettv->vval.v_number = VVAL_NULL;
3786#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003787 return OK;
3788 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003789 if (STRNCMP(s, "null_partial", 12) == 0)
3790 {
3791 rettv->v_type = VAR_PARTIAL;
3792 rettv->vval.v_partial = NULL;
3793 return OK;
3794 }
3795 break;
3796 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3797 {
3798 rettv->v_type = VAR_FUNC;
3799 rettv->vval.v_string = NULL;
3800 return OK;
3801 }
3802 break;
3803 }
3804 return FAIL;
3805}
3806
3807/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 * Handle sixth level expression:
3809 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003810 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003811 * "string" string constant
3812 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 * &option-name option value
3814 * @r register contents
3815 * identifier variable value
3816 * function() function call
3817 * $VAR environment variable
3818 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003819 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003820 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003821 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003822 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 *
3824 * Also handle:
3825 * ! in front logical NOT
3826 * - in front unary minus
3827 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003828 * trailing [] subscript in String or List
3829 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003830 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 *
3832 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003833 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 *
3835 * Return OK or FAIL.
3836 */
3837 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003838eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003839 char_u **arg,
3840 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003841 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003844 int evaluate = evalarg != NULL
3845 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 int len;
3847 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003848 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 char_u *start_leader, *end_leader;
3850 int ret = OK;
3851 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003852 static int recurse = 0;
3853 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854
3855 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003856 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003857 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003859 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860
3861 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003862 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 */
3864 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003865 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003866 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 end_leader = *arg;
3868
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003869 if (**arg == '.' && (!isdigit(*(*arg + 1))
3870#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003871 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003872#endif
3873 ))
3874 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003875 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003876 ++*arg;
3877 return FAIL;
3878 }
3879
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003880 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003881 // and crash. With MSVC the stack is smaller.
3882 if (recurse ==
3883#ifdef _MSC_VER
3884 300
3885#else
3886 1000
3887#endif
3888 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003889 {
3890 semsg(_(e_expression_too_recursive_str), *arg);
3891 return FAIL;
3892 }
3893 ++recurse;
3894
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 switch (**arg)
3896 {
3897 /*
3898 * Number constant.
3899 */
3900 case '0':
3901 case '1':
3902 case '2':
3903 case '3':
3904 case '4':
3905 case '5':
3906 case '6':
3907 case '7':
3908 case '8':
3909 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003910 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003911
3912 // Apply prefixed "-" and "+" now. Matters especially when
3913 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003914 if (ret == OK && evaluate && end_leader > start_leader
3915 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003916 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 break;
3918
3919 /*
3920 * String constant: "string".
3921 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003922 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 break;
3924
3925 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003926 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003928 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003929 break;
3930
3931 /*
3932 * List: [expr, expr]
3933 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003934 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 break;
3936
3937 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003938 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003939 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003940 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003941 {
3942 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3943 }
3944 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003945 {
3946 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003947 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003948 }
3949 else
3950 ret = NOTDONE;
3951 break;
3952
3953 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003954 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003955 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003956 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003957 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003958 ret = NOTDONE;
3959 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00003960 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003961 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003962 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003963 break;
3964
3965 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003966 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003968 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 break;
3970
3971 /*
3972 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01003973 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 */
LemonBoy2eaef102022-05-06 13:14:50 +01003975 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
3976 ret = eval_interp_string(arg, rettv, evaluate);
3977 else
3978 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 break;
3980
3981 /*
3982 * Register contents: @r.
3983 */
3984 case '@': ++*arg;
3985 if (evaluate)
3986 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003987 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003988 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003989 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003990 emsg_invreg(**arg);
3991 else
3992 {
3993 rettv->v_type = VAR_STRING;
3994 rettv->vval.v_string = get_reg_contents(**arg,
3995 GREG_EXPR_SRC);
3996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 }
3998 if (**arg != NUL)
3999 ++*arg;
4000 break;
4001
4002 /*
4003 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004004 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004006 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004007 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004008 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004009 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004010 if (ret == OK && evaluate)
4011 {
4012 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4013
Bram Moolenaara9931532021-06-12 15:58:16 +02004014 // Compile it here to get the return type. The return
4015 // type is optional, when it's missing use t_unknown.
4016 // This is recognized in compile_return().
4017 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4018 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004019 if (compile_def_function(ufunc, FALSE,
4020 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004021 {
4022 clear_tv(rettv);
4023 ret = FAIL;
4024 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004025 }
4026 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004027 if (ret == NOTDONE)
4028 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004029 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004030 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004031
Bram Moolenaar9215f012020-06-27 21:18:00 +02004032 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004033 if (**arg == ')')
4034 ++*arg;
4035 else if (ret == OK)
4036 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004037 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004038 clear_tv(rettv);
4039 ret = FAIL;
4040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 }
4042 break;
4043
Bram Moolenaar8c711452005-01-14 21:53:12 +00004044 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 break;
4046 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004047
4048 if (ret == NOTDONE)
4049 {
4050 /*
4051 * Must be a variable or function name.
4052 * Can also be a curly-braces kind of name: {expr}.
4053 */
4054 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004055 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004056 if (alias != NULL)
4057 s = alias;
4058
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004059 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004060 ret = FAIL;
4061 else
4062 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004063 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4064
Bram Moolenaar4525a572022-02-13 11:57:33 +00004065 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004066 {
4067 emsg(_(e_cannot_use_underscore_here));
4068 ret = FAIL;
4069 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004070 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004071 && s[0] == 's' && s[1] == ':')
4072 {
4073 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4074 ret = FAIL;
4075 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004076 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004077 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004078 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004079 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004080 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004081 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02004082 else if (flags & EVAL_CONSTANT)
4083 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004084 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004085 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004086 // get the value of "true", "false", etc. or a variable
4087 ret = FAIL;
4088 if (vim9script)
4089 ret = handle_predefined(s, len, rettv);
4090 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004091 {
4092 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004093 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004094 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004095 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004096 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004097 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004098 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004099 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004100 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004101 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004102 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004103 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004104 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004105 }
4106
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004107 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4108 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004109 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004110 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111
4112 /*
4113 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4114 */
4115 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004116 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004117
4118 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004119 return ret;
4120}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004121
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004122/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004123 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004124 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004125 * Adjusts "end_leaderp" until it is at "start_leader".
4126 */
4127 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004128eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004129 typval_T *rettv,
4130 int numeric_only,
4131 char_u *start_leader,
4132 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004133{
4134 char_u *end_leader = *end_leaderp;
4135 int ret = OK;
4136 int error = FALSE;
4137 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004138 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004139 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004140#ifdef FEAT_FLOAT
4141 float_T f = 0.0;
4142
4143 if (rettv->v_type == VAR_FLOAT)
4144 f = rettv->vval.v_float;
4145 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004146#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004147 {
4148 while (VIM_ISWHITE(end_leader[-1]))
4149 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004150 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004151 val = tv2bool(rettv);
4152 else
4153 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004154 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004155 if (error)
4156 {
4157 clear_tv(rettv);
4158 ret = FAIL;
4159 }
4160 else
4161 {
4162 while (end_leader > start_leader)
4163 {
4164 --end_leader;
4165 if (*end_leader == '!')
4166 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004167 if (numeric_only)
4168 {
4169 ++end_leader;
4170 break;
4171 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004172#ifdef FEAT_FLOAT
4173 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004174 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004175 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004176 {
4177 rettv->v_type = VAR_BOOL;
4178 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4179 }
4180 else
4181 f = !f;
4182 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004183 else
4184#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004185 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004186 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004187 type = VAR_BOOL;
4188 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004189 }
4190 else if (*end_leader == '-')
4191 {
4192#ifdef FEAT_FLOAT
4193 if (rettv->v_type == VAR_FLOAT)
4194 f = -f;
4195 else
4196#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004197 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004198 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004199 type = VAR_NUMBER;
4200 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004201 }
4202 }
4203#ifdef FEAT_FLOAT
4204 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004206 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004207 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004209 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004210#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004211 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004212 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004213 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004214 rettv->v_type = type;
4215 else
4216 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004217 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004220 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 return ret;
4222}
4223
4224/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004225 * Call the function referred to in "rettv".
4226 */
4227 static int
4228call_func_rettv(
4229 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004230 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004231 typval_T *rettv,
4232 int evaluate,
4233 dict_T *selfdict,
4234 typval_T *basetv)
4235{
4236 partial_T *pt = NULL;
4237 funcexe_T funcexe;
4238 typval_T functv;
4239 char_u *s;
4240 int ret;
4241
4242 // need to copy the funcref so that we can clear rettv
4243 if (evaluate)
4244 {
4245 functv = *rettv;
4246 rettv->v_type = VAR_UNKNOWN;
4247
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004248 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004249 if (functv.v_type == VAR_PARTIAL)
4250 {
4251 pt = functv.vval.v_partial;
4252 s = partial_name(pt);
4253 }
4254 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004255 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004256 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004257 if (s == NULL || *s == NUL)
4258 {
4259 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004260 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004261 goto theend;
4262 }
4263 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004264 }
4265 else
4266 s = (char_u *)"";
4267
Bram Moolenaara80faa82020-04-12 19:37:17 +02004268 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004269 funcexe.fe_firstline = curwin->w_cursor.lnum;
4270 funcexe.fe_lastline = curwin->w_cursor.lnum;
4271 funcexe.fe_evaluate = evaluate;
4272 funcexe.fe_partial = pt;
4273 funcexe.fe_selfdict = selfdict;
4274 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004275 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004276
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004277theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004278 // Clear the funcref afterwards, so that deleting it while
4279 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004280 if (evaluate)
4281 clear_tv(&functv);
4282
4283 return ret;
4284}
4285
4286/*
4287 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004288 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004289 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4290 */
4291 static int
4292eval_lambda(
4293 char_u **arg,
4294 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004295 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004296 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004297{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004298 int evaluate = evalarg != NULL
4299 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004300 typval_T base = *rettv;
4301 int ret;
4302
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004303 rettv->v_type = VAR_UNKNOWN;
4304
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004305 if (**arg == '{')
4306 {
4307 // ->{lambda}()
4308 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4309 }
4310 else
4311 {
4312 // ->(lambda)()
4313 ++*arg;
4314 ret = eval1(arg, rettv, evalarg);
4315 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004316 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004317 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004318 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004319 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004320 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004321 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4322 && rettv->v_type != VAR_PARTIAL)
4323 {
4324 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4325 return FAIL;
4326 }
4327 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004328 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004329 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004330 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004331
4332 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004333 {
4334 if (verbose)
4335 {
4336 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004337 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004338 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004339 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004340 }
4341 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004342 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004343 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004344 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004345 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004346
4347 // Clear the funcref afterwards, so that deleting it while
4348 // evaluating the arguments is possible (see test55).
4349 if (evaluate)
4350 clear_tv(&base);
4351
4352 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004353}
4354
4355/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004356 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004357 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004358 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4359 */
4360 static int
4361eval_method(
4362 char_u **arg,
4363 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004364 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004365 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004366{
4367 char_u *name;
4368 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004369 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004370 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004371 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004372 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004373 int evaluate = evalarg != NULL
4374 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004375
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004376 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004377
Bram Moolenaarac92e252019-08-03 21:58:38 +02004378 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004379 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004380 if (alias != NULL)
4381 name = alias;
4382
4383 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004384 {
4385 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004386 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004387 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004388 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004389 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004390 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004391 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004392
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004393 // If there is no "(" immediately following, but there is further on,
4394 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4395 // Does not handle anything where "(" is part of the expression.
4396 *arg = skipwhite(*arg);
4397
4398 if (**arg != '(' && alias == NULL
4399 && (paren = vim_strchr(*arg, '(')) != NULL)
4400 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004401 char_u *deref;
4402
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004403 *arg = name;
4404 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004405 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4406 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004407 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004408 *arg = name + len;
4409 ret = FAIL;
4410 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004411 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004412 {
4413 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004414 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004415 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004416 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004417 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004418
4419 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004420 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004421 *arg = skipwhite(*arg);
4422
4423 if (**arg != '(')
4424 {
4425 if (verbose)
4426 semsg(_(e_missing_parenthesis_str), name);
4427 ret = FAIL;
4428 }
4429 else if (VIM_ISWHITE((*arg)[-1]))
4430 {
4431 if (verbose)
4432 emsg(_(e_no_white_space_allowed_before_parenthesis));
4433 ret = FAIL;
4434 }
4435 else
4436 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004437 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004438 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004439 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004440
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004441 // Clear the funcref afterwards, so that deleting it while
4442 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004443 if (evaluate)
4444 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004445 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004446
Bram Moolenaarac92e252019-08-03 21:58:38 +02004447 return ret;
4448}
4449
4450/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004451 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4452 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004453 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4454 */
4455 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004456eval_index(
4457 char_u **arg,
4458 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004459 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004460 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004461{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004462 int evaluate = evalarg != NULL
4463 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004464 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004465 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004466 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004467 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004468 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004469 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004470
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004471 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4472 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004473
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004474 init_tv(&var1);
4475 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004476 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004477 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004478 /*
4479 * dict.name
4480 */
4481 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004482 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004483 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004484 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004485 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004486 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004487 }
4488 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004489 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004490 /*
4491 * something[idx]
4492 *
4493 * Get the (first) variable from inside the [].
4494 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004495 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004496 if (**arg == ':')
4497 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004498 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004499 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004500 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004501 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004502 semsg(_(e_white_space_required_before_and_after_str_at_str),
4503 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004504 clear_tv(&var1);
4505 return FAIL;
4506 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004507 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004508 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004509 int error = FALSE;
4510
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004511#ifdef FEAT_FLOAT
4512 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004513 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004514 && var1.v_type == VAR_FLOAT)
4515 {
4516 var1.vval.v_string = typval_tostring(&var1, TRUE);
4517 var1.v_type = VAR_STRING;
4518 }
4519#endif
Bram Moolenaar4525a572022-02-13 11:57:33 +00004520 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004521 tv_get_number_chk(&var1, &error);
4522 else
4523 error = tv_get_string_chk(&var1) == NULL;
4524 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004525 {
4526 // not a number or string
4527 clear_tv(&var1);
4528 return FAIL;
4529 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004530 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004531
4532 /*
4533 * Get the second variable from inside the [:].
4534 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004535 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004536 if (**arg == ':')
4537 {
4538 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004539 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004540 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004541 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004542 semsg(_(e_white_space_required_before_and_after_str_at_str),
4543 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004544 if (!empty1)
4545 clear_tv(&var1);
4546 return FAIL;
4547 }
4548 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004549 if (**arg == ']')
4550 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004551 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004552 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004553 if (!empty1)
4554 clear_tv(&var1);
4555 return FAIL;
4556 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004557 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004558 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004559 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004560 if (!empty1)
4561 clear_tv(&var1);
4562 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004563 return FAIL;
4564 }
4565 }
4566
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004567 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004568 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004569 if (**arg != ']')
4570 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004571 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004572 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004573 clear_tv(&var1);
4574 if (range)
4575 clear_tv(&var2);
4576 return FAIL;
4577 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004578 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004579 }
4580
4581 if (evaluate)
4582 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004583 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004584 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004585 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004586
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004587 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004588 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004589 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004590 clear_tv(&var2);
4591 return res;
4592 }
4593 return OK;
4594}
4595
4596/*
4597 * Check if "rettv" can have an [index] or [sli:ce]
4598 */
4599 int
4600check_can_index(typval_T *rettv, int evaluate, int verbose)
4601{
4602 switch (rettv->v_type)
4603 {
4604 case VAR_FUNC:
4605 case VAR_PARTIAL:
4606 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004607 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004608 return FAIL;
4609 case VAR_FLOAT:
4610#ifdef FEAT_FLOAT
4611 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004612 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004613 return FAIL;
4614#endif
4615 case VAR_BOOL:
4616 case VAR_SPECIAL:
4617 case VAR_JOB:
4618 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004619 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004620 if (verbose)
4621 emsg(_(e_cannot_index_special_variable));
4622 return FAIL;
4623 case VAR_UNKNOWN:
4624 case VAR_ANY:
4625 case VAR_VOID:
4626 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004627 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004628 emsg(_(e_cannot_index_special_variable));
4629 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004630 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004631 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004632
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004633 case VAR_STRING:
4634 case VAR_LIST:
4635 case VAR_DICT:
4636 case VAR_BLOB:
4637 break;
4638 case VAR_NUMBER:
4639 if (in_vim9script())
4640 emsg(_(e_cannot_index_number));
4641 break;
4642 }
4643 return OK;
4644}
4645
4646/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004647 * slice() function
4648 */
4649 void
4650f_slice(typval_T *argvars, typval_T *rettv)
4651{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004652 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004653 && ((argvars[0].v_type != VAR_STRING
4654 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004655 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004656 && check_for_list_arg(argvars, 0) == FAIL)
4657 || check_for_number_arg(argvars, 1) == FAIL
4658 || check_for_opt_number_arg(argvars, 2) == FAIL))
4659 return;
4660
Bram Moolenaar6601b622021-01-13 21:47:15 +01004661 if (check_can_index(argvars, TRUE, FALSE) == OK)
4662 {
4663 copy_tv(argvars, rettv);
4664 eval_index_inner(rettv, TRUE, argvars + 1,
4665 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4666 TRUE, NULL, 0, FALSE);
4667 }
4668}
4669
4670/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004671 * Apply index or range to "rettv".
4672 * "var1" is the first index, NULL for [:expr].
4673 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004674 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4675 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004676 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4677 */
4678 int
4679eval_index_inner(
4680 typval_T *rettv,
4681 int is_range,
4682 typval_T *var1,
4683 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004684 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004685 char_u *key,
4686 int keylen,
4687 int verbose)
4688{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004689 varnumber_T n1, n2 = 0;
4690 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004691
4692 n1 = 0;
4693 if (var1 != NULL && rettv->v_type != VAR_DICT)
4694 n1 = tv_get_number(var1);
4695
4696 if (is_range)
4697 {
4698 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004699 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004700 if (verbose)
4701 emsg(_(e_cannot_slice_dictionary));
4702 return FAIL;
4703 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004704 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004705 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004706 else
4707 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004708 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004709
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004710 switch (rettv->v_type)
4711 {
4712 case VAR_UNKNOWN:
4713 case VAR_ANY:
4714 case VAR_VOID:
4715 case VAR_FUNC:
4716 case VAR_PARTIAL:
4717 case VAR_FLOAT:
4718 case VAR_BOOL:
4719 case VAR_SPECIAL:
4720 case VAR_JOB:
4721 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004722 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004723 break; // not evaluating, skipping over subscript
4724
4725 case VAR_NUMBER:
4726 case VAR_STRING:
4727 {
4728 char_u *s = tv_get_string(rettv);
4729
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004730 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004731 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004732 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004733 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004734 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004735 else
4736 s = char_from_string(s, n1);
4737 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004738 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004739 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004740 // The resulting variable is a substring. If the indexes
4741 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004742 if (n1 < 0)
4743 {
4744 n1 = len + n1;
4745 if (n1 < 0)
4746 n1 = 0;
4747 }
4748 if (n2 < 0)
4749 n2 = len + n2;
4750 else if (n2 >= len)
4751 n2 = len;
4752 if (n1 >= len || n2 < 0 || n1 > n2)
4753 s = NULL;
4754 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004755 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004756 }
4757 else
4758 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004759 // The resulting variable is a string of a single
4760 // character. If the index is too big or negative the
4761 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004762 if (n1 >= len || n1 < 0)
4763 s = NULL;
4764 else
4765 s = vim_strnsave(s + n1, 1);
4766 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004767 clear_tv(rettv);
4768 rettv->v_type = VAR_STRING;
4769 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004770 }
4771 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004772
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004773 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004774 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4775 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004776 break;
4777
4778 case VAR_LIST:
4779 if (var1 == NULL)
4780 n1 = 0;
4781 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004782 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004783 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004784 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004785 return FAIL;
4786 break;
4787
4788 case VAR_DICT:
4789 {
4790 dictitem_T *item;
4791 typval_T tmp;
4792
4793 if (key == NULL)
4794 {
4795 key = tv_get_string_chk(var1);
4796 if (key == NULL)
4797 return FAIL;
4798 }
4799
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004800 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004801
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004802 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004803 {
4804 if (verbose)
4805 {
4806 if (keylen > 0)
4807 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004808 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004809 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004810 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004811 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004812
4813 copy_tv(&item->di_tv, &tmp);
4814 clear_tv(rettv);
4815 *rettv = tmp;
4816 }
4817 break;
4818 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004819 return OK;
4820}
4821
4822/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004823 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004824 */
4825 char_u *
4826partial_name(partial_T *pt)
4827{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004828 if (pt != NULL)
4829 {
4830 if (pt->pt_name != NULL)
4831 return pt->pt_name;
4832 if (pt->pt_func != NULL)
4833 return pt->pt_func->uf_name;
4834 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004835 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004836}
4837
Bram Moolenaarddecc252016-04-06 22:59:37 +02004838 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004839partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004840{
4841 int i;
4842
4843 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004844 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004845 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004846 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004847 if (pt->pt_name != NULL)
4848 {
4849 func_unref(pt->pt_name);
4850 vim_free(pt->pt_name);
4851 }
4852 else
4853 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004854
Bram Moolenaar54656012021-06-09 20:50:46 +02004855 // "out_up" is no longer used, decrement refcount on partial that owns it.
4856 partial_unref(pt->pt_outer.out_up_partial);
4857
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004858 // Using pt_outer from another partial.
4859 partial_unref(pt->pt_outer_partial);
4860
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004861 // Decrease the reference count for the context of a closure. If down
4862 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004863 if (pt->pt_funcstack != NULL)
4864 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004865 --pt->pt_funcstack->fs_refcount;
4866 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004867 }
4868
Bram Moolenaarddecc252016-04-06 22:59:37 +02004869 vim_free(pt);
4870}
4871
4872/*
4873 * Unreference a closure: decrement the reference count and free it when it
4874 * becomes zero.
4875 */
4876 void
4877partial_unref(partial_T *pt)
4878{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004879 if (pt != NULL)
4880 {
4881 if (--pt->pt_refcount <= 0)
4882 partial_free(pt);
4883
4884 // If the reference count goes down to one, the funcstack may be the
4885 // only reference and can be freed if no other partials reference it.
4886 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4887 funcstack_check_refcount(pt->pt_funcstack);
4888 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004889}
4890
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004891/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004892 * Return the next (unique) copy ID.
4893 * Used for serializing nested structures.
4894 */
4895 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004896get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004897{
4898 current_copyID += COPYID_INC;
4899 return current_copyID;
4900}
4901
4902/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004903 * Garbage collection for lists and dictionaries.
4904 *
4905 * We use reference counts to be able to free most items right away when they
4906 * are no longer used. But for composite items it's possible that it becomes
4907 * unused while the reference count is > 0: When there is a recursive
4908 * reference. Example:
4909 * :let l = [1, 2, 3]
4910 * :let d = {9: l}
4911 * :let l[1] = d
4912 *
4913 * Since this is quite unusual we handle this with garbage collection: every
4914 * once in a while find out which lists and dicts are not referenced from any
4915 * variable.
4916 *
4917 * Here is a good reference text about garbage collection (refers to Python
4918 * but it applies to all reference-counting mechanisms):
4919 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004920 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004921
4922/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004923 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004924 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004925 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004926 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004927 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004928garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004929{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004930 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004931 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004932 buf_T *buf;
4933 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004934 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004935 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004936
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004937 if (!testing)
4938 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004939 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004940 want_garbage_collect = FALSE;
4941 may_garbage_collect = FALSE;
4942 garbage_collect_at_exit = FALSE;
4943 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004944
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004945 // The execution stack can grow big, limit the size.
4946 if (exestack.ga_maxlen - exestack.ga_len > 500)
4947 {
4948 size_t new_len;
4949 char_u *pp;
4950 int n;
4951
4952 // Keep 150% of the current size, with a minimum of the growth size.
4953 n = exestack.ga_len / 2;
4954 if (n < exestack.ga_growsize)
4955 n = exestack.ga_growsize;
4956
4957 // Don't make it bigger though.
4958 if (exestack.ga_len + n < exestack.ga_maxlen)
4959 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004960 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004961 pp = vim_realloc(exestack.ga_data, new_len);
4962 if (pp == NULL)
4963 return FAIL;
4964 exestack.ga_maxlen = exestack.ga_len + n;
4965 exestack.ga_data = pp;
4966 }
4967 }
4968
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004969 // We advance by two because we add one for items referenced through
4970 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004971 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004972
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004973 /*
4974 * 1. Go through all accessible variables and mark all lists and dicts
4975 * with copyID.
4976 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004977
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004978 // Don't free variables in the previous_funccal list unless they are only
4979 // referenced through previous_funccal. This must be first, because if
4980 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004981 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004982
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004983 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004984 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004985
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004986 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004987 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004988 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4989 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004990
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004991 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004992 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004993 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4994 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004995 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004996 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4997 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004998#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004999 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005000 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5001 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005002 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005003 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005004 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5005 NULL, NULL);
5006#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005007
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005008 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005009 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005010 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5011 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005012 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005013 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005014
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005015 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005016 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005017
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005018 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005019 abort = abort || set_ref_in_functions(copyID);
5020
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005021 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005022 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005023
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005024 // funcstacks keep variables for closures
5025 abort = abort || set_ref_in_funcstacks(copyID);
5026
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005027 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005028 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005029
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005030 // callbacks in buffers
5031 abort = abort || set_ref_in_buffers(copyID);
5032
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005033 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5034 abort = abort || set_ref_in_insexpand_funcs(copyID);
5035
5036 // 'operatorfunc' callback
5037 abort = abort || set_ref_in_opfunc(copyID);
5038
5039 // 'tagfunc' callback
5040 abort = abort || set_ref_in_tagfunc(copyID);
5041
5042 // 'imactivatefunc' and 'imstatusfunc' callbacks
5043 abort = abort || set_ref_in_im_funcs(copyID);
5044
Bram Moolenaar1dced572012-04-05 16:54:08 +02005045#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005046 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005047#endif
5048
Bram Moolenaardb913952012-06-29 12:54:53 +02005049#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005050 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005051#endif
5052
5053#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005054 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005055#endif
5056
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005057#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005058 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005059 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005060#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005061#ifdef FEAT_NETBEANS_INTG
5062 abort = abort || set_ref_in_nb_channel(copyID);
5063#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005064
Bram Moolenaare3188e22016-05-31 21:13:04 +02005065#ifdef FEAT_TIMERS
5066 abort = abort || set_ref_in_timer(copyID);
5067#endif
5068
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005069#ifdef FEAT_QUICKFIX
5070 abort = abort || set_ref_in_quickfix(copyID);
5071#endif
5072
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005073#ifdef FEAT_TERMINAL
5074 abort = abort || set_ref_in_term(copyID);
5075#endif
5076
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005077#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005078 abort = abort || set_ref_in_popups(copyID);
5079#endif
5080
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005081 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005082 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005083 /*
5084 * 2. Free lists and dictionaries that are not referenced.
5085 */
5086 did_free = free_unref_items(copyID);
5087
5088 /*
5089 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005090 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005091 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005092 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005093 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005094 else if (p_verbose > 0)
5095 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005096 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005097 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005098
5099 return did_free;
5100}
5101
5102/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005103 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005104 */
5105 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005106free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005107{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005108 int did_free = FALSE;
5109
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005110 // Let all "free" functions know that we are here. This means no
5111 // dictionaries, lists, channels or jobs are to be freed, because we will
5112 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005113 in_free_unref_items = TRUE;
5114
5115 /*
5116 * PASS 1: free the contents of the items. We don't free the items
5117 * themselves yet, so that it is possible to decrement refcount counters
5118 */
5119
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005120 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005121 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005122
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005123 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005124 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005125
5126#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005127 // Go through the list of jobs and free items without the copyID. This
5128 // must happen before doing channels, because jobs refer to channels, but
5129 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005130 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5131
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005132 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005133 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5134#endif
5135
5136 /*
5137 * PASS 2: free the items themselves.
5138 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005139 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005140 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005141
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005142#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005143 // Go through the list of jobs and free items without the copyID. This
5144 // must happen before doing channels, because jobs refer to channels, but
5145 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005146 free_unused_jobs(copyID, COPYID_MASK);
5147
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005148 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005149 free_unused_channels(copyID, COPYID_MASK);
5150#endif
5151
5152 in_free_unref_items = FALSE;
5153
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005154 return did_free;
5155}
5156
5157/*
5158 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005159 * "list_stack" is used to add lists to be marked. Can be NULL.
5160 *
5161 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005162 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005163 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005164set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005165{
5166 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005167 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005168 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005169 hashtab_T *cur_ht;
5170 ht_stack_T *ht_stack = NULL;
5171 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005172
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005173 cur_ht = ht;
5174 for (;;)
5175 {
5176 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005177 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005178 // Mark each item in the hashtab. If the item contains a hashtab
5179 // it is added to ht_stack, if it contains a list it is added to
5180 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005181 todo = (int)cur_ht->ht_used;
5182 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5183 if (!HASHITEM_EMPTY(hi))
5184 {
5185 --todo;
5186 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5187 &ht_stack, list_stack);
5188 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005189 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005190
5191 if (ht_stack == NULL)
5192 break;
5193
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005194 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005195 cur_ht = ht_stack->ht;
5196 tempitem = ht_stack;
5197 ht_stack = ht_stack->prev;
5198 free(tempitem);
5199 }
5200
5201 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005202}
5203
Dominique Pelle748b3082022-01-08 12:41:16 +00005204#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5205 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005206/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005207 * Mark a dict and its items with "copyID".
5208 * Returns TRUE if setting references failed somehow.
5209 */
5210 int
5211set_ref_in_dict(dict_T *d, int copyID)
5212{
5213 if (d != NULL && d->dv_copyID != copyID)
5214 {
5215 d->dv_copyID = copyID;
5216 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5217 }
5218 return FALSE;
5219}
Dominique Pelle748b3082022-01-08 12:41:16 +00005220#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005221
5222/*
5223 * Mark a list and its items with "copyID".
5224 * Returns TRUE if setting references failed somehow.
5225 */
5226 int
5227set_ref_in_list(list_T *ll, int copyID)
5228{
5229 if (ll != NULL && ll->lv_copyID != copyID)
5230 {
5231 ll->lv_copyID = copyID;
5232 return set_ref_in_list_items(ll, copyID, NULL);
5233 }
5234 return FALSE;
5235}
5236
5237/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005238 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005239 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5240 *
5241 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005242 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005243 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005244set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005245{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005246 listitem_T *li;
5247 int abort = FALSE;
5248 list_T *cur_l;
5249 list_stack_T *list_stack = NULL;
5250 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005251
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005252 cur_l = l;
5253 for (;;)
5254 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005255 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005256 // Mark each item in the list. If the item contains a hashtab
5257 // it is added to ht_stack, if it contains a list it is added to
5258 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005259 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5260 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5261 ht_stack, &list_stack);
5262 if (list_stack == NULL)
5263 break;
5264
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005265 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005266 cur_l = list_stack->list;
5267 tempitem = list_stack;
5268 list_stack = list_stack->prev;
5269 free(tempitem);
5270 }
5271
5272 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005273}
5274
5275/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005276 * Mark the partial in callback 'cb' with "copyID".
5277 */
5278 int
5279set_ref_in_callback(callback_T *cb, int copyID)
5280{
5281 typval_T tv;
5282
5283 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5284 return FALSE;
5285
5286 tv.v_type = VAR_PARTIAL;
5287 tv.vval.v_partial = cb->cb_partial;
5288 return set_ref_in_item(&tv, copyID, NULL, NULL);
5289}
5290
5291/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005292 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005293 * "list_stack" is used to add lists to be marked. Can be NULL.
5294 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5295 *
5296 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005297 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005298 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005299set_ref_in_item(
5300 typval_T *tv,
5301 int copyID,
5302 ht_stack_T **ht_stack,
5303 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005304{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005305 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005306
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005307 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005308 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005309 dict_T *dd = tv->vval.v_dict;
5310
Bram Moolenaara03f2332016-02-06 18:09:59 +01005311 if (dd != NULL && dd->dv_copyID != copyID)
5312 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005313 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005314 dd->dv_copyID = copyID;
5315 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005316 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005317 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5318 }
5319 else
5320 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005321 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5322
Bram Moolenaara03f2332016-02-06 18:09:59 +01005323 if (newitem == NULL)
5324 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005325 else
5326 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005327 newitem->ht = &dd->dv_hashtab;
5328 newitem->prev = *ht_stack;
5329 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005330 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005331 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005332 }
5333 }
5334 else if (tv->v_type == VAR_LIST)
5335 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005336 list_T *ll = tv->vval.v_list;
5337
Bram Moolenaara03f2332016-02-06 18:09:59 +01005338 if (ll != NULL && ll->lv_copyID != copyID)
5339 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005340 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005341 ll->lv_copyID = copyID;
5342 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005343 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005344 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005345 }
5346 else
5347 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005348 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5349
Bram Moolenaara03f2332016-02-06 18:09:59 +01005350 if (newitem == NULL)
5351 abort = TRUE;
5352 else
5353 {
5354 newitem->list = ll;
5355 newitem->prev = *list_stack;
5356 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005357 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005358 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005359 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005360 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005361 else if (tv->v_type == VAR_FUNC)
5362 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005363 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005364 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005365 else if (tv->v_type == VAR_PARTIAL)
5366 {
5367 partial_T *pt = tv->vval.v_partial;
5368 int i;
5369
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005370 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005371 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005372 // Didn't see this partial yet.
5373 pt->pt_copyID = copyID;
5374
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005375 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005376
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005377 if (pt->pt_dict != NULL)
5378 {
5379 typval_T dtv;
5380
5381 dtv.v_type = VAR_DICT;
5382 dtv.vval.v_dict = pt->pt_dict;
5383 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5384 }
5385
5386 for (i = 0; i < pt->pt_argc; ++i)
5387 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5388 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005389 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005390 }
5391 }
5392#ifdef FEAT_JOB_CHANNEL
5393 else if (tv->v_type == VAR_JOB)
5394 {
5395 job_T *job = tv->vval.v_job;
5396 typval_T dtv;
5397
5398 if (job != NULL && job->jv_copyID != copyID)
5399 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005400 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005401 if (job->jv_channel != NULL)
5402 {
5403 dtv.v_type = VAR_CHANNEL;
5404 dtv.vval.v_channel = job->jv_channel;
5405 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5406 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005407 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005408 {
5409 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005410 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005411 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5412 }
5413 }
5414 }
5415 else if (tv->v_type == VAR_CHANNEL)
5416 {
5417 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005418 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005419 typval_T dtv;
5420 jsonq_T *jq;
5421 cbq_T *cq;
5422
5423 if (ch != NULL && ch->ch_copyID != copyID)
5424 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005425 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005426 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005427 {
5428 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5429 jq = jq->jq_next)
5430 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5431 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5432 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005433 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005434 {
5435 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005436 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005437 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5438 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005439 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005440 {
5441 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005442 dtv.vval.v_partial =
5443 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005444 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5445 }
5446 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005447 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005448 {
5449 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005450 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005451 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5452 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005453 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005454 {
5455 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005456 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005457 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5458 }
5459 }
5460 }
5461#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005462 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005463}
5464
Bram Moolenaar8c711452005-01-14 21:53:12 +00005465/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466 * Return a string with the string representation of a variable.
5467 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005468 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005469 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005470 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005471 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005472 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005473 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5474 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005475 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005476 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005477 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005478echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005479 typval_T *tv,
5480 char_u **tofree,
5481 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005482 int copyID,
5483 int echo_style,
5484 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005485 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005486{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005487 static int recurse = 0;
5488 char_u *r = NULL;
5489
Bram Moolenaar33570922005-01-25 22:26:29 +00005490 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005491 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005492 if (!did_echo_string_emsg)
5493 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005494 // Only give this message once for a recursive call to avoid
5495 // flooding the user with errors. And stop iterating over lists
5496 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005497 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005498 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005499 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005500 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005501 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005502 }
5503 ++recurse;
5504
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005505 switch (tv->v_type)
5506 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005507 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005508 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005509 {
5510 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005511 r = tv->vval.v_string;
5512 if (r == NULL)
5513 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005514 }
5515 else
5516 {
5517 *tofree = string_quote(tv->vval.v_string, FALSE);
5518 r = *tofree;
5519 }
5520 break;
5521
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005522 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005523 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005524 char_u buf[MAX_FUNC_NAME_LEN];
5525
5526 if (echo_style)
5527 {
LemonBoya5d35902022-04-29 21:15:02 +01005528 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5529 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005530 buf, MAX_FUNC_NAME_LEN);
5531 if (r == buf)
5532 {
5533 r = vim_strsave(buf);
5534 *tofree = r;
5535 }
5536 else
5537 *tofree = NULL;
5538 }
5539 else
5540 {
5541 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5542 : make_ufunc_name_readable(
5543 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5544 TRUE);
5545 r = *tofree;
5546 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005547 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005548 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005549
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005550 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005551 {
5552 partial_T *pt = tv->vval.v_partial;
5553 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005554 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005555 garray_T ga;
5556 int i;
5557 char_u *tf;
5558
5559 ga_init2(&ga, 1, 100);
5560 ga_concat(&ga, (char_u *)"function(");
5561 if (fname != NULL)
5562 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005563 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005564 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005565 && vim_isupper(fname[1]))
5566 {
5567 ga_concat(&ga, (char_u *)"'g:");
5568 ga_concat(&ga, fname + 1);
5569 }
5570 else
5571 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005572 vim_free(fname);
5573 }
5574 if (pt != NULL && pt->pt_argc > 0)
5575 {
5576 ga_concat(&ga, (char_u *)", [");
5577 for (i = 0; i < pt->pt_argc; ++i)
5578 {
5579 if (i > 0)
5580 ga_concat(&ga, (char_u *)", ");
5581 ga_concat(&ga,
5582 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5583 vim_free(tf);
5584 }
5585 ga_concat(&ga, (char_u *)"]");
5586 }
5587 if (pt != NULL && pt->pt_dict != NULL)
5588 {
5589 typval_T dtv;
5590
5591 ga_concat(&ga, (char_u *)", ");
5592 dtv.v_type = VAR_DICT;
5593 dtv.vval.v_dict = pt->pt_dict;
5594 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5595 vim_free(tf);
5596 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005597 // terminate with ')' and a NUL
5598 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005599
5600 *tofree = ga.ga_data;
5601 r = *tofree;
5602 break;
5603 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005604
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005605 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005606 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005607 break;
5608
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005609 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005610 if (tv->vval.v_list == NULL)
5611 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005612 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005613 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005614 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005615 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005616 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5617 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005618 {
5619 *tofree = NULL;
5620 r = (char_u *)"[...]";
5621 }
5622 else
5623 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005624 int old_copyID = tv->vval.v_list->lv_copyID;
5625
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005626 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005627 *tofree = list2string(tv, copyID, restore_copyID);
5628 if (restore_copyID)
5629 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005630 r = *tofree;
5631 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005632 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005633
Bram Moolenaar8c711452005-01-14 21:53:12 +00005634 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005635 if (tv->vval.v_dict == NULL)
5636 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005637 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005638 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005639 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005640 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005641 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5642 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005643 {
5644 *tofree = NULL;
5645 r = (char_u *)"{...}";
5646 }
5647 else
5648 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005649 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005650
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005651 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005652 *tofree = dict2string(tv, copyID, restore_copyID);
5653 if (restore_copyID)
5654 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005655 r = *tofree;
5656 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005657 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005658
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005659 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005660 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005661 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005662 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005663 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005664 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005665 break;
5666
Bram Moolenaar835dc632016-02-07 14:27:38 +01005667 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005668 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005669#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005670 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005671 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5672 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005673 if (composite_val)
5674 {
5675 *tofree = string_quote(r, FALSE);
5676 r = *tofree;
5677 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005678#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005679 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005680
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005681 case VAR_INSTR:
5682 *tofree = NULL;
5683 r = (char_u *)"instructions";
5684 break;
5685
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005686 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005687#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005688 *tofree = NULL;
5689 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5690 r = numbuf;
5691 break;
5692#endif
5693
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005694 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005695 case VAR_SPECIAL:
5696 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005697 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005698 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005699 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005700
Bram Moolenaar8502c702014-06-17 12:51:16 +02005701 if (--recurse == 0)
5702 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005703 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005704}
5705
5706/*
5707 * Return a string with the string representation of a variable.
5708 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5709 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005710 * Does not put quotes around strings, as ":echo" displays values.
5711 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5712 * May return NULL.
5713 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005714 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005715echo_string(
5716 typval_T *tv,
5717 char_u **tofree,
5718 char_u *numbuf,
5719 int copyID)
5720{
5721 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5722}
5723
5724/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005725 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5726 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005727 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005728 */
5729 int
5730buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5731{
5732 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005733 char_u *t;
5734 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005735
5736 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5737 return -1;
5738
5739 if (lnum > buf->b_ml.ml_line_count)
5740 lnum = buf->b_ml.ml_line_count;
5741
5742 str = ml_get_buf(buf, lnum, FALSE);
5743 if (str == NULL)
5744 return -1;
5745
5746 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005747 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005748
Bram Moolenaar91458462021-01-13 20:08:38 +01005749 // count the number of characters
5750 t = str;
5751 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5752 t += mb_ptr2len(t);
5753
5754 // In insert mode, when the cursor is at the end of a non-empty line,
5755 // byteidx points to the NUL character immediately past the end of the
5756 // string. In this case, add one to the character count.
5757 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5758 count++;
5759
5760 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005761}
5762
5763/*
5764 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005765 * byte index. Works only for loaded buffers. Returns -1 on failure.
5766 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005767 */
5768 int
5769buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5770{
5771 char_u *str;
5772 char_u *t;
5773
5774 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5775 return -1;
5776
5777 if (lnum > buf->b_ml.ml_line_count)
5778 lnum = buf->b_ml.ml_line_count;
5779
5780 str = ml_get_buf(buf, lnum, FALSE);
5781 if (str == NULL)
5782 return -1;
5783
5784 // Convert the character offset to a byte offset
5785 t = str;
5786 while (*t != NUL && --charidx > 0)
5787 t += mb_ptr2len(t);
5788
Bram Moolenaar91458462021-01-13 20:08:38 +01005789 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005790}
5791
5792/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005794 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005796 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005797var2fpos(
5798 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005799 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005800 int *fnum, // set to fnum for '0, 'A, etc.
5801 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005803 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005805 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005807 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005808 if (varp->v_type == VAR_LIST)
5809 {
5810 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005811 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005812 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005813 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005814
5815 l = varp->vval.v_list;
5816 if (l == NULL)
5817 return NULL;
5818
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005819 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005820 pos.lnum = list_find_nr(l, 0L, &error);
5821 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005822 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005823 if (charcol)
5824 len = (long)mb_charlen(ml_get(pos.lnum));
5825 else
5826 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005827
Bram Moolenaarec65d772020-08-20 22:29:12 +02005828 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005829 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005830 li = list_find(l, 1L);
5831 if (li != NULL && li->li_tv.v_type == VAR_STRING
5832 && li->li_tv.vval.v_string != NULL
5833 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005834 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005835 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005836 }
5837 else
5838 {
5839 pos.col = list_find_nr(l, 1L, &error);
5840 if (error)
5841 return NULL;
5842 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005843
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005844 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005845 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005846 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005847 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005848
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005849 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005850 pos.coladd = list_find_nr(l, 2L, &error);
5851 if (error)
5852 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005853
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005854 return &pos;
5855 }
5856
Bram Moolenaarc5809432021-03-27 21:23:30 +01005857 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5858 return NULL;
5859
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005860 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005861 if (name == NULL)
5862 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005863
5864 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005865 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005866 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005867 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005868 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005869 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005870 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005871 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005872 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005873 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005874 pos = VIsual;
5875 else
5876 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005877 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005878 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005879 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005881 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005882 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5884 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005885 pos = *pp;
5886 }
5887 if (pos.lnum != 0)
5888 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005889 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005890 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5891 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005893
Bram Moolenaara5525202006-03-02 22:52:09 +00005894 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005895
Bram Moolenaar477933c2007-07-17 14:32:23 +00005896 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005897 {
5898 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005899 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005900 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005901 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005902 // In silent Ex mode topline is zero, but that's not a valid line
5903 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005904 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005905 return &pos;
5906 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005907 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005908 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005909 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005910 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005911 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005912 return &pos;
5913 }
5914 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005915 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005917 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 {
5919 pos.lnum = curbuf->b_ml.ml_line_count;
5920 pos.col = 0;
5921 }
5922 else
5923 {
5924 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005925 if (charcol)
5926 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5927 else
5928 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 }
5930 return &pos;
5931 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005932 if (in_vim9script())
5933 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 return NULL;
5935}
5936
5937/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005938 * Convert list in "arg" into a position and optional file number.
5939 * When "fnump" is NULL there is no file number, only 3 items.
5940 * Note that the column is passed on as-is, the caller may want to decrement
5941 * it to use 1 for the first column.
5942 * Return FAIL when conversion is not possible, doesn't check the position for
5943 * validity.
5944 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005945 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005946list2fpos(
5947 typval_T *arg,
5948 pos_T *posp,
5949 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005950 colnr_T *curswantp,
5951 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005952{
5953 list_T *l = arg->vval.v_list;
5954 long i = 0;
5955 long n;
5956
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005957 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5958 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005959 if (arg->v_type != VAR_LIST
5960 || l == NULL
5961 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005962 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005963 return FAIL;
5964
5965 if (fnump != NULL)
5966 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005967 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005968 if (n < 0)
5969 return FAIL;
5970 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005971 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005972 *fnump = n;
5973 }
5974
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005975 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005976 if (n < 0)
5977 return FAIL;
5978 posp->lnum = n;
5979
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005980 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005981 if (n < 0)
5982 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005983 // If character position is specified, then convert to byte position
5984 if (charcol)
5985 {
5986 buf_T *buf;
5987
5988 // Get the text for the specified line in a loaded buffer
5989 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5990 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5991 return FAIL;
5992
Bram Moolenaar91458462021-01-13 20:08:38 +01005993 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005994 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005995 posp->col = n;
5996
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005997 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005998 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005999 posp->coladd = 0;
6000 else
6001 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006002
Bram Moolenaar493c1782014-05-28 14:34:46 +02006003 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006004 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006005
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006006 return OK;
6007}
6008
6009/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 * Get the length of an environment variable name.
6011 * Advance "arg" to the first character after the name.
6012 * Return 0 for error.
6013 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006014 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006015get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016{
6017 char_u *p;
6018 int len;
6019
6020 for (p = *arg; vim_isIDc(*p); ++p)
6021 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006022 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 return 0;
6024
6025 len = (int)(p - *arg);
6026 *arg = p;
6027 return len;
6028}
6029
6030/*
6031 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006032 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 * Return 0 if something is wrong.
6034 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006035 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006036get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037{
6038 char_u *p;
6039 int len;
6040
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006041 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006043 {
6044 if (*p == ':')
6045 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006046 // "s:" is start of "s:var", but "n:" is not and can be used in
6047 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006048 len = (int)(p - *arg);
6049 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6050 || len > 1)
6051 break;
6052 }
6053 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006054 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 return 0;
6056
6057 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006058 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059
6060 return len;
6061}
6062
6063/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006064 * Get the length of the name of a variable or function.
6065 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006067 * Return -1 if curly braces expansion failed.
6068 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 * If the name contains 'magic' {}'s, expand them and return the
6070 * expanded name in an allocated string via 'alias' - caller must free.
6071 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006072 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006073get_name_len(
6074 char_u **arg,
6075 char_u **alias,
6076 int evaluate,
6077 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078{
6079 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 char_u *p;
6081 char_u *expr_start;
6082 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006084 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085
6086 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6087 && (*arg)[2] == (int)KE_SNR)
6088 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006089 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 *arg += 3;
6091 return get_id_len(arg) + 3;
6092 }
6093 len = eval_fname_script(*arg);
6094 if (len > 0)
6095 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006096 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 *arg += len;
6098 }
6099
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006101 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006103 p = find_name_end(*arg, &expr_start, &expr_end,
6104 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 if (expr_start != NULL)
6106 {
6107 char_u *temp_string;
6108
6109 if (!evaluate)
6110 {
6111 len += (int)(p - *arg);
6112 *arg = skipwhite(p);
6113 return len;
6114 }
6115
6116 /*
6117 * Include any <SID> etc in the expanded string:
6118 * Thus the -len here.
6119 */
6120 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6121 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006122 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 *alias = temp_string;
6124 *arg = skipwhite(p);
6125 return (int)STRLEN(temp_string);
6126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127
6128 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006129 // Only give an error when there is something, otherwise it will be
6130 // reported at a higher level.
6131 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006132 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133
6134 return len;
6135}
6136
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006137/*
6138 * Find the end of a variable or function name, taking care of magic braces.
6139 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6140 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006141 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006142 * Return a pointer to just after the name. Equal to "arg" if there is no
6143 * valid name.
6144 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006145 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006146find_name_end(
6147 char_u *arg,
6148 char_u **expr_start,
6149 char_u **expr_end,
6150 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006152 int mb_nest = 0;
6153 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006155 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02006156 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006158 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006160 *expr_start = NULL;
6161 *expr_end = NULL;
6162 }
6163
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006164 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006165 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
6166 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006167 return arg;
6168
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006169 for (p = arg; *p != NUL
6170 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006171 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006172 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006173 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006174 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006175 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006176 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006177 if (*p == '\'')
6178 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006179 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006180 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006181 ;
6182 if (*p == NUL)
6183 break;
6184 }
6185 else if (*p == '"')
6186 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006187 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006188 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006189 if (*p == '\\' && p[1] != NUL)
6190 ++p;
6191 if (*p == NUL)
6192 break;
6193 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006194 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6195 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006196 // "s:" is start of "s:var", but "n:" is not and can be used in
6197 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006198 len = (int)(p - arg);
6199 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006200 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006201 break;
6202 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006203
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006204 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006206 if (*p == '[')
6207 ++br_nest;
6208 else if (*p == ']')
6209 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006211
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006212 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006214 if (*p == '{')
6215 {
6216 mb_nest++;
6217 if (expr_start != NULL && *expr_start == NULL)
6218 *expr_start = p;
6219 }
6220 else if (*p == '}')
6221 {
6222 mb_nest--;
6223 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6224 *expr_end = p;
6225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 }
6228
6229 return p;
6230}
6231
6232/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006233 * Expands out the 'magic' {}'s in a variable/function name.
6234 * Note that this can call itself recursively, to deal with
6235 * constructs like foo{bar}{baz}{bam}
6236 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6237 * "in_start" ^
6238 * "expr_start" ^
6239 * "expr_end" ^
6240 * "in_end" ^
6241 *
6242 * Returns a new allocated string, which the caller must free.
6243 * Returns NULL for failure.
6244 */
6245 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006246make_expanded_name(
6247 char_u *in_start,
6248 char_u *expr_start,
6249 char_u *expr_end,
6250 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006251{
6252 char_u c1;
6253 char_u *retval = NULL;
6254 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006255
6256 if (expr_end == NULL || in_end == NULL)
6257 return NULL;
6258 *expr_start = NUL;
6259 *expr_end = NUL;
6260 c1 = *in_end;
6261 *in_end = NUL;
6262
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006263 temp_result = eval_to_string(expr_start + 1, FALSE);
6264 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006265 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006266 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6267 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006268 if (retval != NULL)
6269 {
6270 STRCPY(retval, in_start);
6271 STRCAT(retval, temp_result);
6272 STRCAT(retval, expr_end + 1);
6273 }
6274 }
6275 vim_free(temp_result);
6276
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006277 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006278 *expr_start = '{';
6279 *expr_end = '}';
6280
6281 if (retval != NULL)
6282 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006283 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006284 if (expr_start != NULL)
6285 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006286 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006287 temp_result = make_expanded_name(retval, expr_start,
6288 expr_end, temp_result);
6289 vim_free(retval);
6290 retval = temp_result;
6291 }
6292 }
6293
6294 return retval;
6295}
6296
6297/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006299 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006301 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006302eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006304 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006305}
6306
6307/*
6308 * Return TRUE if character "c" can be used as the first character in a
6309 * variable or function name (excluding '{' and '}').
6310 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006311 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006312eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006313{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006314 return ASCII_ISALPHA(c) || c == '_';
6315}
6316
6317/*
6318 * Return TRUE if character "c" can be used as the first character of a
6319 * dictionary key.
6320 */
6321 int
6322eval_isdictc(int c)
6323{
6324 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325}
6326
6327/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006328 * Handle:
6329 * - expr[expr], expr[expr:expr] subscript
6330 * - ".name" lookup
6331 * - function call with Funcref variable: func(expr)
6332 * - method call: var->method()
6333 *
6334 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006335 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006336 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006337 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006338handle_subscript(
6339 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006340 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006341 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006342 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006343 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006344{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006345 int evaluate = evalarg != NULL
6346 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006347 int ret = OK;
6348 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006349 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006350 int getnext;
6351 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006352
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006353 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006354 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006355 // When at the end of the line and ".name" or "->{" or "->X" follows in
6356 // the next line then consume the line break.
6357 p = eval_next_non_blank(*arg, evalarg, &getnext);
6358 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006359 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006360 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6361 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6362 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006363 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006364 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006365 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006366 check_white = FALSE;
6367 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006368
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006369 if (rettv->v_type == VAR_ANY)
6370 {
6371 char_u *exp_name;
6372 int cc;
6373 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006374 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006375 type_T *type;
6376
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006377 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006378 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006379 if (**arg != '.')
6380 {
6381 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006382 semsg(_(e_expected_dot_after_name_str),
6383 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006384 ret = FAIL;
6385 break;
6386 }
6387 ++*arg;
6388 if (IS_WHITE_OR_NUL(**arg))
6389 {
6390 if (verbose)
6391 emsg(_(e_no_white_space_allowed_after_dot));
6392 ret = FAIL;
6393 break;
6394 }
6395
6396 // isolate the name
6397 exp_name = *arg;
6398 while (eval_isnamec(**arg))
6399 ++*arg;
6400 cc = **arg;
6401 **arg = NUL;
6402
6403 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00006404 evalarg->eval_cctx, evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006405 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006406
6407 if (idx < 0 && ufunc == NULL)
6408 {
6409 ret = FAIL;
6410 break;
6411 }
6412 if (idx >= 0)
6413 {
6414 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6415 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6416
6417 copy_tv(sv->sv_tv, rettv);
6418 }
6419 else
6420 {
6421 rettv->v_type = VAR_FUNC;
6422 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6423 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006424 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006425 }
6426
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006427 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6428 || rettv->v_type == VAR_PARTIAL))
6429 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006430 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006431 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6432 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006433
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006434 // Stop the expression evaluation when immediately aborting on
6435 // error, or when an interrupt occurred or an exception was thrown
6436 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006437 if (aborting())
6438 {
6439 if (ret == OK)
6440 clear_tv(rettv);
6441 ret = FAIL;
6442 }
6443 dict_unref(selfdict);
6444 selfdict = NULL;
6445 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006446 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006447 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006448 if (in_vim9script())
6449 *arg = skipwhite(p + 2);
6450 else
6451 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006452 if (ret == OK)
6453 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006454 if (VIM_ISWHITE(**arg))
6455 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006456 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006457 ret = FAIL;
6458 }
6459 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006460 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006461 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006462 else
6463 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006464 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006465 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006466 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006467 // "." is ".name" lookup when we found a dict or when evaluating and
6468 // scriptversion is at least 2, where string concatenation is "..".
6469 else if (**arg == '['
6470 || (**arg == '.' && (rettv->v_type == VAR_DICT
6471 || (!evaluate
6472 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006473 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006474 {
6475 dict_unref(selfdict);
6476 if (rettv->v_type == VAR_DICT)
6477 {
6478 selfdict = rettv->vval.v_dict;
6479 if (selfdict != NULL)
6480 ++selfdict->dv_refcount;
6481 }
6482 else
6483 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006484 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006485 {
6486 clear_tv(rettv);
6487 ret = FAIL;
6488 }
6489 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006490 else
6491 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006492 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006493
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006494 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6495 // Don't do this when "Func" is already a partial that was bound
6496 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006497 if (selfdict != NULL
6498 && (rettv->v_type == VAR_FUNC
6499 || (rettv->v_type == VAR_PARTIAL
6500 && (rettv->vval.v_partial->pt_auto
6501 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006502 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006503
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006504 dict_unref(selfdict);
6505 return ret;
6506}
6507
6508/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006509 * Make a copy of an item.
6510 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006511 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006512 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6513 * reference to an already copied list/dict can be used.
6514 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006515 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006516 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006517item_copy(
6518 typval_T *from,
6519 typval_T *to,
6520 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006521 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006522 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006523{
6524 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006525 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006526
Bram Moolenaar33570922005-01-25 22:26:29 +00006527 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006528 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006529 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006530 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006531 }
6532 ++recurse;
6533
6534 switch (from->v_type)
6535 {
6536 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006537 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006538 case VAR_STRING:
6539 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006540 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006541 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006542 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006543 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006544 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006545 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006546 copy_tv(from, to);
6547 break;
6548 case VAR_LIST:
6549 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006550 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006551 if (from->vval.v_list == NULL)
6552 to->vval.v_list = NULL;
6553 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6554 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006555 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006556 to->vval.v_list = from->vval.v_list->lv_copylist;
6557 ++to->vval.v_list->lv_refcount;
6558 }
6559 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006560 to->vval.v_list = list_copy(from->vval.v_list,
6561 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006562 if (to->vval.v_list == NULL)
6563 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006564 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006565 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006566 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006567 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006568 case VAR_DICT:
6569 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006570 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006571 if (from->vval.v_dict == NULL)
6572 to->vval.v_dict = NULL;
6573 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6574 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006575 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006576 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6577 ++to->vval.v_dict->dv_refcount;
6578 }
6579 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006580 to->vval.v_dict = dict_copy(from->vval.v_dict,
6581 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006582 if (to->vval.v_dict == NULL)
6583 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006584 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006585 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006586 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006587 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006588 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006589 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006590 }
6591 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006592 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006593}
6594
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006595 void
6596echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6597{
6598 char_u *tofree;
6599 char_u numbuf[NUMBUFLEN];
6600 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6601
6602 if (*atstart)
6603 {
6604 *atstart = FALSE;
6605 // Call msg_start() after eval1(), evaluating the expression
6606 // may cause a message to appear.
6607 if (with_space)
6608 {
6609 // Mark the saved text as finishing the line, so that what
6610 // follows is displayed on a new line when scrolling back
6611 // at the more prompt.
6612 msg_sb_eol();
6613 msg_start();
6614 }
6615 }
6616 else if (with_space)
6617 msg_puts_attr(" ", echo_attr);
6618
6619 if (p != NULL)
6620 for ( ; *p != NUL && !got_int; ++p)
6621 {
6622 if (*p == '\n' || *p == '\r' || *p == TAB)
6623 {
6624 if (*p != TAB && *needclr)
6625 {
6626 // remove any text still there from the command
6627 msg_clr_eos();
6628 *needclr = FALSE;
6629 }
6630 msg_putchar_attr(*p, echo_attr);
6631 }
6632 else
6633 {
6634 if (has_mbyte)
6635 {
6636 int i = (*mb_ptr2len)(p);
6637
6638 (void)msg_outtrans_len_attr(p, i, echo_attr);
6639 p += i - 1;
6640 }
6641 else
6642 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6643 }
6644 }
6645 vim_free(tofree);
6646}
6647
Bram Moolenaare9a41262005-01-15 22:18:47 +00006648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 * ":echo expr1 ..." print each argument separated with a space, add a
6650 * newline at the end.
6651 * ":echon expr1 ..." print each argument plain.
6652 */
6653 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006654ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655{
6656 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006657 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006658 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 int needclr = TRUE;
6660 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006661 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006662 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006663 evalarg_T evalarg;
6664
Bram Moolenaare707c882020-07-01 13:07:37 +02006665 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666
6667 if (eap->skip)
6668 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006669 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006671 // If eval1() causes an error message the text from the command may
6672 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006673 need_clr_eos = needclr;
6674
Bram Moolenaar68db9962021-05-09 23:19:22 +02006675 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006676 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 {
6678 /*
6679 * Report the invalid expression unless the expression evaluation
6680 * has been cancelled due to an aborting error, an interrupt, or an
6681 * exception.
6682 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006683 if (!aborting() && did_emsg == did_emsg_before
6684 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006685 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006686 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 break;
6688 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006689 need_clr_eos = FALSE;
6690
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006692 {
6693 if (rettv.v_type == VAR_VOID)
6694 {
6695 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6696 break;
6697 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006698 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006699 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006700
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006701 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 arg = skipwhite(arg);
6703 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006704 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006705 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706
6707 if (eap->skip)
6708 --emsg_skip;
6709 else
6710 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006711 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 if (needclr)
6713 msg_clr_eos();
6714 if (eap->cmdidx == CMD_echo)
6715 msg_end();
6716 }
6717}
6718
6719/*
6720 * ":echohl {name}".
6721 */
6722 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006723ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006725 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726}
6727
6728/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006729 * Returns the :echo attribute
6730 */
6731 int
6732get_echo_attr(void)
6733{
6734 return echo_attr;
6735}
6736
6737/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 * ":execute expr1 ..." execute the result of an expression.
6739 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01006740 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006742 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 * Each gets spaces around each argument and a newline at the end for
6744 * echo commands
6745 */
6746 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006747ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748{
6749 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006750 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 int ret = OK;
6752 char_u *p;
6753 garray_T ga;
6754 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006755 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756
6757 ga_init2(&ga, 1, 80);
6758
6759 if (eap->skip)
6760 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006761 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006763 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006764 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766
6767 if (!eap->skip)
6768 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006769 char_u buf[NUMBUFLEN];
6770
6771 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006772 {
6773 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6774 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006775 semsg(_(e_using_invalid_value_as_string_str),
6776 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006777 p = NULL;
6778 }
6779 else
6780 p = tv_get_string_buf(&rettv, buf);
6781 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006782 else
6783 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006784 if (p == NULL)
6785 {
6786 clear_tv(&rettv);
6787 ret = FAIL;
6788 break;
6789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 len = (int)STRLEN(p);
6791 if (ga_grow(&ga, len + 2) == FAIL)
6792 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006793 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 ret = FAIL;
6795 break;
6796 }
6797 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 ga.ga_len += len;
6801 }
6802
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006803 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 arg = skipwhite(arg);
6805 }
6806
6807 if (ret != FAIL && ga.ga_data != NULL)
6808 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006809 // use the first line of continuation lines for messages
6810 SOURCING_LNUM = start_lnum;
6811
Bram Moolenaar37fef162022-08-29 18:16:32 +01006812 if (eap->cmdidx == CMD_echomsg
6813 || eap->cmdidx == CMD_echowindow
6814 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006815 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006816 // Mark the already saved text as finishing the line, so that what
6817 // follows is displayed on a new line when scrolling back at the
6818 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006819 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006820 }
6821
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006822 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006823 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006824 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006825 out_flush();
6826 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006827 else if (eap->cmdidx == CMD_echowindow)
6828 {
6829#ifdef HAS_MESSAGE_WINDOW
6830 start_echowindow();
6831#endif
6832 msg_attr(ga.ga_data, echo_attr);
6833#ifdef HAS_MESSAGE_WINDOW
6834 end_echowindow();
6835#endif
6836 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006837 else if (eap->cmdidx == CMD_echoconsole)
6838 {
6839 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6840 ui_write((char_u *)"\r\n", 2, TRUE);
6841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 else if (eap->cmdidx == CMD_echoerr)
6843 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006844 int save_did_emsg = did_emsg;
6845
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006846 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006847 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 if (!force_abort)
6849 did_emsg = save_did_emsg;
6850 }
6851 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006852 {
6853 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6854
6855 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6856 sticky_cmdmod_flags = cmdmod.cmod_flags
6857 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 do_cmdline((char_u *)ga.ga_data,
6859 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006860 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 }
6863
6864 ga_clear(&ga);
6865
6866 if (eap->skip)
6867 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02006868 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869}
6870
6871/*
6872 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6873 * "arg" points to the "&" or '+' when called, to "option" when returning.
6874 * Returns NULL when no option name found. Otherwise pointer to the char
6875 * after the option name.
6876 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006877 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006878find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879{
6880 char_u *p = *arg;
6881
6882 ++p;
6883 if (*p == 'g' && p[1] == ':')
6884 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006885 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 p += 2;
6887 }
6888 else if (*p == 'l' && p[1] == ':')
6889 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006890 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 p += 2;
6892 }
6893 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006894 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895
6896 if (!ASCII_ISALPHA(*p))
6897 return NULL;
6898 *arg = p;
6899
6900 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006901 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 else
6903 while (ASCII_ISALPHA(*p))
6904 ++p;
6905 return p;
6906}
6907
6908/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006909 * Display script name where an item was last set.
6910 * Should only be invoked when 'verbose' is non-zero.
6911 */
6912 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006913last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006914{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006915 char_u *p;
6916
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006917 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006918 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006919 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006920 if (p != NULL)
6921 {
6922 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006923 msg_puts(_("\n\tLast set from "));
6924 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006925 if (script_ctx.sc_lnum > 0)
6926 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006927 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006928 msg_outnum((long)script_ctx.sc_lnum);
6929 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006930 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006931 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006932 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006933 }
6934}
6935
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006936#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937
6938/*
6939 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006940 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 * "flags" can be "g" to do a global substitute.
6942 * Returns an allocated string, NULL for error.
6943 */
6944 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006945do_string_sub(
6946 char_u *str,
6947 char_u *pat,
6948 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006949 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006950 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951{
6952 int sublen;
6953 regmatch_T regmatch;
6954 int i;
6955 int do_all;
6956 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006957 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 garray_T ga;
6959 char_u *ret;
6960 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006961 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006963 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006965 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966
6967 ga_init2(&ga, 1, 200);
6968
6969 do_all = (flags[0] == 'g');
6970
6971 regmatch.rm_ic = p_ic;
6972 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6973 if (regmatch.regprog != NULL)
6974 {
6975 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006976 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6978 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006979 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006980 if (regmatch.startp[0] == regmatch.endp[0])
6981 {
6982 if (zero_width == regmatch.startp[0])
6983 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006984 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006985 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006986 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6987 (size_t)i);
6988 ga.ga_len += i;
6989 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006990 continue;
6991 }
6992 zero_width = regmatch.startp[0];
6993 }
6994
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 /*
6996 * Get some space for a temporary buffer to do the substitution
6997 * into. It will contain:
6998 * - The text up to where the match is.
6999 * - The substituted text.
7000 * - The text after the match.
7001 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007002 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaare90c8532014-11-05 16:03:44 +01007003 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7005 {
7006 ga_clear(&ga);
7007 break;
7008 }
7009
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007010 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 i = (int)(regmatch.startp[0] - tail);
7012 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007013 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007014 (void)vim_regsub(&regmatch, sub, expr,
7015 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7016 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007018 tail = regmatch.endp[0];
7019 if (*tail == NUL)
7020 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 if (!do_all)
7022 break;
7023 }
7024
7025 if (ga.ga_data != NULL)
7026 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7027
Bram Moolenaar473de612013-06-08 18:19:48 +02007028 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 }
7030
7031 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7032 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007033 if (p_cpo == empty_option)
7034 p_cpo = save_cpo;
7035 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007036 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007037 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007038 // If it's still empty it was changed and restored, need to restore in
7039 // the complicated way.
7040 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007041 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007042 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044
7045 return ret;
7046}