blob: f55d9de8ecf3b50c588742181a69ff24fd7cdcdf [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 Moolenaar6f5b6df2020-05-16 21:20:12 +0200266 if (call_def_function(partial->pt_func, argc, argv,
267 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100268 return FAIL;
269 }
270 else
271 {
272 s = partial_name(partial);
273 if (s == NULL || *s == NUL)
274 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200275 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000276 funcexe.fe_evaluate = TRUE;
277 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
279 return FAIL;
280 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100281 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200282 else if (expr->v_type == VAR_INSTR)
283 {
284 return exe_typval_instr(expr, rettv);
285 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100286 else
287 {
Bram Moolenaarfc4c4482022-01-26 21:17:04 +0000288 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
Bram Moolenaar48570482017-10-30 21:48:41 +0100289 if (s == NULL)
290 return FAIL;
291 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200292 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100293 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200294 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100295 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200296 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200297 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100298 return FAIL;
299 }
300 }
301 return OK;
302}
303
304/*
305 * Like eval_to_bool() but using a typval_T instead of a string.
306 * Works for string, funcref and partial.
307 */
308 int
309eval_expr_to_bool(typval_T *expr, int *error)
310{
311 typval_T rettv;
312 int res;
313
314 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
315 {
316 *error = TRUE;
317 return FALSE;
318 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200319 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100320 clear_tv(&rettv);
321 return res;
322}
323
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324/*
325 * Top level evaluation function, returning a string. If "skip" is TRUE,
326 * only parsing to "nextcmd" is done, without reporting errors. Return
327 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
328 */
329 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100330eval_to_string_skip(
331 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200332 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100333 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334{
Bram Moolenaar33570922005-01-25 22:26:29 +0000335 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200337 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338
Bram Moolenaar37c83712020-06-30 21:18:36 +0200339 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 if (skip)
341 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200342 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 retval = NULL;
344 else
345 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100346 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000347 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 }
349 if (skip)
350 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200351 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352
353 return retval;
354}
355
356/*
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100357 * Initialize "evalarg" for use.
358 */
359 void
360init_evalarg(evalarg_T *evalarg)
361{
362 CLEAR_POINTER(evalarg);
363 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
364}
365
366/*
367 * If "evalarg->eval_tofree" is not NULL free it later.
368 * Caller is expected to overwrite "evalarg->eval_tofree" next.
369 */
370 static void
371free_eval_tofree_later(evalarg_T *evalarg)
372{
373 if (evalarg->eval_tofree != NULL)
374 {
375 if (ga_grow(&evalarg->eval_tofree_ga, 1) == OK)
376 ((char_u **)evalarg->eval_tofree_ga.ga_data)
377 [evalarg->eval_tofree_ga.ga_len++]
378 = evalarg->eval_tofree;
379 else
380 vim_free(evalarg->eval_tofree);
381 }
382}
383
384/*
385 * After using "evalarg" filled from "eap": free the memory.
386 */
387 void
388clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
389{
390 if (evalarg != NULL)
391 {
392 if (evalarg->eval_tofree != NULL)
393 {
394 if (eap != NULL)
395 {
396 // We may need to keep the original command line, e.g. for
397 // ":let" it has the variable names. But we may also need the
398 // new one, "nextcmd" points into it. Keep both.
399 vim_free(eap->cmdline_tofree);
400 eap->cmdline_tofree = *eap->cmdlinep;
401 *eap->cmdlinep = evalarg->eval_tofree;
402 }
403 else
404 vim_free(evalarg->eval_tofree);
405 evalarg->eval_tofree = NULL;
406 }
407
408 ga_clear_strings(&evalarg->eval_tofree_ga);
409 VIM_CLEAR(evalarg->eval_tofree_lambda);
410 }
411}
412
413/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000414 * Skip over an expression at "*pp".
415 * Return FAIL for an error, OK otherwise.
416 */
417 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200418skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000419{
Bram Moolenaar33570922005-01-25 22:26:29 +0000420 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000421
422 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200423 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000424}
425
426/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200427 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200428 * If in Vim9 script and line breaks are encountered, the lines are
429 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200430 * "arg" is advanced to just after the expression.
431 * "start" is set to the start of the expression, "end" to just after the end.
432 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200433 * Return FAIL for an error, OK otherwise.
434 */
435 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200436skip_expr_concatenate(
437 char_u **arg,
438 char_u **start,
439 char_u **end,
440 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200441{
442 typval_T rettv;
443 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200444 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200445 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200446 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200447 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200448 int evaluate = evalarg == NULL
449 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200450
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200451 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200452 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200453 {
454 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200455 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200456 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200457 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200458 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200459 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200460 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200461
462 // Don't evaluate the expression.
463 if (evalarg != NULL)
464 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200465 *arg = skipwhite(*arg);
466 res = eval1(arg, &rettv, evalarg);
467 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200468 if (evalarg != NULL)
469 evalarg->eval_flags = save_flags;
470
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200471 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200472 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200473 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200474 if (evalarg->eval_ga.ga_len == 1)
475 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200476 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200477 ga_clear(gap);
478 gap->ga_itemsize = 0;
479 }
480 else
481 {
482 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200483 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200484
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200485 // Line breaks encountered, concatenate all the lines.
486 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200487 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200488
489 // free the lines only when using getsourceline()
490 if (evalarg->eval_cookie != NULL)
491 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200492 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200493 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200494 // Do not free the last line, "arg" points into it, free it
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100495 // later. Also free "eval_tofree" later if needed.
496 free_eval_tofree_later(evalarg);
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200497 evalarg->eval_tofree =
498 ((char_u **)gap->ga_data)[gap->ga_len - 1];
499 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200500 ga_clear_strings(gap);
501 }
502 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200503 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200504 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200505
506 // free lines that were explicitly marked for freeing
507 ga_clear_strings(freegap);
508 }
509
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200510 gap->ga_itemsize = 0;
511 if (p == NULL)
512 return FAIL;
513 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200514 vim_free(evalarg->eval_tofree_lambda);
515 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200516 // Compute "end" relative to the end.
517 *end = *start + STRLEN(*start) - endoff;
518 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200519 }
520
521 return res;
522}
523
524/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100525 * Convert "tv" to a string.
526 * When "convert" is TRUE convert a List into a sequence of lines and convert
527 * a Float to a String.
528 * Returns an allocated string (NULL when out of memory).
529 */
530 char_u *
531typval2string(typval_T *tv, int convert)
532{
533 garray_T ga;
534 char_u *retval;
535#ifdef FEAT_FLOAT
536 char_u numbuf[NUMBUFLEN];
537#endif
538
539 if (convert && tv->v_type == VAR_LIST)
540 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000541 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100542 if (tv->vval.v_list != NULL)
543 {
544 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
545 if (tv->vval.v_list->lv_len > 0)
546 ga_append(&ga, NL);
547 }
548 ga_append(&ga, NUL);
549 retval = (char_u *)ga.ga_data;
550 }
551#ifdef FEAT_FLOAT
552 else if (convert && tv->v_type == VAR_FLOAT)
553 {
554 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
555 retval = vim_strsave(numbuf);
556 }
557#endif
558 else
559 retval = vim_strsave(tv_get_string(tv));
560 return retval;
561}
562
563/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200564 * Top level evaluation function, returning a string. Does not handle line
565 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000566 * When "convert" is TRUE convert a List into a sequence of lines and convert
567 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 * Return pointer to allocated memory, or NULL for failure.
569 */
570 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100571eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100572 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100573 int convert,
574 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575{
Bram Moolenaar33570922005-01-25 22:26:29 +0000576 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100578 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100580 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
581 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 retval = NULL;
583 else
584 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100585 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000586 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100588 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589
590 return retval;
591}
592
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100593 char_u *
594eval_to_string(
595 char_u *arg,
596 int convert)
597{
598 return eval_to_string_eap(arg, convert, NULL);
599}
600
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000602 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100603 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200604 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 */
606 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100607eval_to_string_safe(
608 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000609 int use_sandbox,
610 int keep_script_version)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611{
612 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200613 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200614 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200615 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616
Bram Moolenaar9530b582022-01-22 13:39:08 +0000617 if (!keep_script_version)
618 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200619 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000620 if (use_sandbox)
621 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100622 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200623 may_garbage_collect = FALSE;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200624 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000625 if (use_sandbox)
626 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100627 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200628 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200629 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200630 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 return retval;
632}
633
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634/*
635 * Top level evaluation function, returning a number.
636 * Evaluates "expr" silently.
637 * Returns -1 for an error.
638 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200639 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100640eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
Bram Moolenaar33570922005-01-25 22:26:29 +0000642 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200643 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000644 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645
646 ++emsg_off;
647
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200648 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 retval = -1;
650 else
651 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100652 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000653 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 }
655 --emsg_off;
656
657 return retval;
658}
659
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000660/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000661 * Top level evaluation function.
662 * Returns an allocated typval_T with the result.
663 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000664 */
665 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200666eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000667{
668 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200669 evalarg_T evalarg;
670
671 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000672
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200673 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200674 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100675 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000676
Bram Moolenaar37c83712020-06-30 21:18:36 +0200677 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000678 return tv;
679}
680
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000682 * "*arg" points to what can be a function name in the form of "import.Name" or
683 * "Funcref". Return the name of the function. Set "tofree" to something that
684 * was allocated.
685 * If "verbose" is FALSE no errors are given.
686 * Return NULL for any failure.
687 */
688 static char_u *
689deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000690 char_u **arg,
691 char_u **tofree,
692 evalarg_T *evalarg,
693 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000694{
695 typval_T ref;
696 char_u *name = *arg;
697
698 ref.v_type = VAR_UNKNOWN;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100699 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100700 {
701 dictitem_T *v;
702
703 // If <SID>VarName was used it would not be found, try another way.
704 v = find_var_also_in_script(name, NULL, FALSE);
705 if (v == NULL)
706 return NULL;
707 copy_tv(&v->di_tv, &ref);
708 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000709 if (*skipwhite(*arg) != NUL)
710 {
711 if (verbose)
712 semsg(_(e_trailing_characters_str), *arg);
713 name = NULL;
714 }
715 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
716 {
717 name = ref.vval.v_string;
718 ref.vval.v_string = NULL;
719 *tofree = name;
720 }
721 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
722 {
723 if (ref.vval.v_partial->pt_argc > 0
724 || ref.vval.v_partial->pt_dict != NULL)
725 {
726 if (verbose)
727 emsg(_(e_cannot_use_partial_here));
728 name = NULL;
729 }
730 else
731 {
732 name = vim_strsave(partial_name(ref.vval.v_partial));
733 *tofree = name;
734 }
735 }
736 else
737 {
738 if (verbose)
739 semsg(_(e_not_callable_type_str), name);
740 name = NULL;
741 }
742 clear_tv(&ref);
743 return name;
744}
745
746/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100747 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200748 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
749 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000750 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200752 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100753call_vim_function(
754 char_u *func,
755 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200756 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200757 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000759 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200760 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000761 char_u *arg;
762 char_u *name;
763 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000764 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100766 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200767 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000768 funcexe.fe_firstline = curwin->w_cursor.lnum;
769 funcexe.fe_lastline = curwin->w_cursor.lnum;
770 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000771
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000772 // The name might be "import.Func" or "Funcref". We don't know, we need to
773 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000774 // autoload script has errors. Guess that when there is a dot in the name
775 // showing errors is the right choice.
776 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000777 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000778 if (ignore_errors)
779 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000780 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000781 if (ignore_errors)
782 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000783 if (name == NULL)
784 name = func;
785
786 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
787
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000788 if (ret == FAIL)
789 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000790 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000791
792 return ret;
793}
794
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100795/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100796 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000797 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
798 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000799 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000800 */
801 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100802call_func_retstr(
803 char_u *func,
804 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200805 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000806{
807 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000808 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000809
Bram Moolenaarded27a12018-08-01 19:06:03 +0200810 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000811 return NULL;
812
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100813 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000814 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 return retval;
816}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000817
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000818/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100819 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000820 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000821 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000822 */
823 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100824call_func_retlist(
825 char_u *func,
826 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200827 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000828{
829 typval_T rettv;
830
Bram Moolenaarded27a12018-08-01 19:06:03 +0200831 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000832 return NULL;
833
834 if (rettv.v_type != VAR_LIST)
835 {
836 clear_tv(&rettv);
837 return NULL;
838 }
839
840 return rettv.vval.v_list;
841}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842
Bram Moolenaare70dd112022-01-21 16:31:11 +0000843#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100845 * Evaluate "arg", which is 'foldexpr'.
846 * Note: caller must set "curwin" to match "arg".
847 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
848 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 */
850 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000851eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000853 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000854 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200855 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000857 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000858 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
859 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860
Bram Moolenaare70dd112022-01-21 16:31:11 +0000861 arg = wp->w_p_fde;
862 current_sctx = wp->w_p_script_ctx[WV_FDE];
863
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000865 if (use_sandbox)
866 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100867 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200869 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 retval = 0;
871 else
872 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100873 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000874 if (tv.v_type == VAR_NUMBER)
875 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000876 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 retval = 0;
878 else
879 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100880 // If the result is a string, check if there is a non-digit before
881 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000882 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 if (!VIM_ISDIGIT(*s) && *s != '-')
884 *cp = *s++;
885 retval = atol((char *)s);
886 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000887 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 }
889 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000890 if (use_sandbox)
891 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100892 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200893 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000894 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200896 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897}
898#endif
899
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000901 * Get an lval: variable, Dict item or List item that can be assigned a value
902 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
903 * "name.key", "name.key[expr]" etc.
904 * Indexing only works if "name" is an existing List or Dictionary.
905 * "name" points to the start of the name.
906 * If "rettv" is not NULL it points to the value to be assigned.
907 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
908 * wrong; must end in space or cmd separator.
909 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100910 * flags:
911 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100912 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100913 * GLV_NO_AUTOLOAD: do not use script autoloading
914 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000915 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000916 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000917 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000918 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200919 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100920get_lval(
921 char_u *name,
922 typval_T *rettv,
923 lval_T *lp,
924 int unlet,
925 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100926 int flags, // GLV_ values
927 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000928{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000929 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000930 char_u *expr_start, *expr_end;
931 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000932 dictitem_T *v;
933 typval_T var1;
934 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000935 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000936 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000937 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100938 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100939 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100940 int writing;
Bram Moolenaar4525a572022-02-13 11:57:33 +0000941 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000942
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100943 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200944 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000945
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100946 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000947 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100948 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000949 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200950 lp->ll_name_end = find_name_end(name, NULL, NULL,
951 FNE_INCL_BR | fne_flags);
952 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000953 }
954
Bram Moolenaara749a422022-02-12 19:52:25 +0000955 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +0000956 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +0000957 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
958 {
959 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
960 return NULL;
961 }
962
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100963 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000964 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100965 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000966 if (expr_start != NULL)
967 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100968 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100969 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000970 && *p != '[' && *p != '.')
971 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000972 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000973 return NULL;
974 }
975
976 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
977 if (lp->ll_exp_name == NULL)
978 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100979 // Report an invalid expression in braces, unless the
980 // expression evaluation has been cancelled due to an
981 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000982 if (!aborting() && !quiet)
983 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000984 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000985 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000986 return NULL;
987 }
988 }
989 lp->ll_name = lp->ll_exp_name;
990 }
991 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100992 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000993 lp->ll_name = name;
994
Bram Moolenaar4525a572022-02-13 11:57:33 +0000995 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100996 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200997 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +0000998 // However, "g:[key]" is indexing a dictionary.
999 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001000 {
1001 --p;
1002 lp->ll_name_end = p;
1003 }
1004 if (*p == ':')
1005 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001006 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001007
1008 if (tp == p + 1 && !quiet)
1009 {
1010 semsg(_(e_white_space_required_after_str_str), ":", p);
1011 return NULL;
1012 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001013
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001014 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001015 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001016 semsg(_(e_using_type_not_in_script_context_str), p);
1017 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001018 }
1019
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001020 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001021 lp->ll_type = parse_type(&tp,
1022 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1023 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001024 if (lp->ll_type == NULL && !quiet)
1025 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001026 lp->ll_name_end = tp;
1027 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001028 }
1029 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001030 if (lp->ll_name == NULL)
1031 return p;
1032
Bram Moolenaar62aec932022-01-29 21:45:34 +00001033 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001034 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001035 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001036
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001037 if (import != NULL)
1038 {
1039 ufunc_T *ufunc;
1040 type_T *type;
1041
Bram Moolenaar753885b2022-08-24 16:30:36 +01001042 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001043 lp->ll_sid = import->imp_sid;
1044 lp->ll_name = skipwhite(p + 1);
1045 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1046 lp->ll_name_end = p;
1047
1048 // check the item is exported
1049 cc = *p;
1050 *p = NUL;
1051 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001052 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001053 {
1054 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001055 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001056 }
1057 *p = cc;
1058 }
1059 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001060
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001061 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001062 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001063 return p;
1064
Bram Moolenaar4525a572022-02-13 11:57:33 +00001065 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001066 {
1067 // using local variable
1068 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001069 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001070 }
1071 else
1072 {
1073 cc = *p;
1074 *p = NUL;
1075 // When we would write to the variable pass &ht and prevent autoload.
1076 writing = !(flags & GLV_READ_ONLY);
1077 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001078 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001079 if (v == NULL && !quiet)
1080 semsg(_(e_undefined_variable_str), lp->ll_name);
1081 *p = cc;
1082 if (v == NULL)
1083 return NULL;
1084 lp->ll_tv = &v->di_tv;
1085 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001086
Bram Moolenaar4525a572022-02-13 11:57:33 +00001087 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001088 {
1089 if (!quiet)
1090 semsg(_(e_variable_already_declared), lp->ll_name);
1091 return NULL;
1092 }
1093
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001094 /*
1095 * Loop until no more [idx] or .key is following.
1096 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001097 var1.v_type = VAR_UNKNOWN;
1098 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001099 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001100 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001101 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1102 {
1103 if (!quiet)
1104 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1105 return NULL;
1106 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001107 if (lp->ll_tv->v_type != VAR_LIST
1108 && lp->ll_tv->v_type != VAR_DICT
1109 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001110 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001111 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001112 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001113 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001114 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001115
1116 // a NULL list/blob works like an empty list/blob, allocate one now.
1117 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1118 rettv_list_alloc(lp->ll_tv);
1119 else if (lp->ll_tv->v_type == VAR_BLOB
1120 && lp->ll_tv->vval.v_blob == NULL)
1121 rettv_blob_alloc(lp->ll_tv);
1122
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001123 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001124 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001125 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001126 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001127 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001128 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001129
Bram Moolenaar4525a572022-02-13 11:57:33 +00001130 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001131 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001132 && lp->ll_tv == &v->di_tv
1133 && ht != NULL && ht == get_script_local_ht())
1134 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001135 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001136
1137 // Vim9 script local variable: get the type
1138 if (sv != NULL)
1139 lp->ll_valtype = sv->sv_type;
1140 }
1141
Bram Moolenaar8c711452005-01-14 21:53:12 +00001142 len = -1;
1143 if (*p == '.')
1144 {
1145 key = p + 1;
1146 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1147 ;
1148 if (len == 0)
1149 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001150 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001151 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001152 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001153 }
1154 p = key + len;
1155 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001156 else
1157 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001158 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001159 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001160 if (*p == ':')
1161 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001162 else
1163 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001164 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001165 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001166 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001167 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001168 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001169 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001170 clear_tv(&var1);
1171 return NULL;
1172 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001173 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001174 }
1175
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001176 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001177 if (*p == ':')
1178 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001179 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001180 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001181 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001182 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001183 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001184 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001185 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001186 if (rettv != NULL
1187 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001188 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001189 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001190 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001191 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001192 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001193 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001194 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001195 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001196 }
1197 p = skipwhite(p + 1);
1198 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001199 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001200 else
1201 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001202 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001203 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001204 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001205 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001206 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001207 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001208 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001209 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001210 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001211 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001212 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001213 clear_tv(&var2);
1214 return NULL;
1215 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001216 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001217 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001218 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001219 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001220 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001221
Bram Moolenaar8c711452005-01-14 21:53:12 +00001222 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001223 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001224 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001225 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001226 clear_tv(&var1);
1227 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001228 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001229 }
1230
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001231 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001232 ++p;
1233 }
1234
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001235 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001236 {
1237 if (len == -1)
1238 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001239 // "[key]": get key from "var1"
1240 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001241 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001242 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001243 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001244 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001245 }
1246 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001247 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001248
1249 // a NULL dict is equivalent with an empty dict
1250 if (lp->ll_tv->vval.v_dict == NULL)
1251 {
1252 lp->ll_tv->vval.v_dict = dict_alloc();
1253 if (lp->ll_tv->vval.v_dict == NULL)
1254 {
1255 clear_tv(&var1);
1256 return NULL;
1257 }
1258 ++lp->ll_tv->vval.v_dict->dv_refcount;
1259 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001260 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001261
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001262 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001263
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001264 // When assigning to a scope dictionary check that a function and
1265 // variable name is valid (only variable name unless it is l: or
1266 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001267 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001268 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001269 int prevval;
1270 int wrong;
1271
1272 if (len != -1)
1273 {
1274 prevval = key[len];
1275 key[len] = NUL;
1276 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001277 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001278 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001279 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1280 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001281 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001282 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001283 if (len != -1)
1284 key[len] = prevval;
1285 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001286 {
1287 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001288 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001289 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001290 }
1291
Bram Moolenaar10c65862020-10-08 21:16:42 +02001292 if (lp->ll_valtype != NULL)
1293 // use the type of the member
1294 lp->ll_valtype = lp->ll_valtype->tt_member;
1295
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001296 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001297 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001298 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001299 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001300 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001301 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001302 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001303 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001304 return NULL;
1305 }
1306
Bram Moolenaar31b81602019-02-10 22:14:27 +01001307 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001308 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001309 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001310 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001311 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001312 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001313 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001314 }
1315 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001316 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001317 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001318 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001319 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001320 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001321 p = NULL;
1322 break;
1323 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001324 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001325 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001326 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1327 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001328 {
1329 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001330 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001331 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001332
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001333 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001334 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001335 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001336 else if (lp->ll_tv->v_type == VAR_BLOB)
1337 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001338 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1339
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001340 /*
1341 * Get the number and item for the only or first index of the List.
1342 */
1343 if (empty1)
1344 lp->ll_n1 = 0;
1345 else
1346 // is number or string
1347 lp->ll_n1 = (long)tv_get_number(&var1);
1348 clear_tv(&var1);
1349
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001350 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001351 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001352 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001353 return NULL;
1354 }
1355 if (lp->ll_range && !lp->ll_empty2)
1356 {
1357 lp->ll_n2 = (long)tv_get_number(&var2);
1358 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001359 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1360 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001361 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001362 }
1363 lp->ll_blob = lp->ll_tv->vval.v_blob;
1364 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001365 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001366 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001367 else
1368 {
1369 /*
1370 * Get the number and item for the only or first index of the List.
1371 */
1372 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001373 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001374 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001375 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001376 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001377 clear_tv(&var1);
1378
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001379 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001380 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001381 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1382 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001383 if (lp->ll_li == NULL)
1384 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001385 clear_tv(&var2);
1386 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001387 }
1388
Bram Moolenaar10c65862020-10-08 21:16:42 +02001389 if (lp->ll_valtype != NULL)
1390 // use the type of the member
1391 lp->ll_valtype = lp->ll_valtype->tt_member;
1392
Bram Moolenaar8c711452005-01-14 21:53:12 +00001393 /*
1394 * May need to find the item or absolute index for the second
1395 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001396 * When no index given: "lp->ll_empty2" is TRUE.
1397 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001398 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001399 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001400 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001401 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001402 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001403 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001404 if (check_range_index_two(lp->ll_list,
1405 &lp->ll_n1, lp->ll_li,
1406 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001407 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001408 }
1409
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001410 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001411 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001412 }
1413
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001414 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001415 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001416 return p;
1417}
1418
1419/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001420 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001421 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001422 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001423clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001424{
1425 vim_free(lp->ll_exp_name);
1426 vim_free(lp->ll_newkey);
1427}
1428
1429/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001430 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001431 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001432 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1433 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001434 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001435 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001436set_var_lval(
1437 lval_T *lp,
1438 char_u *endp,
1439 typval_T *rettv,
1440 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001441 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1442 char_u *op,
1443 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001444{
1445 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001446 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001447
1448 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001449 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001450 cc = *endp;
1451 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001452 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1453 return;
1454
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001455 if (lp->ll_blob != NULL)
1456 {
1457 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001458
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001459 if (op != NULL && *op != '=')
1460 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001461 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001462 return;
1463 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001464 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001465 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001466
1467 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1468 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001469 if (lp->ll_empty2)
1470 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1471
Bram Moolenaar68452172021-04-12 21:21:02 +02001472 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1473 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001474 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001475 }
1476 else
1477 {
1478 val = (int)tv_get_number_chk(rettv, &error);
1479 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001480 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001481 }
1482 }
1483 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001484 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001485 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001486
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001487 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1488 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001489 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001490 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001491 *endp = cc;
1492 return;
1493 }
1494
Bram Moolenaarff697e62019-02-12 22:28:33 +01001495 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001496 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001497 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001498 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001499 {
1500 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001501 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1502 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001503 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001504 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001505 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001506 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001507 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001508 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001509 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001510 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001511 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1512 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001513 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001514 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001515 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001516 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001517 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001518 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001519 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001520 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001521 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001522 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001523 else if (lp->ll_range)
1524 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001525 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1526 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001527 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001528 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001529 return;
1530 }
1531
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001532 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1533 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001534 }
1535 else
1536 {
1537 /*
1538 * Assign to a List or Dictionary item.
1539 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001540 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1541 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001542 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001543 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001544 return;
1545 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001546
1547 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001548 && check_typval_arg_type(lp->ll_valtype, rettv,
1549 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001550 return;
1551
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001552 if (lp->ll_newkey != NULL)
1553 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001554 if (op != NULL && *op != '=')
1555 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001556 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001557 return;
1558 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001559 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1560 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001561 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001562
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001563 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001564 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001565 if (di == NULL)
1566 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001567 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1568 {
1569 vim_free(di);
1570 return;
1571 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001572 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001573 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001574 else if (op != NULL && *op != '=')
1575 {
1576 tv_op(lp->ll_tv, rettv, op);
1577 return;
1578 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001579 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001580 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001581
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001582 /*
1583 * Assign the value to the variable or list item.
1584 */
1585 if (copy)
1586 copy_tv(rettv, lp->ll_tv);
1587 else
1588 {
1589 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001590 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001591 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001592 }
1593 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001594}
1595
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001596/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001597 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1598 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001599 * Returns OK or FAIL.
1600 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001601 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001602tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001603{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001604 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001605 char_u numbuf[NUMBUFLEN];
1606 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001607 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001608
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001609 // Can't do anything with a Funcref or Dict on the right.
1610 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001611 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001612 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1613 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001614 {
1615 switch (tv1->v_type)
1616 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001617 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001618 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001619 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001620 case VAR_DICT:
1621 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001622 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001623 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001624 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001625 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001626 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001627 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001628 break;
1629
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001630 case VAR_BLOB:
1631 if (*op != '+' || tv2->v_type != VAR_BLOB)
1632 break;
1633 // BLOB += BLOB
1634 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1635 {
1636 blob_T *b1 = tv1->vval.v_blob;
1637 blob_T *b2 = tv2->vval.v_blob;
1638 int i, len = blob_len(b2);
1639 for (i = 0; i < len; i++)
1640 ga_append(&b1->bv_ga, blob_get(b2, i));
1641 }
1642 return OK;
1643
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001644 case VAR_LIST:
1645 if (*op != '+' || tv2->v_type != VAR_LIST)
1646 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001647 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001648 if (tv2->vval.v_list != NULL)
1649 {
1650 if (tv1->vval.v_list == NULL)
1651 {
1652 tv1->vval.v_list = tv2->vval.v_list;
1653 ++tv1->vval.v_list->lv_refcount;
1654 }
1655 else
1656 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1657 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001658 return OK;
1659
1660 case VAR_NUMBER:
1661 case VAR_STRING:
1662 if (tv2->v_type == VAR_LIST)
1663 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001664 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001665 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001666 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001667 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001668#ifdef FEAT_FLOAT
1669 if (tv2->v_type == VAR_FLOAT)
1670 {
1671 float_T f = n;
1672
Bram Moolenaarff697e62019-02-12 22:28:33 +01001673 if (*op == '%')
1674 break;
1675 switch (*op)
1676 {
1677 case '+': f += tv2->vval.v_float; break;
1678 case '-': f -= tv2->vval.v_float; break;
1679 case '*': f *= tv2->vval.v_float; break;
1680 case '/': f /= tv2->vval.v_float; break;
1681 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001682 clear_tv(tv1);
1683 tv1->v_type = VAR_FLOAT;
1684 tv1->vval.v_float = f;
1685 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001686 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001687#endif
1688 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001689 switch (*op)
1690 {
1691 case '+': n += tv_get_number(tv2); break;
1692 case '-': n -= tv_get_number(tv2); break;
1693 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001694 case '/': n = num_divide(n, tv_get_number(tv2),
1695 &failed); break;
1696 case '%': n = num_modulus(n, tv_get_number(tv2),
1697 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001698 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001699 clear_tv(tv1);
1700 tv1->v_type = VAR_NUMBER;
1701 tv1->vval.v_number = n;
1702 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001703 }
1704 else
1705 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001706 if (tv2->v_type == VAR_FLOAT)
1707 break;
1708
Bram Moolenaarff697e62019-02-12 22:28:33 +01001709 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001710 s = tv_get_string(tv1);
1711 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001712 clear_tv(tv1);
1713 tv1->v_type = VAR_STRING;
1714 tv1->vval.v_string = s;
1715 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001716 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001717
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001718 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001719#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001720 {
1721 float_T f;
1722
Bram Moolenaarff697e62019-02-12 22:28:33 +01001723 if (*op == '%' || *op == '.'
1724 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001725 && tv2->v_type != VAR_NUMBER
1726 && tv2->v_type != VAR_STRING))
1727 break;
1728 if (tv2->v_type == VAR_FLOAT)
1729 f = tv2->vval.v_float;
1730 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001731 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001732 switch (*op)
1733 {
1734 case '+': tv1->vval.v_float += f; break;
1735 case '-': tv1->vval.v_float -= f; break;
1736 case '*': tv1->vval.v_float *= f; break;
1737 case '/': tv1->vval.v_float /= f; break;
1738 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001739 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001740#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001741 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001742 }
1743 }
1744
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001745 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001746 return FAIL;
1747}
1748
1749/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001750 * Evaluate the expression used in a ":for var in expr" command.
1751 * "arg" points to "var".
1752 * Set "*errp" to TRUE for an error, FALSE otherwise;
1753 * Return a pointer that holds the info. Null when there is an error.
1754 */
1755 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001756eval_for_line(
1757 char_u *arg,
1758 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001759 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001760 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001761{
Bram Moolenaar33570922005-01-25 22:26:29 +00001762 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001763 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001764 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001765 typval_T tv;
1766 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001767 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001768
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001769 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001770
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001771 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001772 if (fi == NULL)
1773 return NULL;
1774
Bram Moolenaar404557e2021-07-05 21:41:48 +02001775 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1776 &fi->fi_semicolon, FALSE);
1777 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001778 return fi;
1779
Bram Moolenaar404557e2021-07-05 21:41:48 +02001780 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001781 if (expr[0] != 'i' || expr[1] != 'n'
1782 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001783 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001784 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1785 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1786 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001787 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001788 return fi;
1789 }
1790
1791 if (skip)
1792 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001793 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1794 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001795 {
1796 *errp = FALSE;
1797 if (!skip)
1798 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001799 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001800 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001801 l = tv.vval.v_list;
1802 if (l == NULL)
1803 {
1804 // a null list is like an empty list: do nothing
1805 clear_tv(&tv);
1806 }
1807 else
1808 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001809 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001810 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001811
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001812 // No need to increment the refcount, it's already set for
1813 // the list being used in "tv".
1814 fi->fi_list = l;
1815 list_add_watch(l, &fi->fi_lw);
1816 fi->fi_lw.lw_item = l->lv_first;
1817 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001818 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001819 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001820 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001821 fi->fi_bi = 0;
1822 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001823 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001824 typval_T btv;
1825
1826 // Make a copy, so that the iteration still works when the
1827 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001829 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001830 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001831 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001832 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001833 else if (tv.v_type == VAR_STRING)
1834 {
1835 fi->fi_byte_idx = 0;
1836 fi->fi_string = tv.vval.v_string;
1837 tv.vval.v_string = NULL;
1838 if (fi->fi_string == NULL)
1839 fi->fi_string = vim_strsave((char_u *)"");
1840 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001841 else
1842 {
Sean Dewar80d73952021-08-04 19:25:54 +02001843 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001844 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001845 }
1846 }
1847 }
1848 if (skip)
1849 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001850 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851
1852 return fi;
1853}
1854
1855/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001856 * Used when looping over a :for line, skip the "in expr" part.
1857 */
1858 void
1859skip_for_lines(void *fi_void, evalarg_T *evalarg)
1860{
1861 forinfo_T *fi = (forinfo_T *)fi_void;
1862 int i;
1863
1864 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01001865 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001866}
1867
1868/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001869 * Use the first item in a ":for" list. Advance to the next.
1870 * Assign the values to the variable (list). "arg" points to the first one.
1871 * Return TRUE when a valid item was found, FALSE when at end of list or
1872 * something wrong.
1873 */
1874 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001875next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001876{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001877 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001878 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001879 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001880 ? (ASSIGN_FINAL
1881 // first round: error if variable exists
1882 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1883 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001884 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001885 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001886 int skip_assign = in_vim9script() && arg[0] == '_'
1887 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001888
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001889 if (fi->fi_blob != NULL)
1890 {
1891 typval_T tv;
1892
1893 if (fi->fi_bi >= blob_len(fi->fi_blob))
1894 return FALSE;
1895 tv.v_type = VAR_NUMBER;
1896 tv.v_lock = VAR_FIXED;
1897 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1898 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001899 if (skip_assign)
1900 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001901 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001902 fi->fi_varcount, flag, NULL) == OK;
1903 }
1904
1905 if (fi->fi_string != NULL)
1906 {
1907 typval_T tv;
1908 int len;
1909
1910 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1911 if (len == 0)
1912 return FALSE;
1913 tv.v_type = VAR_STRING;
1914 tv.v_lock = VAR_FIXED;
1915 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1916 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001917 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001918 if (skip_assign)
1919 result = TRUE;
1920 else
1921 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001922 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001923 vim_free(tv.vval.v_string);
1924 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001925 }
1926
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001927 item = fi->fi_lw.lw_item;
1928 if (item == NULL)
1929 result = FALSE;
1930 else
1931 {
1932 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001933 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001934 if (skip_assign)
1935 result = TRUE;
1936 else
1937 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001938 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001939 }
1940 return result;
1941}
1942
1943/*
1944 * Free the structure used to store info used by ":for".
1945 */
1946 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001947free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948{
Bram Moolenaar33570922005-01-25 22:26:29 +00001949 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001951 if (fi == NULL)
1952 return;
1953 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001954 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001955 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001956 list_unref(fi->fi_list);
1957 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001958 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001959 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001960 else
1961 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001962 vim_free(fi);
1963}
1964
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001966set_context_for_expression(
1967 expand_T *xp,
1968 char_u *arg,
1969 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001971 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001975 if (cmdidx == CMD_let || cmdidx == CMD_var
1976 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001977 {
1978 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001979 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001980 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001981 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001982 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001983 {
1984 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001985 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001986 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001987 break;
1988 }
1989 return;
1990 }
1991 }
1992 else
1993 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1994 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 while ((xp->xp_pattern = vim_strpbrk(arg,
1996 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1997 {
1998 c = *xp->xp_pattern;
1999 if (c == '&')
2000 {
2001 c = xp->xp_pattern[1];
2002 if (c == '&')
2003 {
2004 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002005 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 }
2007 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002008 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002010 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2011 xp->xp_pattern += 2;
2012
2013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 }
2015 else if (c == '$')
2016 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002017 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 xp->xp_context = EXPAND_ENV_VARS;
2019 }
2020 else if (c == '=')
2021 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002022 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 xp->xp_context = EXPAND_EXPRESSION;
2024 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002025 else if (c == '#'
2026 && xp->xp_context == EXPAND_EXPRESSION)
2027 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002028 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002029 break;
2030 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002031 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 && xp->xp_context == EXPAND_FUNCTIONS
2033 && vim_strchr(xp->xp_pattern, '(') == NULL)
2034 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002035 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 break;
2037 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002038 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002040 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 {
2042 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2043 if (c == '\\' && xp->xp_pattern[1] != NUL)
2044 ++xp->xp_pattern;
2045 xp->xp_context = EXPAND_NOTHING;
2046 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002047 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002049 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2051 /* skip */ ;
2052 xp->xp_context = EXPAND_NOTHING;
2053 }
2054 else if (c == '|')
2055 {
2056 if (xp->xp_pattern[1] == '|')
2057 {
2058 ++xp->xp_pattern;
2059 xp->xp_context = EXPAND_EXPRESSION;
2060 }
2061 else
2062 xp->xp_context = EXPAND_COMMANDS;
2063 }
2064 else
2065 xp->xp_context = EXPAND_EXPRESSION;
2066 }
2067 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002068 // Doesn't look like something valid, expand as an expression
2069 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002070 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 arg = xp->xp_pattern;
2072 if (*arg != NUL)
2073 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2074 /* skip */ ;
2075 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002076
2077 // ":exe one two" completes "two"
2078 if ((cmdidx == CMD_execute
2079 || cmdidx == CMD_echo
2080 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002081 || cmdidx == CMD_echomsg
2082 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002083 && xp->xp_context == EXPAND_EXPRESSION)
2084 {
2085 for (;;)
2086 {
2087 char_u *n = skiptowhite(arg);
2088
2089 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2090 break;
2091 arg = skipwhite(n);
2092 }
2093 }
2094
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 xp->xp_pattern = arg;
2096}
2097
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002099 * Return TRUE if "pat" matches "text".
2100 * Does not use 'cpo' and always uses 'magic'.
2101 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002102 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002103pattern_match(char_u *pat, char_u *text, int ic)
2104{
2105 int matches = FALSE;
2106 char_u *save_cpo;
2107 regmatch_T regmatch;
2108
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002109 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002110 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002111 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002112 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2113 if (regmatch.regprog != NULL)
2114 {
2115 regmatch.rm_ic = ic;
2116 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2117 vim_regfree(regmatch.regprog);
2118 }
2119 p_cpo = save_cpo;
2120 return matches;
2121}
2122
2123/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002124 * Handle a name followed by "(". Both for just "name(arg)" and for
2125 * "expr->name(arg)".
2126 * Returns OK or FAIL.
2127 */
2128 static int
2129eval_func(
2130 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002131 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002132 char_u *name,
2133 int name_len,
2134 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002135 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002136 typval_T *basetv) // "expr" for "expr->name(arg)"
2137{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002138 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002139 char_u *s = name;
2140 int len = name_len;
2141 partial_T *partial;
2142 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002143 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002144 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002145
2146 if (!evaluate)
2147 check_vars(s, len);
2148
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002149 // If "s" is the name of a variable of type VAR_FUNC
2150 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002151 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002152 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002153
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002154 // Need to make a copy, in case evaluating the arguments makes
2155 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002156 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002157 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002158 ret = FAIL;
2159 else
2160 {
2161 funcexe_T funcexe;
2162
2163 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002164 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002165 funcexe.fe_firstline = curwin->w_cursor.lnum;
2166 funcexe.fe_lastline = curwin->w_cursor.lnum;
2167 funcexe.fe_evaluate = evaluate;
2168 funcexe.fe_partial = partial;
2169 funcexe.fe_basetv = basetv;
2170 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002171 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002172 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002173 }
2174 vim_free(s);
2175
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002176 // If evaluate is FALSE rettv->v_type was not set in
2177 // get_func_tv, but it's needed in handle_subscript() to parse
2178 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002179 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2180 {
2181 rettv->vval.v_string = NULL;
2182 rettv->v_type = VAR_FUNC;
2183 }
2184
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002185 // Stop the expression evaluation when immediately
2186 // aborting on error, or when an interrupt occurred or
2187 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002188 if (evaluate && aborting())
2189 {
2190 if (ret == OK)
2191 clear_tv(rettv);
2192 ret = FAIL;
2193 }
2194 return ret;
2195}
2196
2197/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002198 * After a NL, skip over empty lines and comment-only lines.
2199 */
2200 static char_u *
2201newline_skip_comments(char_u *arg)
2202{
2203 char_u *p = arg + 1;
2204
2205 for (;;)
2206 {
2207 p = skipwhite(p);
2208
2209 if (*p == NUL)
2210 break;
2211 if (vim9_comment_start(p))
2212 {
2213 char_u *nl = vim_strchr(p, NL);
2214
2215 if (nl == NULL)
2216 break;
2217 p = nl;
2218 }
2219 if (*p != NL)
2220 break;
2221 ++p; // skip another NL
2222 }
2223 return p;
2224}
2225
2226/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002227 * Get the next line source line without advancing. But do skip over comment
2228 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002229 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002230 */
2231 static char_u *
2232getline_peek_skip_comments(evalarg_T *evalarg)
2233{
2234 for (;;)
2235 {
2236 char_u *next = getline_peek(evalarg->eval_getline,
2237 evalarg->eval_cookie);
2238 char_u *p;
2239
2240 if (next == NULL)
2241 break;
2242 p = skipwhite(next);
2243 if (*p != NUL && !vim9_comment_start(p))
2244 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002245 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002246 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002247 }
2248 return NULL;
2249}
2250
2251/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002252 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2253 * comment) and there is a next line, return the next line (skipping blanks)
2254 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002255 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2256 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002257 * "arg" must point somewhere inside a line, not at the start.
2258 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002259 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002260eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2261{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002262 char_u *p = skipwhite(arg);
2263
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002264 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002265 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002266 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002267 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2268 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002269 && (*p == NUL || *p == NL
2270 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002271 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002272 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002273
Bram Moolenaare442d592022-05-05 12:20:28 +01002274 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002275 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002276 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002277 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002278 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002279 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002280
Bram Moolenaar9d489562020-07-30 20:08:50 +02002281 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002282 {
2283 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002284 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002285 }
2286 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002287 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002288}
2289
2290/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002291 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002292 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002293 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002294 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002295eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002296{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002297 garray_T *gap = &evalarg->eval_ga;
2298 char_u *line;
2299
Bram Moolenaar39be4982022-05-06 21:51:50 +01002300 if (arg != NULL)
2301 {
2302 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002303 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002304 // Truncate before a trailing comment, so that concatenating the lines
2305 // won't turn the rest into a comment.
2306 if (*skipwhite(arg) == '#')
2307 *arg = NUL;
2308 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002309
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002310 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002311 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2312 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002313 else
2314 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002315 if (line == NULL)
2316 return NULL;
2317
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002318 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002319 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2320 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002321 char_u *p = skipwhite(line);
2322
2323 // Going to concatenate the lines after parsing. For an empty or
2324 // comment line use an empty string.
2325 if (*p == NUL || vim9_comment_start(p))
2326 {
2327 vim_free(line);
2328 line = vim_strsave((char_u *)"");
2329 }
2330
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002331 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2332 ++gap->ga_len;
2333 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002334 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002335 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002336 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002337 evalarg->eval_tofree = line;
2338 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002339
2340 // Advanced to the next line, "arg" no longer points into the previous
2341 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002342 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002343 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002344}
2345
Bram Moolenaare6b53242020-07-01 17:28:33 +02002346/*
2347 * Call eval_next_non_blank() and get the next line if needed.
2348 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002349 char_u *
2350skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2351{
2352 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002353 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002354
Bram Moolenaar962d7212020-07-04 14:15:00 +02002355 if (evalarg == NULL)
2356 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002357 eval_next_non_blank(p, evalarg, &getnext);
2358 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002359 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002360 return p;
2361}
2362
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002363/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2367 */
2368
2369/*
2370 * Handle zero level expression.
2371 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002372 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002373 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002374 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 * Return OK or FAIL.
2376 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002377 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002378eval0(
2379 char_u *arg,
2380 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002381 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002382 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002384 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2385}
2386
2387/*
2388 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2389 * expression and don't check what comes after the expression.
2390 */
2391 int
2392eval0_retarg(
2393 char_u *arg,
2394 typval_T *rettv,
2395 exarg_T *eap,
2396 evalarg_T *evalarg,
2397 char_u **retarg)
2398{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 int ret;
2400 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002401 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002402 int did_emsg_before = did_emsg;
2403 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002404 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002405 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002406 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407
2408 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002409 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002410
Bram Moolenaar79481362022-06-27 20:15:10 +01002411 if (ret != FAIL)
2412 {
2413 expr_end = p;
2414 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002415
Bram Moolenaar79481362022-06-27 20:15:10 +01002416 // In Vim9 script a command block is not split at NL characters for
2417 // commands using an expression argument. Skip over a '#' comment to
2418 // check for a following NL. Require white space before the '#'.
2419 if (in_vim9script() && p > expr_end && retarg == NULL)
2420 while (*p == '#')
2421 {
2422 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002423
Bram Moolenaar79481362022-06-27 20:15:10 +01002424 if (nl == NULL)
2425 break;
2426 p = skipwhite(nl + 1);
2427 if (eap != NULL && *p != NUL)
2428 eap->nextcmd = p;
2429 check_for_end = FALSE;
2430 }
2431
2432 if (check_for_end)
2433 end_error = !ends_excmd2(arg, p);
2434 }
2435
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002436 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 {
2438 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002439 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 /*
2441 * Report the invalid expression unless the expression evaluation has
2442 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002443 * exception, or we already gave a more specific error.
2444 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002446 if (!aborting()
2447 && did_emsg == did_emsg_before
2448 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002449 && (flags & EVAL_CONSTANT) == 0
2450 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002451 {
2452 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002453 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002454 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002455 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002456 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002457
2458 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002459 // a next command to avoid more errors, unless "|" is following, which
2460 // could only be a command separator.
Bram Moolenaar79481362022-06-27 20:15:10 +01002461 if (eap != NULL && p != NULL
2462 && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
Bram Moolenaar8143a532020-12-13 20:26:29 +01002463 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002464 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002466
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002467 if (retarg != NULL)
2468 *retarg = p;
2469 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002470 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002471
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 return ret;
2473}
2474
2475/*
2476 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002477 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002478 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 *
2480 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002481 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002483 * Note: "rettv.v_lock" is not set.
2484 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 * Return OK or FAIL.
2486 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002487 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002488eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002490 char_u *p;
2491 int getnext;
2492
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002493 CLEAR_POINTER(rettv);
2494
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 /*
2496 * Get the first variable.
2497 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002498 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 return FAIL;
2500
Bram Moolenaar793648f2020-06-26 21:28:25 +02002501 p = eval_next_non_blank(*arg, evalarg, &getnext);
2502 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002504 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002505 int result;
2506 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002507 evalarg_T *evalarg_used = evalarg;
2508 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002509 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002510 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002511 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002512
2513 if (evalarg == NULL)
2514 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002515 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002516 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002517 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002518 orig_flags = evalarg_used->eval_flags;
2519 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002520
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002521 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002522 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002523 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002524 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002525 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002526 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002527 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002528 clear_tv(rettv);
2529 return FAIL;
2530 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002531 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002532 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002533
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002535 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002537 int error = FALSE;
2538
Bram Moolenaar13106602020-10-04 16:06:05 +02002539 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002540 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002541 else if (vim9script)
2542 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002543 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002545 if (error || !op_falsy || !result)
2546 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002547 if (error)
2548 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 }
2550
2551 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002552 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002554 if (op_falsy)
2555 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002556 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002557 {
mityu4ac198c2021-05-28 17:52:40 +02002558 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002559 clear_tv(rettv);
2560 return FAIL;
2561 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002562 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002563 evalarg_used->eval_flags = (op_falsy ? !result : result)
2564 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2565 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002566 {
2567 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002569 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002570 if (!op_falsy || !result)
2571 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002573 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002575 /*
2576 * Check for the ":".
2577 */
2578 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2579 if (*p != ':')
2580 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002581 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002582 if (evaluate && result)
2583 clear_tv(rettv);
2584 evalarg_used->eval_flags = orig_flags;
2585 return FAIL;
2586 }
2587 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002588 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002589 else
2590 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002591 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002592 {
2593 error_white_both(p, 1);
2594 clear_tv(rettv);
2595 evalarg_used->eval_flags = orig_flags;
2596 return FAIL;
2597 }
2598 *arg = p;
2599 }
2600
2601 /*
2602 * Get the third variable. Recursive!
2603 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002604 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002605 {
mityu4ac198c2021-05-28 17:52:40 +02002606 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002607 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002608 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002609 return FAIL;
2610 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002611 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2612 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002613 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002614 if (eval1(arg, &var2, evalarg_used) == FAIL)
2615 {
2616 if (evaluate && result)
2617 clear_tv(rettv);
2618 evalarg_used->eval_flags = orig_flags;
2619 return FAIL;
2620 }
2621 if (evaluate && !result)
2622 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002624
2625 if (evalarg == NULL)
2626 clear_evalarg(&local_evalarg, NULL);
2627 else
2628 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 }
2630
2631 return OK;
2632}
2633
2634/*
2635 * Handle first level expression:
2636 * expr2 || expr2 || expr2 logical OR
2637 *
2638 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002639 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 *
2641 * Return OK or FAIL.
2642 */
2643 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002644eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002646 char_u *p;
2647 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648
2649 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002650 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002652 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 return FAIL;
2654
2655 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002656 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002658 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002659 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002661 evalarg_T *evalarg_used = evalarg;
2662 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002663 int evaluate;
2664 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002665 long result = FALSE;
2666 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002667 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002668 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002669
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002670 if (evalarg == NULL)
2671 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002672 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002673 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002674 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002675 orig_flags = evalarg_used->eval_flags;
2676 evaluate = orig_flags & EVAL_EVALUATE;
2677 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002678 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002679 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002680 result = tv_get_bool_chk(rettv, &error);
2681 else if (tv_get_number_chk(rettv, &error) != 0)
2682 result = TRUE;
2683 clear_tv(rettv);
2684 if (error)
2685 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 }
2687
2688 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002689 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002691 while (p[0] == '|' && p[1] == '|')
2692 {
2693 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002694 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002695 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002696 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002697 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002698 {
2699 error_white_both(p, 2);
2700 clear_tv(rettv);
2701 return FAIL;
2702 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002703 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002704 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002705
2706 /*
2707 * Get the second variable.
2708 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002709 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002710 {
mityu4ac198c2021-05-28 17:52:40 +02002711 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002712 clear_tv(rettv);
2713 return FAIL;
2714 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002715 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2716 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002717 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002718 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002719 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002720
2721 /*
2722 * Compute the result.
2723 */
2724 if (evaluate && !result)
2725 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002726 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002727 result = tv_get_bool_chk(&var2, &error);
2728 else if (tv_get_number_chk(&var2, &error) != 0)
2729 result = TRUE;
2730 clear_tv(&var2);
2731 if (error)
2732 return FAIL;
2733 }
2734 if (evaluate)
2735 {
2736 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002737 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002738 rettv->v_type = VAR_BOOL;
2739 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002740 }
2741 else
2742 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002743 rettv->v_type = VAR_NUMBER;
2744 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002745 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002746 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002747
2748 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002750
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002751 if (evalarg == NULL)
2752 clear_evalarg(&local_evalarg, NULL);
2753 else
2754 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 }
2756
2757 return OK;
2758}
2759
2760/*
2761 * Handle second level expression:
2762 * expr3 && expr3 && expr3 logical AND
2763 *
2764 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002765 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 *
2767 * Return OK or FAIL.
2768 */
2769 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002770eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002772 char_u *p;
2773 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774
2775 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002776 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002778 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 return FAIL;
2780
2781 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002782 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002784 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002785 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002787 evalarg_T *evalarg_used = evalarg;
2788 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002789 int orig_flags;
2790 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002791 long result = TRUE;
2792 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002793 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002794 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002795
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002796 if (evalarg == NULL)
2797 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002798 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002799 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002800 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002801 orig_flags = evalarg_used->eval_flags;
2802 evaluate = orig_flags & EVAL_EVALUATE;
2803 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002804 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002805 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002806 result = tv_get_bool_chk(rettv, &error);
2807 else if (tv_get_number_chk(rettv, &error) == 0)
2808 result = FALSE;
2809 clear_tv(rettv);
2810 if (error)
2811 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 }
2813
2814 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002815 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002817 while (p[0] == '&' && p[1] == '&')
2818 {
2819 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002820 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002821 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002822 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002823 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002824 {
2825 error_white_both(p, 2);
2826 clear_tv(rettv);
2827 return FAIL;
2828 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002829 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002830 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002831
2832 /*
2833 * Get the second variable.
2834 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002835 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002836 {
mityu4ac198c2021-05-28 17:52:40 +02002837 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002838 clear_tv(rettv);
2839 return FAIL;
2840 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002841 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2842 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002843 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002844 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002845 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002846 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002847
2848 /*
2849 * Compute the result.
2850 */
2851 if (evaluate && result)
2852 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002853 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002854 result = tv_get_bool_chk(&var2, &error);
2855 else if (tv_get_number_chk(&var2, &error) == 0)
2856 result = FALSE;
2857 clear_tv(&var2);
2858 if (error)
2859 return FAIL;
2860 }
2861 if (evaluate)
2862 {
2863 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002864 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002865 rettv->v_type = VAR_BOOL;
2866 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002867 }
2868 else
2869 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002870 rettv->v_type = VAR_NUMBER;
2871 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002872 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002873 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002874
2875 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002877
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002878 if (evalarg == NULL)
2879 clear_evalarg(&local_evalarg, NULL);
2880 else
2881 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 }
2883
2884 return OK;
2885}
2886
2887/*
2888 * Handle third level expression:
2889 * var1 == var2
2890 * var1 =~ var2
2891 * var1 != var2
2892 * var1 !~ var2
2893 * var1 > var2
2894 * var1 >= var2
2895 * var1 < var2
2896 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002897 * var1 is var2
2898 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 *
2900 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002901 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 *
2903 * Return OK or FAIL.
2904 */
2905 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002906eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002909 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002910 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002912 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913
2914 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002915 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002917 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 return FAIL;
2919
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002920 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002921
Bram Moolenaar696ba232020-07-29 21:20:41 +02002922 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923
2924 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002925 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002927 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002929 typval_T var2;
2930 int ic;
2931 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002932 int evaluate = evalarg == NULL
2933 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002934 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002935
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002936 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002937 {
Bram Moolenaare442d592022-05-05 12:20:28 +01002938 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002939 p = *arg;
2940 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002941 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2942 {
mityu4ac198c2021-05-28 17:52:40 +02002943 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002944 clear_tv(rettv);
2945 return FAIL;
2946 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002947
Bram Moolenaar696ba232020-07-29 21:20:41 +02002948 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2949 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002950 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002951 clear_tv(rettv);
2952 return FAIL;
2953 }
2954
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002955 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 if (p[len] == '?')
2957 {
2958 ic = TRUE;
2959 ++len;
2960 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002961 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 else if (p[len] == '#')
2963 {
2964 ic = FALSE;
2965 ++len;
2966 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002967 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002969 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970
2971 /*
2972 * Get the second variable.
2973 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002974 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2975 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002976 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002977 clear_tv(rettv);
2978 return FAIL;
2979 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002980 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002981 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002983 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 return FAIL;
2985 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002986 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002987 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002988 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002989
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002990 // use the line of the comparison for messages
2991 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002992 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002993 {
2994 ret = FAIL;
2995 clear_tv(rettv);
2996 }
2997 else
2998 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002999 clear_tv(&var2);
3000 return ret;
3001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 }
3003
3004 return OK;
3005}
3006
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003007/*
3008 * Make a copy of blob "tv1" and append blob "tv2".
3009 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 void
3011eval_addblob(typval_T *tv1, typval_T *tv2)
3012{
3013 blob_T *b1 = tv1->vval.v_blob;
3014 blob_T *b2 = tv2->vval.v_blob;
3015 blob_T *b = blob_alloc();
3016 int i;
3017
3018 if (b != NULL)
3019 {
3020 for (i = 0; i < blob_len(b1); i++)
3021 ga_append(&b->bv_ga, blob_get(b1, i));
3022 for (i = 0; i < blob_len(b2); i++)
3023 ga_append(&b->bv_ga, blob_get(b2, i));
3024
3025 clear_tv(tv1);
3026 rettv_blob_set(tv1, b);
3027 }
3028}
3029
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003030/*
3031 * Make a copy of list "tv1" and append list "tv2".
3032 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 int
3034eval_addlist(typval_T *tv1, typval_T *tv2)
3035{
3036 typval_T var3;
3037
3038 // concatenate Lists
3039 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3040 {
3041 clear_tv(tv1);
3042 clear_tv(tv2);
3043 return FAIL;
3044 }
3045 clear_tv(tv1);
3046 *tv1 = var3;
3047 return OK;
3048}
3049
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003051 * Handle the bitwise left/right shift operator expression:
3052 * var1 << var2
3053 * var1 >> var2
3054 *
3055 * "arg" must point to the first non-white of the expression.
3056 * "arg" is advanced to just after the recognized expression.
3057 *
3058 * Return OK or FAIL.
3059 */
3060 static int
3061eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3062{
3063 /*
3064 * Get the first expression.
3065 */
3066 if (eval6(arg, rettv, evalarg) == FAIL)
3067 return FAIL;
3068
3069 /*
3070 * Repeat computing, until no '<<' or '>>' is following.
3071 */
3072 for (;;)
3073 {
3074 char_u *p;
3075 int getnext;
3076 exprtype_T type;
3077 int evaluate;
3078 typval_T var2;
3079 int vim9script;
3080
3081 p = eval_next_non_blank(*arg, evalarg, &getnext);
3082 if (p[0] == '<' && p[1] == '<')
3083 type = EXPR_LSHIFT;
3084 else if (p[0] == '>' && p[1] == '>')
3085 type = EXPR_RSHIFT;
3086 else
3087 return OK;
3088
3089 // Handle a bitwise left or right shift operator
3090 if (rettv->v_type != VAR_NUMBER)
3091 {
3092 // left operand should be a number
3093 emsg(_(e_bitshift_ops_must_be_number));
3094 clear_tv(rettv);
3095 return FAIL;
3096 }
3097
3098 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3099 vim9script = in_vim9script();
3100 if (getnext)
3101 {
3102 *arg = eval_next_line(*arg, evalarg);
3103 p = *arg;
3104 }
3105 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3106 {
3107 error_white_both(*arg, 2);
3108 clear_tv(rettv);
3109 return FAIL;
3110 }
3111
3112 /*
3113 * Get the second variable.
3114 */
3115 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3116 {
3117 error_white_both(p, 2);
3118 clear_tv(rettv);
3119 return FAIL;
3120 }
3121 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3122 if (eval6(arg, &var2, evalarg) == FAIL)
3123 {
3124 clear_tv(rettv);
3125 return FAIL;
3126 }
3127
3128 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3129 {
3130 // right operand should be a positive number
3131 if (var2.v_type != VAR_NUMBER)
3132 emsg(_(e_bitshift_ops_must_be_number));
3133 else
3134 emsg(_(e_bitshift_ops_must_be_postive));
3135 clear_tv(rettv);
3136 clear_tv(&var2);
3137 return FAIL;
3138 }
3139
3140 if (evaluate)
3141 {
3142 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3143 // shifting more bits than we have always results in zero
3144 rettv->vval.v_number = 0;
3145 else if (type == EXPR_LSHIFT)
3146 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003147 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003148 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003149 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003150 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003151 }
3152
3153 clear_tv(&var2);
3154 }
3155
3156 return OK;
3157}
3158
3159/*
3160 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003161 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003163 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003164 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 *
3166 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003167 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 *
3169 * Return OK or FAIL.
3170 */
3171 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003172eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003175 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003177 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 return FAIL;
3179
3180 /*
3181 * Repeat computing, until no '+', '-' or '.' is following.
3182 */
3183 for (;;)
3184 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003185 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003186 int getnext;
3187 char_u *p;
3188 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003189 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003190 int concat;
3191 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003192 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003193
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003194 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003195 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003196 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003197 p = eval_next_non_blank(*arg, evalarg, &getnext);
3198 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003199 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003200 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3201 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003203 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3204 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003205
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003206 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003207 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003208 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003209 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003210 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003211 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003212 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003213 {
mityu4ac198c2021-05-28 17:52:40 +02003214 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003215 clear_tv(rettv);
3216 return FAIL;
3217 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003218 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003219 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003220 if ((op != '+' || (rettv->v_type != VAR_LIST
3221 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003222#ifdef FEAT_FLOAT
3223 && (op == '.' || rettv->v_type != VAR_FLOAT)
3224#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003225 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003226 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003227 int error = FALSE;
3228
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003229 // For "list + ...", an illegal use of the first operand as
3230 // a number cannot be determined before evaluating the 2nd
3231 // operand: if this is also a list, all is ok.
3232 // For "something . ...", "something - ..." or "non-list + ...",
3233 // we know that the first operand needs to be a string or number
3234 // without evaluating the 2nd operand. So check before to avoid
3235 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003236 if (op != '.')
3237 tv_get_number_chk(rettv, &error);
3238 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003239 {
3240 clear_tv(rettv);
3241 return FAIL;
3242 }
3243 }
3244
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 /*
3246 * Get the second variable.
3247 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003248 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003249 {
mityu89dcb4d2021-05-28 13:50:17 +02003250 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003251 clear_tv(rettv);
3252 return FAIL;
3253 }
3254 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003255 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003257 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 return FAIL;
3259 }
3260
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003261 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 {
3263 /*
3264 * Compute the result.
3265 */
3266 if (op == '.')
3267 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003268 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3269 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003270 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003271
Bram Moolenaar2e086612020-08-25 22:37:48 +02003272 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003273 || var2.v_type == VAR_CHANNEL
3274 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003275 semsg(_(e_using_invalid_value_as_string_str),
3276 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003277#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003278 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003279 {
3280 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3281 var2.vval.v_float);
3282 s2 = buf2;
3283 }
3284#endif
3285 else
3286 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003287 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003288 {
3289 clear_tv(rettv);
3290 clear_tv(&var2);
3291 return FAIL;
3292 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003293 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003294 clear_tv(rettv);
3295 rettv->v_type = VAR_STRING;
3296 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003298 else if (op == '+' && rettv->v_type == VAR_BLOB
3299 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003301 else if (op == '+' && rettv->v_type == VAR_LIST
3302 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003303 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003304 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003305 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 else
3308 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003309 int error = FALSE;
3310 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003311#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003312 float_T f1 = 0, f2 = 0;
3313
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003314 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003315 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003316 f1 = rettv->vval.v_float;
3317 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003318 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003319 else
3320#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003321 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003322 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003323 if (error)
3324 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003325 // This can only happen for "list + non-list" or
3326 // "blob + non-blob". For "non-list + ..." or
3327 // "something - ...", we returned before evaluating the
3328 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003329 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003330 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003331 return FAIL;
3332 }
3333#ifdef FEAT_FLOAT
3334 if (var2.v_type == VAR_FLOAT)
3335 f1 = n1;
3336#endif
3337 }
3338#ifdef FEAT_FLOAT
3339 if (var2.v_type == VAR_FLOAT)
3340 {
3341 f2 = var2.vval.v_float;
3342 n2 = 0;
3343 }
3344 else
3345#endif
3346 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003347 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003348 if (error)
3349 {
3350 clear_tv(rettv);
3351 clear_tv(&var2);
3352 return FAIL;
3353 }
3354#ifdef FEAT_FLOAT
3355 if (rettv->v_type == VAR_FLOAT)
3356 f2 = n2;
3357#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003358 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003359 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003360
3361#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003362 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003363 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3364 {
3365 if (op == '+')
3366 f1 = f1 + f2;
3367 else
3368 f1 = f1 - f2;
3369 rettv->v_type = VAR_FLOAT;
3370 rettv->vval.v_float = f1;
3371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003373#endif
3374 {
3375 if (op == '+')
3376 n1 = n1 + n2;
3377 else
3378 n1 = n1 - n2;
3379 rettv->v_type = VAR_NUMBER;
3380 rettv->vval.v_number = n1;
3381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003383 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 }
3385 }
3386 return OK;
3387}
3388
3389/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003390 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 * * number multiplication
3392 * / number division
3393 * % number modulo
3394 *
3395 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003396 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 *
3398 * Return OK or FAIL.
3399 */
3400 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003401eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003402 char_u **arg,
3403 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003404 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003405 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003407#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003408 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410
3411 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003412 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003414 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 return FAIL;
3416
3417 /*
3418 * Repeat computing, until no '*', '/' or '%' is following.
3419 */
3420 for (;;)
3421 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003422 int evaluate;
3423 int getnext;
3424 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003425 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003426 int op;
3427 varnumber_T n1, n2;
3428#ifdef FEAT_FLOAT
3429 float_T f1, f2;
3430#endif
3431 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003432
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003433 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003434 p = eval_next_non_blank(*arg, evalarg, &getnext);
3435 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003436 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003438
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003439 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003440 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003441 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003442 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003443 {
3444 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3445 {
mityu4ac198c2021-05-28 17:52:40 +02003446 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003447 clear_tv(rettv);
3448 return FAIL;
3449 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003450 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003453#ifdef FEAT_FLOAT
3454 f1 = 0;
3455 f2 = 0;
3456#endif
3457 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003458 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003460#ifdef FEAT_FLOAT
3461 if (rettv->v_type == VAR_FLOAT)
3462 {
3463 f1 = rettv->vval.v_float;
3464 use_float = TRUE;
3465 n1 = 0;
3466 }
3467 else
3468#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003469 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003470 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003471 if (error)
3472 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 }
3474 else
3475 n1 = 0;
3476
3477 /*
3478 * Get the second variable.
3479 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003480 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3481 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003482 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003483 clear_tv(rettv);
3484 return FAIL;
3485 }
3486 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003487 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 return FAIL;
3489
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003490 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003492#ifdef FEAT_FLOAT
3493 if (var2.v_type == VAR_FLOAT)
3494 {
3495 if (!use_float)
3496 {
3497 f1 = n1;
3498 use_float = TRUE;
3499 }
3500 f2 = var2.vval.v_float;
3501 n2 = 0;
3502 }
3503 else
3504#endif
3505 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003506 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003507 clear_tv(&var2);
3508 if (error)
3509 return FAIL;
3510#ifdef FEAT_FLOAT
3511 if (use_float)
3512 f2 = n2;
3513#endif
3514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515
3516 /*
3517 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003518 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003520#ifdef FEAT_FLOAT
3521 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003523 if (op == '*')
3524 f1 = f1 * f2;
3525 else if (op == '/')
3526 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003527# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003528 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003529 if (f2 == 0.0)
3530 {
3531 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003532 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003533 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003534 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003535 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003536 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003537 }
3538 else
3539 f1 = f1 / f2;
3540# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003541 // We rely on the floating point library to handle divide
3542 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003543 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003544# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003547 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003548 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003549 return FAIL;
3550 }
3551 rettv->v_type = VAR_FLOAT;
3552 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 }
3554 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003557 int failed = FALSE;
3558
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003559 if (op == '*')
3560 n1 = n1 * n2;
3561 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003562 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003564 n1 = num_modulus(n1, n2, &failed);
3565 if (failed)
3566 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003567
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003568 rettv->v_type = VAR_NUMBER;
3569 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 }
3572 }
3573
3574 return OK;
3575}
3576
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003577/*
3578 * Handle a type cast before a base level expression.
3579 * "arg" must point to the first non-white of the expression.
3580 * "arg" is advanced to just after the recognized expression.
3581 * Return OK or FAIL.
3582 */
3583 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003584eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003585 char_u **arg,
3586 typval_T *rettv,
3587 evalarg_T *evalarg,
3588 int want_string) // after "." operator
3589{
3590 type_T *want_type = NULL;
3591 garray_T type_list; // list of pointers to allocated types
3592 int res;
3593 int evaluate = evalarg == NULL ? 0
3594 : (evalarg->eval_flags & EVAL_EVALUATE);
3595
3596 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003597 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3598 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003599 {
3600 ++*arg;
3601 ga_init2(&type_list, sizeof(type_T *), 10);
3602 want_type = parse_type(arg, &type_list, TRUE);
3603 if (want_type == NULL && (evaluate || **arg != '>'))
3604 {
3605 clear_type_list(&type_list);
3606 return FAIL;
3607 }
3608
3609 if (**arg != '>')
3610 {
3611 if (*skipwhite(*arg) == '>')
3612 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3613 else
3614 emsg(_(e_missing_gt));
3615 clear_type_list(&type_list);
3616 return FAIL;
3617 }
3618 ++*arg;
3619 *arg = skipwhite_and_linebreak(*arg, evalarg);
3620 }
3621
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003622 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003623
3624 if (want_type != NULL && evaluate)
3625 {
3626 if (res == OK)
3627 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003628 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3629 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003630
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003631 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003632 {
3633 if (want_type == &t_bool && actual != &t_bool
3634 && (actual->tt_flags & TTFLAG_BOOL_OK))
3635 {
3636 int n = tv2bool(rettv);
3637
3638 // can use "0" and "1" for boolean in some places
3639 clear_tv(rettv);
3640 rettv->v_type = VAR_BOOL;
3641 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3642 }
3643 else
3644 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003645 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003646
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003647 where.wt_variable = TRUE;
3648 res = check_type(want_type, actual, TRUE, where);
3649 }
3650 }
3651 }
3652 clear_type_list(&type_list);
3653 }
3654
3655 return res;
3656}
3657
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003658 int
3659eval_leader(char_u **arg, int vim9)
3660{
3661 char_u *s = *arg;
3662 char_u *p = *arg;
3663
3664 while (*p == '!' || *p == '-' || *p == '+')
3665 {
3666 char_u *n = skipwhite(p + 1);
3667
3668 // ++, --, -+ and +- are not accepted in Vim9 script
3669 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3670 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003671 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003672 return FAIL;
3673 }
3674 p = n;
3675 }
3676 *arg = p;
3677 return OK;
3678}
3679
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003681 * Check for a predefined value "true", "false" and "null.*".
3682 * Return OK when recognized.
3683 */
3684 int
3685handle_predefined(char_u *s, int len, typval_T *rettv)
3686{
3687 switch (len)
3688 {
3689 case 4: if (STRNCMP(s, "true", 4) == 0)
3690 {
3691 rettv->v_type = VAR_BOOL;
3692 rettv->vval.v_number = VVAL_TRUE;
3693 return OK;
3694 }
3695 if (STRNCMP(s, "null", 4) == 0)
3696 {
3697 rettv->v_type = VAR_SPECIAL;
3698 rettv->vval.v_number = VVAL_NULL;
3699 return OK;
3700 }
3701 break;
3702 case 5: if (STRNCMP(s, "false", 5) == 0)
3703 {
3704 rettv->v_type = VAR_BOOL;
3705 rettv->vval.v_number = VVAL_FALSE;
3706 return OK;
3707 }
3708 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003709 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3710 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003711#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003712 rettv->v_type = VAR_JOB;
3713 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003714#else
3715 rettv->v_type = VAR_SPECIAL;
3716 rettv->vval.v_number = VVAL_NULL;
3717#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003718 return OK;
3719 }
3720 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003721 case 9:
3722 if (STRNCMP(s, "null_", 5) != 0)
3723 break;
3724 if (STRNCMP(s + 5, "list", 4) == 0)
3725 {
3726 rettv->v_type = VAR_LIST;
3727 rettv->vval.v_list = NULL;
3728 return OK;
3729 }
3730 if (STRNCMP(s + 5, "dict", 4) == 0)
3731 {
3732 rettv->v_type = VAR_DICT;
3733 rettv->vval.v_dict = NULL;
3734 return OK;
3735 }
3736 if (STRNCMP(s + 5, "blob", 4) == 0)
3737 {
3738 rettv->v_type = VAR_BLOB;
3739 rettv->vval.v_blob = NULL;
3740 return OK;
3741 }
3742 break;
3743 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3744 {
3745 rettv->v_type = VAR_STRING;
3746 rettv->vval.v_string = NULL;
3747 return OK;
3748 }
3749 break;
3750 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003751 if (STRNCMP(s, "null_channel", 12) == 0)
3752 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003753#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003754 rettv->v_type = VAR_CHANNEL;
3755 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003756#else
3757 rettv->v_type = VAR_SPECIAL;
3758 rettv->vval.v_number = VVAL_NULL;
3759#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003760 return OK;
3761 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003762 if (STRNCMP(s, "null_partial", 12) == 0)
3763 {
3764 rettv->v_type = VAR_PARTIAL;
3765 rettv->vval.v_partial = NULL;
3766 return OK;
3767 }
3768 break;
3769 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3770 {
3771 rettv->v_type = VAR_FUNC;
3772 rettv->vval.v_string = NULL;
3773 return OK;
3774 }
3775 break;
3776 }
3777 return FAIL;
3778}
3779
3780/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 * Handle sixth level expression:
3782 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003783 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003784 * "string" string constant
3785 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 * &option-name option value
3787 * @r register contents
3788 * identifier variable value
3789 * function() function call
3790 * $VAR environment variable
3791 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003792 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003793 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003794 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003795 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 *
3797 * Also handle:
3798 * ! in front logical NOT
3799 * - in front unary minus
3800 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003801 * trailing [] subscript in String or List
3802 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003803 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 *
3805 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003806 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 *
3808 * Return OK or FAIL.
3809 */
3810 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003811eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003812 char_u **arg,
3813 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003814 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003817 int evaluate = evalarg != NULL
3818 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 int len;
3820 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003821 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 char_u *start_leader, *end_leader;
3823 int ret = OK;
3824 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003825 static int recurse = 0;
3826 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827
3828 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003829 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003830 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003832 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833
3834 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003835 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 */
3837 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003838 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003839 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 end_leader = *arg;
3841
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003842 if (**arg == '.' && (!isdigit(*(*arg + 1))
3843#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003844 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003845#endif
3846 ))
3847 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003848 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003849 ++*arg;
3850 return FAIL;
3851 }
3852
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003853 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003854 // and crash. With MSVC the stack is smaller.
3855 if (recurse ==
3856#ifdef _MSC_VER
3857 300
3858#else
3859 1000
3860#endif
3861 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003862 {
3863 semsg(_(e_expression_too_recursive_str), *arg);
3864 return FAIL;
3865 }
3866 ++recurse;
3867
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 switch (**arg)
3869 {
3870 /*
3871 * Number constant.
3872 */
3873 case '0':
3874 case '1':
3875 case '2':
3876 case '3':
3877 case '4':
3878 case '5':
3879 case '6':
3880 case '7':
3881 case '8':
3882 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003883 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003884
3885 // Apply prefixed "-" and "+" now. Matters especially when
3886 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003887 if (ret == OK && evaluate && end_leader > start_leader
3888 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003889 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 break;
3891
3892 /*
3893 * String constant: "string".
3894 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003895 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 break;
3897
3898 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003899 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003901 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003902 break;
3903
3904 /*
3905 * List: [expr, expr]
3906 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003907 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 break;
3909
3910 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003911 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003912 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003913 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003914 {
3915 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3916 }
3917 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003918 {
3919 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003920 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003921 }
3922 else
3923 ret = NOTDONE;
3924 break;
3925
3926 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003927 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003928 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003929 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003930 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003931 ret = NOTDONE;
3932 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00003933 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003934 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003935 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003936 break;
3937
3938 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003939 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003941 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 break;
3943
3944 /*
3945 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01003946 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 */
LemonBoy2eaef102022-05-06 13:14:50 +01003948 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
3949 ret = eval_interp_string(arg, rettv, evaluate);
3950 else
3951 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 break;
3953
3954 /*
3955 * Register contents: @r.
3956 */
3957 case '@': ++*arg;
3958 if (evaluate)
3959 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003960 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003961 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003962 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003963 emsg_invreg(**arg);
3964 else
3965 {
3966 rettv->v_type = VAR_STRING;
3967 rettv->vval.v_string = get_reg_contents(**arg,
3968 GREG_EXPR_SRC);
3969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 }
3971 if (**arg != NUL)
3972 ++*arg;
3973 break;
3974
3975 /*
3976 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003977 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003979 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003980 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01003981 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003982 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003983 if (ret == OK && evaluate)
3984 {
3985 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3986
Bram Moolenaara9931532021-06-12 15:58:16 +02003987 // Compile it here to get the return type. The return
3988 // type is optional, when it's missing use t_unknown.
3989 // This is recognized in compile_return().
3990 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3991 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00003992 if (compile_def_function(ufunc, FALSE,
3993 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003994 {
3995 clear_tv(rettv);
3996 ret = FAIL;
3997 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003998 }
3999 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004000 if (ret == NOTDONE)
4001 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004002 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004003 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004004
Bram Moolenaar9215f012020-06-27 21:18:00 +02004005 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004006 if (**arg == ')')
4007 ++*arg;
4008 else if (ret == OK)
4009 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004010 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004011 clear_tv(rettv);
4012 ret = FAIL;
4013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 }
4015 break;
4016
Bram Moolenaar8c711452005-01-14 21:53:12 +00004017 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 break;
4019 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004020
4021 if (ret == NOTDONE)
4022 {
4023 /*
4024 * Must be a variable or function name.
4025 * Can also be a curly-braces kind of name: {expr}.
4026 */
4027 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004028 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004029 if (alias != NULL)
4030 s = alias;
4031
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004032 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004033 ret = FAIL;
4034 else
4035 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004036 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4037
Bram Moolenaar4525a572022-02-13 11:57:33 +00004038 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004039 {
4040 emsg(_(e_cannot_use_underscore_here));
4041 ret = FAIL;
4042 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004043 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004044 && s[0] == 's' && s[1] == ':')
4045 {
4046 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4047 ret = FAIL;
4048 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004049 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004050 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004051 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004052 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004053 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004054 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02004055 else if (flags & EVAL_CONSTANT)
4056 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004057 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004058 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004059 // get the value of "true", "false", etc. or a variable
4060 ret = FAIL;
4061 if (vim9script)
4062 ret = handle_predefined(s, len, rettv);
4063 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004064 {
4065 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004066 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004067 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004068 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004069 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004070 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004071 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004072 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004073 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004074 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004075 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004076 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004077 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004078 }
4079
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004080 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4081 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004082 if (ret == OK)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004083 ret = handle_subscript(arg, name_start, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084
4085 /*
4086 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4087 */
4088 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004089 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004090
4091 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004092 return ret;
4093}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004094
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004095/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004096 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004097 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004098 * Adjusts "end_leaderp" until it is at "start_leader".
4099 */
4100 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004101eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004102 typval_T *rettv,
4103 int numeric_only,
4104 char_u *start_leader,
4105 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004106{
4107 char_u *end_leader = *end_leaderp;
4108 int ret = OK;
4109 int error = FALSE;
4110 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004111 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004112 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004113#ifdef FEAT_FLOAT
4114 float_T f = 0.0;
4115
4116 if (rettv->v_type == VAR_FLOAT)
4117 f = rettv->vval.v_float;
4118 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004119#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004120 {
4121 while (VIM_ISWHITE(end_leader[-1]))
4122 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004123 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004124 val = tv2bool(rettv);
4125 else
4126 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004127 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004128 if (error)
4129 {
4130 clear_tv(rettv);
4131 ret = FAIL;
4132 }
4133 else
4134 {
4135 while (end_leader > start_leader)
4136 {
4137 --end_leader;
4138 if (*end_leader == '!')
4139 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004140 if (numeric_only)
4141 {
4142 ++end_leader;
4143 break;
4144 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004145#ifdef FEAT_FLOAT
4146 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004147 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004148 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004149 {
4150 rettv->v_type = VAR_BOOL;
4151 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4152 }
4153 else
4154 f = !f;
4155 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004156 else
4157#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004158 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004159 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004160 type = VAR_BOOL;
4161 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004162 }
4163 else if (*end_leader == '-')
4164 {
4165#ifdef FEAT_FLOAT
4166 if (rettv->v_type == VAR_FLOAT)
4167 f = -f;
4168 else
4169#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004170 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004171 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004172 type = VAR_NUMBER;
4173 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004174 }
4175 }
4176#ifdef FEAT_FLOAT
4177 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004179 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004180 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004182 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004183#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004184 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004185 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004186 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004187 rettv->v_type = type;
4188 else
4189 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004190 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004193 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 return ret;
4195}
4196
4197/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004198 * Call the function referred to in "rettv".
4199 */
4200 static int
4201call_func_rettv(
4202 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004203 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004204 typval_T *rettv,
4205 int evaluate,
4206 dict_T *selfdict,
4207 typval_T *basetv)
4208{
4209 partial_T *pt = NULL;
4210 funcexe_T funcexe;
4211 typval_T functv;
4212 char_u *s;
4213 int ret;
4214
4215 // need to copy the funcref so that we can clear rettv
4216 if (evaluate)
4217 {
4218 functv = *rettv;
4219 rettv->v_type = VAR_UNKNOWN;
4220
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004221 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004222 if (functv.v_type == VAR_PARTIAL)
4223 {
4224 pt = functv.vval.v_partial;
4225 s = partial_name(pt);
4226 }
4227 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004228 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004229 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004230 if (s == NULL || *s == NUL)
4231 {
4232 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004233 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004234 goto theend;
4235 }
4236 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004237 }
4238 else
4239 s = (char_u *)"";
4240
Bram Moolenaara80faa82020-04-12 19:37:17 +02004241 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004242 funcexe.fe_firstline = curwin->w_cursor.lnum;
4243 funcexe.fe_lastline = curwin->w_cursor.lnum;
4244 funcexe.fe_evaluate = evaluate;
4245 funcexe.fe_partial = pt;
4246 funcexe.fe_selfdict = selfdict;
4247 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004248 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004249
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004250theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004251 // Clear the funcref afterwards, so that deleting it while
4252 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004253 if (evaluate)
4254 clear_tv(&functv);
4255
4256 return ret;
4257}
4258
4259/*
4260 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004261 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004262 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4263 */
4264 static int
4265eval_lambda(
4266 char_u **arg,
4267 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004268 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004269 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004270{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004271 int evaluate = evalarg != NULL
4272 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004273 typval_T base = *rettv;
4274 int ret;
4275
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004276 rettv->v_type = VAR_UNKNOWN;
4277
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004278 if (**arg == '{')
4279 {
4280 // ->{lambda}()
4281 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4282 }
4283 else
4284 {
4285 // ->(lambda)()
4286 ++*arg;
4287 ret = eval1(arg, rettv, evalarg);
4288 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004289 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004290 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004291 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004292 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004293 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004294 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4295 && rettv->v_type != VAR_PARTIAL)
4296 {
4297 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4298 return FAIL;
4299 }
4300 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004301 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004302 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004303 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004304
4305 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004306 {
4307 if (verbose)
4308 {
4309 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004310 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004311 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004312 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004313 }
4314 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004315 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004316 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004317 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004318 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004319
4320 // Clear the funcref afterwards, so that deleting it while
4321 // evaluating the arguments is possible (see test55).
4322 if (evaluate)
4323 clear_tv(&base);
4324
4325 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004326}
4327
4328/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004329 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004330 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004331 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4332 */
4333 static int
4334eval_method(
4335 char_u **arg,
4336 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004337 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004338 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004339{
4340 char_u *name;
4341 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004342 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004343 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004344 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004345 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004346 int evaluate = evalarg != NULL
4347 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004348
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004349 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004350
Bram Moolenaarac92e252019-08-03 21:58:38 +02004351 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004352 len = get_name_len(arg, &alias, evaluate, TRUE);
4353 if (alias != NULL)
4354 name = alias;
4355
4356 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004357 {
4358 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004359 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004360 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004361 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004362 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004363 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004364 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004365
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004366 // If there is no "(" immediately following, but there is further on,
4367 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4368 // Does not handle anything where "(" is part of the expression.
4369 *arg = skipwhite(*arg);
4370
4371 if (**arg != '(' && alias == NULL
4372 && (paren = vim_strchr(*arg, '(')) != NULL)
4373 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004374 char_u *deref;
4375
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004376 *arg = name;
4377 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004378 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4379 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004380 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004381 *arg = name + len;
4382 ret = FAIL;
4383 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004384 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004385 {
4386 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004387 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004388 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004389 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004390 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004391
4392 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004393 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004394 *arg = skipwhite(*arg);
4395
4396 if (**arg != '(')
4397 {
4398 if (verbose)
4399 semsg(_(e_missing_parenthesis_str), name);
4400 ret = FAIL;
4401 }
4402 else if (VIM_ISWHITE((*arg)[-1]))
4403 {
4404 if (verbose)
4405 emsg(_(e_no_white_space_allowed_before_parenthesis));
4406 ret = FAIL;
4407 }
4408 else
4409 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004410 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004411 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004412 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004413
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004414 // Clear the funcref afterwards, so that deleting it while
4415 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004416 if (evaluate)
4417 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004418 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004419
Bram Moolenaarac92e252019-08-03 21:58:38 +02004420 return ret;
4421}
4422
4423/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004424 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4425 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004426 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4427 */
4428 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004429eval_index(
4430 char_u **arg,
4431 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004432 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004433 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004434{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004435 int evaluate = evalarg != NULL
4436 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004437 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004438 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004439 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004440 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004441 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004442 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004443
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004444 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4445 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004446
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004447 init_tv(&var1);
4448 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004449 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004450 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004451 /*
4452 * dict.name
4453 */
4454 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004455 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004456 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004457 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004458 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004459 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004460 }
4461 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004462 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004463 /*
4464 * something[idx]
4465 *
4466 * Get the (first) variable from inside the [].
4467 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004468 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004469 if (**arg == ':')
4470 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004471 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004472 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004473 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004474 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004475 semsg(_(e_white_space_required_before_and_after_str_at_str),
4476 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004477 clear_tv(&var1);
4478 return FAIL;
4479 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004480 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004481 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004482 int error = FALSE;
4483
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004484#ifdef FEAT_FLOAT
4485 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004486 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004487 && var1.v_type == VAR_FLOAT)
4488 {
4489 var1.vval.v_string = typval_tostring(&var1, TRUE);
4490 var1.v_type = VAR_STRING;
4491 }
4492#endif
Bram Moolenaar4525a572022-02-13 11:57:33 +00004493 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004494 tv_get_number_chk(&var1, &error);
4495 else
4496 error = tv_get_string_chk(&var1) == NULL;
4497 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004498 {
4499 // not a number or string
4500 clear_tv(&var1);
4501 return FAIL;
4502 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004503 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004504
4505 /*
4506 * Get the second variable from inside the [:].
4507 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004508 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004509 if (**arg == ':')
4510 {
4511 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004512 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004513 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004514 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004515 semsg(_(e_white_space_required_before_and_after_str_at_str),
4516 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004517 if (!empty1)
4518 clear_tv(&var1);
4519 return FAIL;
4520 }
4521 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004522 if (**arg == ']')
4523 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004524 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004525 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004526 if (!empty1)
4527 clear_tv(&var1);
4528 return FAIL;
4529 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004530 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004531 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004532 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004533 if (!empty1)
4534 clear_tv(&var1);
4535 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004536 return FAIL;
4537 }
4538 }
4539
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004540 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004541 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004542 if (**arg != ']')
4543 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004544 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004545 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004546 clear_tv(&var1);
4547 if (range)
4548 clear_tv(&var2);
4549 return FAIL;
4550 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004551 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004552 }
4553
4554 if (evaluate)
4555 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004556 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004557 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004558 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004559
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004560 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004561 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004562 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004563 clear_tv(&var2);
4564 return res;
4565 }
4566 return OK;
4567}
4568
4569/*
4570 * Check if "rettv" can have an [index] or [sli:ce]
4571 */
4572 int
4573check_can_index(typval_T *rettv, int evaluate, int verbose)
4574{
4575 switch (rettv->v_type)
4576 {
4577 case VAR_FUNC:
4578 case VAR_PARTIAL:
4579 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004580 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004581 return FAIL;
4582 case VAR_FLOAT:
4583#ifdef FEAT_FLOAT
4584 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004585 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004586 return FAIL;
4587#endif
4588 case VAR_BOOL:
4589 case VAR_SPECIAL:
4590 case VAR_JOB:
4591 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004592 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004593 if (verbose)
4594 emsg(_(e_cannot_index_special_variable));
4595 return FAIL;
4596 case VAR_UNKNOWN:
4597 case VAR_ANY:
4598 case VAR_VOID:
4599 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004600 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004601 emsg(_(e_cannot_index_special_variable));
4602 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004603 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004604 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004605
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004606 case VAR_STRING:
4607 case VAR_LIST:
4608 case VAR_DICT:
4609 case VAR_BLOB:
4610 break;
4611 case VAR_NUMBER:
4612 if (in_vim9script())
4613 emsg(_(e_cannot_index_number));
4614 break;
4615 }
4616 return OK;
4617}
4618
4619/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004620 * slice() function
4621 */
4622 void
4623f_slice(typval_T *argvars, typval_T *rettv)
4624{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004625 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004626 && ((argvars[0].v_type != VAR_STRING
4627 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004628 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004629 && check_for_list_arg(argvars, 0) == FAIL)
4630 || check_for_number_arg(argvars, 1) == FAIL
4631 || check_for_opt_number_arg(argvars, 2) == FAIL))
4632 return;
4633
Bram Moolenaar6601b622021-01-13 21:47:15 +01004634 if (check_can_index(argvars, TRUE, FALSE) == OK)
4635 {
4636 copy_tv(argvars, rettv);
4637 eval_index_inner(rettv, TRUE, argvars + 1,
4638 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4639 TRUE, NULL, 0, FALSE);
4640 }
4641}
4642
4643/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004644 * Apply index or range to "rettv".
4645 * "var1" is the first index, NULL for [:expr].
4646 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004647 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4648 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004649 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4650 */
4651 int
4652eval_index_inner(
4653 typval_T *rettv,
4654 int is_range,
4655 typval_T *var1,
4656 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004657 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004658 char_u *key,
4659 int keylen,
4660 int verbose)
4661{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004662 varnumber_T n1, n2 = 0;
4663 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004664
4665 n1 = 0;
4666 if (var1 != NULL && rettv->v_type != VAR_DICT)
4667 n1 = tv_get_number(var1);
4668
4669 if (is_range)
4670 {
4671 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004672 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004673 if (verbose)
4674 emsg(_(e_cannot_slice_dictionary));
4675 return FAIL;
4676 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004677 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004678 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004679 else
4680 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004681 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004682
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004683 switch (rettv->v_type)
4684 {
4685 case VAR_UNKNOWN:
4686 case VAR_ANY:
4687 case VAR_VOID:
4688 case VAR_FUNC:
4689 case VAR_PARTIAL:
4690 case VAR_FLOAT:
4691 case VAR_BOOL:
4692 case VAR_SPECIAL:
4693 case VAR_JOB:
4694 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004695 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004696 break; // not evaluating, skipping over subscript
4697
4698 case VAR_NUMBER:
4699 case VAR_STRING:
4700 {
4701 char_u *s = tv_get_string(rettv);
4702
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004703 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004704 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004705 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004706 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004707 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004708 else
4709 s = char_from_string(s, n1);
4710 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004711 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004712 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004713 // The resulting variable is a substring. If the indexes
4714 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004715 if (n1 < 0)
4716 {
4717 n1 = len + n1;
4718 if (n1 < 0)
4719 n1 = 0;
4720 }
4721 if (n2 < 0)
4722 n2 = len + n2;
4723 else if (n2 >= len)
4724 n2 = len;
4725 if (n1 >= len || n2 < 0 || n1 > n2)
4726 s = NULL;
4727 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004728 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004729 }
4730 else
4731 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004732 // The resulting variable is a string of a single
4733 // character. If the index is too big or negative the
4734 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004735 if (n1 >= len || n1 < 0)
4736 s = NULL;
4737 else
4738 s = vim_strnsave(s + n1, 1);
4739 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004740 clear_tv(rettv);
4741 rettv->v_type = VAR_STRING;
4742 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004743 }
4744 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004745
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004746 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004747 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4748 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004749 break;
4750
4751 case VAR_LIST:
4752 if (var1 == NULL)
4753 n1 = 0;
4754 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004755 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004756 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004757 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004758 return FAIL;
4759 break;
4760
4761 case VAR_DICT:
4762 {
4763 dictitem_T *item;
4764 typval_T tmp;
4765
4766 if (key == NULL)
4767 {
4768 key = tv_get_string_chk(var1);
4769 if (key == NULL)
4770 return FAIL;
4771 }
4772
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004773 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004774
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004775 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004776 {
4777 if (verbose)
4778 {
4779 if (keylen > 0)
4780 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004781 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004782 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004783 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004784 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004785
4786 copy_tv(&item->di_tv, &tmp);
4787 clear_tv(rettv);
4788 *rettv = tmp;
4789 }
4790 break;
4791 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004792 return OK;
4793}
4794
4795/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004796 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004797 */
4798 char_u *
4799partial_name(partial_T *pt)
4800{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004801 if (pt != NULL)
4802 {
4803 if (pt->pt_name != NULL)
4804 return pt->pt_name;
4805 if (pt->pt_func != NULL)
4806 return pt->pt_func->uf_name;
4807 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004808 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004809}
4810
Bram Moolenaarddecc252016-04-06 22:59:37 +02004811 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004812partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004813{
4814 int i;
4815
4816 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004817 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004818 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004819 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004820 if (pt->pt_name != NULL)
4821 {
4822 func_unref(pt->pt_name);
4823 vim_free(pt->pt_name);
4824 }
4825 else
4826 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004827
Bram Moolenaar54656012021-06-09 20:50:46 +02004828 // "out_up" is no longer used, decrement refcount on partial that owns it.
4829 partial_unref(pt->pt_outer.out_up_partial);
4830
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004831 // Using pt_outer from another partial.
4832 partial_unref(pt->pt_outer_partial);
4833
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004834 // Decrease the reference count for the context of a closure. If down
4835 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004836 if (pt->pt_funcstack != NULL)
4837 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004838 --pt->pt_funcstack->fs_refcount;
4839 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004840 }
4841
Bram Moolenaarddecc252016-04-06 22:59:37 +02004842 vim_free(pt);
4843}
4844
4845/*
4846 * Unreference a closure: decrement the reference count and free it when it
4847 * becomes zero.
4848 */
4849 void
4850partial_unref(partial_T *pt)
4851{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004852 if (pt != NULL)
4853 {
4854 if (--pt->pt_refcount <= 0)
4855 partial_free(pt);
4856
4857 // If the reference count goes down to one, the funcstack may be the
4858 // only reference and can be freed if no other partials reference it.
4859 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4860 funcstack_check_refcount(pt->pt_funcstack);
4861 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004862}
4863
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004864/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004865 * Return the next (unique) copy ID.
4866 * Used for serializing nested structures.
4867 */
4868 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004869get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004870{
4871 current_copyID += COPYID_INC;
4872 return current_copyID;
4873}
4874
4875/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004876 * Garbage collection for lists and dictionaries.
4877 *
4878 * We use reference counts to be able to free most items right away when they
4879 * are no longer used. But for composite items it's possible that it becomes
4880 * unused while the reference count is > 0: When there is a recursive
4881 * reference. Example:
4882 * :let l = [1, 2, 3]
4883 * :let d = {9: l}
4884 * :let l[1] = d
4885 *
4886 * Since this is quite unusual we handle this with garbage collection: every
4887 * once in a while find out which lists and dicts are not referenced from any
4888 * variable.
4889 *
4890 * Here is a good reference text about garbage collection (refers to Python
4891 * but it applies to all reference-counting mechanisms):
4892 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004893 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004894
4895/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004896 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004897 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004898 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004899 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004900 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004901garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004902{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004903 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004904 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004905 buf_T *buf;
4906 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004907 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004908 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004909
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004910 if (!testing)
4911 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004912 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004913 want_garbage_collect = FALSE;
4914 may_garbage_collect = FALSE;
4915 garbage_collect_at_exit = FALSE;
4916 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004917
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004918 // The execution stack can grow big, limit the size.
4919 if (exestack.ga_maxlen - exestack.ga_len > 500)
4920 {
4921 size_t new_len;
4922 char_u *pp;
4923 int n;
4924
4925 // Keep 150% of the current size, with a minimum of the growth size.
4926 n = exestack.ga_len / 2;
4927 if (n < exestack.ga_growsize)
4928 n = exestack.ga_growsize;
4929
4930 // Don't make it bigger though.
4931 if (exestack.ga_len + n < exestack.ga_maxlen)
4932 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004933 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004934 pp = vim_realloc(exestack.ga_data, new_len);
4935 if (pp == NULL)
4936 return FAIL;
4937 exestack.ga_maxlen = exestack.ga_len + n;
4938 exestack.ga_data = pp;
4939 }
4940 }
4941
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004942 // We advance by two because we add one for items referenced through
4943 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004944 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004945
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004946 /*
4947 * 1. Go through all accessible variables and mark all lists and dicts
4948 * with copyID.
4949 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004950
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004951 // Don't free variables in the previous_funccal list unless they are only
4952 // referenced through previous_funccal. This must be first, because if
4953 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004954 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004955
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004956 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004957 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004958
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004959 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004960 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004961 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4962 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004963
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004964 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004965 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004966 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4967 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004968 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004969 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4970 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004971#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004972 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004973 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4974 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004975 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004976 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004977 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4978 NULL, NULL);
4979#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004980
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004981 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004982 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004983 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4984 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004985 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004986 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004987
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004988 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004989 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004990
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004991 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004992 abort = abort || set_ref_in_functions(copyID);
4993
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004994 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004995 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004996
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004997 // funcstacks keep variables for closures
4998 abort = abort || set_ref_in_funcstacks(copyID);
4999
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005000 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005001 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005002
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005003 // callbacks in buffers
5004 abort = abort || set_ref_in_buffers(copyID);
5005
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005006 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5007 abort = abort || set_ref_in_insexpand_funcs(copyID);
5008
5009 // 'operatorfunc' callback
5010 abort = abort || set_ref_in_opfunc(copyID);
5011
5012 // 'tagfunc' callback
5013 abort = abort || set_ref_in_tagfunc(copyID);
5014
5015 // 'imactivatefunc' and 'imstatusfunc' callbacks
5016 abort = abort || set_ref_in_im_funcs(copyID);
5017
Bram Moolenaar1dced572012-04-05 16:54:08 +02005018#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005019 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005020#endif
5021
Bram Moolenaardb913952012-06-29 12:54:53 +02005022#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005023 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005024#endif
5025
5026#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005027 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005028#endif
5029
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005030#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005031 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005032 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005033#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005034#ifdef FEAT_NETBEANS_INTG
5035 abort = abort || set_ref_in_nb_channel(copyID);
5036#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005037
Bram Moolenaare3188e22016-05-31 21:13:04 +02005038#ifdef FEAT_TIMERS
5039 abort = abort || set_ref_in_timer(copyID);
5040#endif
5041
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005042#ifdef FEAT_QUICKFIX
5043 abort = abort || set_ref_in_quickfix(copyID);
5044#endif
5045
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005046#ifdef FEAT_TERMINAL
5047 abort = abort || set_ref_in_term(copyID);
5048#endif
5049
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005050#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005051 abort = abort || set_ref_in_popups(copyID);
5052#endif
5053
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005054 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005055 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005056 /*
5057 * 2. Free lists and dictionaries that are not referenced.
5058 */
5059 did_free = free_unref_items(copyID);
5060
5061 /*
5062 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005063 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005064 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005065 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005066 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005067 else if (p_verbose > 0)
5068 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005069 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005070 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005071
5072 return did_free;
5073}
5074
5075/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005076 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005077 */
5078 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005079free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005080{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005081 int did_free = FALSE;
5082
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005083 // Let all "free" functions know that we are here. This means no
5084 // dictionaries, lists, channels or jobs are to be freed, because we will
5085 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005086 in_free_unref_items = TRUE;
5087
5088 /*
5089 * PASS 1: free the contents of the items. We don't free the items
5090 * themselves yet, so that it is possible to decrement refcount counters
5091 */
5092
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005093 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005094 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005095
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005096 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005097 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005098
5099#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005100 // Go through the list of jobs and free items without the copyID. This
5101 // must happen before doing channels, because jobs refer to channels, but
5102 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005103 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5104
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005105 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005106 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5107#endif
5108
5109 /*
5110 * PASS 2: free the items themselves.
5111 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005112 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005113 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005114
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005115#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005116 // Go through the list of jobs and free items without the copyID. This
5117 // must happen before doing channels, because jobs refer to channels, but
5118 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005119 free_unused_jobs(copyID, COPYID_MASK);
5120
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005121 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005122 free_unused_channels(copyID, COPYID_MASK);
5123#endif
5124
5125 in_free_unref_items = FALSE;
5126
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005127 return did_free;
5128}
5129
5130/*
5131 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005132 * "list_stack" is used to add lists to be marked. Can be NULL.
5133 *
5134 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005135 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005136 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005137set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005138{
5139 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005140 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005141 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005142 hashtab_T *cur_ht;
5143 ht_stack_T *ht_stack = NULL;
5144 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005145
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005146 cur_ht = ht;
5147 for (;;)
5148 {
5149 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005150 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005151 // Mark each item in the hashtab. If the item contains a hashtab
5152 // it is added to ht_stack, if it contains a list it is added to
5153 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005154 todo = (int)cur_ht->ht_used;
5155 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5156 if (!HASHITEM_EMPTY(hi))
5157 {
5158 --todo;
5159 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5160 &ht_stack, list_stack);
5161 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005162 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005163
5164 if (ht_stack == NULL)
5165 break;
5166
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005167 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005168 cur_ht = ht_stack->ht;
5169 tempitem = ht_stack;
5170 ht_stack = ht_stack->prev;
5171 free(tempitem);
5172 }
5173
5174 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005175}
5176
Dominique Pelle748b3082022-01-08 12:41:16 +00005177#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5178 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005179/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005180 * Mark a dict and its items with "copyID".
5181 * Returns TRUE if setting references failed somehow.
5182 */
5183 int
5184set_ref_in_dict(dict_T *d, int copyID)
5185{
5186 if (d != NULL && d->dv_copyID != copyID)
5187 {
5188 d->dv_copyID = copyID;
5189 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5190 }
5191 return FALSE;
5192}
Dominique Pelle748b3082022-01-08 12:41:16 +00005193#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005194
5195/*
5196 * Mark a list and its items with "copyID".
5197 * Returns TRUE if setting references failed somehow.
5198 */
5199 int
5200set_ref_in_list(list_T *ll, int copyID)
5201{
5202 if (ll != NULL && ll->lv_copyID != copyID)
5203 {
5204 ll->lv_copyID = copyID;
5205 return set_ref_in_list_items(ll, copyID, NULL);
5206 }
5207 return FALSE;
5208}
5209
5210/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005211 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005212 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5213 *
5214 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005215 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005216 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005217set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005218{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005219 listitem_T *li;
5220 int abort = FALSE;
5221 list_T *cur_l;
5222 list_stack_T *list_stack = NULL;
5223 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005224
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005225 cur_l = l;
5226 for (;;)
5227 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005228 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005229 // Mark each item in the list. If the item contains a hashtab
5230 // it is added to ht_stack, if it contains a list it is added to
5231 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005232 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5233 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5234 ht_stack, &list_stack);
5235 if (list_stack == NULL)
5236 break;
5237
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005238 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005239 cur_l = list_stack->list;
5240 tempitem = list_stack;
5241 list_stack = list_stack->prev;
5242 free(tempitem);
5243 }
5244
5245 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005246}
5247
5248/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005249 * Mark the partial in callback 'cb' with "copyID".
5250 */
5251 int
5252set_ref_in_callback(callback_T *cb, int copyID)
5253{
5254 typval_T tv;
5255
5256 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5257 return FALSE;
5258
5259 tv.v_type = VAR_PARTIAL;
5260 tv.vval.v_partial = cb->cb_partial;
5261 return set_ref_in_item(&tv, copyID, NULL, NULL);
5262}
5263
5264/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005265 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005266 * "list_stack" is used to add lists to be marked. Can be NULL.
5267 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5268 *
5269 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005270 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005271 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005272set_ref_in_item(
5273 typval_T *tv,
5274 int copyID,
5275 ht_stack_T **ht_stack,
5276 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005277{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005278 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005279
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005280 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005281 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005282 dict_T *dd = tv->vval.v_dict;
5283
Bram Moolenaara03f2332016-02-06 18:09:59 +01005284 if (dd != NULL && dd->dv_copyID != copyID)
5285 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005286 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005287 dd->dv_copyID = copyID;
5288 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005289 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005290 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5291 }
5292 else
5293 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005294 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5295
Bram Moolenaara03f2332016-02-06 18:09:59 +01005296 if (newitem == NULL)
5297 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005298 else
5299 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005300 newitem->ht = &dd->dv_hashtab;
5301 newitem->prev = *ht_stack;
5302 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005303 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005304 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005305 }
5306 }
5307 else if (tv->v_type == VAR_LIST)
5308 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005309 list_T *ll = tv->vval.v_list;
5310
Bram Moolenaara03f2332016-02-06 18:09:59 +01005311 if (ll != NULL && ll->lv_copyID != copyID)
5312 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005313 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005314 ll->lv_copyID = copyID;
5315 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005316 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005317 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005318 }
5319 else
5320 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005321 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5322
Bram Moolenaara03f2332016-02-06 18:09:59 +01005323 if (newitem == NULL)
5324 abort = TRUE;
5325 else
5326 {
5327 newitem->list = ll;
5328 newitem->prev = *list_stack;
5329 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005330 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005331 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005332 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005333 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005334 else if (tv->v_type == VAR_FUNC)
5335 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005336 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005337 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005338 else if (tv->v_type == VAR_PARTIAL)
5339 {
5340 partial_T *pt = tv->vval.v_partial;
5341 int i;
5342
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005343 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005344 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005345 // Didn't see this partial yet.
5346 pt->pt_copyID = copyID;
5347
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005348 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005349
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005350 if (pt->pt_dict != NULL)
5351 {
5352 typval_T dtv;
5353
5354 dtv.v_type = VAR_DICT;
5355 dtv.vval.v_dict = pt->pt_dict;
5356 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5357 }
5358
5359 for (i = 0; i < pt->pt_argc; ++i)
5360 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5361 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005362 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005363 }
5364 }
5365#ifdef FEAT_JOB_CHANNEL
5366 else if (tv->v_type == VAR_JOB)
5367 {
5368 job_T *job = tv->vval.v_job;
5369 typval_T dtv;
5370
5371 if (job != NULL && job->jv_copyID != copyID)
5372 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005373 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005374 if (job->jv_channel != NULL)
5375 {
5376 dtv.v_type = VAR_CHANNEL;
5377 dtv.vval.v_channel = job->jv_channel;
5378 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5379 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005380 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005381 {
5382 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005383 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005384 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5385 }
5386 }
5387 }
5388 else if (tv->v_type == VAR_CHANNEL)
5389 {
5390 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005391 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005392 typval_T dtv;
5393 jsonq_T *jq;
5394 cbq_T *cq;
5395
5396 if (ch != NULL && ch->ch_copyID != copyID)
5397 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005398 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005399 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005400 {
5401 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5402 jq = jq->jq_next)
5403 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5404 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5405 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005406 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005407 {
5408 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005409 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005410 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5411 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005412 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005413 {
5414 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005415 dtv.vval.v_partial =
5416 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005417 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5418 }
5419 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005420 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005421 {
5422 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005423 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005424 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5425 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005426 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005427 {
5428 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005429 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005430 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5431 }
5432 }
5433 }
5434#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005435 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005436}
5437
Bram Moolenaar8c711452005-01-14 21:53:12 +00005438/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 * Return a string with the string representation of a variable.
5440 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005441 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005442 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005443 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005444 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005445 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005446 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5447 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005448 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005449 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005450 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005451echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005452 typval_T *tv,
5453 char_u **tofree,
5454 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005455 int copyID,
5456 int echo_style,
5457 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005458 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005460 static int recurse = 0;
5461 char_u *r = NULL;
5462
Bram Moolenaar33570922005-01-25 22:26:29 +00005463 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005464 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005465 if (!did_echo_string_emsg)
5466 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005467 // Only give this message once for a recursive call to avoid
5468 // flooding the user with errors. And stop iterating over lists
5469 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005470 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005471 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005472 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005473 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005474 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005475 }
5476 ++recurse;
5477
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005478 switch (tv->v_type)
5479 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005480 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005481 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005482 {
5483 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005484 r = tv->vval.v_string;
5485 if (r == NULL)
5486 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005487 }
5488 else
5489 {
5490 *tofree = string_quote(tv->vval.v_string, FALSE);
5491 r = *tofree;
5492 }
5493 break;
5494
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005495 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005496 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005497 char_u buf[MAX_FUNC_NAME_LEN];
5498
5499 if (echo_style)
5500 {
LemonBoya5d35902022-04-29 21:15:02 +01005501 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5502 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005503 buf, MAX_FUNC_NAME_LEN);
5504 if (r == buf)
5505 {
5506 r = vim_strsave(buf);
5507 *tofree = r;
5508 }
5509 else
5510 *tofree = NULL;
5511 }
5512 else
5513 {
5514 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5515 : make_ufunc_name_readable(
5516 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5517 TRUE);
5518 r = *tofree;
5519 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005520 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005521 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005522
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005523 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005524 {
5525 partial_T *pt = tv->vval.v_partial;
5526 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005527 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005528 garray_T ga;
5529 int i;
5530 char_u *tf;
5531
5532 ga_init2(&ga, 1, 100);
5533 ga_concat(&ga, (char_u *)"function(");
5534 if (fname != NULL)
5535 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005536 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005537 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005538 && vim_isupper(fname[1]))
5539 {
5540 ga_concat(&ga, (char_u *)"'g:");
5541 ga_concat(&ga, fname + 1);
5542 }
5543 else
5544 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005545 vim_free(fname);
5546 }
5547 if (pt != NULL && pt->pt_argc > 0)
5548 {
5549 ga_concat(&ga, (char_u *)", [");
5550 for (i = 0; i < pt->pt_argc; ++i)
5551 {
5552 if (i > 0)
5553 ga_concat(&ga, (char_u *)", ");
5554 ga_concat(&ga,
5555 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5556 vim_free(tf);
5557 }
5558 ga_concat(&ga, (char_u *)"]");
5559 }
5560 if (pt != NULL && pt->pt_dict != NULL)
5561 {
5562 typval_T dtv;
5563
5564 ga_concat(&ga, (char_u *)", ");
5565 dtv.v_type = VAR_DICT;
5566 dtv.vval.v_dict = pt->pt_dict;
5567 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5568 vim_free(tf);
5569 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005570 // terminate with ')' and a NUL
5571 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005572
5573 *tofree = ga.ga_data;
5574 r = *tofree;
5575 break;
5576 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005577
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005578 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005579 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005580 break;
5581
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005582 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005583 if (tv->vval.v_list == NULL)
5584 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005585 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005586 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005587 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005588 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005589 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5590 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005591 {
5592 *tofree = NULL;
5593 r = (char_u *)"[...]";
5594 }
5595 else
5596 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005597 int old_copyID = tv->vval.v_list->lv_copyID;
5598
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005599 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005600 *tofree = list2string(tv, copyID, restore_copyID);
5601 if (restore_copyID)
5602 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005603 r = *tofree;
5604 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005605 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005606
Bram Moolenaar8c711452005-01-14 21:53:12 +00005607 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005608 if (tv->vval.v_dict == NULL)
5609 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005610 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005611 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005612 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005613 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005614 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5615 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005616 {
5617 *tofree = NULL;
5618 r = (char_u *)"{...}";
5619 }
5620 else
5621 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005622 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005623
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005624 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005625 *tofree = dict2string(tv, copyID, restore_copyID);
5626 if (restore_copyID)
5627 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005628 r = *tofree;
5629 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005630 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005631
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005632 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005633 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005634 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005635 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005636 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005637 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005638 break;
5639
Bram Moolenaar835dc632016-02-07 14:27:38 +01005640 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005641 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005642#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005643 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005644 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5645 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005646 if (composite_val)
5647 {
5648 *tofree = string_quote(r, FALSE);
5649 r = *tofree;
5650 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005651#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005652 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005653
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005654 case VAR_INSTR:
5655 *tofree = NULL;
5656 r = (char_u *)"instructions";
5657 break;
5658
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005659 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005660#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005661 *tofree = NULL;
5662 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5663 r = numbuf;
5664 break;
5665#endif
5666
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005667 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005668 case VAR_SPECIAL:
5669 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005670 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005671 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005672 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005673
Bram Moolenaar8502c702014-06-17 12:51:16 +02005674 if (--recurse == 0)
5675 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005676 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005677}
5678
5679/*
5680 * Return a string with the string representation of a variable.
5681 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5682 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005683 * Does not put quotes around strings, as ":echo" displays values.
5684 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5685 * May return NULL.
5686 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005687 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005688echo_string(
5689 typval_T *tv,
5690 char_u **tofree,
5691 char_u *numbuf,
5692 int copyID)
5693{
5694 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5695}
5696
5697/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005698 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5699 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005700 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005701 */
5702 int
5703buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5704{
5705 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005706 char_u *t;
5707 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005708
5709 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5710 return -1;
5711
5712 if (lnum > buf->b_ml.ml_line_count)
5713 lnum = buf->b_ml.ml_line_count;
5714
5715 str = ml_get_buf(buf, lnum, FALSE);
5716 if (str == NULL)
5717 return -1;
5718
5719 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005720 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005721
Bram Moolenaar91458462021-01-13 20:08:38 +01005722 // count the number of characters
5723 t = str;
5724 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5725 t += mb_ptr2len(t);
5726
5727 // In insert mode, when the cursor is at the end of a non-empty line,
5728 // byteidx points to the NUL character immediately past the end of the
5729 // string. In this case, add one to the character count.
5730 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5731 count++;
5732
5733 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005734}
5735
5736/*
5737 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005738 * byte index. Works only for loaded buffers. Returns -1 on failure.
5739 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005740 */
5741 int
5742buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5743{
5744 char_u *str;
5745 char_u *t;
5746
5747 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5748 return -1;
5749
5750 if (lnum > buf->b_ml.ml_line_count)
5751 lnum = buf->b_ml.ml_line_count;
5752
5753 str = ml_get_buf(buf, lnum, FALSE);
5754 if (str == NULL)
5755 return -1;
5756
5757 // Convert the character offset to a byte offset
5758 t = str;
5759 while (*t != NUL && --charidx > 0)
5760 t += mb_ptr2len(t);
5761
Bram Moolenaar91458462021-01-13 20:08:38 +01005762 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005763}
5764
5765/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005767 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005769 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005770var2fpos(
5771 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005772 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005773 int *fnum, // set to fnum for '0, 'A, etc.
5774 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005776 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005778 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005780 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005781 if (varp->v_type == VAR_LIST)
5782 {
5783 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005784 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005785 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005786 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005787
5788 l = varp->vval.v_list;
5789 if (l == NULL)
5790 return NULL;
5791
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005792 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005793 pos.lnum = list_find_nr(l, 0L, &error);
5794 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005795 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005796 if (charcol)
5797 len = (long)mb_charlen(ml_get(pos.lnum));
5798 else
5799 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005800
Bram Moolenaarec65d772020-08-20 22:29:12 +02005801 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005802 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005803 li = list_find(l, 1L);
5804 if (li != NULL && li->li_tv.v_type == VAR_STRING
5805 && li->li_tv.vval.v_string != NULL
5806 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005807 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005808 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005809 }
5810 else
5811 {
5812 pos.col = list_find_nr(l, 1L, &error);
5813 if (error)
5814 return NULL;
5815 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005816
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005817 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005818 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005819 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005820 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005821
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005822 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005823 pos.coladd = list_find_nr(l, 2L, &error);
5824 if (error)
5825 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005826
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005827 return &pos;
5828 }
5829
Bram Moolenaarc5809432021-03-27 21:23:30 +01005830 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5831 return NULL;
5832
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005833 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005834 if (name == NULL)
5835 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005836
5837 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005838 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005839 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005840 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005841 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005842 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005843 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005844 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005845 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005846 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005847 pos = VIsual;
5848 else
5849 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005850 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005851 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005852 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005854 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005855 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5857 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005858 pos = *pp;
5859 }
5860 if (pos.lnum != 0)
5861 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005862 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005863 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5864 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005866
Bram Moolenaara5525202006-03-02 22:52:09 +00005867 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005868
Bram Moolenaar477933c2007-07-17 14:32:23 +00005869 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005870 {
5871 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005872 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005873 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005874 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005875 // In silent Ex mode topline is zero, but that's not a valid line
5876 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005877 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005878 return &pos;
5879 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005880 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005881 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005882 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005883 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005884 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005885 return &pos;
5886 }
5887 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005888 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005890 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 {
5892 pos.lnum = curbuf->b_ml.ml_line_count;
5893 pos.col = 0;
5894 }
5895 else
5896 {
5897 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005898 if (charcol)
5899 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5900 else
5901 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 }
5903 return &pos;
5904 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005905 if (in_vim9script())
5906 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 return NULL;
5908}
5909
5910/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005911 * Convert list in "arg" into a position and optional file number.
5912 * When "fnump" is NULL there is no file number, only 3 items.
5913 * Note that the column is passed on as-is, the caller may want to decrement
5914 * it to use 1 for the first column.
5915 * Return FAIL when conversion is not possible, doesn't check the position for
5916 * validity.
5917 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005918 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005919list2fpos(
5920 typval_T *arg,
5921 pos_T *posp,
5922 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005923 colnr_T *curswantp,
5924 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005925{
5926 list_T *l = arg->vval.v_list;
5927 long i = 0;
5928 long n;
5929
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005930 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5931 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005932 if (arg->v_type != VAR_LIST
5933 || l == NULL
5934 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005935 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005936 return FAIL;
5937
5938 if (fnump != NULL)
5939 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005940 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005941 if (n < 0)
5942 return FAIL;
5943 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005944 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005945 *fnump = n;
5946 }
5947
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005948 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005949 if (n < 0)
5950 return FAIL;
5951 posp->lnum = n;
5952
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005953 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005954 if (n < 0)
5955 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005956 // If character position is specified, then convert to byte position
5957 if (charcol)
5958 {
5959 buf_T *buf;
5960
5961 // Get the text for the specified line in a loaded buffer
5962 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5963 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5964 return FAIL;
5965
Bram Moolenaar91458462021-01-13 20:08:38 +01005966 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005967 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005968 posp->col = n;
5969
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005970 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005971 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005972 posp->coladd = 0;
5973 else
5974 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005975
Bram Moolenaar493c1782014-05-28 14:34:46 +02005976 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005977 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005978
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005979 return OK;
5980}
5981
5982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 * Get the length of an environment variable name.
5984 * Advance "arg" to the first character after the name.
5985 * Return 0 for error.
5986 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005987 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005988get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989{
5990 char_u *p;
5991 int len;
5992
5993 for (p = *arg; vim_isIDc(*p); ++p)
5994 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005995 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 return 0;
5997
5998 len = (int)(p - *arg);
5999 *arg = p;
6000 return len;
6001}
6002
6003/*
6004 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006005 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 * Return 0 if something is wrong.
6007 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006008 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006009get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010{
6011 char_u *p;
6012 int len;
6013
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006014 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006016 {
6017 if (*p == ':')
6018 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006019 // "s:" is start of "s:var", but "n:" is not and can be used in
6020 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006021 len = (int)(p - *arg);
6022 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6023 || len > 1)
6024 break;
6025 }
6026 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006027 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 return 0;
6029
6030 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006031 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032
6033 return len;
6034}
6035
6036/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006037 * Get the length of the name of a variable or function.
6038 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006040 * Return -1 if curly braces expansion failed.
6041 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 * If the name contains 'magic' {}'s, expand them and return the
6043 * expanded name in an allocated string via 'alias' - caller must free.
6044 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006045 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006046get_name_len(
6047 char_u **arg,
6048 char_u **alias,
6049 int evaluate,
6050 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051{
6052 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 char_u *p;
6054 char_u *expr_start;
6055 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006057 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058
6059 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6060 && (*arg)[2] == (int)KE_SNR)
6061 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006062 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 *arg += 3;
6064 return get_id_len(arg) + 3;
6065 }
6066 len = eval_fname_script(*arg);
6067 if (len > 0)
6068 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006069 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 *arg += len;
6071 }
6072
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006074 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006076 p = find_name_end(*arg, &expr_start, &expr_end,
6077 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 if (expr_start != NULL)
6079 {
6080 char_u *temp_string;
6081
6082 if (!evaluate)
6083 {
6084 len += (int)(p - *arg);
6085 *arg = skipwhite(p);
6086 return len;
6087 }
6088
6089 /*
6090 * Include any <SID> etc in the expanded string:
6091 * Thus the -len here.
6092 */
6093 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6094 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006095 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 *alias = temp_string;
6097 *arg = skipwhite(p);
6098 return (int)STRLEN(temp_string);
6099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100
6101 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006102 // Only give an error when there is something, otherwise it will be
6103 // reported at a higher level.
6104 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006105 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106
6107 return len;
6108}
6109
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006110/*
6111 * Find the end of a variable or function name, taking care of magic braces.
6112 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6113 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006114 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006115 * Return a pointer to just after the name. Equal to "arg" if there is no
6116 * valid name.
6117 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006118 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006119find_name_end(
6120 char_u *arg,
6121 char_u **expr_start,
6122 char_u **expr_end,
6123 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006125 int mb_nest = 0;
6126 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006128 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02006129 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006131 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006133 *expr_start = NULL;
6134 *expr_end = NULL;
6135 }
6136
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006137 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006138 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
6139 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006140 return arg;
6141
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006142 for (p = arg; *p != NUL
6143 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006144 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006145 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006146 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006147 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006148 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006149 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006150 if (*p == '\'')
6151 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006152 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006153 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006154 ;
6155 if (*p == NUL)
6156 break;
6157 }
6158 else if (*p == '"')
6159 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006160 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006161 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006162 if (*p == '\\' && p[1] != NUL)
6163 ++p;
6164 if (*p == NUL)
6165 break;
6166 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006167 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6168 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006169 // "s:" is start of "s:var", but "n:" is not and can be used in
6170 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006171 len = (int)(p - arg);
6172 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006173 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006174 break;
6175 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006176
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006177 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006179 if (*p == '[')
6180 ++br_nest;
6181 else if (*p == ']')
6182 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006184
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006185 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006187 if (*p == '{')
6188 {
6189 mb_nest++;
6190 if (expr_start != NULL && *expr_start == NULL)
6191 *expr_start = p;
6192 }
6193 else if (*p == '}')
6194 {
6195 mb_nest--;
6196 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6197 *expr_end = p;
6198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 }
6201
6202 return p;
6203}
6204
6205/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006206 * Expands out the 'magic' {}'s in a variable/function name.
6207 * Note that this can call itself recursively, to deal with
6208 * constructs like foo{bar}{baz}{bam}
6209 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6210 * "in_start" ^
6211 * "expr_start" ^
6212 * "expr_end" ^
6213 * "in_end" ^
6214 *
6215 * Returns a new allocated string, which the caller must free.
6216 * Returns NULL for failure.
6217 */
6218 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006219make_expanded_name(
6220 char_u *in_start,
6221 char_u *expr_start,
6222 char_u *expr_end,
6223 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006224{
6225 char_u c1;
6226 char_u *retval = NULL;
6227 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006228
6229 if (expr_end == NULL || in_end == NULL)
6230 return NULL;
6231 *expr_start = NUL;
6232 *expr_end = NUL;
6233 c1 = *in_end;
6234 *in_end = NUL;
6235
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006236 temp_result = eval_to_string(expr_start + 1, FALSE);
6237 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006238 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006239 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6240 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006241 if (retval != NULL)
6242 {
6243 STRCPY(retval, in_start);
6244 STRCAT(retval, temp_result);
6245 STRCAT(retval, expr_end + 1);
6246 }
6247 }
6248 vim_free(temp_result);
6249
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006250 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006251 *expr_start = '{';
6252 *expr_end = '}';
6253
6254 if (retval != NULL)
6255 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006256 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006257 if (expr_start != NULL)
6258 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006259 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006260 temp_result = make_expanded_name(retval, expr_start,
6261 expr_end, temp_result);
6262 vim_free(retval);
6263 retval = temp_result;
6264 }
6265 }
6266
6267 return retval;
6268}
6269
6270/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006272 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006274 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006275eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006277 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006278}
6279
6280/*
6281 * Return TRUE if character "c" can be used as the first character in a
6282 * variable or function name (excluding '{' and '}').
6283 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006284 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006285eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006286{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006287 return ASCII_ISALPHA(c) || c == '_';
6288}
6289
6290/*
6291 * Return TRUE if character "c" can be used as the first character of a
6292 * dictionary key.
6293 */
6294 int
6295eval_isdictc(int c)
6296{
6297 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298}
6299
6300/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006301 * Handle:
6302 * - expr[expr], expr[expr:expr] subscript
6303 * - ".name" lookup
6304 * - function call with Funcref variable: func(expr)
6305 * - method call: var->method()
6306 *
6307 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006308 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006309 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006310 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006311handle_subscript(
6312 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006313 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006314 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006315 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006316 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006317{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006318 int evaluate = evalarg != NULL
6319 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006320 int ret = OK;
6321 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006322 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006323 int getnext;
6324 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006325
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006326 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006327 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006328 // When at the end of the line and ".name" or "->{" or "->X" follows in
6329 // the next line then consume the line break.
6330 p = eval_next_non_blank(*arg, evalarg, &getnext);
6331 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006332 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006333 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6334 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6335 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006336 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006337 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006338 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006339 check_white = FALSE;
6340 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006341
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006342 if (rettv->v_type == VAR_ANY)
6343 {
6344 char_u *exp_name;
6345 int cc;
6346 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006347 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006348 type_T *type;
6349
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006350 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006351 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006352 if (**arg != '.')
6353 {
6354 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006355 semsg(_(e_expected_dot_after_name_str),
6356 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006357 ret = FAIL;
6358 break;
6359 }
6360 ++*arg;
6361 if (IS_WHITE_OR_NUL(**arg))
6362 {
6363 if (verbose)
6364 emsg(_(e_no_white_space_allowed_after_dot));
6365 ret = FAIL;
6366 break;
6367 }
6368
6369 // isolate the name
6370 exp_name = *arg;
6371 while (eval_isnamec(**arg))
6372 ++*arg;
6373 cc = **arg;
6374 **arg = NUL;
6375
6376 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00006377 evalarg->eval_cctx, evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006378 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006379
6380 if (idx < 0 && ufunc == NULL)
6381 {
6382 ret = FAIL;
6383 break;
6384 }
6385 if (idx >= 0)
6386 {
6387 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6388 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6389
6390 copy_tv(sv->sv_tv, rettv);
6391 }
6392 else
6393 {
6394 rettv->v_type = VAR_FUNC;
6395 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6396 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006397 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006398 }
6399
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006400 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6401 || rettv->v_type == VAR_PARTIAL))
6402 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006403 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006404 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6405 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006406
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006407 // Stop the expression evaluation when immediately aborting on
6408 // error, or when an interrupt occurred or an exception was thrown
6409 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006410 if (aborting())
6411 {
6412 if (ret == OK)
6413 clear_tv(rettv);
6414 ret = FAIL;
6415 }
6416 dict_unref(selfdict);
6417 selfdict = NULL;
6418 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006419 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006420 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006421 if (in_vim9script())
6422 *arg = skipwhite(p + 2);
6423 else
6424 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006425 if (ret == OK)
6426 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006427 if (VIM_ISWHITE(**arg))
6428 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006429 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006430 ret = FAIL;
6431 }
6432 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006433 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006434 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006435 else
6436 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006437 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006438 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006439 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006440 // "." is ".name" lookup when we found a dict or when evaluating and
6441 // scriptversion is at least 2, where string concatenation is "..".
6442 else if (**arg == '['
6443 || (**arg == '.' && (rettv->v_type == VAR_DICT
6444 || (!evaluate
6445 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006446 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006447 {
6448 dict_unref(selfdict);
6449 if (rettv->v_type == VAR_DICT)
6450 {
6451 selfdict = rettv->vval.v_dict;
6452 if (selfdict != NULL)
6453 ++selfdict->dv_refcount;
6454 }
6455 else
6456 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006457 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006458 {
6459 clear_tv(rettv);
6460 ret = FAIL;
6461 }
6462 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006463 else
6464 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006465 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006466
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006467 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6468 // Don't do this when "Func" is already a partial that was bound
6469 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006470 if (selfdict != NULL
6471 && (rettv->v_type == VAR_FUNC
6472 || (rettv->v_type == VAR_PARTIAL
6473 && (rettv->vval.v_partial->pt_auto
6474 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006475 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006476
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006477 dict_unref(selfdict);
6478 return ret;
6479}
6480
6481/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006482 * Make a copy of an item.
6483 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006484 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006485 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6486 * reference to an already copied list/dict can be used.
6487 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006488 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006489 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006490item_copy(
6491 typval_T *from,
6492 typval_T *to,
6493 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006494 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006495 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006496{
6497 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006498 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006499
Bram Moolenaar33570922005-01-25 22:26:29 +00006500 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006501 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006502 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006503 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006504 }
6505 ++recurse;
6506
6507 switch (from->v_type)
6508 {
6509 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006510 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006511 case VAR_STRING:
6512 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006513 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006514 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006515 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006516 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006517 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006518 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006519 copy_tv(from, to);
6520 break;
6521 case VAR_LIST:
6522 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006523 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006524 if (from->vval.v_list == NULL)
6525 to->vval.v_list = NULL;
6526 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6527 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006528 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006529 to->vval.v_list = from->vval.v_list->lv_copylist;
6530 ++to->vval.v_list->lv_refcount;
6531 }
6532 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006533 to->vval.v_list = list_copy(from->vval.v_list,
6534 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006535 if (to->vval.v_list == NULL)
6536 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006537 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006538 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006539 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006540 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006541 case VAR_DICT:
6542 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006543 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006544 if (from->vval.v_dict == NULL)
6545 to->vval.v_dict = NULL;
6546 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6547 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006548 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006549 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6550 ++to->vval.v_dict->dv_refcount;
6551 }
6552 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006553 to->vval.v_dict = dict_copy(from->vval.v_dict,
6554 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006555 if (to->vval.v_dict == NULL)
6556 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006557 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006558 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006559 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006560 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006561 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006562 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006563 }
6564 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006565 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006566}
6567
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006568 void
6569echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6570{
6571 char_u *tofree;
6572 char_u numbuf[NUMBUFLEN];
6573 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6574
6575 if (*atstart)
6576 {
6577 *atstart = FALSE;
6578 // Call msg_start() after eval1(), evaluating the expression
6579 // may cause a message to appear.
6580 if (with_space)
6581 {
6582 // Mark the saved text as finishing the line, so that what
6583 // follows is displayed on a new line when scrolling back
6584 // at the more prompt.
6585 msg_sb_eol();
6586 msg_start();
6587 }
6588 }
6589 else if (with_space)
6590 msg_puts_attr(" ", echo_attr);
6591
6592 if (p != NULL)
6593 for ( ; *p != NUL && !got_int; ++p)
6594 {
6595 if (*p == '\n' || *p == '\r' || *p == TAB)
6596 {
6597 if (*p != TAB && *needclr)
6598 {
6599 // remove any text still there from the command
6600 msg_clr_eos();
6601 *needclr = FALSE;
6602 }
6603 msg_putchar_attr(*p, echo_attr);
6604 }
6605 else
6606 {
6607 if (has_mbyte)
6608 {
6609 int i = (*mb_ptr2len)(p);
6610
6611 (void)msg_outtrans_len_attr(p, i, echo_attr);
6612 p += i - 1;
6613 }
6614 else
6615 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6616 }
6617 }
6618 vim_free(tofree);
6619}
6620
Bram Moolenaare9a41262005-01-15 22:18:47 +00006621/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 * ":echo expr1 ..." print each argument separated with a space, add a
6623 * newline at the end.
6624 * ":echon expr1 ..." print each argument plain.
6625 */
6626 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006627ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628{
6629 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006630 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006631 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 int needclr = TRUE;
6633 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006634 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006635 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006636 evalarg_T evalarg;
6637
Bram Moolenaare707c882020-07-01 13:07:37 +02006638 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639
6640 if (eap->skip)
6641 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006642 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006644 // If eval1() causes an error message the text from the command may
6645 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006646 need_clr_eos = needclr;
6647
Bram Moolenaar68db9962021-05-09 23:19:22 +02006648 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006649 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 {
6651 /*
6652 * Report the invalid expression unless the expression evaluation
6653 * has been cancelled due to an aborting error, an interrupt, or an
6654 * exception.
6655 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006656 if (!aborting() && did_emsg == did_emsg_before
6657 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006658 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006659 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 break;
6661 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006662 need_clr_eos = FALSE;
6663
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006665 {
6666 if (rettv.v_type == VAR_VOID)
6667 {
6668 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6669 break;
6670 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006671 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006672 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006673
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006674 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 arg = skipwhite(arg);
6676 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006677 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006678 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679
6680 if (eap->skip)
6681 --emsg_skip;
6682 else
6683 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006684 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 if (needclr)
6686 msg_clr_eos();
6687 if (eap->cmdidx == CMD_echo)
6688 msg_end();
6689 }
6690}
6691
6692/*
6693 * ":echohl {name}".
6694 */
6695 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006696ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006698 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699}
6700
6701/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006702 * Returns the :echo attribute
6703 */
6704 int
6705get_echo_attr(void)
6706{
6707 return echo_attr;
6708}
6709
6710/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 * ":execute expr1 ..." execute the result of an expression.
6712 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01006713 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006715 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 * Each gets spaces around each argument and a newline at the end for
6717 * echo commands
6718 */
6719 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006720ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721{
6722 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006723 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 int ret = OK;
6725 char_u *p;
6726 garray_T ga;
6727 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006728 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729
6730 ga_init2(&ga, 1, 80);
Bram Moolenaar37fef162022-08-29 18:16:32 +01006731#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarb5b4f612022-09-01 16:43:17 +01006732 if (eap->cmdidx == CMD_echowindow)
6733 start_echowindow();
Bram Moolenaar37fef162022-08-29 18:16:32 +01006734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735
6736 if (eap->skip)
6737 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006738 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006740 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006741 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743
6744 if (!eap->skip)
6745 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006746 char_u buf[NUMBUFLEN];
6747
6748 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006749 {
6750 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6751 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006752 semsg(_(e_using_invalid_value_as_string_str),
6753 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006754 p = NULL;
6755 }
6756 else
6757 p = tv_get_string_buf(&rettv, buf);
6758 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006759 else
6760 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006761 if (p == NULL)
6762 {
6763 clear_tv(&rettv);
6764 ret = FAIL;
6765 break;
6766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 len = (int)STRLEN(p);
6768 if (ga_grow(&ga, len + 2) == FAIL)
6769 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006770 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 ret = FAIL;
6772 break;
6773 }
6774 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 ga.ga_len += len;
6778 }
6779
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006780 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 arg = skipwhite(arg);
6782 }
6783
6784 if (ret != FAIL && ga.ga_data != NULL)
6785 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006786 // use the first line of continuation lines for messages
6787 SOURCING_LNUM = start_lnum;
6788
Bram Moolenaar37fef162022-08-29 18:16:32 +01006789 if (eap->cmdidx == CMD_echomsg
6790 || eap->cmdidx == CMD_echowindow
6791 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006792 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006793 // Mark the already saved text as finishing the line, so that what
6794 // follows is displayed on a new line when scrolling back at the
6795 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006796 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006797 }
6798
Bram Moolenaar37fef162022-08-29 18:16:32 +01006799 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echowindow)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006800 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006801 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006802 out_flush();
6803 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006804 else if (eap->cmdidx == CMD_echoconsole)
6805 {
6806 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6807 ui_write((char_u *)"\r\n", 2, TRUE);
6808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 else if (eap->cmdidx == CMD_echoerr)
6810 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006811 int save_did_emsg = did_emsg;
6812
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006813 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006814 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 if (!force_abort)
6816 did_emsg = save_did_emsg;
6817 }
6818 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006819 {
6820 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6821
6822 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6823 sticky_cmdmod_flags = cmdmod.cmod_flags
6824 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 do_cmdline((char_u *)ga.ga_data,
6826 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006827 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 }
6830
6831 ga_clear(&ga);
6832
6833 if (eap->skip)
6834 --emsg_skip;
Bram Moolenaar134b8652022-08-28 21:36:43 +01006835#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +01006836 if (eap->cmdidx == CMD_echowindow)
Bram Moolenaarb5b4f612022-09-01 16:43:17 +01006837 end_echowindow();
Bram Moolenaar134b8652022-08-28 21:36:43 +01006838#endif
Bram Moolenaar63b91732021-08-05 20:40:03 +02006839 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840}
6841
6842/*
6843 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6844 * "arg" points to the "&" or '+' when called, to "option" when returning.
6845 * Returns NULL when no option name found. Otherwise pointer to the char
6846 * after the option name.
6847 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006848 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006849find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850{
6851 char_u *p = *arg;
6852
6853 ++p;
6854 if (*p == 'g' && p[1] == ':')
6855 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006856 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 p += 2;
6858 }
6859 else if (*p == 'l' && p[1] == ':')
6860 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006861 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 p += 2;
6863 }
6864 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006865 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866
6867 if (!ASCII_ISALPHA(*p))
6868 return NULL;
6869 *arg = p;
6870
6871 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006872 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 else
6874 while (ASCII_ISALPHA(*p))
6875 ++p;
6876 return p;
6877}
6878
6879/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006880 * Display script name where an item was last set.
6881 * Should only be invoked when 'verbose' is non-zero.
6882 */
6883 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006884last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006885{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006886 char_u *p;
6887
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006888 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006889 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006890 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006891 if (p != NULL)
6892 {
6893 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006894 msg_puts(_("\n\tLast set from "));
6895 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006896 if (script_ctx.sc_lnum > 0)
6897 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006898 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006899 msg_outnum((long)script_ctx.sc_lnum);
6900 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006901 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006902 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006903 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006904 }
6905}
6906
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006907#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908
6909/*
6910 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006911 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 * "flags" can be "g" to do a global substitute.
6913 * Returns an allocated string, NULL for error.
6914 */
6915 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006916do_string_sub(
6917 char_u *str,
6918 char_u *pat,
6919 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006920 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006921 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922{
6923 int sublen;
6924 regmatch_T regmatch;
6925 int i;
6926 int do_all;
6927 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006928 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 garray_T ga;
6930 char_u *ret;
6931 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006932 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006934 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006936 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937
6938 ga_init2(&ga, 1, 200);
6939
6940 do_all = (flags[0] == 'g');
6941
6942 regmatch.rm_ic = p_ic;
6943 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6944 if (regmatch.regprog != NULL)
6945 {
6946 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006947 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6949 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006950 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006951 if (regmatch.startp[0] == regmatch.endp[0])
6952 {
6953 if (zero_width == regmatch.startp[0])
6954 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006955 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006956 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006957 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6958 (size_t)i);
6959 ga.ga_len += i;
6960 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006961 continue;
6962 }
6963 zero_width = regmatch.startp[0];
6964 }
6965
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 /*
6967 * Get some space for a temporary buffer to do the substitution
6968 * into. It will contain:
6969 * - The text up to where the match is.
6970 * - The substituted text.
6971 * - The text after the match.
6972 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01006973 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006974 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6976 {
6977 ga_clear(&ga);
6978 break;
6979 }
6980
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006981 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 i = (int)(regmatch.startp[0] - tail);
6983 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006984 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01006985 (void)vim_regsub(&regmatch, sub, expr,
6986 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
6987 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006989 tail = regmatch.endp[0];
6990 if (*tail == NUL)
6991 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 if (!do_all)
6993 break;
6994 }
6995
6996 if (ga.ga_data != NULL)
6997 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6998
Bram Moolenaar473de612013-06-08 18:19:48 +02006999 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 }
7001
7002 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7003 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007004 if (p_cpo == empty_option)
7005 p_cpo = save_cpo;
7006 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007007 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007008 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007009 // If it's still empty it was changed and restored, need to restore in
7010 // the complicated way.
7011 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007012 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007013 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015
7016 return ret;
7017}