blob: 0ac2dfb35830bf670f68758e9a673e7f487ac2f1 [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 Moolenaar69a7cb42004-06-20 12:51:53 +0000357 * Skip over an expression at "*pp".
358 * Return FAIL for an error, OK otherwise.
359 */
360 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200361skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000362{
Bram Moolenaar33570922005-01-25 22:26:29 +0000363 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000364
365 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200366 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000367}
368
369/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200370 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200371 * If in Vim9 script and line breaks are encountered, the lines are
372 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200373 * "arg" is advanced to just after the expression.
374 * "start" is set to the start of the expression, "end" to just after the end.
375 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200376 * Return FAIL for an error, OK otherwise.
377 */
378 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200379skip_expr_concatenate(
380 char_u **arg,
381 char_u **start,
382 char_u **end,
383 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200384{
385 typval_T rettv;
386 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200387 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200388 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200389 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200390 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200391 int evaluate = evalarg == NULL
392 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200393
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200394 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200395 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200396 {
397 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200398 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200399 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200400 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200401 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200402 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200403 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200404
405 // Don't evaluate the expression.
406 if (evalarg != NULL)
407 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200408 *arg = skipwhite(*arg);
409 res = eval1(arg, &rettv, evalarg);
410 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200411 if (evalarg != NULL)
412 evalarg->eval_flags = save_flags;
413
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200414 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200415 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200416 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200417 if (evalarg->eval_ga.ga_len == 1)
418 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200419 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200420 ga_clear(gap);
421 gap->ga_itemsize = 0;
422 }
423 else
424 {
425 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200426 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200427
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200428 // Line breaks encountered, concatenate all the lines.
429 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200430 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200431
432 // free the lines only when using getsourceline()
433 if (evalarg->eval_cookie != NULL)
434 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200435 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200436 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200437 // Do not free the last line, "arg" points into it, free it
438 // later.
439 vim_free(evalarg->eval_tofree);
440 evalarg->eval_tofree =
441 ((char_u **)gap->ga_data)[gap->ga_len - 1];
442 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200443 ga_clear_strings(gap);
444 }
445 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200446 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200447 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200448
449 // free lines that were explicitly marked for freeing
450 ga_clear_strings(freegap);
451 }
452
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200453 gap->ga_itemsize = 0;
454 if (p == NULL)
455 return FAIL;
456 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200457 vim_free(evalarg->eval_tofree_lambda);
458 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200459 // Compute "end" relative to the end.
460 *end = *start + STRLEN(*start) - endoff;
461 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200462 }
463
464 return res;
465}
466
467/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100468 * Convert "tv" to a string.
469 * When "convert" is TRUE convert a List into a sequence of lines and convert
470 * a Float to a String.
471 * Returns an allocated string (NULL when out of memory).
472 */
473 char_u *
474typval2string(typval_T *tv, int convert)
475{
476 garray_T ga;
477 char_u *retval;
478#ifdef FEAT_FLOAT
479 char_u numbuf[NUMBUFLEN];
480#endif
481
482 if (convert && tv->v_type == VAR_LIST)
483 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000484 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100485 if (tv->vval.v_list != NULL)
486 {
487 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
488 if (tv->vval.v_list->lv_len > 0)
489 ga_append(&ga, NL);
490 }
491 ga_append(&ga, NUL);
492 retval = (char_u *)ga.ga_data;
493 }
494#ifdef FEAT_FLOAT
495 else if (convert && tv->v_type == VAR_FLOAT)
496 {
497 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
498 retval = vim_strsave(numbuf);
499 }
500#endif
501 else
502 retval = vim_strsave(tv_get_string(tv));
503 return retval;
504}
505
506/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200507 * Top level evaluation function, returning a string. Does not handle line
508 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000509 * When "convert" is TRUE convert a List into a sequence of lines and convert
510 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 * Return pointer to allocated memory, or NULL for failure.
512 */
513 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100514eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100515 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100516 int convert,
517 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518{
Bram Moolenaar33570922005-01-25 22:26:29 +0000519 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100521 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100523 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
524 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 retval = NULL;
526 else
527 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100528 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000529 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100531 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532
533 return retval;
534}
535
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100536 char_u *
537eval_to_string(
538 char_u *arg,
539 int convert)
540{
541 return eval_to_string_eap(arg, convert, NULL);
542}
543
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000545 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200546 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200547 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 */
549 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100550eval_to_string_safe(
551 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000552 int use_sandbox,
553 int keep_script_version)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554{
555 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200556 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200557 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200558 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559
Bram Moolenaar9530b582022-01-22 13:39:08 +0000560 if (!keep_script_version)
561 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200562 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000563 if (use_sandbox)
564 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200565 ++textwinlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200566 may_garbage_collect = FALSE;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200567 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000568 if (use_sandbox)
569 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200570 --textwinlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200571 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200572 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200573 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 return retval;
575}
576
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577/*
578 * Top level evaluation function, returning a number.
579 * Evaluates "expr" silently.
580 * Returns -1 for an error.
581 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200582 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100583eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584{
Bram Moolenaar33570922005-01-25 22:26:29 +0000585 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200586 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000587 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588
589 ++emsg_off;
590
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200591 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 retval = -1;
593 else
594 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100595 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000596 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 }
598 --emsg_off;
599
600 return retval;
601}
602
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000603/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000604 * Top level evaluation function.
605 * Returns an allocated typval_T with the result.
606 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000607 */
608 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200609eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000610{
611 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200612 evalarg_T evalarg;
613
614 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000615
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200616 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200617 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100618 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000619
Bram Moolenaar37c83712020-06-30 21:18:36 +0200620 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000621 return tv;
622}
623
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000625 * "*arg" points to what can be a function name in the form of "import.Name" or
626 * "Funcref". Return the name of the function. Set "tofree" to something that
627 * was allocated.
628 * If "verbose" is FALSE no errors are given.
629 * Return NULL for any failure.
630 */
631 static char_u *
632deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000633 char_u **arg,
634 char_u **tofree,
635 evalarg_T *evalarg,
636 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000637{
638 typval_T ref;
639 char_u *name = *arg;
640
641 ref.v_type = VAR_UNKNOWN;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100642 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100643 {
644 dictitem_T *v;
645
646 // If <SID>VarName was used it would not be found, try another way.
647 v = find_var_also_in_script(name, NULL, FALSE);
648 if (v == NULL)
649 return NULL;
650 copy_tv(&v->di_tv, &ref);
651 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000652 if (*skipwhite(*arg) != NUL)
653 {
654 if (verbose)
655 semsg(_(e_trailing_characters_str), *arg);
656 name = NULL;
657 }
658 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
659 {
660 name = ref.vval.v_string;
661 ref.vval.v_string = NULL;
662 *tofree = name;
663 }
664 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
665 {
666 if (ref.vval.v_partial->pt_argc > 0
667 || ref.vval.v_partial->pt_dict != NULL)
668 {
669 if (verbose)
670 emsg(_(e_cannot_use_partial_here));
671 name = NULL;
672 }
673 else
674 {
675 name = vim_strsave(partial_name(ref.vval.v_partial));
676 *tofree = name;
677 }
678 }
679 else
680 {
681 if (verbose)
682 semsg(_(e_not_callable_type_str), name);
683 name = NULL;
684 }
685 clear_tv(&ref);
686 return name;
687}
688
689/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100690 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200691 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
692 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000693 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200695 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100696call_vim_function(
697 char_u *func,
698 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200699 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200700 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000702 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200703 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000704 char_u *arg;
705 char_u *name;
706 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000707 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100709 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200710 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000711 funcexe.fe_firstline = curwin->w_cursor.lnum;
712 funcexe.fe_lastline = curwin->w_cursor.lnum;
713 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000714
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000715 // The name might be "import.Func" or "Funcref". We don't know, we need to
716 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000717 // autoload script has errors. Guess that when there is a dot in the name
718 // showing errors is the right choice.
719 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000720 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000721 if (ignore_errors)
722 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000723 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000724 if (ignore_errors)
725 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000726 if (name == NULL)
727 name = func;
728
729 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
730
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000731 if (ret == FAIL)
732 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000733 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000734
735 return ret;
736}
737
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100738/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100739 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000740 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
741 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000742 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000743 */
744 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100745call_func_retstr(
746 char_u *func,
747 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200748 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000749{
750 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000751 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000752
Bram Moolenaarded27a12018-08-01 19:06:03 +0200753 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000754 return NULL;
755
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100756 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000757 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 return retval;
759}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000760
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000761/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100762 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000763 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000764 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000765 */
766 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100767call_func_retlist(
768 char_u *func,
769 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200770 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000771{
772 typval_T rettv;
773
Bram Moolenaarded27a12018-08-01 19:06:03 +0200774 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000775 return NULL;
776
777 if (rettv.v_type != VAR_LIST)
778 {
779 clear_tv(&rettv);
780 return NULL;
781 }
782
783 return rettv.vval.v_list;
784}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785
Bram Moolenaare70dd112022-01-21 16:31:11 +0000786#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100788 * Evaluate "arg", which is 'foldexpr'.
789 * Note: caller must set "curwin" to match "arg".
790 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
791 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 */
793 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000794eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000796 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000797 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200798 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000800 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000801 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
802 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803
Bram Moolenaare70dd112022-01-21 16:31:11 +0000804 arg = wp->w_p_fde;
805 current_sctx = wp->w_p_script_ctx[WV_FDE];
806
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000808 if (use_sandbox)
809 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200810 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200812 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 retval = 0;
814 else
815 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100816 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000817 if (tv.v_type == VAR_NUMBER)
818 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000819 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 retval = 0;
821 else
822 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100823 // If the result is a string, check if there is a non-digit before
824 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000825 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 if (!VIM_ISDIGIT(*s) && *s != '-')
827 *cp = *s++;
828 retval = atol((char *)s);
829 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000830 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 }
832 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000833 if (use_sandbox)
834 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200835 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200836 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000837 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200839 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840}
841#endif
842
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000844 * Get an lval: variable, Dict item or List item that can be assigned a value
845 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
846 * "name.key", "name.key[expr]" etc.
847 * Indexing only works if "name" is an existing List or Dictionary.
848 * "name" points to the start of the name.
849 * If "rettv" is not NULL it points to the value to be assigned.
850 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
851 * wrong; must end in space or cmd separator.
852 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100853 * flags:
854 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100855 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100856 * GLV_NO_AUTOLOAD: do not use script autoloading
857 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000858 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000859 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000860 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000861 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200862 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100863get_lval(
864 char_u *name,
865 typval_T *rettv,
866 lval_T *lp,
867 int unlet,
868 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100869 int flags, // GLV_ values
870 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000871{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000872 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000873 char_u *expr_start, *expr_end;
874 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000875 dictitem_T *v;
876 typval_T var1;
877 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000878 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000879 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000880 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100881 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100882 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100883 int writing;
Bram Moolenaar4525a572022-02-13 11:57:33 +0000884 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000885
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100886 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200887 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000888
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100889 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000890 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100891 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000892 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200893 lp->ll_name_end = find_name_end(name, NULL, NULL,
894 FNE_INCL_BR | fne_flags);
895 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000896 }
897
Bram Moolenaara749a422022-02-12 19:52:25 +0000898 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +0000899 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +0000900 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
901 {
902 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
903 return NULL;
904 }
905
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100906 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000907 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100908 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000909 if (expr_start != NULL)
910 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100911 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100912 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000913 && *p != '[' && *p != '.')
914 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000915 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000916 return NULL;
917 }
918
919 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
920 if (lp->ll_exp_name == NULL)
921 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100922 // Report an invalid expression in braces, unless the
923 // expression evaluation has been cancelled due to an
924 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000925 if (!aborting() && !quiet)
926 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000927 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000928 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000929 return NULL;
930 }
931 }
932 lp->ll_name = lp->ll_exp_name;
933 }
934 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100935 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000936 lp->ll_name = name;
937
Bram Moolenaar4525a572022-02-13 11:57:33 +0000938 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100939 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200940 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +0000941 // However, "g:[key]" is indexing a dictionary.
942 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200943 {
944 --p;
945 lp->ll_name_end = p;
946 }
947 if (*p == ':')
948 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000949 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +0200950
951 if (tp == p + 1 && !quiet)
952 {
953 semsg(_(e_white_space_required_after_str_str), ":", p);
954 return NULL;
955 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100956
Bram Moolenaar4c8b5462022-03-16 20:01:39 +0000957 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000958 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +0000959 semsg(_(e_using_type_not_in_script_context_str), p);
960 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000961 }
962
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200963 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +0000964 lp->ll_type = parse_type(&tp,
965 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
966 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100967 if (lp->ll_type == NULL && !quiet)
968 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200969 lp->ll_name_end = tp;
970 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100971 }
972 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000973 if (lp->ll_name == NULL)
974 return p;
975
Bram Moolenaar62aec932022-01-29 21:45:34 +0000976 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000977 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000978 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000979
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000980 if (import != NULL)
981 {
982 ufunc_T *ufunc;
983 type_T *type;
984
985 lp->ll_sid = import->imp_sid;
986 lp->ll_name = skipwhite(p + 1);
987 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
988 lp->ll_name_end = p;
989
990 // check the item is exported
991 cc = *p;
992 *p = NUL;
993 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000994 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000995 {
996 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +0000997 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000998 }
999 *p = cc;
1000 }
1001 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001002
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001003 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001004 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001005 return p;
1006
Bram Moolenaar4525a572022-02-13 11:57:33 +00001007 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001008 {
1009 // using local variable
1010 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001011 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001012 }
1013 else
1014 {
1015 cc = *p;
1016 *p = NUL;
1017 // When we would write to the variable pass &ht and prevent autoload.
1018 writing = !(flags & GLV_READ_ONLY);
1019 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001020 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001021 if (v == NULL && !quiet)
1022 semsg(_(e_undefined_variable_str), lp->ll_name);
1023 *p = cc;
1024 if (v == NULL)
1025 return NULL;
1026 lp->ll_tv = &v->di_tv;
1027 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001028
Bram Moolenaar4525a572022-02-13 11:57:33 +00001029 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001030 {
1031 if (!quiet)
1032 semsg(_(e_variable_already_declared), lp->ll_name);
1033 return NULL;
1034 }
1035
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001036 /*
1037 * Loop until no more [idx] or .key is following.
1038 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001039 var1.v_type = VAR_UNKNOWN;
1040 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001041 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001042 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001043 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1044 {
1045 if (!quiet)
1046 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1047 return NULL;
1048 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001049 if (lp->ll_tv->v_type != VAR_LIST
1050 && lp->ll_tv->v_type != VAR_DICT
1051 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001052 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001053 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001054 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001055 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001056 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001057
1058 // a NULL list/blob works like an empty list/blob, allocate one now.
1059 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1060 rettv_list_alloc(lp->ll_tv);
1061 else if (lp->ll_tv->v_type == VAR_BLOB
1062 && lp->ll_tv->vval.v_blob == NULL)
1063 rettv_blob_alloc(lp->ll_tv);
1064
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001065 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001066 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001067 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001068 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001069 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001070 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001071
Bram Moolenaar4525a572022-02-13 11:57:33 +00001072 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001073 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001074 && lp->ll_tv == &v->di_tv
1075 && ht != NULL && ht == get_script_local_ht())
1076 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001077 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001078
1079 // Vim9 script local variable: get the type
1080 if (sv != NULL)
1081 lp->ll_valtype = sv->sv_type;
1082 }
1083
Bram Moolenaar8c711452005-01-14 21:53:12 +00001084 len = -1;
1085 if (*p == '.')
1086 {
1087 key = p + 1;
1088 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1089 ;
1090 if (len == 0)
1091 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001092 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001093 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001094 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001095 }
1096 p = key + len;
1097 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001098 else
1099 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001100 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001101 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001102 if (*p == ':')
1103 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001104 else
1105 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001106 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001107 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001108 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001109 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001110 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001111 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001112 clear_tv(&var1);
1113 return NULL;
1114 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001115 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001116 }
1117
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001118 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001119 if (*p == ':')
1120 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001121 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001122 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001123 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001124 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001125 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001126 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001127 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001128 if (rettv != NULL
1129 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001130 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001131 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001132 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001133 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001134 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001135 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001136 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001137 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001138 }
1139 p = skipwhite(p + 1);
1140 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001141 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001142 else
1143 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001144 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001145 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001146 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001147 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001148 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001149 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001150 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001151 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001152 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001153 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001154 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001155 clear_tv(&var2);
1156 return NULL;
1157 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001158 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001159 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001160 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001161 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001162 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001163
Bram Moolenaar8c711452005-01-14 21:53:12 +00001164 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001165 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001166 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001167 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001168 clear_tv(&var1);
1169 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001170 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001171 }
1172
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001173 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001174 ++p;
1175 }
1176
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001177 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001178 {
1179 if (len == -1)
1180 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001181 // "[key]": get key from "var1"
1182 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001183 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001184 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001185 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001186 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001187 }
1188 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001189 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001190
1191 // a NULL dict is equivalent with an empty dict
1192 if (lp->ll_tv->vval.v_dict == NULL)
1193 {
1194 lp->ll_tv->vval.v_dict = dict_alloc();
1195 if (lp->ll_tv->vval.v_dict == NULL)
1196 {
1197 clear_tv(&var1);
1198 return NULL;
1199 }
1200 ++lp->ll_tv->vval.v_dict->dv_refcount;
1201 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001202 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001203
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001204 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001205
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001206 // When assigning to a scope dictionary check that a function and
1207 // variable name is valid (only variable name unless it is l: or
1208 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001209 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001210 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001211 int prevval;
1212 int wrong;
1213
1214 if (len != -1)
1215 {
1216 prevval = key[len];
1217 key[len] = NUL;
1218 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001219 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001220 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001221 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1222 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001223 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001224 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001225 if (len != -1)
1226 key[len] = prevval;
1227 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001228 {
1229 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001230 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001231 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001232 }
1233
Bram Moolenaar10c65862020-10-08 21:16:42 +02001234 if (lp->ll_valtype != NULL)
1235 // use the type of the member
1236 lp->ll_valtype = lp->ll_valtype->tt_member;
1237
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001238 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001239 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001240 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001241 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001242 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001243 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001244 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001245 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001246 return NULL;
1247 }
1248
Bram Moolenaar31b81602019-02-10 22:14:27 +01001249 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001250 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001251 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001252 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001253 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001254 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001255 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001256 }
1257 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001258 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001259 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001260 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001261 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001262 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001263 p = NULL;
1264 break;
1265 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001266 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001267 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001268 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1269 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001270 {
1271 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001272 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001273 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001274
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001275 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001276 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001277 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001278 else if (lp->ll_tv->v_type == VAR_BLOB)
1279 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001280 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1281
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001282 /*
1283 * Get the number and item for the only or first index of the List.
1284 */
1285 if (empty1)
1286 lp->ll_n1 = 0;
1287 else
1288 // is number or string
1289 lp->ll_n1 = (long)tv_get_number(&var1);
1290 clear_tv(&var1);
1291
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001292 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001293 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001294 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001295 return NULL;
1296 }
1297 if (lp->ll_range && !lp->ll_empty2)
1298 {
1299 lp->ll_n2 = (long)tv_get_number(&var2);
1300 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001301 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1302 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001303 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001304 }
1305 lp->ll_blob = lp->ll_tv->vval.v_blob;
1306 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001307 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001308 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001309 else
1310 {
1311 /*
1312 * Get the number and item for the only or first index of the List.
1313 */
1314 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001315 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001316 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001317 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001318 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001319 clear_tv(&var1);
1320
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001321 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001322 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001323 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1324 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001325 if (lp->ll_li == NULL)
1326 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001327 clear_tv(&var2);
1328 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001329 }
1330
Bram Moolenaar10c65862020-10-08 21:16:42 +02001331 if (lp->ll_valtype != NULL)
1332 // use the type of the member
1333 lp->ll_valtype = lp->ll_valtype->tt_member;
1334
Bram Moolenaar8c711452005-01-14 21:53:12 +00001335 /*
1336 * May need to find the item or absolute index for the second
1337 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001338 * When no index given: "lp->ll_empty2" is TRUE.
1339 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001340 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001341 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001342 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001343 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001344 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001345 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001346 if (check_range_index_two(lp->ll_list,
1347 &lp->ll_n1, lp->ll_li,
1348 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001349 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001350 }
1351
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001352 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001353 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001354 }
1355
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001356 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001358 return p;
1359}
1360
1361/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001362 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001363 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001364 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001365clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001366{
1367 vim_free(lp->ll_exp_name);
1368 vim_free(lp->ll_newkey);
1369}
1370
1371/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001372 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001373 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001374 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1375 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001376 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001377 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001378set_var_lval(
1379 lval_T *lp,
1380 char_u *endp,
1381 typval_T *rettv,
1382 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001383 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1384 char_u *op,
1385 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001386{
1387 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001388 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001389
1390 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001391 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001392 cc = *endp;
1393 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001394 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1395 return;
1396
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001397 if (lp->ll_blob != NULL)
1398 {
1399 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001400
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001401 if (op != NULL && *op != '=')
1402 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001403 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001404 return;
1405 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001406 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001407 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001408
1409 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1410 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001411 if (lp->ll_empty2)
1412 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1413
Bram Moolenaar68452172021-04-12 21:21:02 +02001414 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1415 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001416 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001417 }
1418 else
1419 {
1420 val = (int)tv_get_number_chk(rettv, &error);
1421 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001422 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001423 }
1424 }
1425 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001426 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001427 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001428
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001429 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1430 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001431 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001432 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001433 *endp = cc;
1434 return;
1435 }
1436
Bram Moolenaarff697e62019-02-12 22:28:33 +01001437 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001438 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001439 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001440 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001441 {
1442 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001443 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1444 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001445 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001446 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001447 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001448 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001449 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001450 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001451 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001452 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001453 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1454 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001455 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001456 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001457 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001458 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001459 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001460 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001461 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001462 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001463 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001464 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001465 else if (lp->ll_range)
1466 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001467 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1468 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001469 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001470 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001471 return;
1472 }
1473
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001474 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1475 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001476 }
1477 else
1478 {
1479 /*
1480 * Assign to a List or Dictionary item.
1481 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001482 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1483 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001484 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001485 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001486 return;
1487 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001488
1489 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001490 && check_typval_arg_type(lp->ll_valtype, rettv,
1491 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001492 return;
1493
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001494 if (lp->ll_newkey != NULL)
1495 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001496 if (op != NULL && *op != '=')
1497 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001498 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001499 return;
1500 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001501 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1502 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001503 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001504
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001505 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001506 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001507 if (di == NULL)
1508 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001509 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1510 {
1511 vim_free(di);
1512 return;
1513 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001514 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001515 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001516 else if (op != NULL && *op != '=')
1517 {
1518 tv_op(lp->ll_tv, rettv, op);
1519 return;
1520 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001521 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001522 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001523
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001524 /*
1525 * Assign the value to the variable or list item.
1526 */
1527 if (copy)
1528 copy_tv(rettv, lp->ll_tv);
1529 else
1530 {
1531 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001532 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001533 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001534 }
1535 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001536}
1537
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001538/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001539 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1540 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001541 * Returns OK or FAIL.
1542 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001543 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001544tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001545{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001546 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001547 char_u numbuf[NUMBUFLEN];
1548 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001549 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001550
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001551 // Can't do anything with a Funcref or Dict on the right.
1552 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001553 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001554 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1555 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001556 {
1557 switch (tv1->v_type)
1558 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001559 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001560 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001562 case VAR_DICT:
1563 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001564 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001565 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001566 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001567 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001568 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001569 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001570 break;
1571
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001572 case VAR_BLOB:
1573 if (*op != '+' || tv2->v_type != VAR_BLOB)
1574 break;
1575 // BLOB += BLOB
1576 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1577 {
1578 blob_T *b1 = tv1->vval.v_blob;
1579 blob_T *b2 = tv2->vval.v_blob;
1580 int i, len = blob_len(b2);
1581 for (i = 0; i < len; i++)
1582 ga_append(&b1->bv_ga, blob_get(b2, i));
1583 }
1584 return OK;
1585
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001586 case VAR_LIST:
1587 if (*op != '+' || tv2->v_type != VAR_LIST)
1588 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001589 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001590 if (tv2->vval.v_list != NULL)
1591 {
1592 if (tv1->vval.v_list == NULL)
1593 {
1594 tv1->vval.v_list = tv2->vval.v_list;
1595 ++tv1->vval.v_list->lv_refcount;
1596 }
1597 else
1598 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1599 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001600 return OK;
1601
1602 case VAR_NUMBER:
1603 case VAR_STRING:
1604 if (tv2->v_type == VAR_LIST)
1605 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001606 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001607 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001608 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001609 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001610#ifdef FEAT_FLOAT
1611 if (tv2->v_type == VAR_FLOAT)
1612 {
1613 float_T f = n;
1614
Bram Moolenaarff697e62019-02-12 22:28:33 +01001615 if (*op == '%')
1616 break;
1617 switch (*op)
1618 {
1619 case '+': f += tv2->vval.v_float; break;
1620 case '-': f -= tv2->vval.v_float; break;
1621 case '*': f *= tv2->vval.v_float; break;
1622 case '/': f /= tv2->vval.v_float; break;
1623 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001624 clear_tv(tv1);
1625 tv1->v_type = VAR_FLOAT;
1626 tv1->vval.v_float = f;
1627 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001628 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001629#endif
1630 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001631 switch (*op)
1632 {
1633 case '+': n += tv_get_number(tv2); break;
1634 case '-': n -= tv_get_number(tv2); break;
1635 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001636 case '/': n = num_divide(n, tv_get_number(tv2),
1637 &failed); break;
1638 case '%': n = num_modulus(n, tv_get_number(tv2),
1639 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001640 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001641 clear_tv(tv1);
1642 tv1->v_type = VAR_NUMBER;
1643 tv1->vval.v_number = n;
1644 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001645 }
1646 else
1647 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001648 if (tv2->v_type == VAR_FLOAT)
1649 break;
1650
Bram Moolenaarff697e62019-02-12 22:28:33 +01001651 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001652 s = tv_get_string(tv1);
1653 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001654 clear_tv(tv1);
1655 tv1->v_type = VAR_STRING;
1656 tv1->vval.v_string = s;
1657 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001658 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001659
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001660 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001661#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001662 {
1663 float_T f;
1664
Bram Moolenaarff697e62019-02-12 22:28:33 +01001665 if (*op == '%' || *op == '.'
1666 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001667 && tv2->v_type != VAR_NUMBER
1668 && tv2->v_type != VAR_STRING))
1669 break;
1670 if (tv2->v_type == VAR_FLOAT)
1671 f = tv2->vval.v_float;
1672 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001673 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001674 switch (*op)
1675 {
1676 case '+': tv1->vval.v_float += f; break;
1677 case '-': tv1->vval.v_float -= f; break;
1678 case '*': tv1->vval.v_float *= f; break;
1679 case '/': tv1->vval.v_float /= f; break;
1680 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001681 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001682#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001683 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001684 }
1685 }
1686
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001687 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001688 return FAIL;
1689}
1690
1691/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001692 * Evaluate the expression used in a ":for var in expr" command.
1693 * "arg" points to "var".
1694 * Set "*errp" to TRUE for an error, FALSE otherwise;
1695 * Return a pointer that holds the info. Null when there is an error.
1696 */
1697 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001698eval_for_line(
1699 char_u *arg,
1700 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001701 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001702 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001703{
Bram Moolenaar33570922005-01-25 22:26:29 +00001704 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001705 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001706 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001707 typval_T tv;
1708 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001709 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001710
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001711 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001712
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001713 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001714 if (fi == NULL)
1715 return NULL;
1716
Bram Moolenaar404557e2021-07-05 21:41:48 +02001717 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1718 &fi->fi_semicolon, FALSE);
1719 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001720 return fi;
1721
Bram Moolenaar404557e2021-07-05 21:41:48 +02001722 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001723 if (expr[0] != 'i' || expr[1] != 'n'
1724 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001725 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001726 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1727 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1728 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001729 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001730 return fi;
1731 }
1732
1733 if (skip)
1734 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001735 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1736 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001737 {
1738 *errp = FALSE;
1739 if (!skip)
1740 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001741 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001742 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001743 l = tv.vval.v_list;
1744 if (l == NULL)
1745 {
1746 // a null list is like an empty list: do nothing
1747 clear_tv(&tv);
1748 }
1749 else
1750 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001751 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001752 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001753
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001754 // No need to increment the refcount, it's already set for
1755 // the list being used in "tv".
1756 fi->fi_list = l;
1757 list_add_watch(l, &fi->fi_lw);
1758 fi->fi_lw.lw_item = l->lv_first;
1759 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001760 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001761 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001762 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001763 fi->fi_bi = 0;
1764 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001765 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001766 typval_T btv;
1767
1768 // Make a copy, so that the iteration still works when the
1769 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001770 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001771 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001772 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001773 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001774 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001775 else if (tv.v_type == VAR_STRING)
1776 {
1777 fi->fi_byte_idx = 0;
1778 fi->fi_string = tv.vval.v_string;
1779 tv.vval.v_string = NULL;
1780 if (fi->fi_string == NULL)
1781 fi->fi_string = vim_strsave((char_u *)"");
1782 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001783 else
1784 {
Sean Dewar80d73952021-08-04 19:25:54 +02001785 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001786 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001787 }
1788 }
1789 }
1790 if (skip)
1791 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001792 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001793
1794 return fi;
1795}
1796
1797/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001798 * Used when looping over a :for line, skip the "in expr" part.
1799 */
1800 void
1801skip_for_lines(void *fi_void, evalarg_T *evalarg)
1802{
1803 forinfo_T *fi = (forinfo_T *)fi_void;
1804 int i;
1805
1806 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01001807 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001808}
1809
1810/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001811 * Use the first item in a ":for" list. Advance to the next.
1812 * Assign the values to the variable (list). "arg" points to the first one.
1813 * Return TRUE when a valid item was found, FALSE when at end of list or
1814 * something wrong.
1815 */
1816 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001817next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001818{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001819 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001820 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001821 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001822 ? (ASSIGN_FINAL
1823 // first round: error if variable exists
1824 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1825 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001826 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001827 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001828 int skip_assign = in_vim9script() && arg[0] == '_'
1829 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001830
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001831 if (fi->fi_blob != NULL)
1832 {
1833 typval_T tv;
1834
1835 if (fi->fi_bi >= blob_len(fi->fi_blob))
1836 return FALSE;
1837 tv.v_type = VAR_NUMBER;
1838 tv.v_lock = VAR_FIXED;
1839 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1840 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001841 if (skip_assign)
1842 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001843 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001844 fi->fi_varcount, flag, NULL) == OK;
1845 }
1846
1847 if (fi->fi_string != NULL)
1848 {
1849 typval_T tv;
1850 int len;
1851
1852 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1853 if (len == 0)
1854 return FALSE;
1855 tv.v_type = VAR_STRING;
1856 tv.v_lock = VAR_FIXED;
1857 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1858 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001859 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001860 if (skip_assign)
1861 result = TRUE;
1862 else
1863 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001864 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001865 vim_free(tv.vval.v_string);
1866 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001867 }
1868
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001869 item = fi->fi_lw.lw_item;
1870 if (item == NULL)
1871 result = FALSE;
1872 else
1873 {
1874 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001875 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001876 if (skip_assign)
1877 result = TRUE;
1878 else
1879 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001880 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001881 }
1882 return result;
1883}
1884
1885/*
1886 * Free the structure used to store info used by ":for".
1887 */
1888 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001889free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001890{
Bram Moolenaar33570922005-01-25 22:26:29 +00001891 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001892
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001893 if (fi == NULL)
1894 return;
1895 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001896 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001897 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001898 list_unref(fi->fi_list);
1899 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001900 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001901 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001902 else
1903 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001904 vim_free(fi);
1905}
1906
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001908set_context_for_expression(
1909 expand_T *xp,
1910 char_u *arg,
1911 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001913 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001915 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001917 if (cmdidx == CMD_let || cmdidx == CMD_var
1918 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 {
1920 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001921 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001923 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001924 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925 {
1926 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001927 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001928 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929 break;
1930 }
1931 return;
1932 }
1933 }
1934 else
1935 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1936 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 while ((xp->xp_pattern = vim_strpbrk(arg,
1938 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1939 {
1940 c = *xp->xp_pattern;
1941 if (c == '&')
1942 {
1943 c = xp->xp_pattern[1];
1944 if (c == '&')
1945 {
1946 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001947 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 }
1949 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001950 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001952 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1953 xp->xp_pattern += 2;
1954
1955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 }
1957 else if (c == '$')
1958 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001959 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 xp->xp_context = EXPAND_ENV_VARS;
1961 }
1962 else if (c == '=')
1963 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001964 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 xp->xp_context = EXPAND_EXPRESSION;
1966 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001967 else if (c == '#'
1968 && xp->xp_context == EXPAND_EXPRESSION)
1969 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001970 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001971 break;
1972 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001973 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 && xp->xp_context == EXPAND_FUNCTIONS
1975 && vim_strchr(xp->xp_pattern, '(') == NULL)
1976 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001977 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 break;
1979 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001980 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001982 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 {
1984 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1985 if (c == '\\' && xp->xp_pattern[1] != NUL)
1986 ++xp->xp_pattern;
1987 xp->xp_context = EXPAND_NOTHING;
1988 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001989 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001991 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1993 /* skip */ ;
1994 xp->xp_context = EXPAND_NOTHING;
1995 }
1996 else if (c == '|')
1997 {
1998 if (xp->xp_pattern[1] == '|')
1999 {
2000 ++xp->xp_pattern;
2001 xp->xp_context = EXPAND_EXPRESSION;
2002 }
2003 else
2004 xp->xp_context = EXPAND_COMMANDS;
2005 }
2006 else
2007 xp->xp_context = EXPAND_EXPRESSION;
2008 }
2009 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002010 // Doesn't look like something valid, expand as an expression
2011 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002012 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 arg = xp->xp_pattern;
2014 if (*arg != NUL)
2015 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2016 /* skip */ ;
2017 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002018
2019 // ":exe one two" completes "two"
2020 if ((cmdidx == CMD_execute
2021 || cmdidx == CMD_echo
2022 || cmdidx == CMD_echon
2023 || cmdidx == CMD_echomsg)
2024 && xp->xp_context == EXPAND_EXPRESSION)
2025 {
2026 for (;;)
2027 {
2028 char_u *n = skiptowhite(arg);
2029
2030 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2031 break;
2032 arg = skipwhite(n);
2033 }
2034 }
2035
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 xp->xp_pattern = arg;
2037}
2038
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002040 * Return TRUE if "pat" matches "text".
2041 * Does not use 'cpo' and always uses 'magic'.
2042 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002043 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002044pattern_match(char_u *pat, char_u *text, int ic)
2045{
2046 int matches = FALSE;
2047 char_u *save_cpo;
2048 regmatch_T regmatch;
2049
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002050 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002051 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002052 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002053 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2054 if (regmatch.regprog != NULL)
2055 {
2056 regmatch.rm_ic = ic;
2057 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2058 vim_regfree(regmatch.regprog);
2059 }
2060 p_cpo = save_cpo;
2061 return matches;
2062}
2063
2064/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002065 * Handle a name followed by "(". Both for just "name(arg)" and for
2066 * "expr->name(arg)".
2067 * Returns OK or FAIL.
2068 */
2069 static int
2070eval_func(
2071 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002072 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002073 char_u *name,
2074 int name_len,
2075 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002076 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002077 typval_T *basetv) // "expr" for "expr->name(arg)"
2078{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002079 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002080 char_u *s = name;
2081 int len = name_len;
2082 partial_T *partial;
2083 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002084 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002085 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002086
2087 if (!evaluate)
2088 check_vars(s, len);
2089
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002090 // If "s" is the name of a variable of type VAR_FUNC
2091 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002092 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002093 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002094
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002095 // Need to make a copy, in case evaluating the arguments makes
2096 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002097 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002098 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002099 ret = FAIL;
2100 else
2101 {
2102 funcexe_T funcexe;
2103
2104 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002105 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002106 funcexe.fe_firstline = curwin->w_cursor.lnum;
2107 funcexe.fe_lastline = curwin->w_cursor.lnum;
2108 funcexe.fe_evaluate = evaluate;
2109 funcexe.fe_partial = partial;
2110 funcexe.fe_basetv = basetv;
2111 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002112 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002113 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002114 }
2115 vim_free(s);
2116
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002117 // If evaluate is FALSE rettv->v_type was not set in
2118 // get_func_tv, but it's needed in handle_subscript() to parse
2119 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002120 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2121 {
2122 rettv->vval.v_string = NULL;
2123 rettv->v_type = VAR_FUNC;
2124 }
2125
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002126 // Stop the expression evaluation when immediately
2127 // aborting on error, or when an interrupt occurred or
2128 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002129 if (evaluate && aborting())
2130 {
2131 if (ret == OK)
2132 clear_tv(rettv);
2133 ret = FAIL;
2134 }
2135 return ret;
2136}
2137
2138/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002139 * Get the next line source line without advancing. But do skip over comment
2140 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002141 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002142 */
2143 static char_u *
2144getline_peek_skip_comments(evalarg_T *evalarg)
2145{
2146 for (;;)
2147 {
2148 char_u *next = getline_peek(evalarg->eval_getline,
2149 evalarg->eval_cookie);
2150 char_u *p;
2151
2152 if (next == NULL)
2153 break;
2154 p = skipwhite(next);
2155 if (*p != NUL && !vim9_comment_start(p))
2156 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002157 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002158 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002159 }
2160 return NULL;
2161}
2162
2163/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002164 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2165 * comment) and there is a next line, return the next line (skipping blanks)
2166 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002167 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2168 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002169 * "arg" must point somewhere inside a line, not at the start.
2170 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002171 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002172eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2173{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002174 char_u *p = skipwhite(arg);
2175
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002176 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002177 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002178 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002179 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2180 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002181 && (*p == NUL || *p == NL
2182 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002183 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002184 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002185
Bram Moolenaare442d592022-05-05 12:20:28 +01002186 if (*p == NL)
2187 next = p + 1;
2188 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002189 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002190 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002191 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002192
Bram Moolenaar9d489562020-07-30 20:08:50 +02002193 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002194 {
2195 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002196 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002197 }
2198 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002199 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002200}
2201
2202/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002203 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002204 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002205 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002206 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002207eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002208{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002209 garray_T *gap = &evalarg->eval_ga;
2210 char_u *line;
2211
Bram Moolenaar39be4982022-05-06 21:51:50 +01002212 if (arg != NULL)
2213 {
2214 if (*arg == NL)
2215 return skipwhite(arg + 1);
2216 // Truncate before a trailing comment, so that concatenating the lines
2217 // won't turn the rest into a comment.
2218 if (*skipwhite(arg) == '#')
2219 *arg = NUL;
2220 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002221
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002222 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002223 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2224 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002225 else
2226 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002227 if (line == NULL)
2228 return NULL;
2229
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002230 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002231 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2232 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002233 char_u *p = skipwhite(line);
2234
2235 // Going to concatenate the lines after parsing. For an empty or
2236 // comment line use an empty string.
2237 if (*p == NUL || vim9_comment_start(p))
2238 {
2239 vim_free(line);
2240 line = vim_strsave((char_u *)"");
2241 }
2242
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002243 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2244 ++gap->ga_len;
2245 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002246 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002247 {
2248 vim_free(evalarg->eval_tofree);
2249 evalarg->eval_tofree = line;
2250 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002251
2252 // Advanced to the next line, "arg" no longer points into the previous
2253 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002254 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002255 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002256}
2257
Bram Moolenaare6b53242020-07-01 17:28:33 +02002258/*
2259 * Call eval_next_non_blank() and get the next line if needed.
2260 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002261 char_u *
2262skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2263{
2264 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002265 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002266
Bram Moolenaar962d7212020-07-04 14:15:00 +02002267 if (evalarg == NULL)
2268 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002269 eval_next_non_blank(p, evalarg, &getnext);
2270 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002271 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002272 return p;
2273}
2274
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002275/*
Bram Moolenaar844fb642021-10-23 13:32:30 +01002276 * Initialize "evalarg" for use.
2277 */
2278 void
2279init_evalarg(evalarg_T *evalarg)
2280{
2281 CLEAR_POINTER(evalarg);
2282 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
2283}
2284
2285/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002286 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002287 */
2288 void
2289clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2290{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002291 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002292 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002293 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002294 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002295 if (eap != NULL)
2296 {
2297 // We may need to keep the original command line, e.g. for
2298 // ":let" it has the variable names. But we may also need the
2299 // new one, "nextcmd" points into it. Keep both.
2300 vim_free(eap->cmdline_tofree);
2301 eap->cmdline_tofree = *eap->cmdlinep;
2302 *eap->cmdlinep = evalarg->eval_tofree;
2303 }
2304 else
2305 vim_free(evalarg->eval_tofree);
2306 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002307 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002308
Bram Moolenaar844fb642021-10-23 13:32:30 +01002309 ga_clear_strings(&evalarg->eval_tofree_ga);
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002310 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002311 }
2312}
2313
2314/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2318 */
2319
2320/*
2321 * Handle zero level expression.
2322 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002324 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002325 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 * Return OK or FAIL.
2327 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002328 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002329eval0(
2330 char_u *arg,
2331 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002332 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002333 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002335 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2336}
2337
2338/*
2339 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2340 * expression and don't check what comes after the expression.
2341 */
2342 int
2343eval0_retarg(
2344 char_u *arg,
2345 typval_T *rettv,
2346 exarg_T *eap,
2347 evalarg_T *evalarg,
2348 char_u **retarg)
2349{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 int ret;
2351 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002352 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002353 int did_emsg_before = did_emsg;
2354 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002355 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002356 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002357 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358
2359 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002360 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002361 expr_end = p;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002362 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002363
Bram Moolenaar02929a32021-12-17 14:46:12 +00002364 // In Vim9 script a command block is not split at NL characters for
2365 // commands using an expression argument. Skip over a '#' comment to check
2366 // for a following NL. Require white space before the '#'.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002367 if (in_vim9script() && p > expr_end && retarg == NULL)
Bram Moolenaar02929a32021-12-17 14:46:12 +00002368 while (*p == '#')
2369 {
2370 char_u *nl = vim_strchr(p, NL);
2371
2372 if (nl == NULL)
2373 break;
2374 p = skipwhite(nl + 1);
2375 if (eap != NULL && *p != NUL)
2376 eap->nextcmd = p;
2377 check_for_end = FALSE;
2378 }
2379
2380 if (ret != FAIL && check_for_end)
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002381 end_error = !ends_excmd2(arg, p);
2382 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 {
2384 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002385 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 /*
2387 * Report the invalid expression unless the expression evaluation has
2388 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002389 * exception, or we already gave a more specific error.
2390 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002392 if (!aborting()
2393 && did_emsg == did_emsg_before
2394 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002395 && (flags & EVAL_CONSTANT) == 0
2396 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002397 {
2398 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002399 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002400 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002401 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002402 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002403
2404 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002405 // a next command to avoid more errors, unless "|" is following, which
2406 // could only be a command separator.
2407 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2408 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002409 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002411
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002412 if (retarg != NULL)
2413 *retarg = p;
2414 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002415 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002416
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 return ret;
2418}
2419
2420/*
2421 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002422 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002423 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 *
2425 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002426 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002428 * Note: "rettv.v_lock" is not set.
2429 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 * Return OK or FAIL.
2431 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002432 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002433eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002435 char_u *p;
2436 int getnext;
2437
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002438 CLEAR_POINTER(rettv);
2439
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 /*
2441 * Get the first variable.
2442 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002443 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 return FAIL;
2445
Bram Moolenaar793648f2020-06-26 21:28:25 +02002446 p = eval_next_non_blank(*arg, evalarg, &getnext);
2447 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002449 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002450 int result;
2451 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002452 evalarg_T *evalarg_used = evalarg;
2453 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002454 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002455 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002456 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002457
2458 if (evalarg == NULL)
2459 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002460 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002461 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002462 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002463 orig_flags = evalarg_used->eval_flags;
2464 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002465
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002466 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002467 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002468 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002469 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002470 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002471 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002472 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002473 clear_tv(rettv);
2474 return FAIL;
2475 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002476 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002477 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002478
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002480 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002482 int error = FALSE;
2483
Bram Moolenaar13106602020-10-04 16:06:05 +02002484 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002485 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002486 else if (vim9script)
2487 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002488 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002490 if (error || !op_falsy || !result)
2491 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002492 if (error)
2493 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 }
2495
2496 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002497 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002499 if (op_falsy)
2500 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002501 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002502 {
mityu4ac198c2021-05-28 17:52:40 +02002503 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002504 clear_tv(rettv);
2505 return FAIL;
2506 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002507 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002508 evalarg_used->eval_flags = (op_falsy ? !result : result)
2509 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2510 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002511 {
2512 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002514 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002515 if (!op_falsy || !result)
2516 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002518 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002520 /*
2521 * Check for the ":".
2522 */
2523 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2524 if (*p != ':')
2525 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002526 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002527 if (evaluate && result)
2528 clear_tv(rettv);
2529 evalarg_used->eval_flags = orig_flags;
2530 return FAIL;
2531 }
2532 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002533 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002534 else
2535 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002536 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002537 {
2538 error_white_both(p, 1);
2539 clear_tv(rettv);
2540 evalarg_used->eval_flags = orig_flags;
2541 return FAIL;
2542 }
2543 *arg = p;
2544 }
2545
2546 /*
2547 * Get the third variable. Recursive!
2548 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002549 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002550 {
mityu4ac198c2021-05-28 17:52:40 +02002551 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002552 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002553 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002554 return FAIL;
2555 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002556 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2557 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002558 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002559 if (eval1(arg, &var2, evalarg_used) == FAIL)
2560 {
2561 if (evaluate && result)
2562 clear_tv(rettv);
2563 evalarg_used->eval_flags = orig_flags;
2564 return FAIL;
2565 }
2566 if (evaluate && !result)
2567 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002569
2570 if (evalarg == NULL)
2571 clear_evalarg(&local_evalarg, NULL);
2572 else
2573 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 }
2575
2576 return OK;
2577}
2578
2579/*
2580 * Handle first level expression:
2581 * expr2 || expr2 || expr2 logical OR
2582 *
2583 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002584 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 *
2586 * Return OK or FAIL.
2587 */
2588 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002589eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002591 char_u *p;
2592 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593
2594 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002595 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002597 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 return FAIL;
2599
2600 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002601 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002603 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002604 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002606 evalarg_T *evalarg_used = evalarg;
2607 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002608 int evaluate;
2609 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002610 long result = FALSE;
2611 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002612 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002613 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002614
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002615 if (evalarg == NULL)
2616 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002617 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002618 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002619 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002620 orig_flags = evalarg_used->eval_flags;
2621 evaluate = orig_flags & EVAL_EVALUATE;
2622 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002623 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002624 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002625 result = tv_get_bool_chk(rettv, &error);
2626 else if (tv_get_number_chk(rettv, &error) != 0)
2627 result = TRUE;
2628 clear_tv(rettv);
2629 if (error)
2630 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 }
2632
2633 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002634 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002636 while (p[0] == '|' && p[1] == '|')
2637 {
2638 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002639 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002640 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002641 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002642 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002643 {
2644 error_white_both(p, 2);
2645 clear_tv(rettv);
2646 return FAIL;
2647 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002648 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002649 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002650
2651 /*
2652 * Get the second variable.
2653 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002654 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002655 {
mityu4ac198c2021-05-28 17:52:40 +02002656 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002657 clear_tv(rettv);
2658 return FAIL;
2659 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002660 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2661 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002662 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002663 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002664 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002665
2666 /*
2667 * Compute the result.
2668 */
2669 if (evaluate && !result)
2670 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002671 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002672 result = tv_get_bool_chk(&var2, &error);
2673 else if (tv_get_number_chk(&var2, &error) != 0)
2674 result = TRUE;
2675 clear_tv(&var2);
2676 if (error)
2677 return FAIL;
2678 }
2679 if (evaluate)
2680 {
2681 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002682 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002683 rettv->v_type = VAR_BOOL;
2684 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002685 }
2686 else
2687 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002688 rettv->v_type = VAR_NUMBER;
2689 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002690 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002691 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002692
2693 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002695
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002696 if (evalarg == NULL)
2697 clear_evalarg(&local_evalarg, NULL);
2698 else
2699 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 }
2701
2702 return OK;
2703}
2704
2705/*
2706 * Handle second level expression:
2707 * expr3 && expr3 && expr3 logical AND
2708 *
2709 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002710 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 *
2712 * Return OK or FAIL.
2713 */
2714 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002715eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002717 char_u *p;
2718 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719
2720 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002721 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002723 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 return FAIL;
2725
2726 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002727 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002729 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002730 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002732 evalarg_T *evalarg_used = evalarg;
2733 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002734 int orig_flags;
2735 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002736 long result = TRUE;
2737 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002738 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002739 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002740
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002741 if (evalarg == NULL)
2742 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002743 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002744 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002745 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002746 orig_flags = evalarg_used->eval_flags;
2747 evaluate = orig_flags & EVAL_EVALUATE;
2748 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002749 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002750 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002751 result = tv_get_bool_chk(rettv, &error);
2752 else if (tv_get_number_chk(rettv, &error) == 0)
2753 result = FALSE;
2754 clear_tv(rettv);
2755 if (error)
2756 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 }
2758
2759 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002760 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002762 while (p[0] == '&' && p[1] == '&')
2763 {
2764 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002765 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002766 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002767 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002768 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002769 {
2770 error_white_both(p, 2);
2771 clear_tv(rettv);
2772 return FAIL;
2773 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002774 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002775 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002776
2777 /*
2778 * Get the second variable.
2779 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002780 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002781 {
mityu4ac198c2021-05-28 17:52:40 +02002782 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002783 clear_tv(rettv);
2784 return FAIL;
2785 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002786 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2787 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002788 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002789 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002790 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002791 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002792
2793 /*
2794 * Compute the result.
2795 */
2796 if (evaluate && result)
2797 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002798 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002799 result = tv_get_bool_chk(&var2, &error);
2800 else if (tv_get_number_chk(&var2, &error) == 0)
2801 result = FALSE;
2802 clear_tv(&var2);
2803 if (error)
2804 return FAIL;
2805 }
2806 if (evaluate)
2807 {
2808 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002809 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002810 rettv->v_type = VAR_BOOL;
2811 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002812 }
2813 else
2814 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002815 rettv->v_type = VAR_NUMBER;
2816 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002817 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002818 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002819
2820 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002822
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002823 if (evalarg == NULL)
2824 clear_evalarg(&local_evalarg, NULL);
2825 else
2826 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 }
2828
2829 return OK;
2830}
2831
2832/*
2833 * Handle third level expression:
2834 * var1 == var2
2835 * var1 =~ var2
2836 * var1 != var2
2837 * var1 !~ var2
2838 * var1 > var2
2839 * var1 >= var2
2840 * var1 < var2
2841 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002842 * var1 is var2
2843 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 *
2845 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002846 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 *
2848 * Return OK or FAIL.
2849 */
2850 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002851eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002854 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002855 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002857 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858
2859 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002860 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002862 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 return FAIL;
2864
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002865 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002866
Bram Moolenaar696ba232020-07-29 21:20:41 +02002867 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868
2869 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002870 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002872 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002874 typval_T var2;
2875 int ic;
2876 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002877 int evaluate = evalarg == NULL
2878 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002879 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002880
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002881 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002882 {
Bram Moolenaare442d592022-05-05 12:20:28 +01002883 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002884 p = *arg;
2885 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002886 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2887 {
mityu4ac198c2021-05-28 17:52:40 +02002888 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002889 clear_tv(rettv);
2890 return FAIL;
2891 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002892
Bram Moolenaar696ba232020-07-29 21:20:41 +02002893 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2894 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002895 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002896 clear_tv(rettv);
2897 return FAIL;
2898 }
2899
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002900 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 if (p[len] == '?')
2902 {
2903 ic = TRUE;
2904 ++len;
2905 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002906 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 else if (p[len] == '#')
2908 {
2909 ic = FALSE;
2910 ++len;
2911 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002912 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002914 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915
2916 /*
2917 * Get the second variable.
2918 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002919 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2920 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002921 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002922 clear_tv(rettv);
2923 return FAIL;
2924 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002925 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002926 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002928 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 return FAIL;
2930 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002931 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002932 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002933 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002934
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002935 // use the line of the comparison for messages
2936 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002937 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002938 {
2939 ret = FAIL;
2940 clear_tv(rettv);
2941 }
2942 else
2943 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002944 clear_tv(&var2);
2945 return ret;
2946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 }
2948
2949 return OK;
2950}
2951
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002952/*
2953 * Make a copy of blob "tv1" and append blob "tv2".
2954 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955 void
2956eval_addblob(typval_T *tv1, typval_T *tv2)
2957{
2958 blob_T *b1 = tv1->vval.v_blob;
2959 blob_T *b2 = tv2->vval.v_blob;
2960 blob_T *b = blob_alloc();
2961 int i;
2962
2963 if (b != NULL)
2964 {
2965 for (i = 0; i < blob_len(b1); i++)
2966 ga_append(&b->bv_ga, blob_get(b1, i));
2967 for (i = 0; i < blob_len(b2); i++)
2968 ga_append(&b->bv_ga, blob_get(b2, i));
2969
2970 clear_tv(tv1);
2971 rettv_blob_set(tv1, b);
2972 }
2973}
2974
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002975/*
2976 * Make a copy of list "tv1" and append list "tv2".
2977 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978 int
2979eval_addlist(typval_T *tv1, typval_T *tv2)
2980{
2981 typval_T var3;
2982
2983 // concatenate Lists
2984 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2985 {
2986 clear_tv(tv1);
2987 clear_tv(tv2);
2988 return FAIL;
2989 }
2990 clear_tv(tv1);
2991 *tv1 = var3;
2992 return OK;
2993}
2994
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002996 * Handle the bitwise left/right shift operator expression:
2997 * var1 << var2
2998 * var1 >> var2
2999 *
3000 * "arg" must point to the first non-white of the expression.
3001 * "arg" is advanced to just after the recognized expression.
3002 *
3003 * Return OK or FAIL.
3004 */
3005 static int
3006eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3007{
3008 /*
3009 * Get the first expression.
3010 */
3011 if (eval6(arg, rettv, evalarg) == FAIL)
3012 return FAIL;
3013
3014 /*
3015 * Repeat computing, until no '<<' or '>>' is following.
3016 */
3017 for (;;)
3018 {
3019 char_u *p;
3020 int getnext;
3021 exprtype_T type;
3022 int evaluate;
3023 typval_T var2;
3024 int vim9script;
3025
3026 p = eval_next_non_blank(*arg, evalarg, &getnext);
3027 if (p[0] == '<' && p[1] == '<')
3028 type = EXPR_LSHIFT;
3029 else if (p[0] == '>' && p[1] == '>')
3030 type = EXPR_RSHIFT;
3031 else
3032 return OK;
3033
3034 // Handle a bitwise left or right shift operator
3035 if (rettv->v_type != VAR_NUMBER)
3036 {
3037 // left operand should be a number
3038 emsg(_(e_bitshift_ops_must_be_number));
3039 clear_tv(rettv);
3040 return FAIL;
3041 }
3042
3043 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3044 vim9script = in_vim9script();
3045 if (getnext)
3046 {
3047 *arg = eval_next_line(*arg, evalarg);
3048 p = *arg;
3049 }
3050 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3051 {
3052 error_white_both(*arg, 2);
3053 clear_tv(rettv);
3054 return FAIL;
3055 }
3056
3057 /*
3058 * Get the second variable.
3059 */
3060 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3061 {
3062 error_white_both(p, 2);
3063 clear_tv(rettv);
3064 return FAIL;
3065 }
3066 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3067 if (eval6(arg, &var2, evalarg) == FAIL)
3068 {
3069 clear_tv(rettv);
3070 return FAIL;
3071 }
3072
3073 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3074 {
3075 // right operand should be a positive number
3076 if (var2.v_type != VAR_NUMBER)
3077 emsg(_(e_bitshift_ops_must_be_number));
3078 else
3079 emsg(_(e_bitshift_ops_must_be_postive));
3080 clear_tv(rettv);
3081 clear_tv(&var2);
3082 return FAIL;
3083 }
3084
3085 if (evaluate)
3086 {
3087 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3088 // shifting more bits than we have always results in zero
3089 rettv->vval.v_number = 0;
3090 else if (type == EXPR_LSHIFT)
3091 rettv->vval.v_number =
3092 rettv->vval.v_number << var2.vval.v_number;
3093 else
3094 {
3095 rettv->vval.v_number =
3096 rettv->vval.v_number >> var2.vval.v_number;
3097 // clear the topmost sign bit
3098 rettv->vval.v_number &= ~((uvarnumber_T)1 << MAX_LSHIFT_BITS);
3099 }
3100 }
3101
3102 clear_tv(&var2);
3103 }
3104
3105 return OK;
3106}
3107
3108/*
3109 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003110 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003112 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003113 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 *
3115 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003116 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 *
3118 * Return OK or FAIL.
3119 */
3120 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003121eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003124 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003126 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 return FAIL;
3128
3129 /*
3130 * Repeat computing, until no '+', '-' or '.' is following.
3131 */
3132 for (;;)
3133 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003134 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003135 int getnext;
3136 char_u *p;
3137 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003138 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003139 int concat;
3140 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003141 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003142
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003143 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003144 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003145 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003146 p = eval_next_non_blank(*arg, evalarg, &getnext);
3147 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003148 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003149 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3150 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003152 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3153 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003154
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003155 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003156 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003157 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003158 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003159 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003160 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003161 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003162 {
mityu4ac198c2021-05-28 17:52:40 +02003163 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003164 clear_tv(rettv);
3165 return FAIL;
3166 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003167 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003168 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003169 if ((op != '+' || (rettv->v_type != VAR_LIST
3170 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003171#ifdef FEAT_FLOAT
3172 && (op == '.' || rettv->v_type != VAR_FLOAT)
3173#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003174 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003175 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003176 int error = FALSE;
3177
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003178 // For "list + ...", an illegal use of the first operand as
3179 // a number cannot be determined before evaluating the 2nd
3180 // operand: if this is also a list, all is ok.
3181 // For "something . ...", "something - ..." or "non-list + ...",
3182 // we know that the first operand needs to be a string or number
3183 // without evaluating the 2nd operand. So check before to avoid
3184 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003185 if (op != '.')
3186 tv_get_number_chk(rettv, &error);
3187 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003188 {
3189 clear_tv(rettv);
3190 return FAIL;
3191 }
3192 }
3193
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 /*
3195 * Get the second variable.
3196 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003197 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003198 {
mityu89dcb4d2021-05-28 13:50:17 +02003199 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003200 clear_tv(rettv);
3201 return FAIL;
3202 }
3203 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003204 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003206 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 return FAIL;
3208 }
3209
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003210 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 {
3212 /*
3213 * Compute the result.
3214 */
3215 if (op == '.')
3216 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003217 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3218 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003219 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003220
Bram Moolenaar2e086612020-08-25 22:37:48 +02003221 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003222 || var2.v_type == VAR_CHANNEL
3223 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003224 semsg(_(e_using_invalid_value_as_string_str),
3225 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003226#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003227 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003228 {
3229 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3230 var2.vval.v_float);
3231 s2 = buf2;
3232 }
3233#endif
3234 else
3235 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003236 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003237 {
3238 clear_tv(rettv);
3239 clear_tv(&var2);
3240 return FAIL;
3241 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003242 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003243 clear_tv(rettv);
3244 rettv->v_type = VAR_STRING;
3245 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003247 else if (op == '+' && rettv->v_type == VAR_BLOB
3248 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003250 else if (op == '+' && rettv->v_type == VAR_LIST
3251 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003252 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003253 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003254 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 else
3257 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003258 int error = FALSE;
3259 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003260#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003261 float_T f1 = 0, f2 = 0;
3262
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003263 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003264 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003265 f1 = rettv->vval.v_float;
3266 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003267 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003268 else
3269#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003270 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003271 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003272 if (error)
3273 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003274 // This can only happen for "list + non-list" or
3275 // "blob + non-blob". For "non-list + ..." or
3276 // "something - ...", we returned before evaluating the
3277 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003278 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003279 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003280 return FAIL;
3281 }
3282#ifdef FEAT_FLOAT
3283 if (var2.v_type == VAR_FLOAT)
3284 f1 = n1;
3285#endif
3286 }
3287#ifdef FEAT_FLOAT
3288 if (var2.v_type == VAR_FLOAT)
3289 {
3290 f2 = var2.vval.v_float;
3291 n2 = 0;
3292 }
3293 else
3294#endif
3295 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003296 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003297 if (error)
3298 {
3299 clear_tv(rettv);
3300 clear_tv(&var2);
3301 return FAIL;
3302 }
3303#ifdef FEAT_FLOAT
3304 if (rettv->v_type == VAR_FLOAT)
3305 f2 = n2;
3306#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003307 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003308 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003309
3310#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003311 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003312 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3313 {
3314 if (op == '+')
3315 f1 = f1 + f2;
3316 else
3317 f1 = f1 - f2;
3318 rettv->v_type = VAR_FLOAT;
3319 rettv->vval.v_float = f1;
3320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003322#endif
3323 {
3324 if (op == '+')
3325 n1 = n1 + n2;
3326 else
3327 n1 = n1 - n2;
3328 rettv->v_type = VAR_NUMBER;
3329 rettv->vval.v_number = n1;
3330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003332 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 }
3334 }
3335 return OK;
3336}
3337
3338/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003339 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 * * number multiplication
3341 * / number division
3342 * % number modulo
3343 *
3344 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003345 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 *
3347 * Return OK or FAIL.
3348 */
3349 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003350eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003351 char_u **arg,
3352 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003353 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003354 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003356#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003357 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359
3360 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003361 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003363 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 return FAIL;
3365
3366 /*
3367 * Repeat computing, until no '*', '/' or '%' is following.
3368 */
3369 for (;;)
3370 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003371 int evaluate;
3372 int getnext;
3373 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003374 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003375 int op;
3376 varnumber_T n1, n2;
3377#ifdef FEAT_FLOAT
3378 float_T f1, f2;
3379#endif
3380 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003381
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003382 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003383 p = eval_next_non_blank(*arg, evalarg, &getnext);
3384 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003385 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003387
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003388 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003389 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003390 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003391 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003392 {
3393 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3394 {
mityu4ac198c2021-05-28 17:52:40 +02003395 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003396 clear_tv(rettv);
3397 return FAIL;
3398 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003399 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003402#ifdef FEAT_FLOAT
3403 f1 = 0;
3404 f2 = 0;
3405#endif
3406 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003407 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003409#ifdef FEAT_FLOAT
3410 if (rettv->v_type == VAR_FLOAT)
3411 {
3412 f1 = rettv->vval.v_float;
3413 use_float = TRUE;
3414 n1 = 0;
3415 }
3416 else
3417#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003418 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003419 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003420 if (error)
3421 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 }
3423 else
3424 n1 = 0;
3425
3426 /*
3427 * Get the second variable.
3428 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003429 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3430 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003431 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003432 clear_tv(rettv);
3433 return FAIL;
3434 }
3435 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003436 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 return FAIL;
3438
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003439 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003441#ifdef FEAT_FLOAT
3442 if (var2.v_type == VAR_FLOAT)
3443 {
3444 if (!use_float)
3445 {
3446 f1 = n1;
3447 use_float = TRUE;
3448 }
3449 f2 = var2.vval.v_float;
3450 n2 = 0;
3451 }
3452 else
3453#endif
3454 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003455 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003456 clear_tv(&var2);
3457 if (error)
3458 return FAIL;
3459#ifdef FEAT_FLOAT
3460 if (use_float)
3461 f2 = n2;
3462#endif
3463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464
3465 /*
3466 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003467 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003469#ifdef FEAT_FLOAT
3470 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003472 if (op == '*')
3473 f1 = f1 * f2;
3474 else if (op == '/')
3475 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003476# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003477 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003478 if (f2 == 0.0)
3479 {
3480 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003481 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003482 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003483 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003484 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003485 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003486 }
3487 else
3488 f1 = f1 / f2;
3489# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003490 // We rely on the floating point library to handle divide
3491 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003492 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003493# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003496 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003497 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003498 return FAIL;
3499 }
3500 rettv->v_type = VAR_FLOAT;
3501 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 }
3503 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003506 int failed = FALSE;
3507
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003508 if (op == '*')
3509 n1 = n1 * n2;
3510 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003511 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003513 n1 = num_modulus(n1, n2, &failed);
3514 if (failed)
3515 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003516
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003517 rettv->v_type = VAR_NUMBER;
3518 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 }
3521 }
3522
3523 return OK;
3524}
3525
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003526/*
3527 * Handle a type cast before a base level expression.
3528 * "arg" must point to the first non-white of the expression.
3529 * "arg" is advanced to just after the recognized expression.
3530 * Return OK or FAIL.
3531 */
3532 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003533eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003534 char_u **arg,
3535 typval_T *rettv,
3536 evalarg_T *evalarg,
3537 int want_string) // after "." operator
3538{
3539 type_T *want_type = NULL;
3540 garray_T type_list; // list of pointers to allocated types
3541 int res;
3542 int evaluate = evalarg == NULL ? 0
3543 : (evalarg->eval_flags & EVAL_EVALUATE);
3544
3545 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003546 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3547 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003548 {
3549 ++*arg;
3550 ga_init2(&type_list, sizeof(type_T *), 10);
3551 want_type = parse_type(arg, &type_list, TRUE);
3552 if (want_type == NULL && (evaluate || **arg != '>'))
3553 {
3554 clear_type_list(&type_list);
3555 return FAIL;
3556 }
3557
3558 if (**arg != '>')
3559 {
3560 if (*skipwhite(*arg) == '>')
3561 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3562 else
3563 emsg(_(e_missing_gt));
3564 clear_type_list(&type_list);
3565 return FAIL;
3566 }
3567 ++*arg;
3568 *arg = skipwhite_and_linebreak(*arg, evalarg);
3569 }
3570
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003571 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003572
3573 if (want_type != NULL && evaluate)
3574 {
3575 if (res == OK)
3576 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003577 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3578 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003579
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003580 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003581 {
3582 if (want_type == &t_bool && actual != &t_bool
3583 && (actual->tt_flags & TTFLAG_BOOL_OK))
3584 {
3585 int n = tv2bool(rettv);
3586
3587 // can use "0" and "1" for boolean in some places
3588 clear_tv(rettv);
3589 rettv->v_type = VAR_BOOL;
3590 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3591 }
3592 else
3593 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003594 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003595
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003596 where.wt_variable = TRUE;
3597 res = check_type(want_type, actual, TRUE, where);
3598 }
3599 }
3600 }
3601 clear_type_list(&type_list);
3602 }
3603
3604 return res;
3605}
3606
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003607 int
3608eval_leader(char_u **arg, int vim9)
3609{
3610 char_u *s = *arg;
3611 char_u *p = *arg;
3612
3613 while (*p == '!' || *p == '-' || *p == '+')
3614 {
3615 char_u *n = skipwhite(p + 1);
3616
3617 // ++, --, -+ and +- are not accepted in Vim9 script
3618 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3619 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003620 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003621 return FAIL;
3622 }
3623 p = n;
3624 }
3625 *arg = p;
3626 return OK;
3627}
3628
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003630 * Check for a predefined value "true", "false" and "null.*".
3631 * Return OK when recognized.
3632 */
3633 int
3634handle_predefined(char_u *s, int len, typval_T *rettv)
3635{
3636 switch (len)
3637 {
3638 case 4: if (STRNCMP(s, "true", 4) == 0)
3639 {
3640 rettv->v_type = VAR_BOOL;
3641 rettv->vval.v_number = VVAL_TRUE;
3642 return OK;
3643 }
3644 if (STRNCMP(s, "null", 4) == 0)
3645 {
3646 rettv->v_type = VAR_SPECIAL;
3647 rettv->vval.v_number = VVAL_NULL;
3648 return OK;
3649 }
3650 break;
3651 case 5: if (STRNCMP(s, "false", 5) == 0)
3652 {
3653 rettv->v_type = VAR_BOOL;
3654 rettv->vval.v_number = VVAL_FALSE;
3655 return OK;
3656 }
3657 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003658 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3659 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003660#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003661 rettv->v_type = VAR_JOB;
3662 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003663#else
3664 rettv->v_type = VAR_SPECIAL;
3665 rettv->vval.v_number = VVAL_NULL;
3666#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003667 return OK;
3668 }
3669 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003670 case 9:
3671 if (STRNCMP(s, "null_", 5) != 0)
3672 break;
3673 if (STRNCMP(s + 5, "list", 4) == 0)
3674 {
3675 rettv->v_type = VAR_LIST;
3676 rettv->vval.v_list = NULL;
3677 return OK;
3678 }
3679 if (STRNCMP(s + 5, "dict", 4) == 0)
3680 {
3681 rettv->v_type = VAR_DICT;
3682 rettv->vval.v_dict = NULL;
3683 return OK;
3684 }
3685 if (STRNCMP(s + 5, "blob", 4) == 0)
3686 {
3687 rettv->v_type = VAR_BLOB;
3688 rettv->vval.v_blob = NULL;
3689 return OK;
3690 }
3691 break;
3692 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3693 {
3694 rettv->v_type = VAR_STRING;
3695 rettv->vval.v_string = NULL;
3696 return OK;
3697 }
3698 break;
3699 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003700 if (STRNCMP(s, "null_channel", 12) == 0)
3701 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003702#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003703 rettv->v_type = VAR_CHANNEL;
3704 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003705#else
3706 rettv->v_type = VAR_SPECIAL;
3707 rettv->vval.v_number = VVAL_NULL;
3708#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003709 return OK;
3710 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003711 if (STRNCMP(s, "null_partial", 12) == 0)
3712 {
3713 rettv->v_type = VAR_PARTIAL;
3714 rettv->vval.v_partial = NULL;
3715 return OK;
3716 }
3717 break;
3718 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3719 {
3720 rettv->v_type = VAR_FUNC;
3721 rettv->vval.v_string = NULL;
3722 return OK;
3723 }
3724 break;
3725 }
3726 return FAIL;
3727}
3728
3729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 * Handle sixth level expression:
3731 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003732 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003733 * "string" string constant
3734 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 * &option-name option value
3736 * @r register contents
3737 * identifier variable value
3738 * function() function call
3739 * $VAR environment variable
3740 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003741 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003742 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003743 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003744 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 *
3746 * Also handle:
3747 * ! in front logical NOT
3748 * - in front unary minus
3749 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003750 * trailing [] subscript in String or List
3751 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003752 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 *
3754 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003755 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 *
3757 * Return OK or FAIL.
3758 */
3759 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003760eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003761 char_u **arg,
3762 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003763 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003764 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003766 int evaluate = evalarg != NULL
3767 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 int len;
3769 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003770 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 char_u *start_leader, *end_leader;
3772 int ret = OK;
3773 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003774 static int recurse = 0;
3775 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776
3777 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003778 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003779 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003781 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782
3783 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003784 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 */
3786 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003787 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003788 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 end_leader = *arg;
3790
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003791 if (**arg == '.' && (!isdigit(*(*arg + 1))
3792#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003793 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003794#endif
3795 ))
3796 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003797 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003798 ++*arg;
3799 return FAIL;
3800 }
3801
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003802 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003803 // and crash. With MSVC the stack is smaller.
3804 if (recurse ==
3805#ifdef _MSC_VER
3806 300
3807#else
3808 1000
3809#endif
3810 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003811 {
3812 semsg(_(e_expression_too_recursive_str), *arg);
3813 return FAIL;
3814 }
3815 ++recurse;
3816
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 switch (**arg)
3818 {
3819 /*
3820 * Number constant.
3821 */
3822 case '0':
3823 case '1':
3824 case '2':
3825 case '3':
3826 case '4':
3827 case '5':
3828 case '6':
3829 case '7':
3830 case '8':
3831 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003832 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003833
3834 // Apply prefixed "-" and "+" now. Matters especially when
3835 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003836 if (ret == OK && evaluate && end_leader > start_leader
3837 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003838 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 break;
3840
3841 /*
3842 * String constant: "string".
3843 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003844 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 break;
3846
3847 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003848 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003850 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003851 break;
3852
3853 /*
3854 * List: [expr, expr]
3855 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003856 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 break;
3858
3859 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003860 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003861 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003862 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003863 {
3864 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3865 }
3866 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003867 {
3868 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003869 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003870 }
3871 else
3872 ret = NOTDONE;
3873 break;
3874
3875 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003876 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003877 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003878 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003879 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003880 ret = NOTDONE;
3881 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00003882 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003883 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003884 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003885 break;
3886
3887 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003888 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003890 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 break;
3892
3893 /*
3894 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01003895 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 */
LemonBoy2eaef102022-05-06 13:14:50 +01003897 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
3898 ret = eval_interp_string(arg, rettv, evaluate);
3899 else
3900 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 break;
3902
3903 /*
3904 * Register contents: @r.
3905 */
3906 case '@': ++*arg;
3907 if (evaluate)
3908 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003909 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003910 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003911 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003912 emsg_invreg(**arg);
3913 else
3914 {
3915 rettv->v_type = VAR_STRING;
3916 rettv->vval.v_string = get_reg_contents(**arg,
3917 GREG_EXPR_SRC);
3918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 }
3920 if (**arg != NUL)
3921 ++*arg;
3922 break;
3923
3924 /*
3925 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003926 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003928 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003929 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01003930 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003931 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003932 if (ret == OK && evaluate)
3933 {
3934 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3935
Bram Moolenaara9931532021-06-12 15:58:16 +02003936 // Compile it here to get the return type. The return
3937 // type is optional, when it's missing use t_unknown.
3938 // This is recognized in compile_return().
3939 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3940 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00003941 if (compile_def_function(ufunc, FALSE,
3942 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003943 {
3944 clear_tv(rettv);
3945 ret = FAIL;
3946 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003947 }
3948 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003949 if (ret == NOTDONE)
3950 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003951 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003952 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003953
Bram Moolenaar9215f012020-06-27 21:18:00 +02003954 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003955 if (**arg == ')')
3956 ++*arg;
3957 else if (ret == OK)
3958 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003959 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003960 clear_tv(rettv);
3961 ret = FAIL;
3962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 }
3964 break;
3965
Bram Moolenaar8c711452005-01-14 21:53:12 +00003966 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 break;
3968 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003969
3970 if (ret == NOTDONE)
3971 {
3972 /*
3973 * Must be a variable or function name.
3974 * Can also be a curly-braces kind of name: {expr}.
3975 */
3976 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003977 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003978 if (alias != NULL)
3979 s = alias;
3980
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003981 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003982 ret = FAIL;
3983 else
3984 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003985 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3986
Bram Moolenaar4525a572022-02-13 11:57:33 +00003987 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003988 {
3989 emsg(_(e_cannot_use_underscore_here));
3990 ret = FAIL;
3991 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00003992 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00003993 && s[0] == 's' && s[1] == ':')
3994 {
3995 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
3996 ret = FAIL;
3997 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00003998 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003999 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004000 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004001 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004002 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004003 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02004004 else if (flags & EVAL_CONSTANT)
4005 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004006 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004007 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004008 // get the value of "true", "false", etc. or a variable
4009 ret = FAIL;
4010 if (vim9script)
4011 ret = handle_predefined(s, len, rettv);
4012 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004013 {
4014 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004015 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004016 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004017 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004018 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004019 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004020 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004021 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004022 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004023 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004024 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004025 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004026 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004027 }
4028
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004029 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4030 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004031 if (ret == OK)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004032 ret = handle_subscript(arg, name_start, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033
4034 /*
4035 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4036 */
4037 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004038 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004039
4040 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004041 return ret;
4042}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004043
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004044/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004045 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004046 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004047 * Adjusts "end_leaderp" until it is at "start_leader".
4048 */
4049 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004050eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004051 typval_T *rettv,
4052 int numeric_only,
4053 char_u *start_leader,
4054 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004055{
4056 char_u *end_leader = *end_leaderp;
4057 int ret = OK;
4058 int error = FALSE;
4059 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004060 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004061 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004062#ifdef FEAT_FLOAT
4063 float_T f = 0.0;
4064
4065 if (rettv->v_type == VAR_FLOAT)
4066 f = rettv->vval.v_float;
4067 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004068#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004069 {
4070 while (VIM_ISWHITE(end_leader[-1]))
4071 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004072 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004073 val = tv2bool(rettv);
4074 else
4075 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004076 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004077 if (error)
4078 {
4079 clear_tv(rettv);
4080 ret = FAIL;
4081 }
4082 else
4083 {
4084 while (end_leader > start_leader)
4085 {
4086 --end_leader;
4087 if (*end_leader == '!')
4088 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004089 if (numeric_only)
4090 {
4091 ++end_leader;
4092 break;
4093 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004094#ifdef FEAT_FLOAT
4095 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004096 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004097 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004098 {
4099 rettv->v_type = VAR_BOOL;
4100 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4101 }
4102 else
4103 f = !f;
4104 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004105 else
4106#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004107 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004108 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004109 type = VAR_BOOL;
4110 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004111 }
4112 else if (*end_leader == '-')
4113 {
4114#ifdef FEAT_FLOAT
4115 if (rettv->v_type == VAR_FLOAT)
4116 f = -f;
4117 else
4118#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004119 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004120 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004121 type = VAR_NUMBER;
4122 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004123 }
4124 }
4125#ifdef FEAT_FLOAT
4126 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004128 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004129 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004131 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004132#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004133 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004134 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004135 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004136 rettv->v_type = type;
4137 else
4138 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004139 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004142 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 return ret;
4144}
4145
4146/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004147 * Call the function referred to in "rettv".
4148 */
4149 static int
4150call_func_rettv(
4151 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004152 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004153 typval_T *rettv,
4154 int evaluate,
4155 dict_T *selfdict,
4156 typval_T *basetv)
4157{
4158 partial_T *pt = NULL;
4159 funcexe_T funcexe;
4160 typval_T functv;
4161 char_u *s;
4162 int ret;
4163
4164 // need to copy the funcref so that we can clear rettv
4165 if (evaluate)
4166 {
4167 functv = *rettv;
4168 rettv->v_type = VAR_UNKNOWN;
4169
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004170 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004171 if (functv.v_type == VAR_PARTIAL)
4172 {
4173 pt = functv.vval.v_partial;
4174 s = partial_name(pt);
4175 }
4176 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004177 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004178 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004179 if (s == NULL || *s == NUL)
4180 {
4181 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004182 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004183 goto theend;
4184 }
4185 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004186 }
4187 else
4188 s = (char_u *)"";
4189
Bram Moolenaara80faa82020-04-12 19:37:17 +02004190 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004191 funcexe.fe_firstline = curwin->w_cursor.lnum;
4192 funcexe.fe_lastline = curwin->w_cursor.lnum;
4193 funcexe.fe_evaluate = evaluate;
4194 funcexe.fe_partial = pt;
4195 funcexe.fe_selfdict = selfdict;
4196 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004197 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004198
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004199theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004200 // Clear the funcref afterwards, so that deleting it while
4201 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004202 if (evaluate)
4203 clear_tv(&functv);
4204
4205 return ret;
4206}
4207
4208/*
4209 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004210 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004211 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4212 */
4213 static int
4214eval_lambda(
4215 char_u **arg,
4216 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004217 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004218 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004219{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004220 int evaluate = evalarg != NULL
4221 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004222 typval_T base = *rettv;
4223 int ret;
4224
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004225 rettv->v_type = VAR_UNKNOWN;
4226
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004227 if (**arg == '{')
4228 {
4229 // ->{lambda}()
4230 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4231 }
4232 else
4233 {
4234 // ->(lambda)()
4235 ++*arg;
4236 ret = eval1(arg, rettv, evalarg);
4237 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004238 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004239 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004240 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004241 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004242 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004243 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4244 && rettv->v_type != VAR_PARTIAL)
4245 {
4246 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4247 return FAIL;
4248 }
4249 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004250 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004251 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004252 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004253
4254 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004255 {
4256 if (verbose)
4257 {
4258 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004259 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004260 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004261 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004262 }
4263 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004264 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004265 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004266 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004267 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004268
4269 // Clear the funcref afterwards, so that deleting it while
4270 // evaluating the arguments is possible (see test55).
4271 if (evaluate)
4272 clear_tv(&base);
4273
4274 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004275}
4276
4277/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004278 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004279 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004280 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4281 */
4282 static int
4283eval_method(
4284 char_u **arg,
4285 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004286 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004287 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004288{
4289 char_u *name;
4290 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004291 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004292 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004293 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004294 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004295 int evaluate = evalarg != NULL
4296 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004297
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004298 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004299
Bram Moolenaarac92e252019-08-03 21:58:38 +02004300 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004301 len = get_name_len(arg, &alias, evaluate, TRUE);
4302 if (alias != NULL)
4303 name = alias;
4304
4305 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004306 {
4307 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004308 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004309 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004310 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004311 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004312 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004313 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004314
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004315 // If there is no "(" immediately following, but there is further on,
4316 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4317 // Does not handle anything where "(" is part of the expression.
4318 *arg = skipwhite(*arg);
4319
4320 if (**arg != '(' && alias == NULL
4321 && (paren = vim_strchr(*arg, '(')) != NULL)
4322 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004323 char_u *deref;
4324
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004325 *arg = name;
4326 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004327 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4328 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004329 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004330 *arg = name + len;
4331 ret = FAIL;
4332 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004333 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004334 {
4335 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004336 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004337 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004338 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004339 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004340
4341 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004342 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004343 *arg = skipwhite(*arg);
4344
4345 if (**arg != '(')
4346 {
4347 if (verbose)
4348 semsg(_(e_missing_parenthesis_str), name);
4349 ret = FAIL;
4350 }
4351 else if (VIM_ISWHITE((*arg)[-1]))
4352 {
4353 if (verbose)
4354 emsg(_(e_no_white_space_allowed_before_parenthesis));
4355 ret = FAIL;
4356 }
4357 else
4358 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004359 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004360 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004361 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004362
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004363 // Clear the funcref afterwards, so that deleting it while
4364 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004365 if (evaluate)
4366 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004367 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004368
Bram Moolenaarac92e252019-08-03 21:58:38 +02004369 return ret;
4370}
4371
4372/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004373 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4374 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004375 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4376 */
4377 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004378eval_index(
4379 char_u **arg,
4380 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004381 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004382 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004383{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004384 int evaluate = evalarg != NULL
4385 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004386 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004387 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004388 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004389 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004390 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004391 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004392
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004393 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4394 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004395
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004396 init_tv(&var1);
4397 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004398 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004399 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004400 /*
4401 * dict.name
4402 */
4403 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004404 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004405 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004406 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004407 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004408 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004409 }
4410 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004411 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004412 /*
4413 * something[idx]
4414 *
4415 * Get the (first) variable from inside the [].
4416 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004417 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004418 if (**arg == ':')
4419 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004420 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004421 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004422 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004423 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004424 semsg(_(e_white_space_required_before_and_after_str_at_str),
4425 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004426 clear_tv(&var1);
4427 return FAIL;
4428 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004429 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004430 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004431 int error = FALSE;
4432
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004433#ifdef FEAT_FLOAT
4434 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004435 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004436 && var1.v_type == VAR_FLOAT)
4437 {
4438 var1.vval.v_string = typval_tostring(&var1, TRUE);
4439 var1.v_type = VAR_STRING;
4440 }
4441#endif
Bram Moolenaar4525a572022-02-13 11:57:33 +00004442 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004443 tv_get_number_chk(&var1, &error);
4444 else
4445 error = tv_get_string_chk(&var1) == NULL;
4446 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004447 {
4448 // not a number or string
4449 clear_tv(&var1);
4450 return FAIL;
4451 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004452 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004453
4454 /*
4455 * Get the second variable from inside the [:].
4456 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004457 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004458 if (**arg == ':')
4459 {
4460 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004461 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004462 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004463 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004464 semsg(_(e_white_space_required_before_and_after_str_at_str),
4465 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004466 if (!empty1)
4467 clear_tv(&var1);
4468 return FAIL;
4469 }
4470 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004471 if (**arg == ']')
4472 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004473 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004474 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004475 if (!empty1)
4476 clear_tv(&var1);
4477 return FAIL;
4478 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004479 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004480 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004481 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004482 if (!empty1)
4483 clear_tv(&var1);
4484 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004485 return FAIL;
4486 }
4487 }
4488
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004489 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004490 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004491 if (**arg != ']')
4492 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004493 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004494 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004495 clear_tv(&var1);
4496 if (range)
4497 clear_tv(&var2);
4498 return FAIL;
4499 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004500 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004501 }
4502
4503 if (evaluate)
4504 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004505 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004506 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004507 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004508
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004509 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004510 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004511 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004512 clear_tv(&var2);
4513 return res;
4514 }
4515 return OK;
4516}
4517
4518/*
4519 * Check if "rettv" can have an [index] or [sli:ce]
4520 */
4521 int
4522check_can_index(typval_T *rettv, int evaluate, int verbose)
4523{
4524 switch (rettv->v_type)
4525 {
4526 case VAR_FUNC:
4527 case VAR_PARTIAL:
4528 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004529 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004530 return FAIL;
4531 case VAR_FLOAT:
4532#ifdef FEAT_FLOAT
4533 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004534 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004535 return FAIL;
4536#endif
4537 case VAR_BOOL:
4538 case VAR_SPECIAL:
4539 case VAR_JOB:
4540 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004541 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004542 if (verbose)
4543 emsg(_(e_cannot_index_special_variable));
4544 return FAIL;
4545 case VAR_UNKNOWN:
4546 case VAR_ANY:
4547 case VAR_VOID:
4548 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004549 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004550 emsg(_(e_cannot_index_special_variable));
4551 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004552 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004553 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004554
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004555 case VAR_STRING:
4556 case VAR_LIST:
4557 case VAR_DICT:
4558 case VAR_BLOB:
4559 break;
4560 case VAR_NUMBER:
4561 if (in_vim9script())
4562 emsg(_(e_cannot_index_number));
4563 break;
4564 }
4565 return OK;
4566}
4567
4568/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004569 * slice() function
4570 */
4571 void
4572f_slice(typval_T *argvars, typval_T *rettv)
4573{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004574 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004575 && ((argvars[0].v_type != VAR_STRING
4576 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004577 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004578 && check_for_list_arg(argvars, 0) == FAIL)
4579 || check_for_number_arg(argvars, 1) == FAIL
4580 || check_for_opt_number_arg(argvars, 2) == FAIL))
4581 return;
4582
Bram Moolenaar6601b622021-01-13 21:47:15 +01004583 if (check_can_index(argvars, TRUE, FALSE) == OK)
4584 {
4585 copy_tv(argvars, rettv);
4586 eval_index_inner(rettv, TRUE, argvars + 1,
4587 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4588 TRUE, NULL, 0, FALSE);
4589 }
4590}
4591
4592/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004593 * Apply index or range to "rettv".
4594 * "var1" is the first index, NULL for [:expr].
4595 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004596 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4597 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004598 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4599 */
4600 int
4601eval_index_inner(
4602 typval_T *rettv,
4603 int is_range,
4604 typval_T *var1,
4605 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004606 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004607 char_u *key,
4608 int keylen,
4609 int verbose)
4610{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004611 varnumber_T n1, n2 = 0;
4612 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004613
4614 n1 = 0;
4615 if (var1 != NULL && rettv->v_type != VAR_DICT)
4616 n1 = tv_get_number(var1);
4617
4618 if (is_range)
4619 {
4620 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004621 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004622 if (verbose)
4623 emsg(_(e_cannot_slice_dictionary));
4624 return FAIL;
4625 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004626 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004627 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004628 else
4629 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004630 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004631
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004632 switch (rettv->v_type)
4633 {
4634 case VAR_UNKNOWN:
4635 case VAR_ANY:
4636 case VAR_VOID:
4637 case VAR_FUNC:
4638 case VAR_PARTIAL:
4639 case VAR_FLOAT:
4640 case VAR_BOOL:
4641 case VAR_SPECIAL:
4642 case VAR_JOB:
4643 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004644 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004645 break; // not evaluating, skipping over subscript
4646
4647 case VAR_NUMBER:
4648 case VAR_STRING:
4649 {
4650 char_u *s = tv_get_string(rettv);
4651
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004652 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004653 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004654 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004655 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004656 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004657 else
4658 s = char_from_string(s, n1);
4659 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004660 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004661 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004662 // The resulting variable is a substring. If the indexes
4663 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004664 if (n1 < 0)
4665 {
4666 n1 = len + n1;
4667 if (n1 < 0)
4668 n1 = 0;
4669 }
4670 if (n2 < 0)
4671 n2 = len + n2;
4672 else if (n2 >= len)
4673 n2 = len;
4674 if (n1 >= len || n2 < 0 || n1 > n2)
4675 s = NULL;
4676 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004677 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004678 }
4679 else
4680 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004681 // The resulting variable is a string of a single
4682 // character. If the index is too big or negative the
4683 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004684 if (n1 >= len || n1 < 0)
4685 s = NULL;
4686 else
4687 s = vim_strnsave(s + n1, 1);
4688 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004689 clear_tv(rettv);
4690 rettv->v_type = VAR_STRING;
4691 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004692 }
4693 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004694
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004695 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004696 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4697 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004698 break;
4699
4700 case VAR_LIST:
4701 if (var1 == NULL)
4702 n1 = 0;
4703 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004704 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004705 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004706 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004707 return FAIL;
4708 break;
4709
4710 case VAR_DICT:
4711 {
4712 dictitem_T *item;
4713 typval_T tmp;
4714
4715 if (key == NULL)
4716 {
4717 key = tv_get_string_chk(var1);
4718 if (key == NULL)
4719 return FAIL;
4720 }
4721
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004722 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004723
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004724 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004725 {
4726 if (verbose)
4727 {
4728 if (keylen > 0)
4729 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004730 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004731 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004732 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004733 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004734
4735 copy_tv(&item->di_tv, &tmp);
4736 clear_tv(rettv);
4737 *rettv = tmp;
4738 }
4739 break;
4740 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004741 return OK;
4742}
4743
4744/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004745 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004746 */
4747 char_u *
4748partial_name(partial_T *pt)
4749{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004750 if (pt != NULL)
4751 {
4752 if (pt->pt_name != NULL)
4753 return pt->pt_name;
4754 if (pt->pt_func != NULL)
4755 return pt->pt_func->uf_name;
4756 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004757 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004758}
4759
Bram Moolenaarddecc252016-04-06 22:59:37 +02004760 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004761partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004762{
4763 int i;
4764
4765 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004766 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004767 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004768 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004769 if (pt->pt_name != NULL)
4770 {
4771 func_unref(pt->pt_name);
4772 vim_free(pt->pt_name);
4773 }
4774 else
4775 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004776
Bram Moolenaar54656012021-06-09 20:50:46 +02004777 // "out_up" is no longer used, decrement refcount on partial that owns it.
4778 partial_unref(pt->pt_outer.out_up_partial);
4779
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004780 // Using pt_outer from another partial.
4781 partial_unref(pt->pt_outer_partial);
4782
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004783 // Decrease the reference count for the context of a closure. If down
4784 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004785 if (pt->pt_funcstack != NULL)
4786 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004787 --pt->pt_funcstack->fs_refcount;
4788 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004789 }
4790
Bram Moolenaarddecc252016-04-06 22:59:37 +02004791 vim_free(pt);
4792}
4793
4794/*
4795 * Unreference a closure: decrement the reference count and free it when it
4796 * becomes zero.
4797 */
4798 void
4799partial_unref(partial_T *pt)
4800{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004801 if (pt != NULL)
4802 {
4803 if (--pt->pt_refcount <= 0)
4804 partial_free(pt);
4805
4806 // If the reference count goes down to one, the funcstack may be the
4807 // only reference and can be freed if no other partials reference it.
4808 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4809 funcstack_check_refcount(pt->pt_funcstack);
4810 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004811}
4812
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004813/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004814 * Return the next (unique) copy ID.
4815 * Used for serializing nested structures.
4816 */
4817 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004818get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004819{
4820 current_copyID += COPYID_INC;
4821 return current_copyID;
4822}
4823
4824/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004825 * Garbage collection for lists and dictionaries.
4826 *
4827 * We use reference counts to be able to free most items right away when they
4828 * are no longer used. But for composite items it's possible that it becomes
4829 * unused while the reference count is > 0: When there is a recursive
4830 * reference. Example:
4831 * :let l = [1, 2, 3]
4832 * :let d = {9: l}
4833 * :let l[1] = d
4834 *
4835 * Since this is quite unusual we handle this with garbage collection: every
4836 * once in a while find out which lists and dicts are not referenced from any
4837 * variable.
4838 *
4839 * Here is a good reference text about garbage collection (refers to Python
4840 * but it applies to all reference-counting mechanisms):
4841 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004842 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004843
4844/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004845 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004846 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004847 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004848 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004849 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004850garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004851{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004852 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004853 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004854 buf_T *buf;
4855 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004856 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004857 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004858
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004859 if (!testing)
4860 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004861 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004862 want_garbage_collect = FALSE;
4863 may_garbage_collect = FALSE;
4864 garbage_collect_at_exit = FALSE;
4865 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004866
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004867 // The execution stack can grow big, limit the size.
4868 if (exestack.ga_maxlen - exestack.ga_len > 500)
4869 {
4870 size_t new_len;
4871 char_u *pp;
4872 int n;
4873
4874 // Keep 150% of the current size, with a minimum of the growth size.
4875 n = exestack.ga_len / 2;
4876 if (n < exestack.ga_growsize)
4877 n = exestack.ga_growsize;
4878
4879 // Don't make it bigger though.
4880 if (exestack.ga_len + n < exestack.ga_maxlen)
4881 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004882 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004883 pp = vim_realloc(exestack.ga_data, new_len);
4884 if (pp == NULL)
4885 return FAIL;
4886 exestack.ga_maxlen = exestack.ga_len + n;
4887 exestack.ga_data = pp;
4888 }
4889 }
4890
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004891 // We advance by two because we add one for items referenced through
4892 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004893 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004894
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004895 /*
4896 * 1. Go through all accessible variables and mark all lists and dicts
4897 * with copyID.
4898 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004899
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004900 // Don't free variables in the previous_funccal list unless they are only
4901 // referenced through previous_funccal. This must be first, because if
4902 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004903 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004904
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004905 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004906 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004907
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004908 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004909 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004910 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4911 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004912
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004913 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004914 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004915 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4916 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004917 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004918 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4919 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004920#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004921 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004922 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4923 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004924 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004925 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004926 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4927 NULL, NULL);
4928#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004929
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004930 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004931 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004932 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4933 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004934 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004935 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004936
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004937 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004938 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004939
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004940 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004941 abort = abort || set_ref_in_functions(copyID);
4942
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004943 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004944 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004945
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004946 // funcstacks keep variables for closures
4947 abort = abort || set_ref_in_funcstacks(copyID);
4948
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004949 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004950 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004951
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004952 // callbacks in buffers
4953 abort = abort || set_ref_in_buffers(copyID);
4954
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004955 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
4956 abort = abort || set_ref_in_insexpand_funcs(copyID);
4957
4958 // 'operatorfunc' callback
4959 abort = abort || set_ref_in_opfunc(copyID);
4960
4961 // 'tagfunc' callback
4962 abort = abort || set_ref_in_tagfunc(copyID);
4963
4964 // 'imactivatefunc' and 'imstatusfunc' callbacks
4965 abort = abort || set_ref_in_im_funcs(copyID);
4966
Bram Moolenaar1dced572012-04-05 16:54:08 +02004967#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004968 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004969#endif
4970
Bram Moolenaardb913952012-06-29 12:54:53 +02004971#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004972 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004973#endif
4974
4975#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004976 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004977#endif
4978
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004979#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004980 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004981 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004982#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004983#ifdef FEAT_NETBEANS_INTG
4984 abort = abort || set_ref_in_nb_channel(copyID);
4985#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004986
Bram Moolenaare3188e22016-05-31 21:13:04 +02004987#ifdef FEAT_TIMERS
4988 abort = abort || set_ref_in_timer(copyID);
4989#endif
4990
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004991#ifdef FEAT_QUICKFIX
4992 abort = abort || set_ref_in_quickfix(copyID);
4993#endif
4994
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004995#ifdef FEAT_TERMINAL
4996 abort = abort || set_ref_in_term(copyID);
4997#endif
4998
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004999#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005000 abort = abort || set_ref_in_popups(copyID);
5001#endif
5002
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005003 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005004 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005005 /*
5006 * 2. Free lists and dictionaries that are not referenced.
5007 */
5008 did_free = free_unref_items(copyID);
5009
5010 /*
5011 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005012 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005013 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005014 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005015 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005016 else if (p_verbose > 0)
5017 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005018 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005019 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005020
5021 return did_free;
5022}
5023
5024/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005025 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005026 */
5027 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005028free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005029{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005030 int did_free = FALSE;
5031
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005032 // Let all "free" functions know that we are here. This means no
5033 // dictionaries, lists, channels or jobs are to be freed, because we will
5034 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005035 in_free_unref_items = TRUE;
5036
5037 /*
5038 * PASS 1: free the contents of the items. We don't free the items
5039 * themselves yet, so that it is possible to decrement refcount counters
5040 */
5041
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005042 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005043 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005044
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005045 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005046 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005047
5048#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005049 // Go through the list of jobs and free items without the copyID. This
5050 // must happen before doing channels, because jobs refer to channels, but
5051 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005052 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5053
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005054 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005055 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5056#endif
5057
5058 /*
5059 * PASS 2: free the items themselves.
5060 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005061 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005062 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005063
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005064#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005065 // Go through the list of jobs and free items without the copyID. This
5066 // must happen before doing channels, because jobs refer to channels, but
5067 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005068 free_unused_jobs(copyID, COPYID_MASK);
5069
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005070 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005071 free_unused_channels(copyID, COPYID_MASK);
5072#endif
5073
5074 in_free_unref_items = FALSE;
5075
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005076 return did_free;
5077}
5078
5079/*
5080 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005081 * "list_stack" is used to add lists to be marked. Can be NULL.
5082 *
5083 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005084 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005085 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005086set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005087{
5088 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005089 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005090 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005091 hashtab_T *cur_ht;
5092 ht_stack_T *ht_stack = NULL;
5093 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005094
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005095 cur_ht = ht;
5096 for (;;)
5097 {
5098 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005099 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005100 // Mark each item in the hashtab. If the item contains a hashtab
5101 // it is added to ht_stack, if it contains a list it is added to
5102 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005103 todo = (int)cur_ht->ht_used;
5104 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5105 if (!HASHITEM_EMPTY(hi))
5106 {
5107 --todo;
5108 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5109 &ht_stack, list_stack);
5110 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005111 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005112
5113 if (ht_stack == NULL)
5114 break;
5115
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005116 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005117 cur_ht = ht_stack->ht;
5118 tempitem = ht_stack;
5119 ht_stack = ht_stack->prev;
5120 free(tempitem);
5121 }
5122
5123 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005124}
5125
Dominique Pelle748b3082022-01-08 12:41:16 +00005126#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5127 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005128/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005129 * Mark a dict and its items with "copyID".
5130 * Returns TRUE if setting references failed somehow.
5131 */
5132 int
5133set_ref_in_dict(dict_T *d, int copyID)
5134{
5135 if (d != NULL && d->dv_copyID != copyID)
5136 {
5137 d->dv_copyID = copyID;
5138 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5139 }
5140 return FALSE;
5141}
Dominique Pelle748b3082022-01-08 12:41:16 +00005142#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005143
5144/*
5145 * Mark a list and its items with "copyID".
5146 * Returns TRUE if setting references failed somehow.
5147 */
5148 int
5149set_ref_in_list(list_T *ll, int copyID)
5150{
5151 if (ll != NULL && ll->lv_copyID != copyID)
5152 {
5153 ll->lv_copyID = copyID;
5154 return set_ref_in_list_items(ll, copyID, NULL);
5155 }
5156 return FALSE;
5157}
5158
5159/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005160 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005161 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5162 *
5163 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005164 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005165 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005166set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005167{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005168 listitem_T *li;
5169 int abort = FALSE;
5170 list_T *cur_l;
5171 list_stack_T *list_stack = NULL;
5172 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005173
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005174 cur_l = l;
5175 for (;;)
5176 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005177 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005178 // Mark each item in the list. If the item contains a hashtab
5179 // it is added to ht_stack, if it contains a list it is added to
5180 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005181 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5182 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5183 ht_stack, &list_stack);
5184 if (list_stack == NULL)
5185 break;
5186
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005187 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005188 cur_l = list_stack->list;
5189 tempitem = list_stack;
5190 list_stack = list_stack->prev;
5191 free(tempitem);
5192 }
5193
5194 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005195}
5196
5197/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005198 * Mark the partial in callback 'cb' with "copyID".
5199 */
5200 int
5201set_ref_in_callback(callback_T *cb, int copyID)
5202{
5203 typval_T tv;
5204
5205 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5206 return FALSE;
5207
5208 tv.v_type = VAR_PARTIAL;
5209 tv.vval.v_partial = cb->cb_partial;
5210 return set_ref_in_item(&tv, copyID, NULL, NULL);
5211}
5212
5213/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005214 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005215 * "list_stack" is used to add lists to be marked. Can be NULL.
5216 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5217 *
5218 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005219 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005220 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005221set_ref_in_item(
5222 typval_T *tv,
5223 int copyID,
5224 ht_stack_T **ht_stack,
5225 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005226{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005227 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005228
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005229 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005230 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005231 dict_T *dd = tv->vval.v_dict;
5232
Bram Moolenaara03f2332016-02-06 18:09:59 +01005233 if (dd != NULL && dd->dv_copyID != copyID)
5234 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005235 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005236 dd->dv_copyID = copyID;
5237 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005238 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005239 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5240 }
5241 else
5242 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005243 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5244
Bram Moolenaara03f2332016-02-06 18:09:59 +01005245 if (newitem == NULL)
5246 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005247 else
5248 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005249 newitem->ht = &dd->dv_hashtab;
5250 newitem->prev = *ht_stack;
5251 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005252 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005253 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005254 }
5255 }
5256 else if (tv->v_type == VAR_LIST)
5257 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005258 list_T *ll = tv->vval.v_list;
5259
Bram Moolenaara03f2332016-02-06 18:09:59 +01005260 if (ll != NULL && ll->lv_copyID != copyID)
5261 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005262 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005263 ll->lv_copyID = copyID;
5264 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005265 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005266 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005267 }
5268 else
5269 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005270 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5271
Bram Moolenaara03f2332016-02-06 18:09:59 +01005272 if (newitem == NULL)
5273 abort = TRUE;
5274 else
5275 {
5276 newitem->list = ll;
5277 newitem->prev = *list_stack;
5278 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005279 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005280 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005281 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005282 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005283 else if (tv->v_type == VAR_FUNC)
5284 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005285 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005286 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005287 else if (tv->v_type == VAR_PARTIAL)
5288 {
5289 partial_T *pt = tv->vval.v_partial;
5290 int i;
5291
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005292 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005293 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005294 // Didn't see this partial yet.
5295 pt->pt_copyID = copyID;
5296
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005297 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005298
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005299 if (pt->pt_dict != NULL)
5300 {
5301 typval_T dtv;
5302
5303 dtv.v_type = VAR_DICT;
5304 dtv.vval.v_dict = pt->pt_dict;
5305 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5306 }
5307
5308 for (i = 0; i < pt->pt_argc; ++i)
5309 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5310 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005311 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005312 }
5313 }
5314#ifdef FEAT_JOB_CHANNEL
5315 else if (tv->v_type == VAR_JOB)
5316 {
5317 job_T *job = tv->vval.v_job;
5318 typval_T dtv;
5319
5320 if (job != NULL && job->jv_copyID != copyID)
5321 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005322 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005323 if (job->jv_channel != NULL)
5324 {
5325 dtv.v_type = VAR_CHANNEL;
5326 dtv.vval.v_channel = job->jv_channel;
5327 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5328 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005329 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005330 {
5331 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005332 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005333 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5334 }
5335 }
5336 }
5337 else if (tv->v_type == VAR_CHANNEL)
5338 {
5339 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005340 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005341 typval_T dtv;
5342 jsonq_T *jq;
5343 cbq_T *cq;
5344
5345 if (ch != NULL && ch->ch_copyID != copyID)
5346 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005347 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005348 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005349 {
5350 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5351 jq = jq->jq_next)
5352 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5353 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5354 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005355 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005356 {
5357 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005358 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005359 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5360 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005361 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005362 {
5363 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005364 dtv.vval.v_partial =
5365 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005366 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5367 }
5368 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005369 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005370 {
5371 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005372 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005373 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5374 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005375 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005376 {
5377 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005378 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005379 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5380 }
5381 }
5382 }
5383#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005384 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005385}
5386
Bram Moolenaar8c711452005-01-14 21:53:12 +00005387/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005388 * Return a string with the string representation of a variable.
5389 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005390 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005391 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005392 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005393 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005394 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005395 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5396 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005397 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005398 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005399 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005400echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005401 typval_T *tv,
5402 char_u **tofree,
5403 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005404 int copyID,
5405 int echo_style,
5406 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005407 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005409 static int recurse = 0;
5410 char_u *r = NULL;
5411
Bram Moolenaar33570922005-01-25 22:26:29 +00005412 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005413 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005414 if (!did_echo_string_emsg)
5415 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005416 // Only give this message once for a recursive call to avoid
5417 // flooding the user with errors. And stop iterating over lists
5418 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005419 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005420 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005421 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005422 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005423 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005424 }
5425 ++recurse;
5426
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427 switch (tv->v_type)
5428 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005429 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005430 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005431 {
5432 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005433 r = tv->vval.v_string;
5434 if (r == NULL)
5435 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005436 }
5437 else
5438 {
5439 *tofree = string_quote(tv->vval.v_string, FALSE);
5440 r = *tofree;
5441 }
5442 break;
5443
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005445 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005446 char_u buf[MAX_FUNC_NAME_LEN];
5447
5448 if (echo_style)
5449 {
LemonBoya5d35902022-04-29 21:15:02 +01005450 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5451 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005452 buf, MAX_FUNC_NAME_LEN);
5453 if (r == buf)
5454 {
5455 r = vim_strsave(buf);
5456 *tofree = r;
5457 }
5458 else
5459 *tofree = NULL;
5460 }
5461 else
5462 {
5463 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5464 : make_ufunc_name_readable(
5465 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5466 TRUE);
5467 r = *tofree;
5468 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005469 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005470 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005471
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005472 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005473 {
5474 partial_T *pt = tv->vval.v_partial;
5475 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005476 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005477 garray_T ga;
5478 int i;
5479 char_u *tf;
5480
5481 ga_init2(&ga, 1, 100);
5482 ga_concat(&ga, (char_u *)"function(");
5483 if (fname != NULL)
5484 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005485 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005486 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005487 && vim_isupper(fname[1]))
5488 {
5489 ga_concat(&ga, (char_u *)"'g:");
5490 ga_concat(&ga, fname + 1);
5491 }
5492 else
5493 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005494 vim_free(fname);
5495 }
5496 if (pt != NULL && pt->pt_argc > 0)
5497 {
5498 ga_concat(&ga, (char_u *)", [");
5499 for (i = 0; i < pt->pt_argc; ++i)
5500 {
5501 if (i > 0)
5502 ga_concat(&ga, (char_u *)", ");
5503 ga_concat(&ga,
5504 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5505 vim_free(tf);
5506 }
5507 ga_concat(&ga, (char_u *)"]");
5508 }
5509 if (pt != NULL && pt->pt_dict != NULL)
5510 {
5511 typval_T dtv;
5512
5513 ga_concat(&ga, (char_u *)", ");
5514 dtv.v_type = VAR_DICT;
5515 dtv.vval.v_dict = pt->pt_dict;
5516 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5517 vim_free(tf);
5518 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005519 // terminate with ')' and a NUL
5520 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005521
5522 *tofree = ga.ga_data;
5523 r = *tofree;
5524 break;
5525 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005526
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005527 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005528 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005529 break;
5530
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005531 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005532 if (tv->vval.v_list == NULL)
5533 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005534 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005535 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005536 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005537 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005538 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5539 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005540 {
5541 *tofree = NULL;
5542 r = (char_u *)"[...]";
5543 }
5544 else
5545 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005546 int old_copyID = tv->vval.v_list->lv_copyID;
5547
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005548 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005549 *tofree = list2string(tv, copyID, restore_copyID);
5550 if (restore_copyID)
5551 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005552 r = *tofree;
5553 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005554 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005555
Bram Moolenaar8c711452005-01-14 21:53:12 +00005556 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005557 if (tv->vval.v_dict == NULL)
5558 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005559 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005560 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005561 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005562 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005563 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5564 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005565 {
5566 *tofree = NULL;
5567 r = (char_u *)"{...}";
5568 }
5569 else
5570 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005571 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005572
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005573 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005574 *tofree = dict2string(tv, copyID, restore_copyID);
5575 if (restore_copyID)
5576 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005577 r = *tofree;
5578 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005579 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005580
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005581 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005582 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005583 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005584 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005585 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005586 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005587 break;
5588
Bram Moolenaar835dc632016-02-07 14:27:38 +01005589 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005590 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005591#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005592 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005593 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5594 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005595 if (composite_val)
5596 {
5597 *tofree = string_quote(r, FALSE);
5598 r = *tofree;
5599 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005600#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005601 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005602
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005603 case VAR_INSTR:
5604 *tofree = NULL;
5605 r = (char_u *)"instructions";
5606 break;
5607
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005608 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005609#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005610 *tofree = NULL;
5611 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5612 r = numbuf;
5613 break;
5614#endif
5615
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005616 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005617 case VAR_SPECIAL:
5618 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005619 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005620 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005621 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005622
Bram Moolenaar8502c702014-06-17 12:51:16 +02005623 if (--recurse == 0)
5624 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005625 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005626}
5627
5628/*
5629 * Return a string with the string representation of a variable.
5630 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5631 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005632 * Does not put quotes around strings, as ":echo" displays values.
5633 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5634 * May return NULL.
5635 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005636 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005637echo_string(
5638 typval_T *tv,
5639 char_u **tofree,
5640 char_u *numbuf,
5641 int copyID)
5642{
5643 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5644}
5645
5646/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005647 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5648 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005649 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005650 */
5651 int
5652buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5653{
5654 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005655 char_u *t;
5656 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005657
5658 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5659 return -1;
5660
5661 if (lnum > buf->b_ml.ml_line_count)
5662 lnum = buf->b_ml.ml_line_count;
5663
5664 str = ml_get_buf(buf, lnum, FALSE);
5665 if (str == NULL)
5666 return -1;
5667
5668 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005669 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005670
Bram Moolenaar91458462021-01-13 20:08:38 +01005671 // count the number of characters
5672 t = str;
5673 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5674 t += mb_ptr2len(t);
5675
5676 // In insert mode, when the cursor is at the end of a non-empty line,
5677 // byteidx points to the NUL character immediately past the end of the
5678 // string. In this case, add one to the character count.
5679 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5680 count++;
5681
5682 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005683}
5684
5685/*
5686 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005687 * byte index. Works only for loaded buffers. Returns -1 on failure.
5688 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005689 */
5690 int
5691buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5692{
5693 char_u *str;
5694 char_u *t;
5695
5696 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5697 return -1;
5698
5699 if (lnum > buf->b_ml.ml_line_count)
5700 lnum = buf->b_ml.ml_line_count;
5701
5702 str = ml_get_buf(buf, lnum, FALSE);
5703 if (str == NULL)
5704 return -1;
5705
5706 // Convert the character offset to a byte offset
5707 t = str;
5708 while (*t != NUL && --charidx > 0)
5709 t += mb_ptr2len(t);
5710
Bram Moolenaar91458462021-01-13 20:08:38 +01005711 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005712}
5713
5714/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005716 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005718 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005719var2fpos(
5720 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005721 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005722 int *fnum, // set to fnum for '0, 'A, etc.
5723 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005725 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005727 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005729 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005730 if (varp->v_type == VAR_LIST)
5731 {
5732 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005733 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005734 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005735 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005736
5737 l = varp->vval.v_list;
5738 if (l == NULL)
5739 return NULL;
5740
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005741 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005742 pos.lnum = list_find_nr(l, 0L, &error);
5743 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005744 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005745 if (charcol)
5746 len = (long)mb_charlen(ml_get(pos.lnum));
5747 else
5748 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005749
Bram Moolenaarec65d772020-08-20 22:29:12 +02005750 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005751 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005752 li = list_find(l, 1L);
5753 if (li != NULL && li->li_tv.v_type == VAR_STRING
5754 && li->li_tv.vval.v_string != NULL
5755 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005756 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005757 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005758 }
5759 else
5760 {
5761 pos.col = list_find_nr(l, 1L, &error);
5762 if (error)
5763 return NULL;
5764 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005765
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005766 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005767 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005768 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005769 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005770
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005771 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005772 pos.coladd = list_find_nr(l, 2L, &error);
5773 if (error)
5774 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005775
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005776 return &pos;
5777 }
5778
Bram Moolenaarc5809432021-03-27 21:23:30 +01005779 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5780 return NULL;
5781
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005782 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005783 if (name == NULL)
5784 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005785
5786 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005787 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005788 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005789 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005790 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005791 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005792 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005793 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005794 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005795 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005796 pos = VIsual;
5797 else
5798 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005799 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005800 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005801 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005803 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005804 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5806 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005807 pos = *pp;
5808 }
5809 if (pos.lnum != 0)
5810 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005811 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005812 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5813 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005815
Bram Moolenaara5525202006-03-02 22:52:09 +00005816 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005817
Bram Moolenaar477933c2007-07-17 14:32:23 +00005818 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005819 {
5820 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005821 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005822 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005823 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005824 // In silent Ex mode topline is zero, but that's not a valid line
5825 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005826 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005827 return &pos;
5828 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005829 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005830 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005831 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005832 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005833 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005834 return &pos;
5835 }
5836 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005837 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005839 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 {
5841 pos.lnum = curbuf->b_ml.ml_line_count;
5842 pos.col = 0;
5843 }
5844 else
5845 {
5846 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005847 if (charcol)
5848 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5849 else
5850 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 }
5852 return &pos;
5853 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005854 if (in_vim9script())
5855 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 return NULL;
5857}
5858
5859/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005860 * Convert list in "arg" into a position and optional file number.
5861 * When "fnump" is NULL there is no file number, only 3 items.
5862 * Note that the column is passed on as-is, the caller may want to decrement
5863 * it to use 1 for the first column.
5864 * Return FAIL when conversion is not possible, doesn't check the position for
5865 * validity.
5866 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005867 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005868list2fpos(
5869 typval_T *arg,
5870 pos_T *posp,
5871 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005872 colnr_T *curswantp,
5873 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005874{
5875 list_T *l = arg->vval.v_list;
5876 long i = 0;
5877 long n;
5878
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005879 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5880 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005881 if (arg->v_type != VAR_LIST
5882 || l == NULL
5883 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005884 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005885 return FAIL;
5886
5887 if (fnump != NULL)
5888 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005889 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005890 if (n < 0)
5891 return FAIL;
5892 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005893 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005894 *fnump = n;
5895 }
5896
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005897 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005898 if (n < 0)
5899 return FAIL;
5900 posp->lnum = n;
5901
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005902 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005903 if (n < 0)
5904 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005905 // If character position is specified, then convert to byte position
5906 if (charcol)
5907 {
5908 buf_T *buf;
5909
5910 // Get the text for the specified line in a loaded buffer
5911 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5912 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5913 return FAIL;
5914
Bram Moolenaar91458462021-01-13 20:08:38 +01005915 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005916 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005917 posp->col = n;
5918
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005919 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005920 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005921 posp->coladd = 0;
5922 else
5923 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005924
Bram Moolenaar493c1782014-05-28 14:34:46 +02005925 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005926 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005927
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005928 return OK;
5929}
5930
5931/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 * Get the length of an environment variable name.
5933 * Advance "arg" to the first character after the name.
5934 * Return 0 for error.
5935 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005936 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005937get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938{
5939 char_u *p;
5940 int len;
5941
5942 for (p = *arg; vim_isIDc(*p); ++p)
5943 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005944 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 return 0;
5946
5947 len = (int)(p - *arg);
5948 *arg = p;
5949 return len;
5950}
5951
5952/*
5953 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005954 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 * Return 0 if something is wrong.
5956 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005957 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005958get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959{
5960 char_u *p;
5961 int len;
5962
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005963 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005965 {
5966 if (*p == ':')
5967 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005968 // "s:" is start of "s:var", but "n:" is not and can be used in
5969 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005970 len = (int)(p - *arg);
5971 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5972 || len > 1)
5973 break;
5974 }
5975 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005976 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 return 0;
5978
5979 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005980 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981
5982 return len;
5983}
5984
5985/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005986 * Get the length of the name of a variable or function.
5987 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005989 * Return -1 if curly braces expansion failed.
5990 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 * If the name contains 'magic' {}'s, expand them and return the
5992 * expanded name in an allocated string via 'alias' - caller must free.
5993 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005994 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005995get_name_len(
5996 char_u **arg,
5997 char_u **alias,
5998 int evaluate,
5999 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000{
6001 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 char_u *p;
6003 char_u *expr_start;
6004 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006006 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007
6008 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6009 && (*arg)[2] == (int)KE_SNR)
6010 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006011 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 *arg += 3;
6013 return get_id_len(arg) + 3;
6014 }
6015 len = eval_fname_script(*arg);
6016 if (len > 0)
6017 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006018 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 *arg += len;
6020 }
6021
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006023 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006025 p = find_name_end(*arg, &expr_start, &expr_end,
6026 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 if (expr_start != NULL)
6028 {
6029 char_u *temp_string;
6030
6031 if (!evaluate)
6032 {
6033 len += (int)(p - *arg);
6034 *arg = skipwhite(p);
6035 return len;
6036 }
6037
6038 /*
6039 * Include any <SID> etc in the expanded string:
6040 * Thus the -len here.
6041 */
6042 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6043 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006044 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 *alias = temp_string;
6046 *arg = skipwhite(p);
6047 return (int)STRLEN(temp_string);
6048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049
6050 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006051 // Only give an error when there is something, otherwise it will be
6052 // reported at a higher level.
6053 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006054 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055
6056 return len;
6057}
6058
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006059/*
6060 * Find the end of a variable or function name, taking care of magic braces.
6061 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6062 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006063 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006064 * Return a pointer to just after the name. Equal to "arg" if there is no
6065 * valid name.
6066 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006067 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006068find_name_end(
6069 char_u *arg,
6070 char_u **expr_start,
6071 char_u **expr_end,
6072 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006074 int mb_nest = 0;
6075 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006077 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02006078 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006080 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006082 *expr_start = NULL;
6083 *expr_end = NULL;
6084 }
6085
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006086 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006087 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
6088 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006089 return arg;
6090
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006091 for (p = arg; *p != NUL
6092 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006093 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006094 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006095 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006096 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006097 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006098 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006099 if (*p == '\'')
6100 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006101 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006102 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006103 ;
6104 if (*p == NUL)
6105 break;
6106 }
6107 else if (*p == '"')
6108 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006109 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006110 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006111 if (*p == '\\' && p[1] != NUL)
6112 ++p;
6113 if (*p == NUL)
6114 break;
6115 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006116 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6117 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006118 // "s:" is start of "s:var", but "n:" is not and can be used in
6119 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006120 len = (int)(p - arg);
6121 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006122 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006123 break;
6124 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006125
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006126 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006128 if (*p == '[')
6129 ++br_nest;
6130 else if (*p == ']')
6131 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006133
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006134 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006136 if (*p == '{')
6137 {
6138 mb_nest++;
6139 if (expr_start != NULL && *expr_start == NULL)
6140 *expr_start = p;
6141 }
6142 else if (*p == '}')
6143 {
6144 mb_nest--;
6145 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6146 *expr_end = p;
6147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 }
6150
6151 return p;
6152}
6153
6154/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006155 * Expands out the 'magic' {}'s in a variable/function name.
6156 * Note that this can call itself recursively, to deal with
6157 * constructs like foo{bar}{baz}{bam}
6158 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6159 * "in_start" ^
6160 * "expr_start" ^
6161 * "expr_end" ^
6162 * "in_end" ^
6163 *
6164 * Returns a new allocated string, which the caller must free.
6165 * Returns NULL for failure.
6166 */
6167 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006168make_expanded_name(
6169 char_u *in_start,
6170 char_u *expr_start,
6171 char_u *expr_end,
6172 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006173{
6174 char_u c1;
6175 char_u *retval = NULL;
6176 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006177
6178 if (expr_end == NULL || in_end == NULL)
6179 return NULL;
6180 *expr_start = NUL;
6181 *expr_end = NUL;
6182 c1 = *in_end;
6183 *in_end = NUL;
6184
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006185 temp_result = eval_to_string(expr_start + 1, FALSE);
6186 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006187 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006188 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6189 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006190 if (retval != NULL)
6191 {
6192 STRCPY(retval, in_start);
6193 STRCAT(retval, temp_result);
6194 STRCAT(retval, expr_end + 1);
6195 }
6196 }
6197 vim_free(temp_result);
6198
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006199 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006200 *expr_start = '{';
6201 *expr_end = '}';
6202
6203 if (retval != NULL)
6204 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006205 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006206 if (expr_start != NULL)
6207 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006208 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006209 temp_result = make_expanded_name(retval, expr_start,
6210 expr_end, temp_result);
6211 vim_free(retval);
6212 retval = temp_result;
6213 }
6214 }
6215
6216 return retval;
6217}
6218
6219/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006221 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006223 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006224eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006226 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006227}
6228
6229/*
6230 * Return TRUE if character "c" can be used as the first character in a
6231 * variable or function name (excluding '{' and '}').
6232 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006233 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006234eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006235{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006236 return ASCII_ISALPHA(c) || c == '_';
6237}
6238
6239/*
6240 * Return TRUE if character "c" can be used as the first character of a
6241 * dictionary key.
6242 */
6243 int
6244eval_isdictc(int c)
6245{
6246 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247}
6248
6249/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006250 * Handle:
6251 * - expr[expr], expr[expr:expr] subscript
6252 * - ".name" lookup
6253 * - function call with Funcref variable: func(expr)
6254 * - method call: var->method()
6255 *
6256 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006257 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006258 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006259 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006260handle_subscript(
6261 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006262 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006263 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006264 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006265 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006266{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006267 int evaluate = evalarg != NULL
6268 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006269 int ret = OK;
6270 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006271 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006272 int getnext;
6273 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006274
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006275 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006276 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006277 // When at the end of the line and ".name" or "->{" or "->X" follows in
6278 // the next line then consume the line break.
6279 p = eval_next_non_blank(*arg, evalarg, &getnext);
6280 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006281 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006282 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6283 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6284 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006285 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006286 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006287 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006288 check_white = FALSE;
6289 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006290
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006291 if (rettv->v_type == VAR_ANY)
6292 {
6293 char_u *exp_name;
6294 int cc;
6295 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006296 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006297 type_T *type;
6298
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006299 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006300 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006301 if (**arg != '.')
6302 {
6303 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006304 semsg(_(e_expected_dot_after_name_str),
6305 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006306 ret = FAIL;
6307 break;
6308 }
6309 ++*arg;
6310 if (IS_WHITE_OR_NUL(**arg))
6311 {
6312 if (verbose)
6313 emsg(_(e_no_white_space_allowed_after_dot));
6314 ret = FAIL;
6315 break;
6316 }
6317
6318 // isolate the name
6319 exp_name = *arg;
6320 while (eval_isnamec(**arg))
6321 ++*arg;
6322 cc = **arg;
6323 **arg = NUL;
6324
6325 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00006326 evalarg->eval_cctx, evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006327 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006328
6329 if (idx < 0 && ufunc == NULL)
6330 {
6331 ret = FAIL;
6332 break;
6333 }
6334 if (idx >= 0)
6335 {
6336 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6337 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6338
6339 copy_tv(sv->sv_tv, rettv);
6340 }
6341 else
6342 {
6343 rettv->v_type = VAR_FUNC;
6344 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6345 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006346 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006347 }
6348
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006349 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6350 || rettv->v_type == VAR_PARTIAL))
6351 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006352 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006353 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6354 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006355
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006356 // Stop the expression evaluation when immediately aborting on
6357 // error, or when an interrupt occurred or an exception was thrown
6358 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006359 if (aborting())
6360 {
6361 if (ret == OK)
6362 clear_tv(rettv);
6363 ret = FAIL;
6364 }
6365 dict_unref(selfdict);
6366 selfdict = NULL;
6367 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006368 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006369 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006370 if (in_vim9script())
6371 *arg = skipwhite(p + 2);
6372 else
6373 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006374 if (ret == OK)
6375 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006376 if (VIM_ISWHITE(**arg))
6377 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006378 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006379 ret = FAIL;
6380 }
6381 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006382 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006383 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006384 else
6385 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006386 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006387 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006388 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006389 // "." is ".name" lookup when we found a dict or when evaluating and
6390 // scriptversion is at least 2, where string concatenation is "..".
6391 else if (**arg == '['
6392 || (**arg == '.' && (rettv->v_type == VAR_DICT
6393 || (!evaluate
6394 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006395 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006396 {
6397 dict_unref(selfdict);
6398 if (rettv->v_type == VAR_DICT)
6399 {
6400 selfdict = rettv->vval.v_dict;
6401 if (selfdict != NULL)
6402 ++selfdict->dv_refcount;
6403 }
6404 else
6405 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006406 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006407 {
6408 clear_tv(rettv);
6409 ret = FAIL;
6410 }
6411 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006412 else
6413 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006414 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006415
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006416 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6417 // Don't do this when "Func" is already a partial that was bound
6418 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006419 if (selfdict != NULL
6420 && (rettv->v_type == VAR_FUNC
6421 || (rettv->v_type == VAR_PARTIAL
6422 && (rettv->vval.v_partial->pt_auto
6423 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006424 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006425
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006426 dict_unref(selfdict);
6427 return ret;
6428}
6429
6430/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006431 * Make a copy of an item.
6432 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006433 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006434 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6435 * reference to an already copied list/dict can be used.
6436 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006437 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006438 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006439item_copy(
6440 typval_T *from,
6441 typval_T *to,
6442 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006443 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006444 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006445{
6446 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006447 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006448
Bram Moolenaar33570922005-01-25 22:26:29 +00006449 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006450 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006451 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006452 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006453 }
6454 ++recurse;
6455
6456 switch (from->v_type)
6457 {
6458 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006459 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006460 case VAR_STRING:
6461 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006462 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006463 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006464 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006465 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006466 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006467 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006468 copy_tv(from, to);
6469 break;
6470 case VAR_LIST:
6471 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006472 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006473 if (from->vval.v_list == NULL)
6474 to->vval.v_list = NULL;
6475 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6476 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006477 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006478 to->vval.v_list = from->vval.v_list->lv_copylist;
6479 ++to->vval.v_list->lv_refcount;
6480 }
6481 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006482 to->vval.v_list = list_copy(from->vval.v_list,
6483 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006484 if (to->vval.v_list == NULL)
6485 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006486 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006487 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006488 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006489 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006490 case VAR_DICT:
6491 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006492 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006493 if (from->vval.v_dict == NULL)
6494 to->vval.v_dict = NULL;
6495 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6496 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006497 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006498 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6499 ++to->vval.v_dict->dv_refcount;
6500 }
6501 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006502 to->vval.v_dict = dict_copy(from->vval.v_dict,
6503 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006504 if (to->vval.v_dict == NULL)
6505 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006506 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006507 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006508 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006509 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006510 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006511 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006512 }
6513 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006514 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006515}
6516
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006517 void
6518echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6519{
6520 char_u *tofree;
6521 char_u numbuf[NUMBUFLEN];
6522 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6523
6524 if (*atstart)
6525 {
6526 *atstart = FALSE;
6527 // Call msg_start() after eval1(), evaluating the expression
6528 // may cause a message to appear.
6529 if (with_space)
6530 {
6531 // Mark the saved text as finishing the line, so that what
6532 // follows is displayed on a new line when scrolling back
6533 // at the more prompt.
6534 msg_sb_eol();
6535 msg_start();
6536 }
6537 }
6538 else if (with_space)
6539 msg_puts_attr(" ", echo_attr);
6540
6541 if (p != NULL)
6542 for ( ; *p != NUL && !got_int; ++p)
6543 {
6544 if (*p == '\n' || *p == '\r' || *p == TAB)
6545 {
6546 if (*p != TAB && *needclr)
6547 {
6548 // remove any text still there from the command
6549 msg_clr_eos();
6550 *needclr = FALSE;
6551 }
6552 msg_putchar_attr(*p, echo_attr);
6553 }
6554 else
6555 {
6556 if (has_mbyte)
6557 {
6558 int i = (*mb_ptr2len)(p);
6559
6560 (void)msg_outtrans_len_attr(p, i, echo_attr);
6561 p += i - 1;
6562 }
6563 else
6564 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6565 }
6566 }
6567 vim_free(tofree);
6568}
6569
Bram Moolenaare9a41262005-01-15 22:18:47 +00006570/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 * ":echo expr1 ..." print each argument separated with a space, add a
6572 * newline at the end.
6573 * ":echon expr1 ..." print each argument plain.
6574 */
6575 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006576ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577{
6578 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006579 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006580 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 int needclr = TRUE;
6582 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006583 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006584 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006585 evalarg_T evalarg;
6586
Bram Moolenaare707c882020-07-01 13:07:37 +02006587 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588
6589 if (eap->skip)
6590 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006591 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006593 // If eval1() causes an error message the text from the command may
6594 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006595 need_clr_eos = needclr;
6596
Bram Moolenaar68db9962021-05-09 23:19:22 +02006597 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006598 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 {
6600 /*
6601 * Report the invalid expression unless the expression evaluation
6602 * has been cancelled due to an aborting error, an interrupt, or an
6603 * exception.
6604 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006605 if (!aborting() && did_emsg == did_emsg_before
6606 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006607 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006608 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 break;
6610 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006611 need_clr_eos = FALSE;
6612
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006614 {
6615 if (rettv.v_type == VAR_VOID)
6616 {
6617 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6618 break;
6619 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006620 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006621 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006622
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006623 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 arg = skipwhite(arg);
6625 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006626 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006627 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628
6629 if (eap->skip)
6630 --emsg_skip;
6631 else
6632 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006633 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 if (needclr)
6635 msg_clr_eos();
6636 if (eap->cmdidx == CMD_echo)
6637 msg_end();
6638 }
6639}
6640
6641/*
6642 * ":echohl {name}".
6643 */
6644 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006645ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006647 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648}
6649
6650/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006651 * Returns the :echo attribute
6652 */
6653 int
6654get_echo_attr(void)
6655{
6656 return echo_attr;
6657}
6658
6659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 * ":execute expr1 ..." execute the result of an expression.
6661 * ":echomsg expr1 ..." Print a message
6662 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006663 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 * Each gets spaces around each argument and a newline at the end for
6665 * echo commands
6666 */
6667 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006668ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669{
6670 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006671 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 int ret = OK;
6673 char_u *p;
6674 garray_T ga;
6675 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006676 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677
6678 ga_init2(&ga, 1, 80);
6679
6680 if (eap->skip)
6681 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006682 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006684 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006685 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687
6688 if (!eap->skip)
6689 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006690 char_u buf[NUMBUFLEN];
6691
6692 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006693 {
6694 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6695 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006696 semsg(_(e_using_invalid_value_as_string_str),
6697 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006698 p = NULL;
6699 }
6700 else
6701 p = tv_get_string_buf(&rettv, buf);
6702 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006703 else
6704 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006705 if (p == NULL)
6706 {
6707 clear_tv(&rettv);
6708 ret = FAIL;
6709 break;
6710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 len = (int)STRLEN(p);
6712 if (ga_grow(&ga, len + 2) == FAIL)
6713 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006714 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 ret = FAIL;
6716 break;
6717 }
6718 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 ga.ga_len += len;
6722 }
6723
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006724 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 arg = skipwhite(arg);
6726 }
6727
6728 if (ret != FAIL && ga.ga_data != NULL)
6729 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006730 // use the first line of continuation lines for messages
6731 SOURCING_LNUM = start_lnum;
6732
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006733 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6734 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006735 // Mark the already saved text as finishing the line, so that what
6736 // follows is displayed on a new line when scrolling back at the
6737 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006738 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006739 }
6740
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006742 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006743 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006744 out_flush();
6745 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006746 else if (eap->cmdidx == CMD_echoconsole)
6747 {
6748 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6749 ui_write((char_u *)"\r\n", 2, TRUE);
6750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 else if (eap->cmdidx == CMD_echoerr)
6752 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006753 int save_did_emsg = did_emsg;
6754
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006755 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006756 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 if (!force_abort)
6758 did_emsg = save_did_emsg;
6759 }
6760 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006761 {
6762 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6763
6764 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6765 sticky_cmdmod_flags = cmdmod.cmod_flags
6766 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 do_cmdline((char_u *)ga.ga_data,
6768 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006769 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 }
6772
6773 ga_clear(&ga);
6774
6775 if (eap->skip)
6776 --emsg_skip;
6777
Bram Moolenaar63b91732021-08-05 20:40:03 +02006778 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779}
6780
6781/*
6782 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6783 * "arg" points to the "&" or '+' when called, to "option" when returning.
6784 * Returns NULL when no option name found. Otherwise pointer to the char
6785 * after the option name.
6786 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006787 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006788find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789{
6790 char_u *p = *arg;
6791
6792 ++p;
6793 if (*p == 'g' && p[1] == ':')
6794 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006795 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 p += 2;
6797 }
6798 else if (*p == 'l' && p[1] == ':')
6799 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006800 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 p += 2;
6802 }
6803 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006804 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805
6806 if (!ASCII_ISALPHA(*p))
6807 return NULL;
6808 *arg = p;
6809
6810 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006811 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 else
6813 while (ASCII_ISALPHA(*p))
6814 ++p;
6815 return p;
6816}
6817
6818/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006819 * Display script name where an item was last set.
6820 * Should only be invoked when 'verbose' is non-zero.
6821 */
6822 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006823last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006824{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006825 char_u *p;
6826
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006827 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006828 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006829 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006830 if (p != NULL)
6831 {
6832 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006833 msg_puts(_("\n\tLast set from "));
6834 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006835 if (script_ctx.sc_lnum > 0)
6836 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006837 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006838 msg_outnum((long)script_ctx.sc_lnum);
6839 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006840 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006841 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006842 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006843 }
6844}
6845
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006846#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847
6848/*
6849 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006850 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 * "flags" can be "g" to do a global substitute.
6852 * Returns an allocated string, NULL for error.
6853 */
6854 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006855do_string_sub(
6856 char_u *str,
6857 char_u *pat,
6858 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006859 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006860 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861{
6862 int sublen;
6863 regmatch_T regmatch;
6864 int i;
6865 int do_all;
6866 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006867 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 garray_T ga;
6869 char_u *ret;
6870 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006871 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006873 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006875 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876
6877 ga_init2(&ga, 1, 200);
6878
6879 do_all = (flags[0] == 'g');
6880
6881 regmatch.rm_ic = p_ic;
6882 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6883 if (regmatch.regprog != NULL)
6884 {
6885 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006886 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6888 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006889 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006890 if (regmatch.startp[0] == regmatch.endp[0])
6891 {
6892 if (zero_width == regmatch.startp[0])
6893 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006894 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006895 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006896 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6897 (size_t)i);
6898 ga.ga_len += i;
6899 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006900 continue;
6901 }
6902 zero_width = regmatch.startp[0];
6903 }
6904
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 /*
6906 * Get some space for a temporary buffer to do the substitution
6907 * into. It will contain:
6908 * - The text up to where the match is.
6909 * - The substituted text.
6910 * - The text after the match.
6911 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006912 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006913 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6915 {
6916 ga_clear(&ga);
6917 break;
6918 }
6919
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006920 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 i = (int)(regmatch.startp[0] - tail);
6922 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006923 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006924 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 + ga.ga_len + i, TRUE, TRUE, FALSE);
6926 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006927 tail = regmatch.endp[0];
6928 if (*tail == NUL)
6929 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 if (!do_all)
6931 break;
6932 }
6933
6934 if (ga.ga_data != NULL)
6935 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6936
Bram Moolenaar473de612013-06-08 18:19:48 +02006937 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 }
6939
6940 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6941 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006942 if (p_cpo == empty_option)
6943 p_cpo = save_cpo;
6944 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006945 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006946 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006947 // If it's still empty it was changed and restored, need to restore in
6948 // the complicated way.
6949 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01006950 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006951 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953
6954 return ret;
6955}