blob: 98914738139b217ba9c7b001e53bf31a1310a000 [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 Moolenaar3a3b10e2021-06-26 15:00:59 +02001124 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1125 {
1126 if (!quiet)
1127 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1128 return NULL;
1129 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001130 if (lp->ll_tv->v_type != VAR_LIST
1131 && lp->ll_tv->v_type != VAR_DICT
1132 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001133 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001134 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001135 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001136 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001137 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001138
1139 // a NULL list/blob works like an empty list/blob, allocate one now.
1140 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1141 rettv_list_alloc(lp->ll_tv);
1142 else if (lp->ll_tv->v_type == VAR_BLOB
1143 && lp->ll_tv->vval.v_blob == NULL)
1144 rettv_blob_alloc(lp->ll_tv);
1145
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001146 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001147 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001148 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001149 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001150 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001151 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001152
Bram Moolenaar4525a572022-02-13 11:57:33 +00001153 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001154 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001155 && lp->ll_tv == &v->di_tv
1156 && ht != NULL && ht == get_script_local_ht())
1157 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001158 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001159
1160 // Vim9 script local variable: get the type
1161 if (sv != NULL)
1162 lp->ll_valtype = sv->sv_type;
1163 }
1164
Bram Moolenaar8c711452005-01-14 21:53:12 +00001165 len = -1;
1166 if (*p == '.')
1167 {
1168 key = p + 1;
1169 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1170 ;
1171 if (len == 0)
1172 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001173 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001174 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001175 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001176 }
1177 p = key + len;
1178 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001179 else
1180 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001181 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001182 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001183 if (*p == ':')
1184 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001185 else
1186 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001187 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001188 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001189 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001190 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001191 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001192 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001193 clear_tv(&var1);
1194 return NULL;
1195 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001196 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001197 }
1198
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001199 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001200 if (*p == ':')
1201 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001202 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001203 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001204 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001205 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001206 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001207 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001208 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001209 if (rettv != NULL
1210 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001211 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001212 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001213 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001214 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001215 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001216 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001217 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001218 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001219 }
1220 p = skipwhite(p + 1);
1221 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001222 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001223 else
1224 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001225 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001226 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001227 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001228 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001229 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001230 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001231 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001232 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001233 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001234 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001235 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001236 clear_tv(&var2);
1237 return NULL;
1238 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001239 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001240 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001241 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001242 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001243 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001244
Bram Moolenaar8c711452005-01-14 21:53:12 +00001245 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001246 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001247 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001248 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001249 clear_tv(&var1);
1250 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001251 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001252 }
1253
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001254 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001255 ++p;
1256 }
1257
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001258 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001259 {
1260 if (len == -1)
1261 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001262 // "[key]": get key from "var1"
1263 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001264 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001265 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001266 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001267 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001268 }
1269 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001270 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001271
1272 // a NULL dict is equivalent with an empty dict
1273 if (lp->ll_tv->vval.v_dict == NULL)
1274 {
1275 lp->ll_tv->vval.v_dict = dict_alloc();
1276 if (lp->ll_tv->vval.v_dict == NULL)
1277 {
1278 clear_tv(&var1);
1279 return NULL;
1280 }
1281 ++lp->ll_tv->vval.v_dict->dv_refcount;
1282 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001283 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001284
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001285 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001286
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001287 // When assigning to a scope dictionary check that a function and
1288 // variable name is valid (only variable name unless it is l: or
1289 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001290 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001291 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001292 int prevval;
1293 int wrong;
1294
1295 if (len != -1)
1296 {
1297 prevval = key[len];
1298 key[len] = NUL;
1299 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001300 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001301 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001302 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1303 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001304 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001305 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001306 if (len != -1)
1307 key[len] = prevval;
1308 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001309 {
1310 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001311 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001312 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001313 }
1314
Bram Moolenaar10c65862020-10-08 21:16:42 +02001315 if (lp->ll_valtype != NULL)
1316 // use the type of the member
1317 lp->ll_valtype = lp->ll_valtype->tt_member;
1318
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001319 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001320 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001321 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001322 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001323 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001324 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001325 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001326 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001327 return NULL;
1328 }
1329
Bram Moolenaar31b81602019-02-10 22:14:27 +01001330 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001331 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001332 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001333 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001334 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001335 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001336 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001337 }
1338 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001339 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001340 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001341 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001342 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001343 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001344 p = NULL;
1345 break;
1346 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001347 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001348 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001349 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1350 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001351 {
1352 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001353 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001354 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001355
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001356 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001357 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001358 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001359 else if (lp->ll_tv->v_type == VAR_BLOB)
1360 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001361 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1362
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001363 /*
1364 * Get the number and item for the only or first index of the List.
1365 */
1366 if (empty1)
1367 lp->ll_n1 = 0;
1368 else
1369 // is number or string
1370 lp->ll_n1 = (long)tv_get_number(&var1);
1371 clear_tv(&var1);
1372
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001373 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001374 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001375 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001376 return NULL;
1377 }
1378 if (lp->ll_range && !lp->ll_empty2)
1379 {
1380 lp->ll_n2 = (long)tv_get_number(&var2);
1381 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001382 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1383 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001384 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001385 }
1386 lp->ll_blob = lp->ll_tv->vval.v_blob;
1387 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001388 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001389 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001390 else
1391 {
1392 /*
1393 * Get the number and item for the only or first index of the List.
1394 */
1395 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001396 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001397 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001398 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001399 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001400 clear_tv(&var1);
1401
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001402 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001403 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001404 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1405 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001406 if (lp->ll_li == NULL)
1407 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001408 clear_tv(&var2);
1409 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001410 }
1411
Bram Moolenaar10c65862020-10-08 21:16:42 +02001412 if (lp->ll_valtype != NULL)
1413 // use the type of the member
1414 lp->ll_valtype = lp->ll_valtype->tt_member;
1415
Bram Moolenaar8c711452005-01-14 21:53:12 +00001416 /*
1417 * May need to find the item or absolute index for the second
1418 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001419 * When no index given: "lp->ll_empty2" is TRUE.
1420 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001421 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001422 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001423 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001424 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001425 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001426 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001427 if (check_range_index_two(lp->ll_list,
1428 &lp->ll_n1, lp->ll_li,
1429 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001430 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001431 }
1432
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001433 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001434 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001435 }
1436
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001437 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001438 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001439 return p;
1440}
1441
1442/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001443 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001444 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001445 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001446clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001447{
1448 vim_free(lp->ll_exp_name);
1449 vim_free(lp->ll_newkey);
1450}
1451
1452/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001453 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001454 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001455 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1456 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001457 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001458 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001459set_var_lval(
1460 lval_T *lp,
1461 char_u *endp,
1462 typval_T *rettv,
1463 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001464 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1465 char_u *op,
1466 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001467{
1468 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001469 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001470
1471 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001472 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001473 cc = *endp;
1474 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001475 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1476 return;
1477
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001478 if (lp->ll_blob != NULL)
1479 {
1480 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001481
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001482 if (op != NULL && *op != '=')
1483 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001484 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001485 return;
1486 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001487 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001488 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001489
1490 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1491 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001492 if (lp->ll_empty2)
1493 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1494
Bram Moolenaar68452172021-04-12 21:21:02 +02001495 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1496 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001497 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001498 }
1499 else
1500 {
1501 val = (int)tv_get_number_chk(rettv, &error);
1502 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001503 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001504 }
1505 }
1506 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001507 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001508 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001509
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001510 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1511 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001512 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001513 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001514 *endp = cc;
1515 return;
1516 }
1517
Bram Moolenaarff697e62019-02-12 22:28:33 +01001518 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001519 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001520 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001521 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001522 {
1523 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001524 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1525 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001526 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001527 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001528 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001529 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001530 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001531 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001532 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001533 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001534 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1535 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001536 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001537 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001538 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001539 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001540 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001541 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001542 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001543 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001544 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001545 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001546 else if (lp->ll_range)
1547 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001548 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1549 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001550 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001551 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001552 return;
1553 }
1554
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001555 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1556 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001557 }
1558 else
1559 {
1560 /*
1561 * Assign to a List or Dictionary item.
1562 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001563 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1564 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001565 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001566 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001567 return;
1568 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001569
1570 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001571 && check_typval_arg_type(lp->ll_valtype, rettv,
1572 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001573 return;
1574
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001575 if (lp->ll_newkey != NULL)
1576 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001577 if (op != NULL && *op != '=')
1578 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001579 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001580 return;
1581 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001582 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1583 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001584 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001585
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001586 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001587 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001588 if (di == NULL)
1589 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001590 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1591 {
1592 vim_free(di);
1593 return;
1594 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001595 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001596 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001597 else if (op != NULL && *op != '=')
1598 {
1599 tv_op(lp->ll_tv, rettv, op);
1600 return;
1601 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001602 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001603 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001604
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001605 /*
1606 * Assign the value to the variable or list item.
1607 */
1608 if (copy)
1609 copy_tv(rettv, lp->ll_tv);
1610 else
1611 {
1612 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001613 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001614 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001615 }
1616 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001617}
1618
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001619/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001620 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1621 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001622 * Returns OK or FAIL.
1623 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001624 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001625tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001626{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001627 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001628 char_u numbuf[NUMBUFLEN];
1629 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001630 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001631
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001632 // Can't do anything with a Funcref or Dict on the right.
1633 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001634 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001635 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1636 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001637 {
1638 switch (tv1->v_type)
1639 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001640 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001641 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001642 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001643 case VAR_DICT:
1644 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001645 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001646 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001647 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001648 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001649 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001650 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001651 break;
1652
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001653 case VAR_BLOB:
1654 if (*op != '+' || tv2->v_type != VAR_BLOB)
1655 break;
1656 // BLOB += BLOB
1657 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1658 {
1659 blob_T *b1 = tv1->vval.v_blob;
1660 blob_T *b2 = tv2->vval.v_blob;
1661 int i, len = blob_len(b2);
1662 for (i = 0; i < len; i++)
1663 ga_append(&b1->bv_ga, blob_get(b2, i));
1664 }
1665 return OK;
1666
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001667 case VAR_LIST:
1668 if (*op != '+' || tv2->v_type != VAR_LIST)
1669 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001670 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001671 if (tv2->vval.v_list != NULL)
1672 {
1673 if (tv1->vval.v_list == NULL)
1674 {
1675 tv1->vval.v_list = tv2->vval.v_list;
1676 ++tv1->vval.v_list->lv_refcount;
1677 }
1678 else
1679 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1680 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001681 return OK;
1682
1683 case VAR_NUMBER:
1684 case VAR_STRING:
1685 if (tv2->v_type == VAR_LIST)
1686 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001687 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001688 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001689 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001690 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001691#ifdef FEAT_FLOAT
1692 if (tv2->v_type == VAR_FLOAT)
1693 {
1694 float_T f = n;
1695
Bram Moolenaarff697e62019-02-12 22:28:33 +01001696 if (*op == '%')
1697 break;
1698 switch (*op)
1699 {
1700 case '+': f += tv2->vval.v_float; break;
1701 case '-': f -= tv2->vval.v_float; break;
1702 case '*': f *= tv2->vval.v_float; break;
1703 case '/': f /= tv2->vval.v_float; break;
1704 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001705 clear_tv(tv1);
1706 tv1->v_type = VAR_FLOAT;
1707 tv1->vval.v_float = f;
1708 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001709 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001710#endif
1711 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001712 switch (*op)
1713 {
1714 case '+': n += tv_get_number(tv2); break;
1715 case '-': n -= tv_get_number(tv2); break;
1716 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001717 case '/': n = num_divide(n, tv_get_number(tv2),
1718 &failed); break;
1719 case '%': n = num_modulus(n, tv_get_number(tv2),
1720 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001721 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001722 clear_tv(tv1);
1723 tv1->v_type = VAR_NUMBER;
1724 tv1->vval.v_number = n;
1725 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001726 }
1727 else
1728 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001729 if (tv2->v_type == VAR_FLOAT)
1730 break;
1731
Bram Moolenaarff697e62019-02-12 22:28:33 +01001732 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001733 s = tv_get_string(tv1);
1734 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001735 clear_tv(tv1);
1736 tv1->v_type = VAR_STRING;
1737 tv1->vval.v_string = s;
1738 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001739 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001740
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001741 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001742#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001743 {
1744 float_T f;
1745
Bram Moolenaarff697e62019-02-12 22:28:33 +01001746 if (*op == '%' || *op == '.'
1747 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001748 && tv2->v_type != VAR_NUMBER
1749 && tv2->v_type != VAR_STRING))
1750 break;
1751 if (tv2->v_type == VAR_FLOAT)
1752 f = tv2->vval.v_float;
1753 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001754 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001755 switch (*op)
1756 {
1757 case '+': tv1->vval.v_float += f; break;
1758 case '-': tv1->vval.v_float -= f; break;
1759 case '*': tv1->vval.v_float *= f; break;
1760 case '/': tv1->vval.v_float /= f; break;
1761 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001762 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001763#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001764 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001765 }
1766 }
1767
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001768 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001769 return FAIL;
1770}
1771
1772/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001773 * Evaluate the expression used in a ":for var in expr" command.
1774 * "arg" points to "var".
1775 * Set "*errp" to TRUE for an error, FALSE otherwise;
1776 * Return a pointer that holds the info. Null when there is an error.
1777 */
1778 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001779eval_for_line(
1780 char_u *arg,
1781 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001782 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001783 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001784{
Bram Moolenaar33570922005-01-25 22:26:29 +00001785 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001786 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001787 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001788 typval_T tv;
1789 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001790 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001791
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001792 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001793
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001794 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001795 if (fi == NULL)
1796 return NULL;
1797
Bram Moolenaar404557e2021-07-05 21:41:48 +02001798 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1799 &fi->fi_semicolon, FALSE);
1800 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001801 return fi;
1802
Bram Moolenaar404557e2021-07-05 21:41:48 +02001803 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001804 if (expr[0] != 'i' || expr[1] != 'n'
1805 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001806 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001807 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1808 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1809 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001810 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001811 return fi;
1812 }
1813
1814 if (skip)
1815 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001816 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1817 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001818 {
1819 *errp = FALSE;
1820 if (!skip)
1821 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001822 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001823 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001824 l = tv.vval.v_list;
1825 if (l == NULL)
1826 {
1827 // a null list is like an empty list: do nothing
1828 clear_tv(&tv);
1829 }
1830 else
1831 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001832 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001833 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001834
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001835 // No need to increment the refcount, it's already set for
1836 // the list being used in "tv".
1837 fi->fi_list = l;
1838 list_add_watch(l, &fi->fi_lw);
1839 fi->fi_lw.lw_item = l->lv_first;
1840 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001841 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001842 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001843 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001844 fi->fi_bi = 0;
1845 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001846 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001847 typval_T btv;
1848
1849 // Make a copy, so that the iteration still works when the
1850 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001851 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001852 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001853 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001854 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001855 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001856 else if (tv.v_type == VAR_STRING)
1857 {
1858 fi->fi_byte_idx = 0;
1859 fi->fi_string = tv.vval.v_string;
1860 tv.vval.v_string = NULL;
1861 if (fi->fi_string == NULL)
1862 fi->fi_string = vim_strsave((char_u *)"");
1863 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001864 else
1865 {
Sean Dewar80d73952021-08-04 19:25:54 +02001866 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001867 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001868 }
1869 }
1870 }
1871 if (skip)
1872 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001873 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001874
1875 return fi;
1876}
1877
1878/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001879 * Used when looping over a :for line, skip the "in expr" part.
1880 */
1881 void
1882skip_for_lines(void *fi_void, evalarg_T *evalarg)
1883{
1884 forinfo_T *fi = (forinfo_T *)fi_void;
1885 int i;
1886
1887 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01001888 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001889}
1890
1891/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001892 * Use the first item in a ":for" list. Advance to the next.
1893 * Assign the values to the variable (list). "arg" points to the first one.
1894 * Return TRUE when a valid item was found, FALSE when at end of list or
1895 * something wrong.
1896 */
1897 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001898next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001899{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001900 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001901 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001902 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001903 ? (ASSIGN_FINAL
1904 // first round: error if variable exists
1905 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1906 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001907 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001908 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001909 int skip_assign = in_vim9script() && arg[0] == '_'
1910 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001911
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001912 if (fi->fi_blob != NULL)
1913 {
1914 typval_T tv;
1915
1916 if (fi->fi_bi >= blob_len(fi->fi_blob))
1917 return FALSE;
1918 tv.v_type = VAR_NUMBER;
1919 tv.v_lock = VAR_FIXED;
1920 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1921 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001922 if (skip_assign)
1923 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001924 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001925 fi->fi_varcount, flag, NULL) == OK;
1926 }
1927
1928 if (fi->fi_string != NULL)
1929 {
1930 typval_T tv;
1931 int len;
1932
1933 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1934 if (len == 0)
1935 return FALSE;
1936 tv.v_type = VAR_STRING;
1937 tv.v_lock = VAR_FIXED;
1938 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1939 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001940 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001941 if (skip_assign)
1942 result = TRUE;
1943 else
1944 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001945 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001946 vim_free(tv.vval.v_string);
1947 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001948 }
1949
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950 item = fi->fi_lw.lw_item;
1951 if (item == NULL)
1952 result = FALSE;
1953 else
1954 {
1955 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001956 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001957 if (skip_assign)
1958 result = TRUE;
1959 else
1960 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001961 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001962 }
1963 return result;
1964}
1965
1966/*
1967 * Free the structure used to store info used by ":for".
1968 */
1969 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001970free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001971{
Bram Moolenaar33570922005-01-25 22:26:29 +00001972 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001974 if (fi == NULL)
1975 return;
1976 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001977 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001978 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001979 list_unref(fi->fi_list);
1980 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001981 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001982 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001983 else
1984 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001985 vim_free(fi);
1986}
1987
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001989set_context_for_expression(
1990 expand_T *xp,
1991 char_u *arg,
1992 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001994 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001996 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001998 if (cmdidx == CMD_let || cmdidx == CMD_var
1999 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002000 {
2001 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002002 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002003 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002004 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002005 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002006 {
2007 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002008 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002009 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002010 break;
2011 }
2012 return;
2013 }
2014 }
2015 else
2016 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2017 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 while ((xp->xp_pattern = vim_strpbrk(arg,
2019 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2020 {
2021 c = *xp->xp_pattern;
2022 if (c == '&')
2023 {
2024 c = xp->xp_pattern[1];
2025 if (c == '&')
2026 {
2027 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002028 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 }
2030 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002031 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002033 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2034 xp->xp_pattern += 2;
2035
2036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 }
2038 else if (c == '$')
2039 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002040 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 xp->xp_context = EXPAND_ENV_VARS;
2042 }
2043 else if (c == '=')
2044 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002045 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 xp->xp_context = EXPAND_EXPRESSION;
2047 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002048 else if (c == '#'
2049 && xp->xp_context == EXPAND_EXPRESSION)
2050 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002051 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002052 break;
2053 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002054 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 && xp->xp_context == EXPAND_FUNCTIONS
2056 && vim_strchr(xp->xp_pattern, '(') == NULL)
2057 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002058 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 break;
2060 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002061 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002063 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 {
2065 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2066 if (c == '\\' && xp->xp_pattern[1] != NUL)
2067 ++xp->xp_pattern;
2068 xp->xp_context = EXPAND_NOTHING;
2069 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002070 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002072 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2074 /* skip */ ;
2075 xp->xp_context = EXPAND_NOTHING;
2076 }
2077 else if (c == '|')
2078 {
2079 if (xp->xp_pattern[1] == '|')
2080 {
2081 ++xp->xp_pattern;
2082 xp->xp_context = EXPAND_EXPRESSION;
2083 }
2084 else
2085 xp->xp_context = EXPAND_COMMANDS;
2086 }
2087 else
2088 xp->xp_context = EXPAND_EXPRESSION;
2089 }
2090 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002091 // Doesn't look like something valid, expand as an expression
2092 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002093 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 arg = xp->xp_pattern;
2095 if (*arg != NUL)
2096 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2097 /* skip */ ;
2098 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002099
2100 // ":exe one two" completes "two"
2101 if ((cmdidx == CMD_execute
2102 || cmdidx == CMD_echo
2103 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002104 || cmdidx == CMD_echomsg
2105 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002106 && xp->xp_context == EXPAND_EXPRESSION)
2107 {
2108 for (;;)
2109 {
2110 char_u *n = skiptowhite(arg);
2111
2112 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2113 break;
2114 arg = skipwhite(n);
2115 }
2116 }
2117
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 xp->xp_pattern = arg;
2119}
2120
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002122 * Return TRUE if "pat" matches "text".
2123 * Does not use 'cpo' and always uses 'magic'.
2124 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002125 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002126pattern_match(char_u *pat, char_u *text, int ic)
2127{
2128 int matches = FALSE;
2129 char_u *save_cpo;
2130 regmatch_T regmatch;
2131
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002132 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002133 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002134 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002135 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2136 if (regmatch.regprog != NULL)
2137 {
2138 regmatch.rm_ic = ic;
2139 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2140 vim_regfree(regmatch.regprog);
2141 }
2142 p_cpo = save_cpo;
2143 return matches;
2144}
2145
2146/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002147 * Handle a name followed by "(". Both for just "name(arg)" and for
2148 * "expr->name(arg)".
2149 * Returns OK or FAIL.
2150 */
2151 static int
2152eval_func(
2153 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002154 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002155 char_u *name,
2156 int name_len,
2157 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002158 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002159 typval_T *basetv) // "expr" for "expr->name(arg)"
2160{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002161 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002162 char_u *s = name;
2163 int len = name_len;
2164 partial_T *partial;
2165 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002166 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002167 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002168
2169 if (!evaluate)
2170 check_vars(s, len);
2171
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002172 // If "s" is the name of a variable of type VAR_FUNC
2173 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002174 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002175 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002176
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002177 // Need to make a copy, in case evaluating the arguments makes
2178 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002179 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002180 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002181 ret = FAIL;
2182 else
2183 {
2184 funcexe_T funcexe;
2185
2186 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002187 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002188 funcexe.fe_firstline = curwin->w_cursor.lnum;
2189 funcexe.fe_lastline = curwin->w_cursor.lnum;
2190 funcexe.fe_evaluate = evaluate;
2191 funcexe.fe_partial = partial;
2192 funcexe.fe_basetv = basetv;
2193 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002194 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002195 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002196 }
2197 vim_free(s);
2198
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002199 // If evaluate is FALSE rettv->v_type was not set in
2200 // get_func_tv, but it's needed in handle_subscript() to parse
2201 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002202 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2203 {
2204 rettv->vval.v_string = NULL;
2205 rettv->v_type = VAR_FUNC;
2206 }
2207
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002208 // Stop the expression evaluation when immediately
2209 // aborting on error, or when an interrupt occurred or
2210 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002211 if (evaluate && aborting())
2212 {
2213 if (ret == OK)
2214 clear_tv(rettv);
2215 ret = FAIL;
2216 }
2217 return ret;
2218}
2219
2220/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002221 * After a NL, skip over empty lines and comment-only lines.
2222 */
2223 static char_u *
2224newline_skip_comments(char_u *arg)
2225{
2226 char_u *p = arg + 1;
2227
2228 for (;;)
2229 {
2230 p = skipwhite(p);
2231
2232 if (*p == NUL)
2233 break;
2234 if (vim9_comment_start(p))
2235 {
2236 char_u *nl = vim_strchr(p, NL);
2237
2238 if (nl == NULL)
2239 break;
2240 p = nl;
2241 }
2242 if (*p != NL)
2243 break;
2244 ++p; // skip another NL
2245 }
2246 return p;
2247}
2248
2249/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002250 * Get the next line source line without advancing. But do skip over comment
2251 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002252 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002253 */
2254 static char_u *
2255getline_peek_skip_comments(evalarg_T *evalarg)
2256{
2257 for (;;)
2258 {
2259 char_u *next = getline_peek(evalarg->eval_getline,
2260 evalarg->eval_cookie);
2261 char_u *p;
2262
2263 if (next == NULL)
2264 break;
2265 p = skipwhite(next);
2266 if (*p != NUL && !vim9_comment_start(p))
2267 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002268 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002269 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002270 }
2271 return NULL;
2272}
2273
2274/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002275 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2276 * comment) and there is a next line, return the next line (skipping blanks)
2277 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002278 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2279 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002280 * "arg" must point somewhere inside a line, not at the start.
2281 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002282 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002283eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2284{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002285 char_u *p = skipwhite(arg);
2286
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002287 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002288 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002289 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002290 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2291 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002292 && (*p == NUL || *p == NL
2293 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002294 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002295 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002296
Bram Moolenaare442d592022-05-05 12:20:28 +01002297 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002298 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002299 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002300 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002301 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002302 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002303
Bram Moolenaar9d489562020-07-30 20:08:50 +02002304 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002305 {
2306 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002307 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002308 }
2309 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002310 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002311}
2312
2313/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002314 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002315 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002316 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002317 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002318eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002319{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002320 garray_T *gap = &evalarg->eval_ga;
2321 char_u *line;
2322
Bram Moolenaar39be4982022-05-06 21:51:50 +01002323 if (arg != NULL)
2324 {
2325 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002326 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002327 // Truncate before a trailing comment, so that concatenating the lines
2328 // won't turn the rest into a comment.
2329 if (*skipwhite(arg) == '#')
2330 *arg = NUL;
2331 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002332
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002333 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002334 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2335 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002336 else
2337 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002338 if (line == NULL)
2339 return NULL;
2340
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002341 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002342 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2343 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002344 char_u *p = skipwhite(line);
2345
2346 // Going to concatenate the lines after parsing. For an empty or
2347 // comment line use an empty string.
2348 if (*p == NUL || vim9_comment_start(p))
2349 {
2350 vim_free(line);
2351 line = vim_strsave((char_u *)"");
2352 }
2353
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002354 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2355 ++gap->ga_len;
2356 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002357 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002358 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002359 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002360 evalarg->eval_tofree = line;
2361 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002362
2363 // Advanced to the next line, "arg" no longer points into the previous
2364 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002365 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002366 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002367}
2368
Bram Moolenaare6b53242020-07-01 17:28:33 +02002369/*
2370 * Call eval_next_non_blank() and get the next line if needed.
2371 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002372 char_u *
2373skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2374{
2375 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002376 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002377
Bram Moolenaar962d7212020-07-04 14:15:00 +02002378 if (evalarg == NULL)
2379 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002380 eval_next_non_blank(p, evalarg, &getnext);
2381 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002382 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002383 return p;
2384}
2385
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002388 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2390 */
2391
2392/*
2393 * Handle zero level expression.
2394 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002395 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002396 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002397 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 * Return OK or FAIL.
2399 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002400 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002401eval0(
2402 char_u *arg,
2403 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002404 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002405 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002407 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2408}
2409
2410/*
2411 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2412 * expression and don't check what comes after the expression.
2413 */
2414 int
2415eval0_retarg(
2416 char_u *arg,
2417 typval_T *rettv,
2418 exarg_T *eap,
2419 evalarg_T *evalarg,
2420 char_u **retarg)
2421{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 int ret;
2423 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002424 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002425 int did_emsg_before = did_emsg;
2426 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002427 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002428 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002429 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430
2431 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002432 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002433
Bram Moolenaar79481362022-06-27 20:15:10 +01002434 if (ret != FAIL)
2435 {
2436 expr_end = p;
2437 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002438
Bram Moolenaar79481362022-06-27 20:15:10 +01002439 // In Vim9 script a command block is not split at NL characters for
2440 // commands using an expression argument. Skip over a '#' comment to
2441 // check for a following NL. Require white space before the '#'.
2442 if (in_vim9script() && p > expr_end && retarg == NULL)
2443 while (*p == '#')
2444 {
2445 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002446
Bram Moolenaar79481362022-06-27 20:15:10 +01002447 if (nl == NULL)
2448 break;
2449 p = skipwhite(nl + 1);
2450 if (eap != NULL && *p != NUL)
2451 eap->nextcmd = p;
2452 check_for_end = FALSE;
2453 }
2454
2455 if (check_for_end)
2456 end_error = !ends_excmd2(arg, p);
2457 }
2458
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002459 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 {
2461 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 /*
2464 * Report the invalid expression unless the expression evaluation has
2465 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002466 * exception, or we already gave a more specific error.
2467 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002469 if (!aborting()
2470 && did_emsg == did_emsg_before
2471 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002472 && (flags & EVAL_CONSTANT) == 0
2473 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002474 {
2475 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002476 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002477 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002478 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002479 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002480
2481 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002482 // a next command to avoid more errors, unless "|" is following, which
2483 // could only be a command separator.
Bram Moolenaar79481362022-06-27 20:15:10 +01002484 if (eap != NULL && p != NULL
2485 && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
Bram Moolenaar8143a532020-12-13 20:26:29 +01002486 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002487 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002489
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002490 if (retarg != NULL)
2491 *retarg = p;
2492 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002493 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002494
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 return ret;
2496}
2497
2498/*
2499 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002500 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002501 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 *
2503 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002504 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002506 * Note: "rettv.v_lock" is not set.
2507 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 * Return OK or FAIL.
2509 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002510 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002511eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002513 char_u *p;
2514 int getnext;
2515
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002516 CLEAR_POINTER(rettv);
2517
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 /*
2519 * Get the first variable.
2520 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002521 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 return FAIL;
2523
Bram Moolenaar793648f2020-06-26 21:28:25 +02002524 p = eval_next_non_blank(*arg, evalarg, &getnext);
2525 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002527 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002528 int result;
2529 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002530 evalarg_T *evalarg_used = evalarg;
2531 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002532 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002533 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002534 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002535
2536 if (evalarg == NULL)
2537 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002538 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002539 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002540 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002541 orig_flags = evalarg_used->eval_flags;
2542 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002543
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002544 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002545 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002546 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002547 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002548 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002549 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002550 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002551 clear_tv(rettv);
2552 return FAIL;
2553 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002554 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002555 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002556
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002558 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002560 int error = FALSE;
2561
Bram Moolenaar13106602020-10-04 16:06:05 +02002562 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002563 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002564 else if (vim9script)
2565 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002566 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002568 if (error || !op_falsy || !result)
2569 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002570 if (error)
2571 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 }
2573
2574 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002575 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002577 if (op_falsy)
2578 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002579 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002580 {
mityu4ac198c2021-05-28 17:52:40 +02002581 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002582 clear_tv(rettv);
2583 return FAIL;
2584 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002585 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002586 evalarg_used->eval_flags = (op_falsy ? !result : result)
2587 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2588 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002589 {
2590 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002592 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002593 if (!op_falsy || !result)
2594 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002596 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002598 /*
2599 * Check for the ":".
2600 */
2601 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2602 if (*p != ':')
2603 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002604 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002605 if (evaluate && result)
2606 clear_tv(rettv);
2607 evalarg_used->eval_flags = orig_flags;
2608 return FAIL;
2609 }
2610 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002611 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002612 else
2613 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002614 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002615 {
2616 error_white_both(p, 1);
2617 clear_tv(rettv);
2618 evalarg_used->eval_flags = orig_flags;
2619 return FAIL;
2620 }
2621 *arg = p;
2622 }
2623
2624 /*
2625 * Get the third variable. Recursive!
2626 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002627 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002628 {
mityu4ac198c2021-05-28 17:52:40 +02002629 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002630 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002631 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002632 return FAIL;
2633 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002634 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2635 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002636 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002637 if (eval1(arg, &var2, evalarg_used) == FAIL)
2638 {
2639 if (evaluate && result)
2640 clear_tv(rettv);
2641 evalarg_used->eval_flags = orig_flags;
2642 return FAIL;
2643 }
2644 if (evaluate && !result)
2645 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002647
2648 if (evalarg == NULL)
2649 clear_evalarg(&local_evalarg, NULL);
2650 else
2651 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 }
2653
2654 return OK;
2655}
2656
2657/*
2658 * Handle first level expression:
2659 * expr2 || expr2 || expr2 logical OR
2660 *
2661 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002662 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 *
2664 * Return OK or FAIL.
2665 */
2666 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002667eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002669 char_u *p;
2670 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671
2672 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002673 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002675 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 return FAIL;
2677
2678 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002679 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002681 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002682 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002684 evalarg_T *evalarg_used = evalarg;
2685 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002686 int evaluate;
2687 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002688 long result = FALSE;
2689 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002690 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002691 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002692
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002693 if (evalarg == NULL)
2694 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002695 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002696 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002697 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002698 orig_flags = evalarg_used->eval_flags;
2699 evaluate = orig_flags & EVAL_EVALUATE;
2700 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002701 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002702 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002703 result = tv_get_bool_chk(rettv, &error);
2704 else if (tv_get_number_chk(rettv, &error) != 0)
2705 result = TRUE;
2706 clear_tv(rettv);
2707 if (error)
2708 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 }
2710
2711 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002712 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002714 while (p[0] == '|' && p[1] == '|')
2715 {
2716 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002717 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002718 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002719 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002720 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002721 {
2722 error_white_both(p, 2);
2723 clear_tv(rettv);
2724 return FAIL;
2725 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002726 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002727 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002728
2729 /*
2730 * Get the second variable.
2731 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002732 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002733 {
mityu4ac198c2021-05-28 17:52:40 +02002734 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002735 clear_tv(rettv);
2736 return FAIL;
2737 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002738 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2739 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002740 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002741 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002742 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002743
2744 /*
2745 * Compute the result.
2746 */
2747 if (evaluate && !result)
2748 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002749 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002750 result = tv_get_bool_chk(&var2, &error);
2751 else if (tv_get_number_chk(&var2, &error) != 0)
2752 result = TRUE;
2753 clear_tv(&var2);
2754 if (error)
2755 return FAIL;
2756 }
2757 if (evaluate)
2758 {
2759 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002760 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002761 rettv->v_type = VAR_BOOL;
2762 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002763 }
2764 else
2765 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002766 rettv->v_type = VAR_NUMBER;
2767 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002768 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002769 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002770
2771 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002773
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002774 if (evalarg == NULL)
2775 clear_evalarg(&local_evalarg, NULL);
2776 else
2777 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 }
2779
2780 return OK;
2781}
2782
2783/*
2784 * Handle second level expression:
2785 * expr3 && expr3 && expr3 logical AND
2786 *
2787 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002788 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 *
2790 * Return OK or FAIL.
2791 */
2792 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002793eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002795 char_u *p;
2796 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797
2798 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002799 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002801 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 return FAIL;
2803
2804 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002805 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002807 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002808 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002810 evalarg_T *evalarg_used = evalarg;
2811 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002812 int orig_flags;
2813 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002814 long result = TRUE;
2815 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002816 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002817 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002818
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002819 if (evalarg == NULL)
2820 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002821 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002822 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002823 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002824 orig_flags = evalarg_used->eval_flags;
2825 evaluate = orig_flags & EVAL_EVALUATE;
2826 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002827 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002828 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002829 result = tv_get_bool_chk(rettv, &error);
2830 else if (tv_get_number_chk(rettv, &error) == 0)
2831 result = FALSE;
2832 clear_tv(rettv);
2833 if (error)
2834 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 }
2836
2837 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002838 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002840 while (p[0] == '&' && p[1] == '&')
2841 {
2842 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002843 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002844 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002845 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002846 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002847 {
2848 error_white_both(p, 2);
2849 clear_tv(rettv);
2850 return FAIL;
2851 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002852 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002853 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002854
2855 /*
2856 * Get the second variable.
2857 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002858 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002859 {
mityu4ac198c2021-05-28 17:52:40 +02002860 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002861 clear_tv(rettv);
2862 return FAIL;
2863 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002864 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2865 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002866 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002867 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002868 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002869 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002870
2871 /*
2872 * Compute the result.
2873 */
2874 if (evaluate && result)
2875 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002876 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002877 result = tv_get_bool_chk(&var2, &error);
2878 else if (tv_get_number_chk(&var2, &error) == 0)
2879 result = FALSE;
2880 clear_tv(&var2);
2881 if (error)
2882 return FAIL;
2883 }
2884 if (evaluate)
2885 {
2886 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002887 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002888 rettv->v_type = VAR_BOOL;
2889 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002890 }
2891 else
2892 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002893 rettv->v_type = VAR_NUMBER;
2894 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002895 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002896 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002897
2898 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002900
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002901 if (evalarg == NULL)
2902 clear_evalarg(&local_evalarg, NULL);
2903 else
2904 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 }
2906
2907 return OK;
2908}
2909
2910/*
2911 * Handle third level expression:
2912 * var1 == var2
2913 * var1 =~ var2
2914 * var1 != var2
2915 * var1 !~ var2
2916 * var1 > var2
2917 * var1 >= var2
2918 * var1 < var2
2919 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002920 * var1 is var2
2921 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 *
2923 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002924 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 *
2926 * Return OK or FAIL.
2927 */
2928 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002929eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002932 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002933 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002935 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936
2937 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002938 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002940 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 return FAIL;
2942
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002943 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002944
Bram Moolenaar696ba232020-07-29 21:20:41 +02002945 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946
2947 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002948 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002950 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002952 typval_T var2;
2953 int ic;
2954 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002955 int evaluate = evalarg == NULL
2956 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002957 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002958
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002959 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002960 {
Bram Moolenaare442d592022-05-05 12:20:28 +01002961 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002962 p = *arg;
2963 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002964 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2965 {
mityu4ac198c2021-05-28 17:52:40 +02002966 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002967 clear_tv(rettv);
2968 return FAIL;
2969 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002970
Bram Moolenaar696ba232020-07-29 21:20:41 +02002971 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2972 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002973 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002974 clear_tv(rettv);
2975 return FAIL;
2976 }
2977
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002978 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 if (p[len] == '?')
2980 {
2981 ic = TRUE;
2982 ++len;
2983 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002984 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 else if (p[len] == '#')
2986 {
2987 ic = FALSE;
2988 ++len;
2989 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002990 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002992 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993
2994 /*
2995 * Get the second variable.
2996 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002997 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2998 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002999 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003000 clear_tv(rettv);
3001 return FAIL;
3002 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003003 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003004 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003006 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 return FAIL;
3008 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003009 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003010 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003011 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003012
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003013 // use the line of the comparison for messages
3014 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003015 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003016 {
3017 ret = FAIL;
3018 clear_tv(rettv);
3019 }
3020 else
3021 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003022 clear_tv(&var2);
3023 return ret;
3024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 }
3026
3027 return OK;
3028}
3029
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003030/*
3031 * Make a copy of blob "tv1" and append blob "tv2".
3032 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 void
3034eval_addblob(typval_T *tv1, typval_T *tv2)
3035{
3036 blob_T *b1 = tv1->vval.v_blob;
3037 blob_T *b2 = tv2->vval.v_blob;
3038 blob_T *b = blob_alloc();
3039 int i;
3040
3041 if (b != NULL)
3042 {
3043 for (i = 0; i < blob_len(b1); i++)
3044 ga_append(&b->bv_ga, blob_get(b1, i));
3045 for (i = 0; i < blob_len(b2); i++)
3046 ga_append(&b->bv_ga, blob_get(b2, i));
3047
3048 clear_tv(tv1);
3049 rettv_blob_set(tv1, b);
3050 }
3051}
3052
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003053/*
3054 * Make a copy of list "tv1" and append list "tv2".
3055 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056 int
3057eval_addlist(typval_T *tv1, typval_T *tv2)
3058{
3059 typval_T var3;
3060
3061 // concatenate Lists
3062 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3063 {
3064 clear_tv(tv1);
3065 clear_tv(tv2);
3066 return FAIL;
3067 }
3068 clear_tv(tv1);
3069 *tv1 = var3;
3070 return OK;
3071}
3072
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003074 * Handle the bitwise left/right shift operator expression:
3075 * var1 << var2
3076 * var1 >> var2
3077 *
3078 * "arg" must point to the first non-white of the expression.
3079 * "arg" is advanced to just after the recognized expression.
3080 *
3081 * Return OK or FAIL.
3082 */
3083 static int
3084eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3085{
3086 /*
3087 * Get the first expression.
3088 */
3089 if (eval6(arg, rettv, evalarg) == FAIL)
3090 return FAIL;
3091
3092 /*
3093 * Repeat computing, until no '<<' or '>>' is following.
3094 */
3095 for (;;)
3096 {
3097 char_u *p;
3098 int getnext;
3099 exprtype_T type;
3100 int evaluate;
3101 typval_T var2;
3102 int vim9script;
3103
3104 p = eval_next_non_blank(*arg, evalarg, &getnext);
3105 if (p[0] == '<' && p[1] == '<')
3106 type = EXPR_LSHIFT;
3107 else if (p[0] == '>' && p[1] == '>')
3108 type = EXPR_RSHIFT;
3109 else
3110 return OK;
3111
3112 // Handle a bitwise left or right shift operator
3113 if (rettv->v_type != VAR_NUMBER)
3114 {
3115 // left operand should be a number
3116 emsg(_(e_bitshift_ops_must_be_number));
3117 clear_tv(rettv);
3118 return FAIL;
3119 }
3120
3121 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3122 vim9script = in_vim9script();
3123 if (getnext)
3124 {
3125 *arg = eval_next_line(*arg, evalarg);
3126 p = *arg;
3127 }
3128 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3129 {
3130 error_white_both(*arg, 2);
3131 clear_tv(rettv);
3132 return FAIL;
3133 }
3134
3135 /*
3136 * Get the second variable.
3137 */
3138 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3139 {
3140 error_white_both(p, 2);
3141 clear_tv(rettv);
3142 return FAIL;
3143 }
3144 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3145 if (eval6(arg, &var2, evalarg) == FAIL)
3146 {
3147 clear_tv(rettv);
3148 return FAIL;
3149 }
3150
3151 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3152 {
3153 // right operand should be a positive number
3154 if (var2.v_type != VAR_NUMBER)
3155 emsg(_(e_bitshift_ops_must_be_number));
3156 else
3157 emsg(_(e_bitshift_ops_must_be_postive));
3158 clear_tv(rettv);
3159 clear_tv(&var2);
3160 return FAIL;
3161 }
3162
3163 if (evaluate)
3164 {
3165 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3166 // shifting more bits than we have always results in zero
3167 rettv->vval.v_number = 0;
3168 else if (type == EXPR_LSHIFT)
3169 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003170 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003171 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003172 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003173 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003174 }
3175
3176 clear_tv(&var2);
3177 }
3178
3179 return OK;
3180}
3181
3182/*
3183 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003184 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003186 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003187 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 *
3189 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003190 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 *
3192 * Return OK or FAIL.
3193 */
3194 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003195eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003198 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003200 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 return FAIL;
3202
3203 /*
3204 * Repeat computing, until no '+', '-' or '.' is following.
3205 */
3206 for (;;)
3207 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003208 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003209 int getnext;
3210 char_u *p;
3211 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003212 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003213 int concat;
3214 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003215 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003216
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003217 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003218 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003219 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003220 p = eval_next_non_blank(*arg, evalarg, &getnext);
3221 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003222 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003223 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3224 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003226 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3227 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003228
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003229 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003230 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003231 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003232 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003233 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003234 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003235 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003236 {
mityu4ac198c2021-05-28 17:52:40 +02003237 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003238 clear_tv(rettv);
3239 return FAIL;
3240 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003241 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003242 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003243 if ((op != '+' || (rettv->v_type != VAR_LIST
3244 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003245#ifdef FEAT_FLOAT
3246 && (op == '.' || rettv->v_type != VAR_FLOAT)
3247#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003248 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003249 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003250 int error = FALSE;
3251
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003252 // For "list + ...", an illegal use of the first operand as
3253 // a number cannot be determined before evaluating the 2nd
3254 // operand: if this is also a list, all is ok.
3255 // For "something . ...", "something - ..." or "non-list + ...",
3256 // we know that the first operand needs to be a string or number
3257 // without evaluating the 2nd operand. So check before to avoid
3258 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003259 if (op != '.')
3260 tv_get_number_chk(rettv, &error);
3261 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003262 {
3263 clear_tv(rettv);
3264 return FAIL;
3265 }
3266 }
3267
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 /*
3269 * Get the second variable.
3270 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003271 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003272 {
mityu89dcb4d2021-05-28 13:50:17 +02003273 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003274 clear_tv(rettv);
3275 return FAIL;
3276 }
3277 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003278 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003280 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 return FAIL;
3282 }
3283
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003284 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 {
3286 /*
3287 * Compute the result.
3288 */
3289 if (op == '.')
3290 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003291 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3292 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003293 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003294
Bram Moolenaar2e086612020-08-25 22:37:48 +02003295 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003296 || var2.v_type == VAR_CHANNEL
3297 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003298 semsg(_(e_using_invalid_value_as_string_str),
3299 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003300#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003301 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003302 {
3303 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3304 var2.vval.v_float);
3305 s2 = buf2;
3306 }
3307#endif
3308 else
3309 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003310 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003311 {
3312 clear_tv(rettv);
3313 clear_tv(&var2);
3314 return FAIL;
3315 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003316 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003317 clear_tv(rettv);
3318 rettv->v_type = VAR_STRING;
3319 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003321 else if (op == '+' && rettv->v_type == VAR_BLOB
3322 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003323 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003324 else if (op == '+' && rettv->v_type == VAR_LIST
3325 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003326 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003328 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 else
3331 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003332 int error = FALSE;
3333 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003334#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003335 float_T f1 = 0, f2 = 0;
3336
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003337 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003338 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003339 f1 = rettv->vval.v_float;
3340 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003341 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003342 else
3343#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003344 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003345 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003346 if (error)
3347 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003348 // This can only happen for "list + non-list" or
3349 // "blob + non-blob". For "non-list + ..." or
3350 // "something - ...", we returned before evaluating the
3351 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003352 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003353 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003354 return FAIL;
3355 }
3356#ifdef FEAT_FLOAT
3357 if (var2.v_type == VAR_FLOAT)
3358 f1 = n1;
3359#endif
3360 }
3361#ifdef FEAT_FLOAT
3362 if (var2.v_type == VAR_FLOAT)
3363 {
3364 f2 = var2.vval.v_float;
3365 n2 = 0;
3366 }
3367 else
3368#endif
3369 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003370 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003371 if (error)
3372 {
3373 clear_tv(rettv);
3374 clear_tv(&var2);
3375 return FAIL;
3376 }
3377#ifdef FEAT_FLOAT
3378 if (rettv->v_type == VAR_FLOAT)
3379 f2 = n2;
3380#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003381 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003382 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003383
3384#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003385 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003386 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3387 {
3388 if (op == '+')
3389 f1 = f1 + f2;
3390 else
3391 f1 = f1 - f2;
3392 rettv->v_type = VAR_FLOAT;
3393 rettv->vval.v_float = f1;
3394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003396#endif
3397 {
3398 if (op == '+')
3399 n1 = n1 + n2;
3400 else
3401 n1 = n1 - n2;
3402 rettv->v_type = VAR_NUMBER;
3403 rettv->vval.v_number = n1;
3404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003406 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 }
3408 }
3409 return OK;
3410}
3411
3412/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003413 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 * * number multiplication
3415 * / number division
3416 * % number modulo
3417 *
3418 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003419 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 *
3421 * Return OK or FAIL.
3422 */
3423 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003424eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003425 char_u **arg,
3426 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003427 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003428 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003430#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003431 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433
3434 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003435 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003437 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 return FAIL;
3439
3440 /*
3441 * Repeat computing, until no '*', '/' or '%' is following.
3442 */
3443 for (;;)
3444 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003445 int evaluate;
3446 int getnext;
3447 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003448 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003449 int op;
3450 varnumber_T n1, n2;
3451#ifdef FEAT_FLOAT
3452 float_T f1, f2;
3453#endif
3454 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003455
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003456 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003457 p = eval_next_non_blank(*arg, evalarg, &getnext);
3458 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003459 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003461
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003462 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003463 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003464 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003465 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003466 {
3467 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3468 {
mityu4ac198c2021-05-28 17:52:40 +02003469 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003470 clear_tv(rettv);
3471 return FAIL;
3472 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003473 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003476#ifdef FEAT_FLOAT
3477 f1 = 0;
3478 f2 = 0;
3479#endif
3480 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003481 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003483#ifdef FEAT_FLOAT
3484 if (rettv->v_type == VAR_FLOAT)
3485 {
3486 f1 = rettv->vval.v_float;
3487 use_float = TRUE;
3488 n1 = 0;
3489 }
3490 else
3491#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003492 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003493 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003494 if (error)
3495 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 }
3497 else
3498 n1 = 0;
3499
3500 /*
3501 * Get the second variable.
3502 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003503 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3504 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003505 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003506 clear_tv(rettv);
3507 return FAIL;
3508 }
3509 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003510 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 return FAIL;
3512
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003513 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003515#ifdef FEAT_FLOAT
3516 if (var2.v_type == VAR_FLOAT)
3517 {
3518 if (!use_float)
3519 {
3520 f1 = n1;
3521 use_float = TRUE;
3522 }
3523 f2 = var2.vval.v_float;
3524 n2 = 0;
3525 }
3526 else
3527#endif
3528 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003529 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003530 clear_tv(&var2);
3531 if (error)
3532 return FAIL;
3533#ifdef FEAT_FLOAT
3534 if (use_float)
3535 f2 = n2;
3536#endif
3537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538
3539 /*
3540 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003541 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003543#ifdef FEAT_FLOAT
3544 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003546 if (op == '*')
3547 f1 = f1 * f2;
3548 else if (op == '/')
3549 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003550# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003551 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003552 if (f2 == 0.0)
3553 {
3554 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003555 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003556 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003557 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003558 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003559 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003560 }
3561 else
3562 f1 = f1 / f2;
3563# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003564 // We rely on the floating point library to handle divide
3565 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003566 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003567# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003570 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003571 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003572 return FAIL;
3573 }
3574 rettv->v_type = VAR_FLOAT;
3575 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 }
3577 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003578#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003580 int failed = FALSE;
3581
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003582 if (op == '*')
3583 n1 = n1 * n2;
3584 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003585 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003587 n1 = num_modulus(n1, n2, &failed);
3588 if (failed)
3589 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003590
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003591 rettv->v_type = VAR_NUMBER;
3592 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 }
3595 }
3596
3597 return OK;
3598}
3599
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003600/*
3601 * Handle a type cast before a base level expression.
3602 * "arg" must point to the first non-white of the expression.
3603 * "arg" is advanced to just after the recognized expression.
3604 * Return OK or FAIL.
3605 */
3606 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003607eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003608 char_u **arg,
3609 typval_T *rettv,
3610 evalarg_T *evalarg,
3611 int want_string) // after "." operator
3612{
3613 type_T *want_type = NULL;
3614 garray_T type_list; // list of pointers to allocated types
3615 int res;
3616 int evaluate = evalarg == NULL ? 0
3617 : (evalarg->eval_flags & EVAL_EVALUATE);
3618
3619 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003620 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3621 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003622 {
3623 ++*arg;
3624 ga_init2(&type_list, sizeof(type_T *), 10);
3625 want_type = parse_type(arg, &type_list, TRUE);
3626 if (want_type == NULL && (evaluate || **arg != '>'))
3627 {
3628 clear_type_list(&type_list);
3629 return FAIL;
3630 }
3631
3632 if (**arg != '>')
3633 {
3634 if (*skipwhite(*arg) == '>')
3635 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3636 else
3637 emsg(_(e_missing_gt));
3638 clear_type_list(&type_list);
3639 return FAIL;
3640 }
3641 ++*arg;
3642 *arg = skipwhite_and_linebreak(*arg, evalarg);
3643 }
3644
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003645 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003646
3647 if (want_type != NULL && evaluate)
3648 {
3649 if (res == OK)
3650 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003651 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3652 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003653
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003654 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003655 {
3656 if (want_type == &t_bool && actual != &t_bool
3657 && (actual->tt_flags & TTFLAG_BOOL_OK))
3658 {
3659 int n = tv2bool(rettv);
3660
3661 // can use "0" and "1" for boolean in some places
3662 clear_tv(rettv);
3663 rettv->v_type = VAR_BOOL;
3664 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3665 }
3666 else
3667 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003668 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003669
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003670 where.wt_variable = TRUE;
3671 res = check_type(want_type, actual, TRUE, where);
3672 }
3673 }
3674 }
3675 clear_type_list(&type_list);
3676 }
3677
3678 return res;
3679}
3680
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003681 int
3682eval_leader(char_u **arg, int vim9)
3683{
3684 char_u *s = *arg;
3685 char_u *p = *arg;
3686
3687 while (*p == '!' || *p == '-' || *p == '+')
3688 {
3689 char_u *n = skipwhite(p + 1);
3690
3691 // ++, --, -+ and +- are not accepted in Vim9 script
3692 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3693 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003694 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003695 return FAIL;
3696 }
3697 p = n;
3698 }
3699 *arg = p;
3700 return OK;
3701}
3702
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003704 * Check for a predefined value "true", "false" and "null.*".
3705 * Return OK when recognized.
3706 */
3707 int
3708handle_predefined(char_u *s, int len, typval_T *rettv)
3709{
3710 switch (len)
3711 {
3712 case 4: if (STRNCMP(s, "true", 4) == 0)
3713 {
3714 rettv->v_type = VAR_BOOL;
3715 rettv->vval.v_number = VVAL_TRUE;
3716 return OK;
3717 }
3718 if (STRNCMP(s, "null", 4) == 0)
3719 {
3720 rettv->v_type = VAR_SPECIAL;
3721 rettv->vval.v_number = VVAL_NULL;
3722 return OK;
3723 }
3724 break;
3725 case 5: if (STRNCMP(s, "false", 5) == 0)
3726 {
3727 rettv->v_type = VAR_BOOL;
3728 rettv->vval.v_number = VVAL_FALSE;
3729 return OK;
3730 }
3731 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003732 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3733 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003734#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003735 rettv->v_type = VAR_JOB;
3736 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003737#else
3738 rettv->v_type = VAR_SPECIAL;
3739 rettv->vval.v_number = VVAL_NULL;
3740#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003741 return OK;
3742 }
3743 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003744 case 9:
3745 if (STRNCMP(s, "null_", 5) != 0)
3746 break;
3747 if (STRNCMP(s + 5, "list", 4) == 0)
3748 {
3749 rettv->v_type = VAR_LIST;
3750 rettv->vval.v_list = NULL;
3751 return OK;
3752 }
3753 if (STRNCMP(s + 5, "dict", 4) == 0)
3754 {
3755 rettv->v_type = VAR_DICT;
3756 rettv->vval.v_dict = NULL;
3757 return OK;
3758 }
3759 if (STRNCMP(s + 5, "blob", 4) == 0)
3760 {
3761 rettv->v_type = VAR_BLOB;
3762 rettv->vval.v_blob = NULL;
3763 return OK;
3764 }
3765 break;
3766 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3767 {
3768 rettv->v_type = VAR_STRING;
3769 rettv->vval.v_string = NULL;
3770 return OK;
3771 }
3772 break;
3773 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003774 if (STRNCMP(s, "null_channel", 12) == 0)
3775 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003776#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003777 rettv->v_type = VAR_CHANNEL;
3778 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003779#else
3780 rettv->v_type = VAR_SPECIAL;
3781 rettv->vval.v_number = VVAL_NULL;
3782#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003783 return OK;
3784 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003785 if (STRNCMP(s, "null_partial", 12) == 0)
3786 {
3787 rettv->v_type = VAR_PARTIAL;
3788 rettv->vval.v_partial = NULL;
3789 return OK;
3790 }
3791 break;
3792 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3793 {
3794 rettv->v_type = VAR_FUNC;
3795 rettv->vval.v_string = NULL;
3796 return OK;
3797 }
3798 break;
3799 }
3800 return FAIL;
3801}
3802
3803/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 * Handle sixth level expression:
3805 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003806 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003807 * "string" string constant
3808 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 * &option-name option value
3810 * @r register contents
3811 * identifier variable value
3812 * function() function call
3813 * $VAR environment variable
3814 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003815 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003816 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003817 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003818 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 *
3820 * Also handle:
3821 * ! in front logical NOT
3822 * - in front unary minus
3823 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003824 * trailing [] subscript in String or List
3825 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003826 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 *
3828 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003829 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 *
3831 * Return OK or FAIL.
3832 */
3833 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003834eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003835 char_u **arg,
3836 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003837 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003840 int evaluate = evalarg != NULL
3841 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 int len;
3843 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003844 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 char_u *start_leader, *end_leader;
3846 int ret = OK;
3847 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003848 static int recurse = 0;
3849 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850
3851 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003852 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003853 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003855 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856
3857 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003858 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 */
3860 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003861 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003862 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 end_leader = *arg;
3864
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003865 if (**arg == '.' && (!isdigit(*(*arg + 1))
3866#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003867 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003868#endif
3869 ))
3870 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003871 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003872 ++*arg;
3873 return FAIL;
3874 }
3875
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003876 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003877 // and crash. With MSVC the stack is smaller.
3878 if (recurse ==
3879#ifdef _MSC_VER
3880 300
3881#else
3882 1000
3883#endif
3884 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003885 {
3886 semsg(_(e_expression_too_recursive_str), *arg);
3887 return FAIL;
3888 }
3889 ++recurse;
3890
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 switch (**arg)
3892 {
3893 /*
3894 * Number constant.
3895 */
3896 case '0':
3897 case '1':
3898 case '2':
3899 case '3':
3900 case '4':
3901 case '5':
3902 case '6':
3903 case '7':
3904 case '8':
3905 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003906 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003907
3908 // Apply prefixed "-" and "+" now. Matters especially when
3909 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003910 if (ret == OK && evaluate && end_leader > start_leader
3911 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003912 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 break;
3914
3915 /*
3916 * String constant: "string".
3917 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003918 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 break;
3920
3921 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003922 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003924 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003925 break;
3926
3927 /*
3928 * List: [expr, expr]
3929 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003930 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 break;
3932
3933 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003934 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003935 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003936 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003937 {
3938 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3939 }
3940 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003941 {
3942 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003943 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003944 }
3945 else
3946 ret = NOTDONE;
3947 break;
3948
3949 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003950 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003951 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003952 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003953 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003954 ret = NOTDONE;
3955 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00003956 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003957 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003958 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003959 break;
3960
3961 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003962 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003964 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 break;
3966
3967 /*
3968 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01003969 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 */
LemonBoy2eaef102022-05-06 13:14:50 +01003971 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
3972 ret = eval_interp_string(arg, rettv, evaluate);
3973 else
3974 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 break;
3976
3977 /*
3978 * Register contents: @r.
3979 */
3980 case '@': ++*arg;
3981 if (evaluate)
3982 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003983 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003984 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003985 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003986 emsg_invreg(**arg);
3987 else
3988 {
3989 rettv->v_type = VAR_STRING;
3990 rettv->vval.v_string = get_reg_contents(**arg,
3991 GREG_EXPR_SRC);
3992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 }
3994 if (**arg != NUL)
3995 ++*arg;
3996 break;
3997
3998 /*
3999 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004000 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004002 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004003 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004004 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004005 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004006 if (ret == OK && evaluate)
4007 {
4008 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4009
Bram Moolenaara9931532021-06-12 15:58:16 +02004010 // Compile it here to get the return type. The return
4011 // type is optional, when it's missing use t_unknown.
4012 // This is recognized in compile_return().
4013 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4014 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004015 if (compile_def_function(ufunc, FALSE,
4016 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004017 {
4018 clear_tv(rettv);
4019 ret = FAIL;
4020 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004021 }
4022 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004023 if (ret == NOTDONE)
4024 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004025 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004026 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004027
Bram Moolenaar9215f012020-06-27 21:18:00 +02004028 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004029 if (**arg == ')')
4030 ++*arg;
4031 else if (ret == OK)
4032 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004033 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004034 clear_tv(rettv);
4035 ret = FAIL;
4036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 }
4038 break;
4039
Bram Moolenaar8c711452005-01-14 21:53:12 +00004040 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 break;
4042 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004043
4044 if (ret == NOTDONE)
4045 {
4046 /*
4047 * Must be a variable or function name.
4048 * Can also be a curly-braces kind of name: {expr}.
4049 */
4050 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004051 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004052 if (alias != NULL)
4053 s = alias;
4054
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004055 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004056 ret = FAIL;
4057 else
4058 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004059 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4060
Bram Moolenaar4525a572022-02-13 11:57:33 +00004061 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004062 {
4063 emsg(_(e_cannot_use_underscore_here));
4064 ret = FAIL;
4065 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004066 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004067 && s[0] == 's' && s[1] == ':')
4068 {
4069 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4070 ret = FAIL;
4071 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004072 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004073 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004074 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004075 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004076 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004077 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02004078 else if (flags & EVAL_CONSTANT)
4079 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004080 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004081 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004082 // get the value of "true", "false", etc. or a variable
4083 ret = FAIL;
4084 if (vim9script)
4085 ret = handle_predefined(s, len, rettv);
4086 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004087 {
4088 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004089 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004090 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004091 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004092 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004093 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004094 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004095 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004096 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004097 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004098 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004099 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004100 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004101 }
4102
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004103 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4104 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004105 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004106 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107
4108 /*
4109 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4110 */
4111 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004112 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004113
4114 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004115 return ret;
4116}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004117
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004118/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004119 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004120 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004121 * Adjusts "end_leaderp" until it is at "start_leader".
4122 */
4123 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004124eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004125 typval_T *rettv,
4126 int numeric_only,
4127 char_u *start_leader,
4128 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004129{
4130 char_u *end_leader = *end_leaderp;
4131 int ret = OK;
4132 int error = FALSE;
4133 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004134 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004135 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004136#ifdef FEAT_FLOAT
4137 float_T f = 0.0;
4138
4139 if (rettv->v_type == VAR_FLOAT)
4140 f = rettv->vval.v_float;
4141 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004142#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004143 {
4144 while (VIM_ISWHITE(end_leader[-1]))
4145 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004146 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004147 val = tv2bool(rettv);
4148 else
4149 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004150 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004151 if (error)
4152 {
4153 clear_tv(rettv);
4154 ret = FAIL;
4155 }
4156 else
4157 {
4158 while (end_leader > start_leader)
4159 {
4160 --end_leader;
4161 if (*end_leader == '!')
4162 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004163 if (numeric_only)
4164 {
4165 ++end_leader;
4166 break;
4167 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004168#ifdef FEAT_FLOAT
4169 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004170 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004171 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004172 {
4173 rettv->v_type = VAR_BOOL;
4174 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4175 }
4176 else
4177 f = !f;
4178 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004179 else
4180#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004181 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004182 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004183 type = VAR_BOOL;
4184 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004185 }
4186 else if (*end_leader == '-')
4187 {
4188#ifdef FEAT_FLOAT
4189 if (rettv->v_type == VAR_FLOAT)
4190 f = -f;
4191 else
4192#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004193 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004194 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004195 type = VAR_NUMBER;
4196 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004197 }
4198 }
4199#ifdef FEAT_FLOAT
4200 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004202 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004203 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004205 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004206#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004207 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004208 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004209 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004210 rettv->v_type = type;
4211 else
4212 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004213 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004216 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 return ret;
4218}
4219
4220/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004221 * Call the function referred to in "rettv".
4222 */
4223 static int
4224call_func_rettv(
4225 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004226 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004227 typval_T *rettv,
4228 int evaluate,
4229 dict_T *selfdict,
4230 typval_T *basetv)
4231{
4232 partial_T *pt = NULL;
4233 funcexe_T funcexe;
4234 typval_T functv;
4235 char_u *s;
4236 int ret;
4237
4238 // need to copy the funcref so that we can clear rettv
4239 if (evaluate)
4240 {
4241 functv = *rettv;
4242 rettv->v_type = VAR_UNKNOWN;
4243
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004244 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004245 if (functv.v_type == VAR_PARTIAL)
4246 {
4247 pt = functv.vval.v_partial;
4248 s = partial_name(pt);
4249 }
4250 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004251 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004252 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004253 if (s == NULL || *s == NUL)
4254 {
4255 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004256 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004257 goto theend;
4258 }
4259 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004260 }
4261 else
4262 s = (char_u *)"";
4263
Bram Moolenaara80faa82020-04-12 19:37:17 +02004264 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004265 funcexe.fe_firstline = curwin->w_cursor.lnum;
4266 funcexe.fe_lastline = curwin->w_cursor.lnum;
4267 funcexe.fe_evaluate = evaluate;
4268 funcexe.fe_partial = pt;
4269 funcexe.fe_selfdict = selfdict;
4270 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004271 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004272
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004273theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004274 // Clear the funcref afterwards, so that deleting it while
4275 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004276 if (evaluate)
4277 clear_tv(&functv);
4278
4279 return ret;
4280}
4281
4282/*
4283 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004284 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004285 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4286 */
4287 static int
4288eval_lambda(
4289 char_u **arg,
4290 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004291 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004292 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004293{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004294 int evaluate = evalarg != NULL
4295 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004296 typval_T base = *rettv;
4297 int ret;
4298
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004299 rettv->v_type = VAR_UNKNOWN;
4300
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004301 if (**arg == '{')
4302 {
4303 // ->{lambda}()
4304 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4305 }
4306 else
4307 {
4308 // ->(lambda)()
4309 ++*arg;
4310 ret = eval1(arg, rettv, evalarg);
4311 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004312 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004313 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004314 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004315 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004316 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004317 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4318 && rettv->v_type != VAR_PARTIAL)
4319 {
4320 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4321 return FAIL;
4322 }
4323 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004324 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004325 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004326 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004327
4328 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004329 {
4330 if (verbose)
4331 {
4332 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004333 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004334 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004335 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004336 }
4337 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004338 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004339 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004340 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004341 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004342
4343 // Clear the funcref afterwards, so that deleting it while
4344 // evaluating the arguments is possible (see test55).
4345 if (evaluate)
4346 clear_tv(&base);
4347
4348 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004349}
4350
4351/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004352 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004353 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004354 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4355 */
4356 static int
4357eval_method(
4358 char_u **arg,
4359 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004360 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004361 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004362{
4363 char_u *name;
4364 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004365 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004366 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004367 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004368 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004369 int evaluate = evalarg != NULL
4370 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004371
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004372 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004373
Bram Moolenaarac92e252019-08-03 21:58:38 +02004374 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004375 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004376 if (alias != NULL)
4377 name = alias;
4378
4379 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004380 {
4381 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004382 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004383 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004384 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004385 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004386 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004387 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004388
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004389 // If there is no "(" immediately following, but there is further on,
4390 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4391 // Does not handle anything where "(" is part of the expression.
4392 *arg = skipwhite(*arg);
4393
4394 if (**arg != '(' && alias == NULL
4395 && (paren = vim_strchr(*arg, '(')) != NULL)
4396 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004397 char_u *deref;
4398
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004399 *arg = name;
4400 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004401 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4402 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004403 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004404 *arg = name + len;
4405 ret = FAIL;
4406 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004407 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004408 {
4409 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004410 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004411 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004412 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004413 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004414
4415 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004416 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004417 *arg = skipwhite(*arg);
4418
4419 if (**arg != '(')
4420 {
4421 if (verbose)
4422 semsg(_(e_missing_parenthesis_str), name);
4423 ret = FAIL;
4424 }
4425 else if (VIM_ISWHITE((*arg)[-1]))
4426 {
4427 if (verbose)
4428 emsg(_(e_no_white_space_allowed_before_parenthesis));
4429 ret = FAIL;
4430 }
4431 else
4432 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004433 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004434 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004435 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004436
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004437 // Clear the funcref afterwards, so that deleting it while
4438 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004439 if (evaluate)
4440 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004441 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004442
Bram Moolenaarac92e252019-08-03 21:58:38 +02004443 return ret;
4444}
4445
4446/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004447 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4448 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004449 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4450 */
4451 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004452eval_index(
4453 char_u **arg,
4454 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004455 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004456 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004457{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004458 int evaluate = evalarg != NULL
4459 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004460 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004461 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004462 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004463 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004464 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004465 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004466
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004467 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4468 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004469
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004470 init_tv(&var1);
4471 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004472 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004473 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004474 /*
4475 * dict.name
4476 */
4477 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004478 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004479 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004480 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004481 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004482 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004483 }
4484 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004485 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004486 /*
4487 * something[idx]
4488 *
4489 * Get the (first) variable from inside the [].
4490 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004491 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004492 if (**arg == ':')
4493 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004494 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004495 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004496 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004497 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004498 semsg(_(e_white_space_required_before_and_after_str_at_str),
4499 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004500 clear_tv(&var1);
4501 return FAIL;
4502 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004503 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004504 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004505 int error = FALSE;
4506
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004507#ifdef FEAT_FLOAT
4508 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004509 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004510 && var1.v_type == VAR_FLOAT)
4511 {
4512 var1.vval.v_string = typval_tostring(&var1, TRUE);
4513 var1.v_type = VAR_STRING;
4514 }
4515#endif
Bram Moolenaar4525a572022-02-13 11:57:33 +00004516 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004517 tv_get_number_chk(&var1, &error);
4518 else
4519 error = tv_get_string_chk(&var1) == NULL;
4520 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004521 {
4522 // not a number or string
4523 clear_tv(&var1);
4524 return FAIL;
4525 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004526 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004527
4528 /*
4529 * Get the second variable from inside the [:].
4530 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004531 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004532 if (**arg == ':')
4533 {
4534 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004535 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004536 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004537 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004538 semsg(_(e_white_space_required_before_and_after_str_at_str),
4539 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004540 if (!empty1)
4541 clear_tv(&var1);
4542 return FAIL;
4543 }
4544 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004545 if (**arg == ']')
4546 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004547 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004548 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004549 if (!empty1)
4550 clear_tv(&var1);
4551 return FAIL;
4552 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004553 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004554 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004555 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004556 if (!empty1)
4557 clear_tv(&var1);
4558 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004559 return FAIL;
4560 }
4561 }
4562
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004563 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004564 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004565 if (**arg != ']')
4566 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004567 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004568 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004569 clear_tv(&var1);
4570 if (range)
4571 clear_tv(&var2);
4572 return FAIL;
4573 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004574 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004575 }
4576
4577 if (evaluate)
4578 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004579 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004580 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004581 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004582
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004583 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004584 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004585 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004586 clear_tv(&var2);
4587 return res;
4588 }
4589 return OK;
4590}
4591
4592/*
4593 * Check if "rettv" can have an [index] or [sli:ce]
4594 */
4595 int
4596check_can_index(typval_T *rettv, int evaluate, int verbose)
4597{
4598 switch (rettv->v_type)
4599 {
4600 case VAR_FUNC:
4601 case VAR_PARTIAL:
4602 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004603 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004604 return FAIL;
4605 case VAR_FLOAT:
4606#ifdef FEAT_FLOAT
4607 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004608 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004609 return FAIL;
4610#endif
4611 case VAR_BOOL:
4612 case VAR_SPECIAL:
4613 case VAR_JOB:
4614 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004615 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004616 if (verbose)
4617 emsg(_(e_cannot_index_special_variable));
4618 return FAIL;
4619 case VAR_UNKNOWN:
4620 case VAR_ANY:
4621 case VAR_VOID:
4622 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004623 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004624 emsg(_(e_cannot_index_special_variable));
4625 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004626 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004627 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004628
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004629 case VAR_STRING:
4630 case VAR_LIST:
4631 case VAR_DICT:
4632 case VAR_BLOB:
4633 break;
4634 case VAR_NUMBER:
4635 if (in_vim9script())
4636 emsg(_(e_cannot_index_number));
4637 break;
4638 }
4639 return OK;
4640}
4641
4642/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004643 * slice() function
4644 */
4645 void
4646f_slice(typval_T *argvars, typval_T *rettv)
4647{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004648 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004649 && ((argvars[0].v_type != VAR_STRING
4650 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004651 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004652 && check_for_list_arg(argvars, 0) == FAIL)
4653 || check_for_number_arg(argvars, 1) == FAIL
4654 || check_for_opt_number_arg(argvars, 2) == FAIL))
4655 return;
4656
Bram Moolenaar6601b622021-01-13 21:47:15 +01004657 if (check_can_index(argvars, TRUE, FALSE) == OK)
4658 {
4659 copy_tv(argvars, rettv);
4660 eval_index_inner(rettv, TRUE, argvars + 1,
4661 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4662 TRUE, NULL, 0, FALSE);
4663 }
4664}
4665
4666/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004667 * Apply index or range to "rettv".
4668 * "var1" is the first index, NULL for [:expr].
4669 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004670 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4671 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004672 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4673 */
4674 int
4675eval_index_inner(
4676 typval_T *rettv,
4677 int is_range,
4678 typval_T *var1,
4679 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004680 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004681 char_u *key,
4682 int keylen,
4683 int verbose)
4684{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004685 varnumber_T n1, n2 = 0;
4686 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004687
4688 n1 = 0;
4689 if (var1 != NULL && rettv->v_type != VAR_DICT)
4690 n1 = tv_get_number(var1);
4691
4692 if (is_range)
4693 {
4694 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004695 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004696 if (verbose)
4697 emsg(_(e_cannot_slice_dictionary));
4698 return FAIL;
4699 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004700 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004701 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004702 else
4703 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004704 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004705
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004706 switch (rettv->v_type)
4707 {
4708 case VAR_UNKNOWN:
4709 case VAR_ANY:
4710 case VAR_VOID:
4711 case VAR_FUNC:
4712 case VAR_PARTIAL:
4713 case VAR_FLOAT:
4714 case VAR_BOOL:
4715 case VAR_SPECIAL:
4716 case VAR_JOB:
4717 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004718 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004719 break; // not evaluating, skipping over subscript
4720
4721 case VAR_NUMBER:
4722 case VAR_STRING:
4723 {
4724 char_u *s = tv_get_string(rettv);
4725
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004726 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004727 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004728 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004729 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004730 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004731 else
4732 s = char_from_string(s, n1);
4733 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004734 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004735 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004736 // The resulting variable is a substring. If the indexes
4737 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004738 if (n1 < 0)
4739 {
4740 n1 = len + n1;
4741 if (n1 < 0)
4742 n1 = 0;
4743 }
4744 if (n2 < 0)
4745 n2 = len + n2;
4746 else if (n2 >= len)
4747 n2 = len;
4748 if (n1 >= len || n2 < 0 || n1 > n2)
4749 s = NULL;
4750 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004751 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004752 }
4753 else
4754 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004755 // The resulting variable is a string of a single
4756 // character. If the index is too big or negative the
4757 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004758 if (n1 >= len || n1 < 0)
4759 s = NULL;
4760 else
4761 s = vim_strnsave(s + n1, 1);
4762 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004763 clear_tv(rettv);
4764 rettv->v_type = VAR_STRING;
4765 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004766 }
4767 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004768
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004769 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004770 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4771 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004772 break;
4773
4774 case VAR_LIST:
4775 if (var1 == NULL)
4776 n1 = 0;
4777 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004778 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004779 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004780 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004781 return FAIL;
4782 break;
4783
4784 case VAR_DICT:
4785 {
4786 dictitem_T *item;
4787 typval_T tmp;
4788
4789 if (key == NULL)
4790 {
4791 key = tv_get_string_chk(var1);
4792 if (key == NULL)
4793 return FAIL;
4794 }
4795
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004796 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004797
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004798 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004799 {
4800 if (verbose)
4801 {
4802 if (keylen > 0)
4803 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004804 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004805 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004806 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004807 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004808
4809 copy_tv(&item->di_tv, &tmp);
4810 clear_tv(rettv);
4811 *rettv = tmp;
4812 }
4813 break;
4814 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004815 return OK;
4816}
4817
4818/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004819 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004820 */
4821 char_u *
4822partial_name(partial_T *pt)
4823{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004824 if (pt != NULL)
4825 {
4826 if (pt->pt_name != NULL)
4827 return pt->pt_name;
4828 if (pt->pt_func != NULL)
4829 return pt->pt_func->uf_name;
4830 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004831 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004832}
4833
Bram Moolenaarddecc252016-04-06 22:59:37 +02004834 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004835partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004836{
4837 int i;
4838
4839 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004840 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004841 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004842 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004843 if (pt->pt_name != NULL)
4844 {
4845 func_unref(pt->pt_name);
4846 vim_free(pt->pt_name);
4847 }
4848 else
4849 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004850
Bram Moolenaar54656012021-06-09 20:50:46 +02004851 // "out_up" is no longer used, decrement refcount on partial that owns it.
4852 partial_unref(pt->pt_outer.out_up_partial);
4853
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004854 // Using pt_outer from another partial.
4855 partial_unref(pt->pt_outer_partial);
4856
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004857 // Decrease the reference count for the context of a closure. If down
4858 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004859 if (pt->pt_funcstack != NULL)
4860 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004861 --pt->pt_funcstack->fs_refcount;
4862 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004863 }
4864
Bram Moolenaarddecc252016-04-06 22:59:37 +02004865 vim_free(pt);
4866}
4867
4868/*
4869 * Unreference a closure: decrement the reference count and free it when it
4870 * becomes zero.
4871 */
4872 void
4873partial_unref(partial_T *pt)
4874{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004875 if (pt != NULL)
4876 {
4877 if (--pt->pt_refcount <= 0)
4878 partial_free(pt);
4879
4880 // If the reference count goes down to one, the funcstack may be the
4881 // only reference and can be freed if no other partials reference it.
4882 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4883 funcstack_check_refcount(pt->pt_funcstack);
4884 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004885}
4886
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004887/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004888 * Return the next (unique) copy ID.
4889 * Used for serializing nested structures.
4890 */
4891 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004892get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004893{
4894 current_copyID += COPYID_INC;
4895 return current_copyID;
4896}
4897
4898/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004899 * Garbage collection for lists and dictionaries.
4900 *
4901 * We use reference counts to be able to free most items right away when they
4902 * are no longer used. But for composite items it's possible that it becomes
4903 * unused while the reference count is > 0: When there is a recursive
4904 * reference. Example:
4905 * :let l = [1, 2, 3]
4906 * :let d = {9: l}
4907 * :let l[1] = d
4908 *
4909 * Since this is quite unusual we handle this with garbage collection: every
4910 * once in a while find out which lists and dicts are not referenced from any
4911 * variable.
4912 *
4913 * Here is a good reference text about garbage collection (refers to Python
4914 * but it applies to all reference-counting mechanisms):
4915 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004916 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004917
4918/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004919 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004920 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004921 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004922 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004923 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004924garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004925{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004926 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004927 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004928 buf_T *buf;
4929 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004930 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004931 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004932
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004933 if (!testing)
4934 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004935 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004936 want_garbage_collect = FALSE;
4937 may_garbage_collect = FALSE;
4938 garbage_collect_at_exit = FALSE;
4939 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004940
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004941 // The execution stack can grow big, limit the size.
4942 if (exestack.ga_maxlen - exestack.ga_len > 500)
4943 {
4944 size_t new_len;
4945 char_u *pp;
4946 int n;
4947
4948 // Keep 150% of the current size, with a minimum of the growth size.
4949 n = exestack.ga_len / 2;
4950 if (n < exestack.ga_growsize)
4951 n = exestack.ga_growsize;
4952
4953 // Don't make it bigger though.
4954 if (exestack.ga_len + n < exestack.ga_maxlen)
4955 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004956 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004957 pp = vim_realloc(exestack.ga_data, new_len);
4958 if (pp == NULL)
4959 return FAIL;
4960 exestack.ga_maxlen = exestack.ga_len + n;
4961 exestack.ga_data = pp;
4962 }
4963 }
4964
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004965 // We advance by two because we add one for items referenced through
4966 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004967 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004968
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004969 /*
4970 * 1. Go through all accessible variables and mark all lists and dicts
4971 * with copyID.
4972 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004973
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004974 // Don't free variables in the previous_funccal list unless they are only
4975 // referenced through previous_funccal. This must be first, because if
4976 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004977 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004978
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004979 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004980 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004981
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004982 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004983 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004984 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4985 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004986
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004987 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004988 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004989 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4990 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004991 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004992 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4993 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004994#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004995 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004996 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4997 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004998 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004999 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005000 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5001 NULL, NULL);
5002#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005003
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005004 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005005 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005006 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5007 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005008 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005009 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005010
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005011 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005012 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005013
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005014 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005015 abort = abort || set_ref_in_functions(copyID);
5016
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005017 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005018 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005019
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005020 // funcstacks keep variables for closures
5021 abort = abort || set_ref_in_funcstacks(copyID);
5022
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005023 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005024 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005025
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005026 // callbacks in buffers
5027 abort = abort || set_ref_in_buffers(copyID);
5028
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005029 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5030 abort = abort || set_ref_in_insexpand_funcs(copyID);
5031
5032 // 'operatorfunc' callback
5033 abort = abort || set_ref_in_opfunc(copyID);
5034
5035 // 'tagfunc' callback
5036 abort = abort || set_ref_in_tagfunc(copyID);
5037
5038 // 'imactivatefunc' and 'imstatusfunc' callbacks
5039 abort = abort || set_ref_in_im_funcs(copyID);
5040
Bram Moolenaar1dced572012-04-05 16:54:08 +02005041#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005042 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005043#endif
5044
Bram Moolenaardb913952012-06-29 12:54:53 +02005045#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005046 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005047#endif
5048
5049#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005050 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005051#endif
5052
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005053#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005054 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005055 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005056#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005057#ifdef FEAT_NETBEANS_INTG
5058 abort = abort || set_ref_in_nb_channel(copyID);
5059#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005060
Bram Moolenaare3188e22016-05-31 21:13:04 +02005061#ifdef FEAT_TIMERS
5062 abort = abort || set_ref_in_timer(copyID);
5063#endif
5064
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005065#ifdef FEAT_QUICKFIX
5066 abort = abort || set_ref_in_quickfix(copyID);
5067#endif
5068
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005069#ifdef FEAT_TERMINAL
5070 abort = abort || set_ref_in_term(copyID);
5071#endif
5072
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005073#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005074 abort = abort || set_ref_in_popups(copyID);
5075#endif
5076
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005077 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005078 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005079 /*
5080 * 2. Free lists and dictionaries that are not referenced.
5081 */
5082 did_free = free_unref_items(copyID);
5083
5084 /*
5085 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005086 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005087 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005088 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005089 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005090 else if (p_verbose > 0)
5091 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005092 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005093 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005094
5095 return did_free;
5096}
5097
5098/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005099 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005100 */
5101 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005102free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005103{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005104 int did_free = FALSE;
5105
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005106 // Let all "free" functions know that we are here. This means no
5107 // dictionaries, lists, channels or jobs are to be freed, because we will
5108 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005109 in_free_unref_items = TRUE;
5110
5111 /*
5112 * PASS 1: free the contents of the items. We don't free the items
5113 * themselves yet, so that it is possible to decrement refcount counters
5114 */
5115
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005116 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005117 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005118
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005119 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005120 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005121
5122#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005123 // Go through the list of jobs and free items without the copyID. This
5124 // must happen before doing channels, because jobs refer to channels, but
5125 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005126 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5127
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005128 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005129 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5130#endif
5131
5132 /*
5133 * PASS 2: free the items themselves.
5134 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005135 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005136 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005137
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005138#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005139 // Go through the list of jobs and free items without the copyID. This
5140 // must happen before doing channels, because jobs refer to channels, but
5141 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005142 free_unused_jobs(copyID, COPYID_MASK);
5143
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005144 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005145 free_unused_channels(copyID, COPYID_MASK);
5146#endif
5147
5148 in_free_unref_items = FALSE;
5149
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005150 return did_free;
5151}
5152
5153/*
5154 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005155 * "list_stack" is used to add lists to be marked. Can be NULL.
5156 *
5157 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005158 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005159 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005160set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005161{
5162 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005163 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005164 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005165 hashtab_T *cur_ht;
5166 ht_stack_T *ht_stack = NULL;
5167 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005168
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005169 cur_ht = ht;
5170 for (;;)
5171 {
5172 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005173 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005174 // Mark each item in the hashtab. If the item contains a hashtab
5175 // it is added to ht_stack, if it contains a list it is added to
5176 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005177 todo = (int)cur_ht->ht_used;
5178 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5179 if (!HASHITEM_EMPTY(hi))
5180 {
5181 --todo;
5182 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5183 &ht_stack, list_stack);
5184 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005185 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005186
5187 if (ht_stack == NULL)
5188 break;
5189
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005190 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005191 cur_ht = ht_stack->ht;
5192 tempitem = ht_stack;
5193 ht_stack = ht_stack->prev;
5194 free(tempitem);
5195 }
5196
5197 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005198}
5199
Dominique Pelle748b3082022-01-08 12:41:16 +00005200#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5201 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005202/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005203 * Mark a dict and its items with "copyID".
5204 * Returns TRUE if setting references failed somehow.
5205 */
5206 int
5207set_ref_in_dict(dict_T *d, int copyID)
5208{
5209 if (d != NULL && d->dv_copyID != copyID)
5210 {
5211 d->dv_copyID = copyID;
5212 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5213 }
5214 return FALSE;
5215}
Dominique Pelle748b3082022-01-08 12:41:16 +00005216#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005217
5218/*
5219 * Mark a list and its items with "copyID".
5220 * Returns TRUE if setting references failed somehow.
5221 */
5222 int
5223set_ref_in_list(list_T *ll, int copyID)
5224{
5225 if (ll != NULL && ll->lv_copyID != copyID)
5226 {
5227 ll->lv_copyID = copyID;
5228 return set_ref_in_list_items(ll, copyID, NULL);
5229 }
5230 return FALSE;
5231}
5232
5233/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005234 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005235 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5236 *
5237 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005238 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005239 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005240set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005241{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005242 listitem_T *li;
5243 int abort = FALSE;
5244 list_T *cur_l;
5245 list_stack_T *list_stack = NULL;
5246 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005247
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005248 cur_l = l;
5249 for (;;)
5250 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005251 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005252 // Mark each item in the list. If the item contains a hashtab
5253 // it is added to ht_stack, if it contains a list it is added to
5254 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005255 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5256 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5257 ht_stack, &list_stack);
5258 if (list_stack == NULL)
5259 break;
5260
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005261 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005262 cur_l = list_stack->list;
5263 tempitem = list_stack;
5264 list_stack = list_stack->prev;
5265 free(tempitem);
5266 }
5267
5268 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005269}
5270
5271/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005272 * Mark the partial in callback 'cb' with "copyID".
5273 */
5274 int
5275set_ref_in_callback(callback_T *cb, int copyID)
5276{
5277 typval_T tv;
5278
5279 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5280 return FALSE;
5281
5282 tv.v_type = VAR_PARTIAL;
5283 tv.vval.v_partial = cb->cb_partial;
5284 return set_ref_in_item(&tv, copyID, NULL, NULL);
5285}
5286
5287/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005288 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005289 * "list_stack" is used to add lists to be marked. Can be NULL.
5290 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5291 *
5292 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005293 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005294 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005295set_ref_in_item(
5296 typval_T *tv,
5297 int copyID,
5298 ht_stack_T **ht_stack,
5299 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005300{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005301 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005302
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005303 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005304 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005305 dict_T *dd = tv->vval.v_dict;
5306
Bram Moolenaara03f2332016-02-06 18:09:59 +01005307 if (dd != NULL && dd->dv_copyID != copyID)
5308 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005309 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005310 dd->dv_copyID = copyID;
5311 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005312 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005313 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5314 }
5315 else
5316 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005317 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5318
Bram Moolenaara03f2332016-02-06 18:09:59 +01005319 if (newitem == NULL)
5320 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005321 else
5322 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005323 newitem->ht = &dd->dv_hashtab;
5324 newitem->prev = *ht_stack;
5325 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005326 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005327 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005328 }
5329 }
5330 else if (tv->v_type == VAR_LIST)
5331 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005332 list_T *ll = tv->vval.v_list;
5333
Bram Moolenaara03f2332016-02-06 18:09:59 +01005334 if (ll != NULL && ll->lv_copyID != copyID)
5335 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005336 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005337 ll->lv_copyID = copyID;
5338 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005339 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005340 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005341 }
5342 else
5343 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005344 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5345
Bram Moolenaara03f2332016-02-06 18:09:59 +01005346 if (newitem == NULL)
5347 abort = TRUE;
5348 else
5349 {
5350 newitem->list = ll;
5351 newitem->prev = *list_stack;
5352 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005353 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005354 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005355 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005356 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005357 else if (tv->v_type == VAR_FUNC)
5358 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005359 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005360 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005361 else if (tv->v_type == VAR_PARTIAL)
5362 {
5363 partial_T *pt = tv->vval.v_partial;
5364 int i;
5365
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005366 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005367 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005368 // Didn't see this partial yet.
5369 pt->pt_copyID = copyID;
5370
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005371 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005372
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005373 if (pt->pt_dict != NULL)
5374 {
5375 typval_T dtv;
5376
5377 dtv.v_type = VAR_DICT;
5378 dtv.vval.v_dict = pt->pt_dict;
5379 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5380 }
5381
5382 for (i = 0; i < pt->pt_argc; ++i)
5383 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5384 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005385 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005386 }
5387 }
5388#ifdef FEAT_JOB_CHANNEL
5389 else if (tv->v_type == VAR_JOB)
5390 {
5391 job_T *job = tv->vval.v_job;
5392 typval_T dtv;
5393
5394 if (job != NULL && job->jv_copyID != copyID)
5395 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005396 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005397 if (job->jv_channel != NULL)
5398 {
5399 dtv.v_type = VAR_CHANNEL;
5400 dtv.vval.v_channel = job->jv_channel;
5401 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5402 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005403 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005404 {
5405 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005406 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005407 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5408 }
5409 }
5410 }
5411 else if (tv->v_type == VAR_CHANNEL)
5412 {
5413 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005414 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005415 typval_T dtv;
5416 jsonq_T *jq;
5417 cbq_T *cq;
5418
5419 if (ch != NULL && ch->ch_copyID != copyID)
5420 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005421 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005422 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005423 {
5424 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5425 jq = jq->jq_next)
5426 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5427 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5428 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005429 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005430 {
5431 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005432 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005433 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5434 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005435 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005436 {
5437 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005438 dtv.vval.v_partial =
5439 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005440 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5441 }
5442 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005443 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005444 {
5445 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005446 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005447 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5448 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005449 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005450 {
5451 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005452 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005453 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5454 }
5455 }
5456 }
5457#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005458 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005459}
5460
Bram Moolenaar8c711452005-01-14 21:53:12 +00005461/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005462 * Return a string with the string representation of a variable.
5463 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005464 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005465 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005466 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005467 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005468 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005469 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5470 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005471 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005472 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005473 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005474echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005475 typval_T *tv,
5476 char_u **tofree,
5477 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005478 int copyID,
5479 int echo_style,
5480 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005481 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005482{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005483 static int recurse = 0;
5484 char_u *r = NULL;
5485
Bram Moolenaar33570922005-01-25 22:26:29 +00005486 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005487 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005488 if (!did_echo_string_emsg)
5489 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005490 // Only give this message once for a recursive call to avoid
5491 // flooding the user with errors. And stop iterating over lists
5492 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005493 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005494 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005495 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005496 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005497 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005498 }
5499 ++recurse;
5500
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005501 switch (tv->v_type)
5502 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005503 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005504 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005505 {
5506 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005507 r = tv->vval.v_string;
5508 if (r == NULL)
5509 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005510 }
5511 else
5512 {
5513 *tofree = string_quote(tv->vval.v_string, FALSE);
5514 r = *tofree;
5515 }
5516 break;
5517
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005518 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005519 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005520 char_u buf[MAX_FUNC_NAME_LEN];
5521
5522 if (echo_style)
5523 {
LemonBoya5d35902022-04-29 21:15:02 +01005524 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5525 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005526 buf, MAX_FUNC_NAME_LEN);
5527 if (r == buf)
5528 {
5529 r = vim_strsave(buf);
5530 *tofree = r;
5531 }
5532 else
5533 *tofree = NULL;
5534 }
5535 else
5536 {
5537 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5538 : make_ufunc_name_readable(
5539 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5540 TRUE);
5541 r = *tofree;
5542 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005543 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005544 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005545
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005546 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005547 {
5548 partial_T *pt = tv->vval.v_partial;
5549 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005550 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005551 garray_T ga;
5552 int i;
5553 char_u *tf;
5554
5555 ga_init2(&ga, 1, 100);
5556 ga_concat(&ga, (char_u *)"function(");
5557 if (fname != NULL)
5558 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005559 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005560 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005561 && vim_isupper(fname[1]))
5562 {
5563 ga_concat(&ga, (char_u *)"'g:");
5564 ga_concat(&ga, fname + 1);
5565 }
5566 else
5567 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005568 vim_free(fname);
5569 }
5570 if (pt != NULL && pt->pt_argc > 0)
5571 {
5572 ga_concat(&ga, (char_u *)", [");
5573 for (i = 0; i < pt->pt_argc; ++i)
5574 {
5575 if (i > 0)
5576 ga_concat(&ga, (char_u *)", ");
5577 ga_concat(&ga,
5578 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5579 vim_free(tf);
5580 }
5581 ga_concat(&ga, (char_u *)"]");
5582 }
5583 if (pt != NULL && pt->pt_dict != NULL)
5584 {
5585 typval_T dtv;
5586
5587 ga_concat(&ga, (char_u *)", ");
5588 dtv.v_type = VAR_DICT;
5589 dtv.vval.v_dict = pt->pt_dict;
5590 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5591 vim_free(tf);
5592 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005593 // terminate with ')' and a NUL
5594 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005595
5596 *tofree = ga.ga_data;
5597 r = *tofree;
5598 break;
5599 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005600
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005601 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005602 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005603 break;
5604
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005605 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005606 if (tv->vval.v_list == NULL)
5607 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005608 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005609 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005610 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005611 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005612 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5613 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005614 {
5615 *tofree = NULL;
5616 r = (char_u *)"[...]";
5617 }
5618 else
5619 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005620 int old_copyID = tv->vval.v_list->lv_copyID;
5621
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005622 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005623 *tofree = list2string(tv, copyID, restore_copyID);
5624 if (restore_copyID)
5625 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005626 r = *tofree;
5627 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005628 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005629
Bram Moolenaar8c711452005-01-14 21:53:12 +00005630 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005631 if (tv->vval.v_dict == NULL)
5632 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005633 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005634 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005635 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005636 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005637 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5638 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005639 {
5640 *tofree = NULL;
5641 r = (char_u *)"{...}";
5642 }
5643 else
5644 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005645 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005646
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005647 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005648 *tofree = dict2string(tv, copyID, restore_copyID);
5649 if (restore_copyID)
5650 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005651 r = *tofree;
5652 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005653 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005654
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005655 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005656 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005657 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005658 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005659 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005660 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005661 break;
5662
Bram Moolenaar835dc632016-02-07 14:27:38 +01005663 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005664 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005665#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005666 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005667 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5668 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005669 if (composite_val)
5670 {
5671 *tofree = string_quote(r, FALSE);
5672 r = *tofree;
5673 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005674#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005675 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005676
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005677 case VAR_INSTR:
5678 *tofree = NULL;
5679 r = (char_u *)"instructions";
5680 break;
5681
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005682 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005683#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005684 *tofree = NULL;
5685 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5686 r = numbuf;
5687 break;
5688#endif
5689
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005690 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005691 case VAR_SPECIAL:
5692 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005693 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005694 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005695 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005696
Bram Moolenaar8502c702014-06-17 12:51:16 +02005697 if (--recurse == 0)
5698 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005699 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005700}
5701
5702/*
5703 * Return a string with the string representation of a variable.
5704 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5705 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005706 * Does not put quotes around strings, as ":echo" displays values.
5707 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5708 * May return NULL.
5709 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005710 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005711echo_string(
5712 typval_T *tv,
5713 char_u **tofree,
5714 char_u *numbuf,
5715 int copyID)
5716{
5717 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5718}
5719
5720/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005721 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5722 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005723 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005724 */
5725 int
5726buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5727{
5728 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005729 char_u *t;
5730 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005731
5732 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5733 return -1;
5734
5735 if (lnum > buf->b_ml.ml_line_count)
5736 lnum = buf->b_ml.ml_line_count;
5737
5738 str = ml_get_buf(buf, lnum, FALSE);
5739 if (str == NULL)
5740 return -1;
5741
5742 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005743 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005744
Bram Moolenaar91458462021-01-13 20:08:38 +01005745 // count the number of characters
5746 t = str;
5747 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5748 t += mb_ptr2len(t);
5749
5750 // In insert mode, when the cursor is at the end of a non-empty line,
5751 // byteidx points to the NUL character immediately past the end of the
5752 // string. In this case, add one to the character count.
5753 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5754 count++;
5755
5756 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005757}
5758
5759/*
5760 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005761 * byte index. Works only for loaded buffers. Returns -1 on failure.
5762 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005763 */
5764 int
5765buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5766{
5767 char_u *str;
5768 char_u *t;
5769
5770 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5771 return -1;
5772
5773 if (lnum > buf->b_ml.ml_line_count)
5774 lnum = buf->b_ml.ml_line_count;
5775
5776 str = ml_get_buf(buf, lnum, FALSE);
5777 if (str == NULL)
5778 return -1;
5779
5780 // Convert the character offset to a byte offset
5781 t = str;
5782 while (*t != NUL && --charidx > 0)
5783 t += mb_ptr2len(t);
5784
Bram Moolenaar91458462021-01-13 20:08:38 +01005785 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005786}
5787
5788/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005790 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005792 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005793var2fpos(
5794 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005795 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005796 int *fnum, // set to fnum for '0, 'A, etc.
5797 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005799 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005801 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005803 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005804 if (varp->v_type == VAR_LIST)
5805 {
5806 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005807 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005808 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005809 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005810
5811 l = varp->vval.v_list;
5812 if (l == NULL)
5813 return NULL;
5814
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005815 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005816 pos.lnum = list_find_nr(l, 0L, &error);
5817 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005818 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005819 if (charcol)
5820 len = (long)mb_charlen(ml_get(pos.lnum));
5821 else
5822 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005823
Bram Moolenaarec65d772020-08-20 22:29:12 +02005824 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005825 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005826 li = list_find(l, 1L);
5827 if (li != NULL && li->li_tv.v_type == VAR_STRING
5828 && li->li_tv.vval.v_string != NULL
5829 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005830 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005831 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005832 }
5833 else
5834 {
5835 pos.col = list_find_nr(l, 1L, &error);
5836 if (error)
5837 return NULL;
5838 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005839
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005840 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005841 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005842 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005843 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005844
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005845 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005846 pos.coladd = list_find_nr(l, 2L, &error);
5847 if (error)
5848 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005849
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005850 return &pos;
5851 }
5852
Bram Moolenaarc5809432021-03-27 21:23:30 +01005853 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5854 return NULL;
5855
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005856 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005857 if (name == NULL)
5858 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005859
5860 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005861 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005862 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005863 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005864 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005865 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005866 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005867 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005868 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005869 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005870 pos = VIsual;
5871 else
5872 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005873 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005874 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005875 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005877 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005878 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5880 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005881 pos = *pp;
5882 }
5883 if (pos.lnum != 0)
5884 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005885 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005886 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5887 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005889
Bram Moolenaara5525202006-03-02 22:52:09 +00005890 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005891
Bram Moolenaar477933c2007-07-17 14:32:23 +00005892 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005893 {
5894 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005895 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005896 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005897 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005898 // In silent Ex mode topline is zero, but that's not a valid line
5899 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005900 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005901 return &pos;
5902 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005903 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005904 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005905 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005906 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005907 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005908 return &pos;
5909 }
5910 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005911 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005913 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 {
5915 pos.lnum = curbuf->b_ml.ml_line_count;
5916 pos.col = 0;
5917 }
5918 else
5919 {
5920 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005921 if (charcol)
5922 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5923 else
5924 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 }
5926 return &pos;
5927 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005928 if (in_vim9script())
5929 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 return NULL;
5931}
5932
5933/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005934 * Convert list in "arg" into a position and optional file number.
5935 * When "fnump" is NULL there is no file number, only 3 items.
5936 * Note that the column is passed on as-is, the caller may want to decrement
5937 * it to use 1 for the first column.
5938 * Return FAIL when conversion is not possible, doesn't check the position for
5939 * validity.
5940 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005941 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005942list2fpos(
5943 typval_T *arg,
5944 pos_T *posp,
5945 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005946 colnr_T *curswantp,
5947 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005948{
5949 list_T *l = arg->vval.v_list;
5950 long i = 0;
5951 long n;
5952
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005953 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5954 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005955 if (arg->v_type != VAR_LIST
5956 || l == NULL
5957 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005958 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005959 return FAIL;
5960
5961 if (fnump != NULL)
5962 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005963 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005964 if (n < 0)
5965 return FAIL;
5966 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005967 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005968 *fnump = n;
5969 }
5970
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005971 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005972 if (n < 0)
5973 return FAIL;
5974 posp->lnum = n;
5975
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005976 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005977 if (n < 0)
5978 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005979 // If character position is specified, then convert to byte position
5980 if (charcol)
5981 {
5982 buf_T *buf;
5983
5984 // Get the text for the specified line in a loaded buffer
5985 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5986 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5987 return FAIL;
5988
Bram Moolenaar91458462021-01-13 20:08:38 +01005989 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005990 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005991 posp->col = n;
5992
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005993 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005994 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005995 posp->coladd = 0;
5996 else
5997 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005998
Bram Moolenaar493c1782014-05-28 14:34:46 +02005999 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006000 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006001
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006002 return OK;
6003}
6004
6005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 * Get the length of an environment variable name.
6007 * Advance "arg" to the first character after the name.
6008 * Return 0 for error.
6009 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006010 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006011get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012{
6013 char_u *p;
6014 int len;
6015
6016 for (p = *arg; vim_isIDc(*p); ++p)
6017 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006018 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 return 0;
6020
6021 len = (int)(p - *arg);
6022 *arg = p;
6023 return len;
6024}
6025
6026/*
6027 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006028 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 * Return 0 if something is wrong.
6030 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006031 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006032get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033{
6034 char_u *p;
6035 int len;
6036
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006037 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006039 {
6040 if (*p == ':')
6041 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006042 // "s:" is start of "s:var", but "n:" is not and can be used in
6043 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006044 len = (int)(p - *arg);
6045 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6046 || len > 1)
6047 break;
6048 }
6049 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006050 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 return 0;
6052
6053 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006054 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055
6056 return len;
6057}
6058
6059/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006060 * Get the length of the name of a variable or function.
6061 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006063 * Return -1 if curly braces expansion failed.
6064 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 * If the name contains 'magic' {}'s, expand them and return the
6066 * expanded name in an allocated string via 'alias' - caller must free.
6067 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006068 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006069get_name_len(
6070 char_u **arg,
6071 char_u **alias,
6072 int evaluate,
6073 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074{
6075 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 char_u *p;
6077 char_u *expr_start;
6078 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006080 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081
6082 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6083 && (*arg)[2] == (int)KE_SNR)
6084 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006085 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 *arg += 3;
6087 return get_id_len(arg) + 3;
6088 }
6089 len = eval_fname_script(*arg);
6090 if (len > 0)
6091 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006092 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 *arg += len;
6094 }
6095
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006097 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006099 p = find_name_end(*arg, &expr_start, &expr_end,
6100 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 if (expr_start != NULL)
6102 {
6103 char_u *temp_string;
6104
6105 if (!evaluate)
6106 {
6107 len += (int)(p - *arg);
6108 *arg = skipwhite(p);
6109 return len;
6110 }
6111
6112 /*
6113 * Include any <SID> etc in the expanded string:
6114 * Thus the -len here.
6115 */
6116 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6117 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006118 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 *alias = temp_string;
6120 *arg = skipwhite(p);
6121 return (int)STRLEN(temp_string);
6122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123
6124 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006125 // Only give an error when there is something, otherwise it will be
6126 // reported at a higher level.
6127 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006128 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129
6130 return len;
6131}
6132
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006133/*
6134 * Find the end of a variable or function name, taking care of magic braces.
6135 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6136 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006137 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006138 * Return a pointer to just after the name. Equal to "arg" if there is no
6139 * valid name.
6140 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006141 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006142find_name_end(
6143 char_u *arg,
6144 char_u **expr_start,
6145 char_u **expr_end,
6146 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006148 int mb_nest = 0;
6149 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006151 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02006152 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006154 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006156 *expr_start = NULL;
6157 *expr_end = NULL;
6158 }
6159
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006160 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006161 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
6162 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006163 return arg;
6164
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006165 for (p = arg; *p != NUL
6166 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006167 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006168 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006169 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006170 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006171 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006172 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006173 if (*p == '\'')
6174 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006175 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006176 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006177 ;
6178 if (*p == NUL)
6179 break;
6180 }
6181 else if (*p == '"')
6182 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006183 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006184 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006185 if (*p == '\\' && p[1] != NUL)
6186 ++p;
6187 if (*p == NUL)
6188 break;
6189 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006190 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6191 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006192 // "s:" is start of "s:var", but "n:" is not and can be used in
6193 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006194 len = (int)(p - arg);
6195 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006196 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006197 break;
6198 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006199
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006200 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006202 if (*p == '[')
6203 ++br_nest;
6204 else if (*p == ']')
6205 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006207
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006208 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006210 if (*p == '{')
6211 {
6212 mb_nest++;
6213 if (expr_start != NULL && *expr_start == NULL)
6214 *expr_start = p;
6215 }
6216 else if (*p == '}')
6217 {
6218 mb_nest--;
6219 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6220 *expr_end = p;
6221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 }
6224
6225 return p;
6226}
6227
6228/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006229 * Expands out the 'magic' {}'s in a variable/function name.
6230 * Note that this can call itself recursively, to deal with
6231 * constructs like foo{bar}{baz}{bam}
6232 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6233 * "in_start" ^
6234 * "expr_start" ^
6235 * "expr_end" ^
6236 * "in_end" ^
6237 *
6238 * Returns a new allocated string, which the caller must free.
6239 * Returns NULL for failure.
6240 */
6241 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006242make_expanded_name(
6243 char_u *in_start,
6244 char_u *expr_start,
6245 char_u *expr_end,
6246 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006247{
6248 char_u c1;
6249 char_u *retval = NULL;
6250 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006251
6252 if (expr_end == NULL || in_end == NULL)
6253 return NULL;
6254 *expr_start = NUL;
6255 *expr_end = NUL;
6256 c1 = *in_end;
6257 *in_end = NUL;
6258
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006259 temp_result = eval_to_string(expr_start + 1, FALSE);
6260 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006261 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006262 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6263 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006264 if (retval != NULL)
6265 {
6266 STRCPY(retval, in_start);
6267 STRCAT(retval, temp_result);
6268 STRCAT(retval, expr_end + 1);
6269 }
6270 }
6271 vim_free(temp_result);
6272
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006273 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006274 *expr_start = '{';
6275 *expr_end = '}';
6276
6277 if (retval != NULL)
6278 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006279 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006280 if (expr_start != NULL)
6281 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006282 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006283 temp_result = make_expanded_name(retval, expr_start,
6284 expr_end, temp_result);
6285 vim_free(retval);
6286 retval = temp_result;
6287 }
6288 }
6289
6290 return retval;
6291}
6292
6293/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006295 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006297 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006298eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006300 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006301}
6302
6303/*
6304 * Return TRUE if character "c" can be used as the first character in a
6305 * variable or function name (excluding '{' and '}').
6306 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006307 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006308eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006309{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006310 return ASCII_ISALPHA(c) || c == '_';
6311}
6312
6313/*
6314 * Return TRUE if character "c" can be used as the first character of a
6315 * dictionary key.
6316 */
6317 int
6318eval_isdictc(int c)
6319{
6320 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321}
6322
6323/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006324 * Handle:
6325 * - expr[expr], expr[expr:expr] subscript
6326 * - ".name" lookup
6327 * - function call with Funcref variable: func(expr)
6328 * - method call: var->method()
6329 *
6330 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006331 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006332 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006333 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006334handle_subscript(
6335 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006336 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006337 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006338 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006339 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006340{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006341 int evaluate = evalarg != NULL
6342 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006343 int ret = OK;
6344 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006345 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006346 int getnext;
6347 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006348
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006349 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006350 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006351 // When at the end of the line and ".name" or "->{" or "->X" follows in
6352 // the next line then consume the line break.
6353 p = eval_next_non_blank(*arg, evalarg, &getnext);
6354 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006355 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006356 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6357 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6358 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006359 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006360 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006361 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006362 check_white = FALSE;
6363 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006364
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006365 if (rettv->v_type == VAR_ANY)
6366 {
6367 char_u *exp_name;
6368 int cc;
6369 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006370 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006371 type_T *type;
6372
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006373 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006374 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006375 if (**arg != '.')
6376 {
6377 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006378 semsg(_(e_expected_dot_after_name_str),
6379 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006380 ret = FAIL;
6381 break;
6382 }
6383 ++*arg;
6384 if (IS_WHITE_OR_NUL(**arg))
6385 {
6386 if (verbose)
6387 emsg(_(e_no_white_space_allowed_after_dot));
6388 ret = FAIL;
6389 break;
6390 }
6391
6392 // isolate the name
6393 exp_name = *arg;
6394 while (eval_isnamec(**arg))
6395 ++*arg;
6396 cc = **arg;
6397 **arg = NUL;
6398
6399 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00006400 evalarg->eval_cctx, evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006401 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006402
6403 if (idx < 0 && ufunc == NULL)
6404 {
6405 ret = FAIL;
6406 break;
6407 }
6408 if (idx >= 0)
6409 {
6410 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6411 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6412
6413 copy_tv(sv->sv_tv, rettv);
6414 }
6415 else
6416 {
6417 rettv->v_type = VAR_FUNC;
6418 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6419 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006420 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006421 }
6422
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006423 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6424 || rettv->v_type == VAR_PARTIAL))
6425 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006426 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006427 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6428 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006429
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006430 // Stop the expression evaluation when immediately aborting on
6431 // error, or when an interrupt occurred or an exception was thrown
6432 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006433 if (aborting())
6434 {
6435 if (ret == OK)
6436 clear_tv(rettv);
6437 ret = FAIL;
6438 }
6439 dict_unref(selfdict);
6440 selfdict = NULL;
6441 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006442 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006443 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006444 if (in_vim9script())
6445 *arg = skipwhite(p + 2);
6446 else
6447 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006448 if (ret == OK)
6449 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006450 if (VIM_ISWHITE(**arg))
6451 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006452 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006453 ret = FAIL;
6454 }
6455 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006456 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006457 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006458 else
6459 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006460 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006461 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006462 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006463 // "." is ".name" lookup when we found a dict or when evaluating and
6464 // scriptversion is at least 2, where string concatenation is "..".
6465 else if (**arg == '['
6466 || (**arg == '.' && (rettv->v_type == VAR_DICT
6467 || (!evaluate
6468 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006469 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006470 {
6471 dict_unref(selfdict);
6472 if (rettv->v_type == VAR_DICT)
6473 {
6474 selfdict = rettv->vval.v_dict;
6475 if (selfdict != NULL)
6476 ++selfdict->dv_refcount;
6477 }
6478 else
6479 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006480 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006481 {
6482 clear_tv(rettv);
6483 ret = FAIL;
6484 }
6485 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006486 else
6487 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006488 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006489
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006490 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6491 // Don't do this when "Func" is already a partial that was bound
6492 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006493 if (selfdict != NULL
6494 && (rettv->v_type == VAR_FUNC
6495 || (rettv->v_type == VAR_PARTIAL
6496 && (rettv->vval.v_partial->pt_auto
6497 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006498 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006499
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006500 dict_unref(selfdict);
6501 return ret;
6502}
6503
6504/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006505 * Make a copy of an item.
6506 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006507 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006508 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6509 * reference to an already copied list/dict can be used.
6510 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006511 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006512 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006513item_copy(
6514 typval_T *from,
6515 typval_T *to,
6516 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006517 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006518 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006519{
6520 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006521 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006522
Bram Moolenaar33570922005-01-25 22:26:29 +00006523 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006524 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006525 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006526 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006527 }
6528 ++recurse;
6529
6530 switch (from->v_type)
6531 {
6532 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006533 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006534 case VAR_STRING:
6535 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006536 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006537 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006538 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006539 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006540 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006541 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006542 copy_tv(from, to);
6543 break;
6544 case VAR_LIST:
6545 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006546 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006547 if (from->vval.v_list == NULL)
6548 to->vval.v_list = NULL;
6549 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6550 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006551 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006552 to->vval.v_list = from->vval.v_list->lv_copylist;
6553 ++to->vval.v_list->lv_refcount;
6554 }
6555 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006556 to->vval.v_list = list_copy(from->vval.v_list,
6557 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006558 if (to->vval.v_list == NULL)
6559 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006560 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006561 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006562 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006563 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006564 case VAR_DICT:
6565 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006566 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006567 if (from->vval.v_dict == NULL)
6568 to->vval.v_dict = NULL;
6569 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6570 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006571 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006572 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6573 ++to->vval.v_dict->dv_refcount;
6574 }
6575 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006576 to->vval.v_dict = dict_copy(from->vval.v_dict,
6577 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006578 if (to->vval.v_dict == NULL)
6579 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006580 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006581 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006582 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006583 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006584 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006585 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006586 }
6587 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006588 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006589}
6590
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006591 void
6592echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6593{
6594 char_u *tofree;
6595 char_u numbuf[NUMBUFLEN];
6596 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6597
6598 if (*atstart)
6599 {
6600 *atstart = FALSE;
6601 // Call msg_start() after eval1(), evaluating the expression
6602 // may cause a message to appear.
6603 if (with_space)
6604 {
6605 // Mark the saved text as finishing the line, so that what
6606 // follows is displayed on a new line when scrolling back
6607 // at the more prompt.
6608 msg_sb_eol();
6609 msg_start();
6610 }
6611 }
6612 else if (with_space)
6613 msg_puts_attr(" ", echo_attr);
6614
6615 if (p != NULL)
6616 for ( ; *p != NUL && !got_int; ++p)
6617 {
6618 if (*p == '\n' || *p == '\r' || *p == TAB)
6619 {
6620 if (*p != TAB && *needclr)
6621 {
6622 // remove any text still there from the command
6623 msg_clr_eos();
6624 *needclr = FALSE;
6625 }
6626 msg_putchar_attr(*p, echo_attr);
6627 }
6628 else
6629 {
6630 if (has_mbyte)
6631 {
6632 int i = (*mb_ptr2len)(p);
6633
6634 (void)msg_outtrans_len_attr(p, i, echo_attr);
6635 p += i - 1;
6636 }
6637 else
6638 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6639 }
6640 }
6641 vim_free(tofree);
6642}
6643
Bram Moolenaare9a41262005-01-15 22:18:47 +00006644/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 * ":echo expr1 ..." print each argument separated with a space, add a
6646 * newline at the end.
6647 * ":echon expr1 ..." print each argument plain.
6648 */
6649 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006650ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651{
6652 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006653 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006654 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 int needclr = TRUE;
6656 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006657 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006658 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006659 evalarg_T evalarg;
6660
Bram Moolenaare707c882020-07-01 13:07:37 +02006661 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662
6663 if (eap->skip)
6664 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006665 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006667 // If eval1() causes an error message the text from the command may
6668 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006669 need_clr_eos = needclr;
6670
Bram Moolenaar68db9962021-05-09 23:19:22 +02006671 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006672 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 {
6674 /*
6675 * Report the invalid expression unless the expression evaluation
6676 * has been cancelled due to an aborting error, an interrupt, or an
6677 * exception.
6678 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006679 if (!aborting() && did_emsg == did_emsg_before
6680 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006681 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006682 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 break;
6684 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006685 need_clr_eos = FALSE;
6686
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006688 {
6689 if (rettv.v_type == VAR_VOID)
6690 {
6691 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6692 break;
6693 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006694 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006695 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006696
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006697 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 arg = skipwhite(arg);
6699 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006700 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006701 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702
6703 if (eap->skip)
6704 --emsg_skip;
6705 else
6706 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006707 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 if (needclr)
6709 msg_clr_eos();
6710 if (eap->cmdidx == CMD_echo)
6711 msg_end();
6712 }
6713}
6714
6715/*
6716 * ":echohl {name}".
6717 */
6718 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006719ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006721 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722}
6723
6724/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006725 * Returns the :echo attribute
6726 */
6727 int
6728get_echo_attr(void)
6729{
6730 return echo_attr;
6731}
6732
6733/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 * ":execute expr1 ..." execute the result of an expression.
6735 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01006736 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006738 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 * Each gets spaces around each argument and a newline at the end for
6740 * echo commands
6741 */
6742 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006743ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744{
6745 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006746 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 int ret = OK;
6748 char_u *p;
6749 garray_T ga;
6750 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006751 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752
6753 ga_init2(&ga, 1, 80);
6754
6755 if (eap->skip)
6756 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006757 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006759 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006760 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762
6763 if (!eap->skip)
6764 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006765 char_u buf[NUMBUFLEN];
6766
6767 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006768 {
6769 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6770 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006771 semsg(_(e_using_invalid_value_as_string_str),
6772 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006773 p = NULL;
6774 }
6775 else
6776 p = tv_get_string_buf(&rettv, buf);
6777 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006778 else
6779 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006780 if (p == NULL)
6781 {
6782 clear_tv(&rettv);
6783 ret = FAIL;
6784 break;
6785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 len = (int)STRLEN(p);
6787 if (ga_grow(&ga, len + 2) == FAIL)
6788 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006789 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 ret = FAIL;
6791 break;
6792 }
6793 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 ga.ga_len += len;
6797 }
6798
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006799 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 arg = skipwhite(arg);
6801 }
6802
6803 if (ret != FAIL && ga.ga_data != NULL)
6804 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006805 // use the first line of continuation lines for messages
6806 SOURCING_LNUM = start_lnum;
6807
Bram Moolenaar37fef162022-08-29 18:16:32 +01006808 if (eap->cmdidx == CMD_echomsg
6809 || eap->cmdidx == CMD_echowindow
6810 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006811 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006812 // Mark the already saved text as finishing the line, so that what
6813 // follows is displayed on a new line when scrolling back at the
6814 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006815 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006816 }
6817
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006818 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006819 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006820 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006821 out_flush();
6822 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006823 else if (eap->cmdidx == CMD_echowindow)
6824 {
6825#ifdef HAS_MESSAGE_WINDOW
6826 start_echowindow();
6827#endif
6828 msg_attr(ga.ga_data, echo_attr);
6829#ifdef HAS_MESSAGE_WINDOW
6830 end_echowindow();
6831#endif
6832 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006833 else if (eap->cmdidx == CMD_echoconsole)
6834 {
6835 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6836 ui_write((char_u *)"\r\n", 2, TRUE);
6837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 else if (eap->cmdidx == CMD_echoerr)
6839 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006840 int save_did_emsg = did_emsg;
6841
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006842 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006843 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 if (!force_abort)
6845 did_emsg = save_did_emsg;
6846 }
6847 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006848 {
6849 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6850
6851 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6852 sticky_cmdmod_flags = cmdmod.cmod_flags
6853 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 do_cmdline((char_u *)ga.ga_data,
6855 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006856 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 }
6859
6860 ga_clear(&ga);
6861
6862 if (eap->skip)
6863 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02006864 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865}
6866
6867/*
6868 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6869 * "arg" points to the "&" or '+' when called, to "option" when returning.
6870 * Returns NULL when no option name found. Otherwise pointer to the char
6871 * after the option name.
6872 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006873 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006874find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875{
6876 char_u *p = *arg;
6877
6878 ++p;
6879 if (*p == 'g' && p[1] == ':')
6880 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006881 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 p += 2;
6883 }
6884 else if (*p == 'l' && p[1] == ':')
6885 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006886 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 p += 2;
6888 }
6889 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006890 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891
6892 if (!ASCII_ISALPHA(*p))
6893 return NULL;
6894 *arg = p;
6895
6896 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006897 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 else
6899 while (ASCII_ISALPHA(*p))
6900 ++p;
6901 return p;
6902}
6903
6904/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006905 * Display script name where an item was last set.
6906 * Should only be invoked when 'verbose' is non-zero.
6907 */
6908 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006909last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006910{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006911 char_u *p;
6912
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006913 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006914 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006915 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006916 if (p != NULL)
6917 {
6918 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006919 msg_puts(_("\n\tLast set from "));
6920 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006921 if (script_ctx.sc_lnum > 0)
6922 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006923 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006924 msg_outnum((long)script_ctx.sc_lnum);
6925 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006926 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006927 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006928 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006929 }
6930}
6931
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006932#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933
6934/*
6935 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006936 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 * "flags" can be "g" to do a global substitute.
6938 * Returns an allocated string, NULL for error.
6939 */
6940 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006941do_string_sub(
6942 char_u *str,
6943 char_u *pat,
6944 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006945 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006946 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947{
6948 int sublen;
6949 regmatch_T regmatch;
6950 int i;
6951 int do_all;
6952 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006953 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 garray_T ga;
6955 char_u *ret;
6956 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006957 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006959 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006961 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962
6963 ga_init2(&ga, 1, 200);
6964
6965 do_all = (flags[0] == 'g');
6966
6967 regmatch.rm_ic = p_ic;
6968 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6969 if (regmatch.regprog != NULL)
6970 {
6971 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006972 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6974 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006975 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006976 if (regmatch.startp[0] == regmatch.endp[0])
6977 {
6978 if (zero_width == regmatch.startp[0])
6979 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006980 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006981 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006982 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6983 (size_t)i);
6984 ga.ga_len += i;
6985 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006986 continue;
6987 }
6988 zero_width = regmatch.startp[0];
6989 }
6990
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 /*
6992 * Get some space for a temporary buffer to do the substitution
6993 * into. It will contain:
6994 * - The text up to where the match is.
6995 * - The substituted text.
6996 * - The text after the match.
6997 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01006998 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006999 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7001 {
7002 ga_clear(&ga);
7003 break;
7004 }
7005
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007006 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 i = (int)(regmatch.startp[0] - tail);
7008 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007009 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007010 (void)vim_regsub(&regmatch, sub, expr,
7011 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7012 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007014 tail = regmatch.endp[0];
7015 if (*tail == NUL)
7016 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 if (!do_all)
7018 break;
7019 }
7020
7021 if (ga.ga_data != NULL)
7022 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7023
Bram Moolenaar473de612013-06-08 18:19:48 +02007024 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 }
7026
7027 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7028 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007029 if (p_cpo == empty_option)
7030 p_cpo = save_cpo;
7031 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007032 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007033 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007034 // If it's still empty it was changed and restored, need to restore in
7035 // the complicated way.
7036 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007037 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007038 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040
7041 return ret;
7042}