blob: dce78f7f04190513630a8cdb53fbac0ba40d6523 [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
zeertzjqcfe45652022-05-27 17:26:55 +0100546 * textlock. 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;
zeertzjqcfe45652022-05-27 17:26:55 +0100565 ++textlock;
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;
zeertzjqcfe45652022-05-27 17:26:55 +0100570 --textlock;
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;
zeertzjqcfe45652022-05-27 17:26:55 +0100810 ++textlock;
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;
zeertzjqcfe45652022-05-27 17:26:55 +0100835 --textlock;
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 Moolenaar75ebd2a2022-06-03 17:39:46 +01002139 * After a NL, skip over empty lines and comment-only lines.
2140 */
2141 static char_u *
2142newline_skip_comments(char_u *arg)
2143{
2144 char_u *p = arg + 1;
2145
2146 for (;;)
2147 {
2148 p = skipwhite(p);
2149
2150 if (*p == NUL)
2151 break;
2152 if (vim9_comment_start(p))
2153 {
2154 char_u *nl = vim_strchr(p, NL);
2155
2156 if (nl == NULL)
2157 break;
2158 p = nl;
2159 }
2160 if (*p != NL)
2161 break;
2162 ++p; // skip another NL
2163 }
2164 return p;
2165}
2166
2167/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002168 * Get the next line source line without advancing. But do skip over comment
2169 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002170 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002171 */
2172 static char_u *
2173getline_peek_skip_comments(evalarg_T *evalarg)
2174{
2175 for (;;)
2176 {
2177 char_u *next = getline_peek(evalarg->eval_getline,
2178 evalarg->eval_cookie);
2179 char_u *p;
2180
2181 if (next == NULL)
2182 break;
2183 p = skipwhite(next);
2184 if (*p != NUL && !vim9_comment_start(p))
2185 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002186 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002187 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002188 }
2189 return NULL;
2190}
2191
2192/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002193 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2194 * comment) and there is a next line, return the next line (skipping blanks)
2195 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002196 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2197 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002198 * "arg" must point somewhere inside a line, not at the start.
2199 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002200 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002201eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2202{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002203 char_u *p = skipwhite(arg);
2204
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002205 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002206 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002207 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002208 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2209 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002210 && (*p == NUL || *p == NL
2211 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002212 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002213 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002214
Bram Moolenaare442d592022-05-05 12:20:28 +01002215 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002216 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002217 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002218 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002219 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002220 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002221
Bram Moolenaar9d489562020-07-30 20:08:50 +02002222 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002223 {
2224 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002225 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002226 }
2227 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002228 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002229}
2230
2231/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002232 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002233 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002234 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002235 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002236eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002237{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002238 garray_T *gap = &evalarg->eval_ga;
2239 char_u *line;
2240
Bram Moolenaar39be4982022-05-06 21:51:50 +01002241 if (arg != NULL)
2242 {
2243 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002244 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002245 // Truncate before a trailing comment, so that concatenating the lines
2246 // won't turn the rest into a comment.
2247 if (*skipwhite(arg) == '#')
2248 *arg = NUL;
2249 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002250
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002251 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002252 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2253 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002254 else
2255 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002256 if (line == NULL)
2257 return NULL;
2258
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002259 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002260 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2261 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002262 char_u *p = skipwhite(line);
2263
2264 // Going to concatenate the lines after parsing. For an empty or
2265 // comment line use an empty string.
2266 if (*p == NUL || vim9_comment_start(p))
2267 {
2268 vim_free(line);
2269 line = vim_strsave((char_u *)"");
2270 }
2271
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002272 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2273 ++gap->ga_len;
2274 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002275 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002276 {
2277 vim_free(evalarg->eval_tofree);
2278 evalarg->eval_tofree = line;
2279 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002280
2281 // Advanced to the next line, "arg" no longer points into the previous
2282 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002283 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002284 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002285}
2286
Bram Moolenaare6b53242020-07-01 17:28:33 +02002287/*
2288 * Call eval_next_non_blank() and get the next line if needed.
2289 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002290 char_u *
2291skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2292{
2293 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002294 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002295
Bram Moolenaar962d7212020-07-04 14:15:00 +02002296 if (evalarg == NULL)
2297 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002298 eval_next_non_blank(p, evalarg, &getnext);
2299 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002300 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002301 return p;
2302}
2303
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002304/*
Bram Moolenaar844fb642021-10-23 13:32:30 +01002305 * Initialize "evalarg" for use.
2306 */
2307 void
2308init_evalarg(evalarg_T *evalarg)
2309{
2310 CLEAR_POINTER(evalarg);
2311 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
2312}
2313
2314/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002315 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002316 */
2317 void
2318clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2319{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002320 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002321 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002322 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002323 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002324 if (eap != NULL)
2325 {
2326 // We may need to keep the original command line, e.g. for
2327 // ":let" it has the variable names. But we may also need the
2328 // new one, "nextcmd" points into it. Keep both.
2329 vim_free(eap->cmdline_tofree);
2330 eap->cmdline_tofree = *eap->cmdlinep;
2331 *eap->cmdlinep = evalarg->eval_tofree;
2332 }
2333 else
2334 vim_free(evalarg->eval_tofree);
2335 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002336 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002337
Bram Moolenaar844fb642021-10-23 13:32:30 +01002338 ga_clear_strings(&evalarg->eval_tofree_ga);
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002339 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002340 }
2341}
2342
2343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002345 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2347 */
2348
2349/*
2350 * Handle zero level expression.
2351 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002352 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002353 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002354 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 * Return OK or FAIL.
2356 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002357 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002358eval0(
2359 char_u *arg,
2360 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002361 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002362 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002364 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2365}
2366
2367/*
2368 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2369 * expression and don't check what comes after the expression.
2370 */
2371 int
2372eval0_retarg(
2373 char_u *arg,
2374 typval_T *rettv,
2375 exarg_T *eap,
2376 evalarg_T *evalarg,
2377 char_u **retarg)
2378{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 int ret;
2380 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002381 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002382 int did_emsg_before = did_emsg;
2383 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002384 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002385 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002386 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387
2388 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002389 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002390 expr_end = p;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002391 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002392
Bram Moolenaar02929a32021-12-17 14:46:12 +00002393 // In Vim9 script a command block is not split at NL characters for
2394 // commands using an expression argument. Skip over a '#' comment to check
2395 // for a following NL. Require white space before the '#'.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002396 if (in_vim9script() && p > expr_end && retarg == NULL)
Bram Moolenaar02929a32021-12-17 14:46:12 +00002397 while (*p == '#')
2398 {
2399 char_u *nl = vim_strchr(p, NL);
2400
2401 if (nl == NULL)
2402 break;
2403 p = skipwhite(nl + 1);
2404 if (eap != NULL && *p != NUL)
2405 eap->nextcmd = p;
2406 check_for_end = FALSE;
2407 }
2408
2409 if (ret != FAIL && check_for_end)
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002410 end_error = !ends_excmd2(arg, p);
2411 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 {
2413 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002414 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 /*
2416 * Report the invalid expression unless the expression evaluation has
2417 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002418 * exception, or we already gave a more specific error.
2419 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002421 if (!aborting()
2422 && did_emsg == did_emsg_before
2423 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002424 && (flags & EVAL_CONSTANT) == 0
2425 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002426 {
2427 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002428 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002429 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002430 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002431 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002432
2433 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002434 // a next command to avoid more errors, unless "|" is following, which
2435 // could only be a command separator.
2436 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2437 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002438 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002440
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002441 if (retarg != NULL)
2442 *retarg = p;
2443 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002444 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002445
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 return ret;
2447}
2448
2449/*
2450 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002451 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002452 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 *
2454 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002455 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002457 * Note: "rettv.v_lock" is not set.
2458 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 * Return OK or FAIL.
2460 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002461 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002462eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002464 char_u *p;
2465 int getnext;
2466
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002467 CLEAR_POINTER(rettv);
2468
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 /*
2470 * Get the first variable.
2471 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002472 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 return FAIL;
2474
Bram Moolenaar793648f2020-06-26 21:28:25 +02002475 p = eval_next_non_blank(*arg, evalarg, &getnext);
2476 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002478 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002479 int result;
2480 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002481 evalarg_T *evalarg_used = evalarg;
2482 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002483 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002484 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002485 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002486
2487 if (evalarg == NULL)
2488 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002489 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002490 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002491 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002492 orig_flags = evalarg_used->eval_flags;
2493 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002494
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002495 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002496 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002497 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002498 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002499 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002500 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002501 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002502 clear_tv(rettv);
2503 return FAIL;
2504 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002505 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002506 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002507
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002509 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002511 int error = FALSE;
2512
Bram Moolenaar13106602020-10-04 16:06:05 +02002513 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002514 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002515 else if (vim9script)
2516 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002517 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002519 if (error || !op_falsy || !result)
2520 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002521 if (error)
2522 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 }
2524
2525 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002526 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002528 if (op_falsy)
2529 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002530 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002531 {
mityu4ac198c2021-05-28 17:52:40 +02002532 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002533 clear_tv(rettv);
2534 return FAIL;
2535 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002536 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002537 evalarg_used->eval_flags = (op_falsy ? !result : result)
2538 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2539 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002540 {
2541 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002543 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002544 if (!op_falsy || !result)
2545 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002547 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002549 /*
2550 * Check for the ":".
2551 */
2552 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2553 if (*p != ':')
2554 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002555 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002556 if (evaluate && result)
2557 clear_tv(rettv);
2558 evalarg_used->eval_flags = orig_flags;
2559 return FAIL;
2560 }
2561 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002562 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002563 else
2564 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002565 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002566 {
2567 error_white_both(p, 1);
2568 clear_tv(rettv);
2569 evalarg_used->eval_flags = orig_flags;
2570 return FAIL;
2571 }
2572 *arg = p;
2573 }
2574
2575 /*
2576 * Get the third variable. Recursive!
2577 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002578 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002579 {
mityu4ac198c2021-05-28 17:52:40 +02002580 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002581 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002582 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002583 return FAIL;
2584 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002585 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2586 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002587 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002588 if (eval1(arg, &var2, evalarg_used) == FAIL)
2589 {
2590 if (evaluate && result)
2591 clear_tv(rettv);
2592 evalarg_used->eval_flags = orig_flags;
2593 return FAIL;
2594 }
2595 if (evaluate && !result)
2596 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002598
2599 if (evalarg == NULL)
2600 clear_evalarg(&local_evalarg, NULL);
2601 else
2602 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 }
2604
2605 return OK;
2606}
2607
2608/*
2609 * Handle first level expression:
2610 * expr2 || expr2 || expr2 logical OR
2611 *
2612 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002613 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 *
2615 * Return OK or FAIL.
2616 */
2617 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002618eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002620 char_u *p;
2621 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622
2623 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002624 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002626 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 return FAIL;
2628
2629 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002630 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002632 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002633 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002635 evalarg_T *evalarg_used = evalarg;
2636 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002637 int evaluate;
2638 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002639 long result = FALSE;
2640 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002641 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002642 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002643
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002644 if (evalarg == NULL)
2645 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002646 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002647 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002648 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002649 orig_flags = evalarg_used->eval_flags;
2650 evaluate = orig_flags & EVAL_EVALUATE;
2651 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002652 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002653 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002654 result = tv_get_bool_chk(rettv, &error);
2655 else if (tv_get_number_chk(rettv, &error) != 0)
2656 result = TRUE;
2657 clear_tv(rettv);
2658 if (error)
2659 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 }
2661
2662 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002663 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002665 while (p[0] == '|' && p[1] == '|')
2666 {
2667 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002668 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002669 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002670 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002671 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002672 {
2673 error_white_both(p, 2);
2674 clear_tv(rettv);
2675 return FAIL;
2676 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002677 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002678 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002679
2680 /*
2681 * Get the second variable.
2682 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002683 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002684 {
mityu4ac198c2021-05-28 17:52:40 +02002685 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002686 clear_tv(rettv);
2687 return FAIL;
2688 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002689 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2690 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002691 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002692 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002693 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002694
2695 /*
2696 * Compute the result.
2697 */
2698 if (evaluate && !result)
2699 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002700 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002701 result = tv_get_bool_chk(&var2, &error);
2702 else if (tv_get_number_chk(&var2, &error) != 0)
2703 result = TRUE;
2704 clear_tv(&var2);
2705 if (error)
2706 return FAIL;
2707 }
2708 if (evaluate)
2709 {
2710 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002711 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002712 rettv->v_type = VAR_BOOL;
2713 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002714 }
2715 else
2716 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002717 rettv->v_type = VAR_NUMBER;
2718 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002719 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002720 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002721
2722 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002724
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002725 if (evalarg == NULL)
2726 clear_evalarg(&local_evalarg, NULL);
2727 else
2728 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 }
2730
2731 return OK;
2732}
2733
2734/*
2735 * Handle second level expression:
2736 * expr3 && expr3 && expr3 logical AND
2737 *
2738 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002739 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 *
2741 * Return OK or FAIL.
2742 */
2743 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002744eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002746 char_u *p;
2747 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748
2749 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002750 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002752 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 return FAIL;
2754
2755 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002756 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002758 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002759 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002761 evalarg_T *evalarg_used = evalarg;
2762 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002763 int orig_flags;
2764 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002765 long result = TRUE;
2766 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002767 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002768 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002769
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002770 if (evalarg == NULL)
2771 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002772 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002773 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002774 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002775 orig_flags = evalarg_used->eval_flags;
2776 evaluate = orig_flags & EVAL_EVALUATE;
2777 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002778 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002779 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002780 result = tv_get_bool_chk(rettv, &error);
2781 else if (tv_get_number_chk(rettv, &error) == 0)
2782 result = FALSE;
2783 clear_tv(rettv);
2784 if (error)
2785 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 }
2787
2788 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002789 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002791 while (p[0] == '&' && p[1] == '&')
2792 {
2793 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002794 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002795 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002796 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002797 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002798 {
2799 error_white_both(p, 2);
2800 clear_tv(rettv);
2801 return FAIL;
2802 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002803 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002804 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002805
2806 /*
2807 * Get the second variable.
2808 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002809 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002810 {
mityu4ac198c2021-05-28 17:52:40 +02002811 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002812 clear_tv(rettv);
2813 return FAIL;
2814 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002815 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2816 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002817 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002818 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002819 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002820 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002821
2822 /*
2823 * Compute the result.
2824 */
2825 if (evaluate && result)
2826 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002827 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002828 result = tv_get_bool_chk(&var2, &error);
2829 else if (tv_get_number_chk(&var2, &error) == 0)
2830 result = FALSE;
2831 clear_tv(&var2);
2832 if (error)
2833 return FAIL;
2834 }
2835 if (evaluate)
2836 {
2837 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002838 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002839 rettv->v_type = VAR_BOOL;
2840 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002841 }
2842 else
2843 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002844 rettv->v_type = VAR_NUMBER;
2845 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002846 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002847 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002848
2849 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002851
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002852 if (evalarg == NULL)
2853 clear_evalarg(&local_evalarg, NULL);
2854 else
2855 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 }
2857
2858 return OK;
2859}
2860
2861/*
2862 * Handle third level expression:
2863 * var1 == var2
2864 * var1 =~ var2
2865 * var1 != var2
2866 * var1 !~ var2
2867 * var1 > var2
2868 * var1 >= var2
2869 * var1 < var2
2870 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002871 * var1 is var2
2872 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 *
2874 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002875 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 *
2877 * Return OK or FAIL.
2878 */
2879 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002880eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002883 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002884 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002886 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887
2888 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002889 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002891 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 return FAIL;
2893
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002894 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002895
Bram Moolenaar696ba232020-07-29 21:20:41 +02002896 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897
2898 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002899 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002901 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002903 typval_T var2;
2904 int ic;
2905 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002906 int evaluate = evalarg == NULL
2907 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002908 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002909
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002910 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002911 {
Bram Moolenaare442d592022-05-05 12:20:28 +01002912 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002913 p = *arg;
2914 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002915 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2916 {
mityu4ac198c2021-05-28 17:52:40 +02002917 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002918 clear_tv(rettv);
2919 return FAIL;
2920 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002921
Bram Moolenaar696ba232020-07-29 21:20:41 +02002922 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2923 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002924 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002925 clear_tv(rettv);
2926 return FAIL;
2927 }
2928
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002929 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 if (p[len] == '?')
2931 {
2932 ic = TRUE;
2933 ++len;
2934 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002935 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 else if (p[len] == '#')
2937 {
2938 ic = FALSE;
2939 ++len;
2940 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002941 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002943 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944
2945 /*
2946 * Get the second variable.
2947 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002948 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2949 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002950 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002951 clear_tv(rettv);
2952 return FAIL;
2953 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002954 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002955 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002957 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 return FAIL;
2959 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002960 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002961 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002962 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002963
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002964 // use the line of the comparison for messages
2965 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002966 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002967 {
2968 ret = FAIL;
2969 clear_tv(rettv);
2970 }
2971 else
2972 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002973 clear_tv(&var2);
2974 return ret;
2975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 }
2977
2978 return OK;
2979}
2980
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002981/*
2982 * Make a copy of blob "tv1" and append blob "tv2".
2983 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984 void
2985eval_addblob(typval_T *tv1, typval_T *tv2)
2986{
2987 blob_T *b1 = tv1->vval.v_blob;
2988 blob_T *b2 = tv2->vval.v_blob;
2989 blob_T *b = blob_alloc();
2990 int i;
2991
2992 if (b != NULL)
2993 {
2994 for (i = 0; i < blob_len(b1); i++)
2995 ga_append(&b->bv_ga, blob_get(b1, i));
2996 for (i = 0; i < blob_len(b2); i++)
2997 ga_append(&b->bv_ga, blob_get(b2, i));
2998
2999 clear_tv(tv1);
3000 rettv_blob_set(tv1, b);
3001 }
3002}
3003
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003004/*
3005 * Make a copy of list "tv1" and append list "tv2".
3006 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007 int
3008eval_addlist(typval_T *tv1, typval_T *tv2)
3009{
3010 typval_T var3;
3011
3012 // concatenate Lists
3013 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3014 {
3015 clear_tv(tv1);
3016 clear_tv(tv2);
3017 return FAIL;
3018 }
3019 clear_tv(tv1);
3020 *tv1 = var3;
3021 return OK;
3022}
3023
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003025 * Handle the bitwise left/right shift operator expression:
3026 * var1 << var2
3027 * var1 >> var2
3028 *
3029 * "arg" must point to the first non-white of the expression.
3030 * "arg" is advanced to just after the recognized expression.
3031 *
3032 * Return OK or FAIL.
3033 */
3034 static int
3035eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3036{
3037 /*
3038 * Get the first expression.
3039 */
3040 if (eval6(arg, rettv, evalarg) == FAIL)
3041 return FAIL;
3042
3043 /*
3044 * Repeat computing, until no '<<' or '>>' is following.
3045 */
3046 for (;;)
3047 {
3048 char_u *p;
3049 int getnext;
3050 exprtype_T type;
3051 int evaluate;
3052 typval_T var2;
3053 int vim9script;
3054
3055 p = eval_next_non_blank(*arg, evalarg, &getnext);
3056 if (p[0] == '<' && p[1] == '<')
3057 type = EXPR_LSHIFT;
3058 else if (p[0] == '>' && p[1] == '>')
3059 type = EXPR_RSHIFT;
3060 else
3061 return OK;
3062
3063 // Handle a bitwise left or right shift operator
3064 if (rettv->v_type != VAR_NUMBER)
3065 {
3066 // left operand should be a number
3067 emsg(_(e_bitshift_ops_must_be_number));
3068 clear_tv(rettv);
3069 return FAIL;
3070 }
3071
3072 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3073 vim9script = in_vim9script();
3074 if (getnext)
3075 {
3076 *arg = eval_next_line(*arg, evalarg);
3077 p = *arg;
3078 }
3079 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3080 {
3081 error_white_both(*arg, 2);
3082 clear_tv(rettv);
3083 return FAIL;
3084 }
3085
3086 /*
3087 * Get the second variable.
3088 */
3089 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3090 {
3091 error_white_both(p, 2);
3092 clear_tv(rettv);
3093 return FAIL;
3094 }
3095 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3096 if (eval6(arg, &var2, evalarg) == FAIL)
3097 {
3098 clear_tv(rettv);
3099 return FAIL;
3100 }
3101
3102 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3103 {
3104 // right operand should be a positive number
3105 if (var2.v_type != VAR_NUMBER)
3106 emsg(_(e_bitshift_ops_must_be_number));
3107 else
3108 emsg(_(e_bitshift_ops_must_be_postive));
3109 clear_tv(rettv);
3110 clear_tv(&var2);
3111 return FAIL;
3112 }
3113
3114 if (evaluate)
3115 {
3116 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3117 // shifting more bits than we have always results in zero
3118 rettv->vval.v_number = 0;
3119 else if (type == EXPR_LSHIFT)
3120 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003121 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003122 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003123 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003124 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003125 }
3126
3127 clear_tv(&var2);
3128 }
3129
3130 return OK;
3131}
3132
3133/*
3134 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003135 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003137 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003138 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 *
3140 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003141 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 *
3143 * Return OK or FAIL.
3144 */
3145 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003146eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003149 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003151 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 return FAIL;
3153
3154 /*
3155 * Repeat computing, until no '+', '-' or '.' is following.
3156 */
3157 for (;;)
3158 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003159 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003160 int getnext;
3161 char_u *p;
3162 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003163 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003164 int concat;
3165 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003166 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003167
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003168 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003169 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003170 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003171 p = eval_next_non_blank(*arg, evalarg, &getnext);
3172 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003173 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003174 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3175 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003177 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3178 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003179
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003180 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003181 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003182 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003183 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003184 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003185 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003186 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003187 {
mityu4ac198c2021-05-28 17:52:40 +02003188 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003189 clear_tv(rettv);
3190 return FAIL;
3191 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003192 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003193 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003194 if ((op != '+' || (rettv->v_type != VAR_LIST
3195 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003196#ifdef FEAT_FLOAT
3197 && (op == '.' || rettv->v_type != VAR_FLOAT)
3198#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003199 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003200 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003201 int error = FALSE;
3202
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003203 // For "list + ...", an illegal use of the first operand as
3204 // a number cannot be determined before evaluating the 2nd
3205 // operand: if this is also a list, all is ok.
3206 // For "something . ...", "something - ..." or "non-list + ...",
3207 // we know that the first operand needs to be a string or number
3208 // without evaluating the 2nd operand. So check before to avoid
3209 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003210 if (op != '.')
3211 tv_get_number_chk(rettv, &error);
3212 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003213 {
3214 clear_tv(rettv);
3215 return FAIL;
3216 }
3217 }
3218
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 /*
3220 * Get the second variable.
3221 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003222 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003223 {
mityu89dcb4d2021-05-28 13:50:17 +02003224 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003225 clear_tv(rettv);
3226 return FAIL;
3227 }
3228 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003229 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003231 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 return FAIL;
3233 }
3234
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003235 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 {
3237 /*
3238 * Compute the result.
3239 */
3240 if (op == '.')
3241 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003242 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3243 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003244 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003245
Bram Moolenaar2e086612020-08-25 22:37:48 +02003246 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003247 || var2.v_type == VAR_CHANNEL
3248 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003249 semsg(_(e_using_invalid_value_as_string_str),
3250 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003251#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003252 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003253 {
3254 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3255 var2.vval.v_float);
3256 s2 = buf2;
3257 }
3258#endif
3259 else
3260 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003261 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003262 {
3263 clear_tv(rettv);
3264 clear_tv(&var2);
3265 return FAIL;
3266 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003267 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003268 clear_tv(rettv);
3269 rettv->v_type = VAR_STRING;
3270 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003272 else if (op == '+' && rettv->v_type == VAR_BLOB
3273 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003275 else if (op == '+' && rettv->v_type == VAR_LIST
3276 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003277 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003279 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 else
3282 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003283 int error = FALSE;
3284 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003285#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003286 float_T f1 = 0, f2 = 0;
3287
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003288 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003289 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003290 f1 = rettv->vval.v_float;
3291 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003292 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003293 else
3294#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003295 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003296 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003297 if (error)
3298 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003299 // This can only happen for "list + non-list" or
3300 // "blob + non-blob". For "non-list + ..." or
3301 // "something - ...", we returned before evaluating the
3302 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003303 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003304 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003305 return FAIL;
3306 }
3307#ifdef FEAT_FLOAT
3308 if (var2.v_type == VAR_FLOAT)
3309 f1 = n1;
3310#endif
3311 }
3312#ifdef FEAT_FLOAT
3313 if (var2.v_type == VAR_FLOAT)
3314 {
3315 f2 = var2.vval.v_float;
3316 n2 = 0;
3317 }
3318 else
3319#endif
3320 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003321 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003322 if (error)
3323 {
3324 clear_tv(rettv);
3325 clear_tv(&var2);
3326 return FAIL;
3327 }
3328#ifdef FEAT_FLOAT
3329 if (rettv->v_type == VAR_FLOAT)
3330 f2 = n2;
3331#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003332 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003333 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003334
3335#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003336 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003337 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3338 {
3339 if (op == '+')
3340 f1 = f1 + f2;
3341 else
3342 f1 = f1 - f2;
3343 rettv->v_type = VAR_FLOAT;
3344 rettv->vval.v_float = f1;
3345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003347#endif
3348 {
3349 if (op == '+')
3350 n1 = n1 + n2;
3351 else
3352 n1 = n1 - n2;
3353 rettv->v_type = VAR_NUMBER;
3354 rettv->vval.v_number = n1;
3355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003357 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 }
3359 }
3360 return OK;
3361}
3362
3363/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003364 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 * * number multiplication
3366 * / number division
3367 * % number modulo
3368 *
3369 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003370 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 *
3372 * Return OK or FAIL.
3373 */
3374 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003375eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003376 char_u **arg,
3377 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003378 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003379 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003381#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003382 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384
3385 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003386 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003388 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 return FAIL;
3390
3391 /*
3392 * Repeat computing, until no '*', '/' or '%' is following.
3393 */
3394 for (;;)
3395 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003396 int evaluate;
3397 int getnext;
3398 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003399 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003400 int op;
3401 varnumber_T n1, n2;
3402#ifdef FEAT_FLOAT
3403 float_T f1, f2;
3404#endif
3405 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003406
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003407 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003408 p = eval_next_non_blank(*arg, evalarg, &getnext);
3409 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003410 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003412
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003413 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003414 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003415 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003416 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003417 {
3418 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3419 {
mityu4ac198c2021-05-28 17:52:40 +02003420 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003421 clear_tv(rettv);
3422 return FAIL;
3423 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003424 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003427#ifdef FEAT_FLOAT
3428 f1 = 0;
3429 f2 = 0;
3430#endif
3431 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003432 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003434#ifdef FEAT_FLOAT
3435 if (rettv->v_type == VAR_FLOAT)
3436 {
3437 f1 = rettv->vval.v_float;
3438 use_float = TRUE;
3439 n1 = 0;
3440 }
3441 else
3442#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003443 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003444 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003445 if (error)
3446 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 }
3448 else
3449 n1 = 0;
3450
3451 /*
3452 * Get the second variable.
3453 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003454 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3455 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003456 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003457 clear_tv(rettv);
3458 return FAIL;
3459 }
3460 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003461 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 return FAIL;
3463
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003464 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003466#ifdef FEAT_FLOAT
3467 if (var2.v_type == VAR_FLOAT)
3468 {
3469 if (!use_float)
3470 {
3471 f1 = n1;
3472 use_float = TRUE;
3473 }
3474 f2 = var2.vval.v_float;
3475 n2 = 0;
3476 }
3477 else
3478#endif
3479 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003480 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003481 clear_tv(&var2);
3482 if (error)
3483 return FAIL;
3484#ifdef FEAT_FLOAT
3485 if (use_float)
3486 f2 = n2;
3487#endif
3488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
3490 /*
3491 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003492 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003494#ifdef FEAT_FLOAT
3495 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003497 if (op == '*')
3498 f1 = f1 * f2;
3499 else if (op == '/')
3500 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003501# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003502 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003503 if (f2 == 0.0)
3504 {
3505 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003506 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003507 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003508 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003509 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003510 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003511 }
3512 else
3513 f1 = f1 / f2;
3514# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003515 // We rely on the floating point library to handle divide
3516 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003517 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003518# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003521 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003522 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003523 return FAIL;
3524 }
3525 rettv->v_type = VAR_FLOAT;
3526 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 }
3528 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003531 int failed = FALSE;
3532
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003533 if (op == '*')
3534 n1 = n1 * n2;
3535 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003536 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003538 n1 = num_modulus(n1, n2, &failed);
3539 if (failed)
3540 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003541
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003542 rettv->v_type = VAR_NUMBER;
3543 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 }
3546 }
3547
3548 return OK;
3549}
3550
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003551/*
3552 * Handle a type cast before a base level expression.
3553 * "arg" must point to the first non-white of the expression.
3554 * "arg" is advanced to just after the recognized expression.
3555 * Return OK or FAIL.
3556 */
3557 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003558eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003559 char_u **arg,
3560 typval_T *rettv,
3561 evalarg_T *evalarg,
3562 int want_string) // after "." operator
3563{
3564 type_T *want_type = NULL;
3565 garray_T type_list; // list of pointers to allocated types
3566 int res;
3567 int evaluate = evalarg == NULL ? 0
3568 : (evalarg->eval_flags & EVAL_EVALUATE);
3569
3570 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003571 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3572 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003573 {
3574 ++*arg;
3575 ga_init2(&type_list, sizeof(type_T *), 10);
3576 want_type = parse_type(arg, &type_list, TRUE);
3577 if (want_type == NULL && (evaluate || **arg != '>'))
3578 {
3579 clear_type_list(&type_list);
3580 return FAIL;
3581 }
3582
3583 if (**arg != '>')
3584 {
3585 if (*skipwhite(*arg) == '>')
3586 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3587 else
3588 emsg(_(e_missing_gt));
3589 clear_type_list(&type_list);
3590 return FAIL;
3591 }
3592 ++*arg;
3593 *arg = skipwhite_and_linebreak(*arg, evalarg);
3594 }
3595
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003596 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003597
3598 if (want_type != NULL && evaluate)
3599 {
3600 if (res == OK)
3601 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003602 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3603 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003604
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003605 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003606 {
3607 if (want_type == &t_bool && actual != &t_bool
3608 && (actual->tt_flags & TTFLAG_BOOL_OK))
3609 {
3610 int n = tv2bool(rettv);
3611
3612 // can use "0" and "1" for boolean in some places
3613 clear_tv(rettv);
3614 rettv->v_type = VAR_BOOL;
3615 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3616 }
3617 else
3618 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003619 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003620
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003621 where.wt_variable = TRUE;
3622 res = check_type(want_type, actual, TRUE, where);
3623 }
3624 }
3625 }
3626 clear_type_list(&type_list);
3627 }
3628
3629 return res;
3630}
3631
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003632 int
3633eval_leader(char_u **arg, int vim9)
3634{
3635 char_u *s = *arg;
3636 char_u *p = *arg;
3637
3638 while (*p == '!' || *p == '-' || *p == '+')
3639 {
3640 char_u *n = skipwhite(p + 1);
3641
3642 // ++, --, -+ and +- are not accepted in Vim9 script
3643 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3644 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003645 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003646 return FAIL;
3647 }
3648 p = n;
3649 }
3650 *arg = p;
3651 return OK;
3652}
3653
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003655 * Check for a predefined value "true", "false" and "null.*".
3656 * Return OK when recognized.
3657 */
3658 int
3659handle_predefined(char_u *s, int len, typval_T *rettv)
3660{
3661 switch (len)
3662 {
3663 case 4: if (STRNCMP(s, "true", 4) == 0)
3664 {
3665 rettv->v_type = VAR_BOOL;
3666 rettv->vval.v_number = VVAL_TRUE;
3667 return OK;
3668 }
3669 if (STRNCMP(s, "null", 4) == 0)
3670 {
3671 rettv->v_type = VAR_SPECIAL;
3672 rettv->vval.v_number = VVAL_NULL;
3673 return OK;
3674 }
3675 break;
3676 case 5: if (STRNCMP(s, "false", 5) == 0)
3677 {
3678 rettv->v_type = VAR_BOOL;
3679 rettv->vval.v_number = VVAL_FALSE;
3680 return OK;
3681 }
3682 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003683 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3684 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003685#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003686 rettv->v_type = VAR_JOB;
3687 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003688#else
3689 rettv->v_type = VAR_SPECIAL;
3690 rettv->vval.v_number = VVAL_NULL;
3691#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003692 return OK;
3693 }
3694 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003695 case 9:
3696 if (STRNCMP(s, "null_", 5) != 0)
3697 break;
3698 if (STRNCMP(s + 5, "list", 4) == 0)
3699 {
3700 rettv->v_type = VAR_LIST;
3701 rettv->vval.v_list = NULL;
3702 return OK;
3703 }
3704 if (STRNCMP(s + 5, "dict", 4) == 0)
3705 {
3706 rettv->v_type = VAR_DICT;
3707 rettv->vval.v_dict = NULL;
3708 return OK;
3709 }
3710 if (STRNCMP(s + 5, "blob", 4) == 0)
3711 {
3712 rettv->v_type = VAR_BLOB;
3713 rettv->vval.v_blob = NULL;
3714 return OK;
3715 }
3716 break;
3717 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3718 {
3719 rettv->v_type = VAR_STRING;
3720 rettv->vval.v_string = NULL;
3721 return OK;
3722 }
3723 break;
3724 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003725 if (STRNCMP(s, "null_channel", 12) == 0)
3726 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003727#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003728 rettv->v_type = VAR_CHANNEL;
3729 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003730#else
3731 rettv->v_type = VAR_SPECIAL;
3732 rettv->vval.v_number = VVAL_NULL;
3733#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003734 return OK;
3735 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003736 if (STRNCMP(s, "null_partial", 12) == 0)
3737 {
3738 rettv->v_type = VAR_PARTIAL;
3739 rettv->vval.v_partial = NULL;
3740 return OK;
3741 }
3742 break;
3743 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3744 {
3745 rettv->v_type = VAR_FUNC;
3746 rettv->vval.v_string = NULL;
3747 return OK;
3748 }
3749 break;
3750 }
3751 return FAIL;
3752}
3753
3754/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 * Handle sixth level expression:
3756 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003757 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003758 * "string" string constant
3759 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 * &option-name option value
3761 * @r register contents
3762 * identifier variable value
3763 * function() function call
3764 * $VAR environment variable
3765 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003766 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003767 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003768 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003769 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 *
3771 * Also handle:
3772 * ! in front logical NOT
3773 * - in front unary minus
3774 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003775 * trailing [] subscript in String or List
3776 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003777 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 *
3779 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003780 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 *
3782 * Return OK or FAIL.
3783 */
3784 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003785eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003786 char_u **arg,
3787 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003788 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003791 int evaluate = evalarg != NULL
3792 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 int len;
3794 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003795 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 char_u *start_leader, *end_leader;
3797 int ret = OK;
3798 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003799 static int recurse = 0;
3800 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801
3802 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003803 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003804 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003806 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807
3808 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003809 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 */
3811 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003812 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003813 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 end_leader = *arg;
3815
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003816 if (**arg == '.' && (!isdigit(*(*arg + 1))
3817#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003818 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003819#endif
3820 ))
3821 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003822 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003823 ++*arg;
3824 return FAIL;
3825 }
3826
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003827 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003828 // and crash. With MSVC the stack is smaller.
3829 if (recurse ==
3830#ifdef _MSC_VER
3831 300
3832#else
3833 1000
3834#endif
3835 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003836 {
3837 semsg(_(e_expression_too_recursive_str), *arg);
3838 return FAIL;
3839 }
3840 ++recurse;
3841
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 switch (**arg)
3843 {
3844 /*
3845 * Number constant.
3846 */
3847 case '0':
3848 case '1':
3849 case '2':
3850 case '3':
3851 case '4':
3852 case '5':
3853 case '6':
3854 case '7':
3855 case '8':
3856 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003857 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003858
3859 // Apply prefixed "-" and "+" now. Matters especially when
3860 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003861 if (ret == OK && evaluate && end_leader > start_leader
3862 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003863 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 break;
3865
3866 /*
3867 * String constant: "string".
3868 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003869 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 break;
3871
3872 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003873 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003875 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003876 break;
3877
3878 /*
3879 * List: [expr, expr]
3880 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003881 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 break;
3883
3884 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003885 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003886 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003887 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003888 {
3889 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3890 }
3891 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003892 {
3893 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003894 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003895 }
3896 else
3897 ret = NOTDONE;
3898 break;
3899
3900 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003901 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003902 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003903 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003904 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003905 ret = NOTDONE;
3906 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00003907 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003908 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003909 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003910 break;
3911
3912 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003913 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003915 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 break;
3917
3918 /*
3919 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01003920 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 */
LemonBoy2eaef102022-05-06 13:14:50 +01003922 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
3923 ret = eval_interp_string(arg, rettv, evaluate);
3924 else
3925 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 break;
3927
3928 /*
3929 * Register contents: @r.
3930 */
3931 case '@': ++*arg;
3932 if (evaluate)
3933 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003934 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003935 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003936 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003937 emsg_invreg(**arg);
3938 else
3939 {
3940 rettv->v_type = VAR_STRING;
3941 rettv->vval.v_string = get_reg_contents(**arg,
3942 GREG_EXPR_SRC);
3943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 }
3945 if (**arg != NUL)
3946 ++*arg;
3947 break;
3948
3949 /*
3950 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003951 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003953 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003954 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01003955 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003956 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003957 if (ret == OK && evaluate)
3958 {
3959 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3960
Bram Moolenaara9931532021-06-12 15:58:16 +02003961 // Compile it here to get the return type. The return
3962 // type is optional, when it's missing use t_unknown.
3963 // This is recognized in compile_return().
3964 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3965 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00003966 if (compile_def_function(ufunc, FALSE,
3967 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003968 {
3969 clear_tv(rettv);
3970 ret = FAIL;
3971 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003972 }
3973 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003974 if (ret == NOTDONE)
3975 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003976 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003977 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003978
Bram Moolenaar9215f012020-06-27 21:18:00 +02003979 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003980 if (**arg == ')')
3981 ++*arg;
3982 else if (ret == OK)
3983 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003984 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003985 clear_tv(rettv);
3986 ret = FAIL;
3987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 }
3989 break;
3990
Bram Moolenaar8c711452005-01-14 21:53:12 +00003991 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 break;
3993 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003994
3995 if (ret == NOTDONE)
3996 {
3997 /*
3998 * Must be a variable or function name.
3999 * Can also be a curly-braces kind of name: {expr}.
4000 */
4001 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004002 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004003 if (alias != NULL)
4004 s = alias;
4005
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004006 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004007 ret = FAIL;
4008 else
4009 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004010 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4011
Bram Moolenaar4525a572022-02-13 11:57:33 +00004012 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004013 {
4014 emsg(_(e_cannot_use_underscore_here));
4015 ret = FAIL;
4016 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004017 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004018 && s[0] == 's' && s[1] == ':')
4019 {
4020 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4021 ret = FAIL;
4022 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004023 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004024 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004025 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004026 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004027 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004028 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02004029 else if (flags & EVAL_CONSTANT)
4030 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004031 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004032 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004033 // get the value of "true", "false", etc. or a variable
4034 ret = FAIL;
4035 if (vim9script)
4036 ret = handle_predefined(s, len, rettv);
4037 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004038 {
4039 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004040 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004041 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004042 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004043 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004044 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004045 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004046 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004047 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004048 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004049 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004050 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004051 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004052 }
4053
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004054 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4055 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004056 if (ret == OK)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004057 ret = handle_subscript(arg, name_start, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058
4059 /*
4060 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4061 */
4062 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004063 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004064
4065 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004066 return ret;
4067}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004068
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004069/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004070 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004071 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004072 * Adjusts "end_leaderp" until it is at "start_leader".
4073 */
4074 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004075eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004076 typval_T *rettv,
4077 int numeric_only,
4078 char_u *start_leader,
4079 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004080{
4081 char_u *end_leader = *end_leaderp;
4082 int ret = OK;
4083 int error = FALSE;
4084 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004085 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004086 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004087#ifdef FEAT_FLOAT
4088 float_T f = 0.0;
4089
4090 if (rettv->v_type == VAR_FLOAT)
4091 f = rettv->vval.v_float;
4092 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004093#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004094 {
4095 while (VIM_ISWHITE(end_leader[-1]))
4096 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004097 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004098 val = tv2bool(rettv);
4099 else
4100 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004101 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004102 if (error)
4103 {
4104 clear_tv(rettv);
4105 ret = FAIL;
4106 }
4107 else
4108 {
4109 while (end_leader > start_leader)
4110 {
4111 --end_leader;
4112 if (*end_leader == '!')
4113 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004114 if (numeric_only)
4115 {
4116 ++end_leader;
4117 break;
4118 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004119#ifdef FEAT_FLOAT
4120 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004121 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004122 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004123 {
4124 rettv->v_type = VAR_BOOL;
4125 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4126 }
4127 else
4128 f = !f;
4129 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004130 else
4131#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004132 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004133 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004134 type = VAR_BOOL;
4135 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004136 }
4137 else if (*end_leader == '-')
4138 {
4139#ifdef FEAT_FLOAT
4140 if (rettv->v_type == VAR_FLOAT)
4141 f = -f;
4142 else
4143#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004144 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004145 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004146 type = VAR_NUMBER;
4147 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004148 }
4149 }
4150#ifdef FEAT_FLOAT
4151 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004153 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004154 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004156 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004157#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004158 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004159 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004160 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004161 rettv->v_type = type;
4162 else
4163 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004164 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004167 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 return ret;
4169}
4170
4171/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004172 * Call the function referred to in "rettv".
4173 */
4174 static int
4175call_func_rettv(
4176 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004177 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004178 typval_T *rettv,
4179 int evaluate,
4180 dict_T *selfdict,
4181 typval_T *basetv)
4182{
4183 partial_T *pt = NULL;
4184 funcexe_T funcexe;
4185 typval_T functv;
4186 char_u *s;
4187 int ret;
4188
4189 // need to copy the funcref so that we can clear rettv
4190 if (evaluate)
4191 {
4192 functv = *rettv;
4193 rettv->v_type = VAR_UNKNOWN;
4194
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004195 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004196 if (functv.v_type == VAR_PARTIAL)
4197 {
4198 pt = functv.vval.v_partial;
4199 s = partial_name(pt);
4200 }
4201 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004202 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004203 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004204 if (s == NULL || *s == NUL)
4205 {
4206 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004207 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004208 goto theend;
4209 }
4210 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004211 }
4212 else
4213 s = (char_u *)"";
4214
Bram Moolenaara80faa82020-04-12 19:37:17 +02004215 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004216 funcexe.fe_firstline = curwin->w_cursor.lnum;
4217 funcexe.fe_lastline = curwin->w_cursor.lnum;
4218 funcexe.fe_evaluate = evaluate;
4219 funcexe.fe_partial = pt;
4220 funcexe.fe_selfdict = selfdict;
4221 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004222 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004223
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004224theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004225 // Clear the funcref afterwards, so that deleting it while
4226 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004227 if (evaluate)
4228 clear_tv(&functv);
4229
4230 return ret;
4231}
4232
4233/*
4234 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004235 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004236 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4237 */
4238 static int
4239eval_lambda(
4240 char_u **arg,
4241 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004242 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004243 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004244{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004245 int evaluate = evalarg != NULL
4246 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004247 typval_T base = *rettv;
4248 int ret;
4249
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004250 rettv->v_type = VAR_UNKNOWN;
4251
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004252 if (**arg == '{')
4253 {
4254 // ->{lambda}()
4255 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4256 }
4257 else
4258 {
4259 // ->(lambda)()
4260 ++*arg;
4261 ret = eval1(arg, rettv, evalarg);
4262 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004263 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004264 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004265 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004266 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004267 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004268 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4269 && rettv->v_type != VAR_PARTIAL)
4270 {
4271 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4272 return FAIL;
4273 }
4274 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004275 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004276 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004277 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004278
4279 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004280 {
4281 if (verbose)
4282 {
4283 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004284 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004285 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004286 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004287 }
4288 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004289 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004290 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004291 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004292 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004293
4294 // Clear the funcref afterwards, so that deleting it while
4295 // evaluating the arguments is possible (see test55).
4296 if (evaluate)
4297 clear_tv(&base);
4298
4299 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004300}
4301
4302/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004303 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004304 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004305 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4306 */
4307 static int
4308eval_method(
4309 char_u **arg,
4310 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004311 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004312 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004313{
4314 char_u *name;
4315 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004316 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004317 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004318 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004319 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004320 int evaluate = evalarg != NULL
4321 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004322
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004323 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004324
Bram Moolenaarac92e252019-08-03 21:58:38 +02004325 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004326 len = get_name_len(arg, &alias, evaluate, TRUE);
4327 if (alias != NULL)
4328 name = alias;
4329
4330 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004331 {
4332 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004333 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004334 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004335 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004336 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004337 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004338 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004339
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004340 // If there is no "(" immediately following, but there is further on,
4341 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4342 // Does not handle anything where "(" is part of the expression.
4343 *arg = skipwhite(*arg);
4344
4345 if (**arg != '(' && alias == NULL
4346 && (paren = vim_strchr(*arg, '(')) != NULL)
4347 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004348 char_u *deref;
4349
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004350 *arg = name;
4351 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004352 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4353 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004354 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004355 *arg = name + len;
4356 ret = FAIL;
4357 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004358 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004359 {
4360 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004361 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004362 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004363 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004364 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004365
4366 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004367 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004368 *arg = skipwhite(*arg);
4369
4370 if (**arg != '(')
4371 {
4372 if (verbose)
4373 semsg(_(e_missing_parenthesis_str), name);
4374 ret = FAIL;
4375 }
4376 else if (VIM_ISWHITE((*arg)[-1]))
4377 {
4378 if (verbose)
4379 emsg(_(e_no_white_space_allowed_before_parenthesis));
4380 ret = FAIL;
4381 }
4382 else
4383 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004384 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004385 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004386 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004387
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004388 // Clear the funcref afterwards, so that deleting it while
4389 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004390 if (evaluate)
4391 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004392 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004393
Bram Moolenaarac92e252019-08-03 21:58:38 +02004394 return ret;
4395}
4396
4397/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004398 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4399 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004400 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4401 */
4402 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004403eval_index(
4404 char_u **arg,
4405 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004406 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004407 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004408{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004409 int evaluate = evalarg != NULL
4410 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004411 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004412 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004413 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004414 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004415 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004416 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004417
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004418 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4419 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004420
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004421 init_tv(&var1);
4422 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004423 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004424 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004425 /*
4426 * dict.name
4427 */
4428 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004429 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004430 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004431 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004432 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004433 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004434 }
4435 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004436 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004437 /*
4438 * something[idx]
4439 *
4440 * Get the (first) variable from inside the [].
4441 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004442 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004443 if (**arg == ':')
4444 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004445 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004446 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004447 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004448 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004449 semsg(_(e_white_space_required_before_and_after_str_at_str),
4450 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004451 clear_tv(&var1);
4452 return FAIL;
4453 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004454 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004455 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004456 int error = FALSE;
4457
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004458#ifdef FEAT_FLOAT
4459 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004460 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004461 && var1.v_type == VAR_FLOAT)
4462 {
4463 var1.vval.v_string = typval_tostring(&var1, TRUE);
4464 var1.v_type = VAR_STRING;
4465 }
4466#endif
Bram Moolenaar4525a572022-02-13 11:57:33 +00004467 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004468 tv_get_number_chk(&var1, &error);
4469 else
4470 error = tv_get_string_chk(&var1) == NULL;
4471 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004472 {
4473 // not a number or string
4474 clear_tv(&var1);
4475 return FAIL;
4476 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004477 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004478
4479 /*
4480 * Get the second variable from inside the [:].
4481 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004482 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004483 if (**arg == ':')
4484 {
4485 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004486 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004487 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004488 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004489 semsg(_(e_white_space_required_before_and_after_str_at_str),
4490 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004491 if (!empty1)
4492 clear_tv(&var1);
4493 return FAIL;
4494 }
4495 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004496 if (**arg == ']')
4497 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004498 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004499 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004500 if (!empty1)
4501 clear_tv(&var1);
4502 return FAIL;
4503 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004504 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004505 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004506 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004507 if (!empty1)
4508 clear_tv(&var1);
4509 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004510 return FAIL;
4511 }
4512 }
4513
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004514 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004515 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004516 if (**arg != ']')
4517 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004518 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004519 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004520 clear_tv(&var1);
4521 if (range)
4522 clear_tv(&var2);
4523 return FAIL;
4524 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004525 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004526 }
4527
4528 if (evaluate)
4529 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004530 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004531 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004532 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004533
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004534 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004535 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004536 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004537 clear_tv(&var2);
4538 return res;
4539 }
4540 return OK;
4541}
4542
4543/*
4544 * Check if "rettv" can have an [index] or [sli:ce]
4545 */
4546 int
4547check_can_index(typval_T *rettv, int evaluate, int verbose)
4548{
4549 switch (rettv->v_type)
4550 {
4551 case VAR_FUNC:
4552 case VAR_PARTIAL:
4553 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004554 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004555 return FAIL;
4556 case VAR_FLOAT:
4557#ifdef FEAT_FLOAT
4558 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004559 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004560 return FAIL;
4561#endif
4562 case VAR_BOOL:
4563 case VAR_SPECIAL:
4564 case VAR_JOB:
4565 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004566 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004567 if (verbose)
4568 emsg(_(e_cannot_index_special_variable));
4569 return FAIL;
4570 case VAR_UNKNOWN:
4571 case VAR_ANY:
4572 case VAR_VOID:
4573 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004574 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004575 emsg(_(e_cannot_index_special_variable));
4576 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004577 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004578 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004579
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004580 case VAR_STRING:
4581 case VAR_LIST:
4582 case VAR_DICT:
4583 case VAR_BLOB:
4584 break;
4585 case VAR_NUMBER:
4586 if (in_vim9script())
4587 emsg(_(e_cannot_index_number));
4588 break;
4589 }
4590 return OK;
4591}
4592
4593/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004594 * slice() function
4595 */
4596 void
4597f_slice(typval_T *argvars, typval_T *rettv)
4598{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004599 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004600 && ((argvars[0].v_type != VAR_STRING
4601 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004602 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004603 && check_for_list_arg(argvars, 0) == FAIL)
4604 || check_for_number_arg(argvars, 1) == FAIL
4605 || check_for_opt_number_arg(argvars, 2) == FAIL))
4606 return;
4607
Bram Moolenaar6601b622021-01-13 21:47:15 +01004608 if (check_can_index(argvars, TRUE, FALSE) == OK)
4609 {
4610 copy_tv(argvars, rettv);
4611 eval_index_inner(rettv, TRUE, argvars + 1,
4612 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4613 TRUE, NULL, 0, FALSE);
4614 }
4615}
4616
4617/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004618 * Apply index or range to "rettv".
4619 * "var1" is the first index, NULL for [:expr].
4620 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004621 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4622 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004623 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4624 */
4625 int
4626eval_index_inner(
4627 typval_T *rettv,
4628 int is_range,
4629 typval_T *var1,
4630 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004631 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004632 char_u *key,
4633 int keylen,
4634 int verbose)
4635{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004636 varnumber_T n1, n2 = 0;
4637 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004638
4639 n1 = 0;
4640 if (var1 != NULL && rettv->v_type != VAR_DICT)
4641 n1 = tv_get_number(var1);
4642
4643 if (is_range)
4644 {
4645 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004646 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004647 if (verbose)
4648 emsg(_(e_cannot_slice_dictionary));
4649 return FAIL;
4650 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004651 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004652 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004653 else
4654 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004655 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004656
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004657 switch (rettv->v_type)
4658 {
4659 case VAR_UNKNOWN:
4660 case VAR_ANY:
4661 case VAR_VOID:
4662 case VAR_FUNC:
4663 case VAR_PARTIAL:
4664 case VAR_FLOAT:
4665 case VAR_BOOL:
4666 case VAR_SPECIAL:
4667 case VAR_JOB:
4668 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004669 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004670 break; // not evaluating, skipping over subscript
4671
4672 case VAR_NUMBER:
4673 case VAR_STRING:
4674 {
4675 char_u *s = tv_get_string(rettv);
4676
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004677 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004678 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004679 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004680 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004681 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004682 else
4683 s = char_from_string(s, n1);
4684 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004685 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004686 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004687 // The resulting variable is a substring. If the indexes
4688 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004689 if (n1 < 0)
4690 {
4691 n1 = len + n1;
4692 if (n1 < 0)
4693 n1 = 0;
4694 }
4695 if (n2 < 0)
4696 n2 = len + n2;
4697 else if (n2 >= len)
4698 n2 = len;
4699 if (n1 >= len || n2 < 0 || n1 > n2)
4700 s = NULL;
4701 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004702 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004703 }
4704 else
4705 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004706 // The resulting variable is a string of a single
4707 // character. If the index is too big or negative the
4708 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004709 if (n1 >= len || n1 < 0)
4710 s = NULL;
4711 else
4712 s = vim_strnsave(s + n1, 1);
4713 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004714 clear_tv(rettv);
4715 rettv->v_type = VAR_STRING;
4716 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004717 }
4718 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004719
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004720 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004721 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4722 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004723 break;
4724
4725 case VAR_LIST:
4726 if (var1 == NULL)
4727 n1 = 0;
4728 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004729 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004730 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004731 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004732 return FAIL;
4733 break;
4734
4735 case VAR_DICT:
4736 {
4737 dictitem_T *item;
4738 typval_T tmp;
4739
4740 if (key == NULL)
4741 {
4742 key = tv_get_string_chk(var1);
4743 if (key == NULL)
4744 return FAIL;
4745 }
4746
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004747 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004748
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004749 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004750 {
4751 if (verbose)
4752 {
4753 if (keylen > 0)
4754 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004755 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004756 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004757 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004758 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004759
4760 copy_tv(&item->di_tv, &tmp);
4761 clear_tv(rettv);
4762 *rettv = tmp;
4763 }
4764 break;
4765 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004766 return OK;
4767}
4768
4769/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004770 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004771 */
4772 char_u *
4773partial_name(partial_T *pt)
4774{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004775 if (pt != NULL)
4776 {
4777 if (pt->pt_name != NULL)
4778 return pt->pt_name;
4779 if (pt->pt_func != NULL)
4780 return pt->pt_func->uf_name;
4781 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004782 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004783}
4784
Bram Moolenaarddecc252016-04-06 22:59:37 +02004785 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004786partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004787{
4788 int i;
4789
4790 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004791 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004792 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004793 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004794 if (pt->pt_name != NULL)
4795 {
4796 func_unref(pt->pt_name);
4797 vim_free(pt->pt_name);
4798 }
4799 else
4800 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004801
Bram Moolenaar54656012021-06-09 20:50:46 +02004802 // "out_up" is no longer used, decrement refcount on partial that owns it.
4803 partial_unref(pt->pt_outer.out_up_partial);
4804
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004805 // Using pt_outer from another partial.
4806 partial_unref(pt->pt_outer_partial);
4807
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004808 // Decrease the reference count for the context of a closure. If down
4809 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004810 if (pt->pt_funcstack != NULL)
4811 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004812 --pt->pt_funcstack->fs_refcount;
4813 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004814 }
4815
Bram Moolenaarddecc252016-04-06 22:59:37 +02004816 vim_free(pt);
4817}
4818
4819/*
4820 * Unreference a closure: decrement the reference count and free it when it
4821 * becomes zero.
4822 */
4823 void
4824partial_unref(partial_T *pt)
4825{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004826 if (pt != NULL)
4827 {
4828 if (--pt->pt_refcount <= 0)
4829 partial_free(pt);
4830
4831 // If the reference count goes down to one, the funcstack may be the
4832 // only reference and can be freed if no other partials reference it.
4833 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4834 funcstack_check_refcount(pt->pt_funcstack);
4835 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004836}
4837
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004838/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004839 * Return the next (unique) copy ID.
4840 * Used for serializing nested structures.
4841 */
4842 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004843get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004844{
4845 current_copyID += COPYID_INC;
4846 return current_copyID;
4847}
4848
4849/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004850 * Garbage collection for lists and dictionaries.
4851 *
4852 * We use reference counts to be able to free most items right away when they
4853 * are no longer used. But for composite items it's possible that it becomes
4854 * unused while the reference count is > 0: When there is a recursive
4855 * reference. Example:
4856 * :let l = [1, 2, 3]
4857 * :let d = {9: l}
4858 * :let l[1] = d
4859 *
4860 * Since this is quite unusual we handle this with garbage collection: every
4861 * once in a while find out which lists and dicts are not referenced from any
4862 * variable.
4863 *
4864 * Here is a good reference text about garbage collection (refers to Python
4865 * but it applies to all reference-counting mechanisms):
4866 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004867 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004868
4869/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004870 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004871 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004872 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004873 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004874 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004875garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004876{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004877 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004878 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004879 buf_T *buf;
4880 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004881 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004882 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004883
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004884 if (!testing)
4885 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004886 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004887 want_garbage_collect = FALSE;
4888 may_garbage_collect = FALSE;
4889 garbage_collect_at_exit = FALSE;
4890 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004891
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004892 // The execution stack can grow big, limit the size.
4893 if (exestack.ga_maxlen - exestack.ga_len > 500)
4894 {
4895 size_t new_len;
4896 char_u *pp;
4897 int n;
4898
4899 // Keep 150% of the current size, with a minimum of the growth size.
4900 n = exestack.ga_len / 2;
4901 if (n < exestack.ga_growsize)
4902 n = exestack.ga_growsize;
4903
4904 // Don't make it bigger though.
4905 if (exestack.ga_len + n < exestack.ga_maxlen)
4906 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004907 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004908 pp = vim_realloc(exestack.ga_data, new_len);
4909 if (pp == NULL)
4910 return FAIL;
4911 exestack.ga_maxlen = exestack.ga_len + n;
4912 exestack.ga_data = pp;
4913 }
4914 }
4915
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004916 // We advance by two because we add one for items referenced through
4917 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004918 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004919
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004920 /*
4921 * 1. Go through all accessible variables and mark all lists and dicts
4922 * with copyID.
4923 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004924
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004925 // Don't free variables in the previous_funccal list unless they are only
4926 // referenced through previous_funccal. This must be first, because if
4927 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004928 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004929
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004930 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004931 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004932
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004933 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004934 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004935 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4936 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004937
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004938 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004939 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004940 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4941 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004942 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004943 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4944 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004945#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004946 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004947 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4948 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004949 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004950 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004951 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4952 NULL, NULL);
4953#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004954
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004955 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004956 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004957 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4958 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004959 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004960 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004961
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004962 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004963 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004964
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004965 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004966 abort = abort || set_ref_in_functions(copyID);
4967
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004968 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004969 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004970
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004971 // funcstacks keep variables for closures
4972 abort = abort || set_ref_in_funcstacks(copyID);
4973
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004974 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004975 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004976
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004977 // callbacks in buffers
4978 abort = abort || set_ref_in_buffers(copyID);
4979
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004980 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
4981 abort = abort || set_ref_in_insexpand_funcs(copyID);
4982
4983 // 'operatorfunc' callback
4984 abort = abort || set_ref_in_opfunc(copyID);
4985
4986 // 'tagfunc' callback
4987 abort = abort || set_ref_in_tagfunc(copyID);
4988
4989 // 'imactivatefunc' and 'imstatusfunc' callbacks
4990 abort = abort || set_ref_in_im_funcs(copyID);
4991
Bram Moolenaar1dced572012-04-05 16:54:08 +02004992#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004993 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004994#endif
4995
Bram Moolenaardb913952012-06-29 12:54:53 +02004996#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004997 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004998#endif
4999
5000#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005001 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005002#endif
5003
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005004#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005005 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005006 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005007#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005008#ifdef FEAT_NETBEANS_INTG
5009 abort = abort || set_ref_in_nb_channel(copyID);
5010#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005011
Bram Moolenaare3188e22016-05-31 21:13:04 +02005012#ifdef FEAT_TIMERS
5013 abort = abort || set_ref_in_timer(copyID);
5014#endif
5015
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005016#ifdef FEAT_QUICKFIX
5017 abort = abort || set_ref_in_quickfix(copyID);
5018#endif
5019
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005020#ifdef FEAT_TERMINAL
5021 abort = abort || set_ref_in_term(copyID);
5022#endif
5023
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005024#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005025 abort = abort || set_ref_in_popups(copyID);
5026#endif
5027
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005028 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005029 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005030 /*
5031 * 2. Free lists and dictionaries that are not referenced.
5032 */
5033 did_free = free_unref_items(copyID);
5034
5035 /*
5036 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005037 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005038 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005039 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005040 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005041 else if (p_verbose > 0)
5042 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005043 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005044 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005045
5046 return did_free;
5047}
5048
5049/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005050 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005051 */
5052 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005053free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005054{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005055 int did_free = FALSE;
5056
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005057 // Let all "free" functions know that we are here. This means no
5058 // dictionaries, lists, channels or jobs are to be freed, because we will
5059 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005060 in_free_unref_items = TRUE;
5061
5062 /*
5063 * PASS 1: free the contents of the items. We don't free the items
5064 * themselves yet, so that it is possible to decrement refcount counters
5065 */
5066
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005067 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005068 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005069
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005070 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005071 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005072
5073#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005074 // Go through the list of jobs and free items without the copyID. This
5075 // must happen before doing channels, because jobs refer to channels, but
5076 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005077 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5078
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005079 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005080 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5081#endif
5082
5083 /*
5084 * PASS 2: free the items themselves.
5085 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005086 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005087 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005088
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005089#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005090 // Go through the list of jobs and free items without the copyID. This
5091 // must happen before doing channels, because jobs refer to channels, but
5092 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005093 free_unused_jobs(copyID, COPYID_MASK);
5094
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005095 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005096 free_unused_channels(copyID, COPYID_MASK);
5097#endif
5098
5099 in_free_unref_items = FALSE;
5100
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005101 return did_free;
5102}
5103
5104/*
5105 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005106 * "list_stack" is used to add lists to be marked. Can be NULL.
5107 *
5108 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005109 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005110 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005111set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005112{
5113 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005114 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005115 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005116 hashtab_T *cur_ht;
5117 ht_stack_T *ht_stack = NULL;
5118 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005119
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005120 cur_ht = ht;
5121 for (;;)
5122 {
5123 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005124 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005125 // Mark each item in the hashtab. If the item contains a hashtab
5126 // it is added to ht_stack, if it contains a list it is added to
5127 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005128 todo = (int)cur_ht->ht_used;
5129 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5130 if (!HASHITEM_EMPTY(hi))
5131 {
5132 --todo;
5133 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5134 &ht_stack, list_stack);
5135 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005136 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005137
5138 if (ht_stack == NULL)
5139 break;
5140
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005141 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005142 cur_ht = ht_stack->ht;
5143 tempitem = ht_stack;
5144 ht_stack = ht_stack->prev;
5145 free(tempitem);
5146 }
5147
5148 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005149}
5150
Dominique Pelle748b3082022-01-08 12:41:16 +00005151#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5152 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005153/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005154 * Mark a dict and its items with "copyID".
5155 * Returns TRUE if setting references failed somehow.
5156 */
5157 int
5158set_ref_in_dict(dict_T *d, int copyID)
5159{
5160 if (d != NULL && d->dv_copyID != copyID)
5161 {
5162 d->dv_copyID = copyID;
5163 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5164 }
5165 return FALSE;
5166}
Dominique Pelle748b3082022-01-08 12:41:16 +00005167#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005168
5169/*
5170 * Mark a list and its items with "copyID".
5171 * Returns TRUE if setting references failed somehow.
5172 */
5173 int
5174set_ref_in_list(list_T *ll, int copyID)
5175{
5176 if (ll != NULL && ll->lv_copyID != copyID)
5177 {
5178 ll->lv_copyID = copyID;
5179 return set_ref_in_list_items(ll, copyID, NULL);
5180 }
5181 return FALSE;
5182}
5183
5184/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005185 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005186 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5187 *
5188 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005189 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005190 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005191set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005192{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005193 listitem_T *li;
5194 int abort = FALSE;
5195 list_T *cur_l;
5196 list_stack_T *list_stack = NULL;
5197 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005198
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005199 cur_l = l;
5200 for (;;)
5201 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005202 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005203 // Mark each item in the list. If the item contains a hashtab
5204 // it is added to ht_stack, if it contains a list it is added to
5205 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005206 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5207 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5208 ht_stack, &list_stack);
5209 if (list_stack == NULL)
5210 break;
5211
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005212 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005213 cur_l = list_stack->list;
5214 tempitem = list_stack;
5215 list_stack = list_stack->prev;
5216 free(tempitem);
5217 }
5218
5219 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005220}
5221
5222/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005223 * Mark the partial in callback 'cb' with "copyID".
5224 */
5225 int
5226set_ref_in_callback(callback_T *cb, int copyID)
5227{
5228 typval_T tv;
5229
5230 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5231 return FALSE;
5232
5233 tv.v_type = VAR_PARTIAL;
5234 tv.vval.v_partial = cb->cb_partial;
5235 return set_ref_in_item(&tv, copyID, NULL, NULL);
5236}
5237
5238/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005239 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005240 * "list_stack" is used to add lists to be marked. Can be NULL.
5241 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5242 *
5243 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005244 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005245 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005246set_ref_in_item(
5247 typval_T *tv,
5248 int copyID,
5249 ht_stack_T **ht_stack,
5250 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005251{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005252 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005253
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005254 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005255 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005256 dict_T *dd = tv->vval.v_dict;
5257
Bram Moolenaara03f2332016-02-06 18:09:59 +01005258 if (dd != NULL && dd->dv_copyID != copyID)
5259 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005260 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005261 dd->dv_copyID = copyID;
5262 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005263 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005264 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5265 }
5266 else
5267 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005268 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5269
Bram Moolenaara03f2332016-02-06 18:09:59 +01005270 if (newitem == NULL)
5271 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005272 else
5273 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005274 newitem->ht = &dd->dv_hashtab;
5275 newitem->prev = *ht_stack;
5276 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005277 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005278 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005279 }
5280 }
5281 else if (tv->v_type == VAR_LIST)
5282 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005283 list_T *ll = tv->vval.v_list;
5284
Bram Moolenaara03f2332016-02-06 18:09:59 +01005285 if (ll != NULL && ll->lv_copyID != copyID)
5286 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005287 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005288 ll->lv_copyID = copyID;
5289 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005290 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005291 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005292 }
5293 else
5294 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005295 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5296
Bram Moolenaara03f2332016-02-06 18:09:59 +01005297 if (newitem == NULL)
5298 abort = TRUE;
5299 else
5300 {
5301 newitem->list = ll;
5302 newitem->prev = *list_stack;
5303 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005304 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005305 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005306 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005307 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005308 else if (tv->v_type == VAR_FUNC)
5309 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005310 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005311 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005312 else if (tv->v_type == VAR_PARTIAL)
5313 {
5314 partial_T *pt = tv->vval.v_partial;
5315 int i;
5316
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005317 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005318 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005319 // Didn't see this partial yet.
5320 pt->pt_copyID = copyID;
5321
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005322 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005323
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005324 if (pt->pt_dict != NULL)
5325 {
5326 typval_T dtv;
5327
5328 dtv.v_type = VAR_DICT;
5329 dtv.vval.v_dict = pt->pt_dict;
5330 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5331 }
5332
5333 for (i = 0; i < pt->pt_argc; ++i)
5334 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5335 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005336 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005337 }
5338 }
5339#ifdef FEAT_JOB_CHANNEL
5340 else if (tv->v_type == VAR_JOB)
5341 {
5342 job_T *job = tv->vval.v_job;
5343 typval_T dtv;
5344
5345 if (job != NULL && job->jv_copyID != copyID)
5346 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005347 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005348 if (job->jv_channel != NULL)
5349 {
5350 dtv.v_type = VAR_CHANNEL;
5351 dtv.vval.v_channel = job->jv_channel;
5352 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5353 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005354 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005355 {
5356 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005357 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005358 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5359 }
5360 }
5361 }
5362 else if (tv->v_type == VAR_CHANNEL)
5363 {
5364 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005365 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005366 typval_T dtv;
5367 jsonq_T *jq;
5368 cbq_T *cq;
5369
5370 if (ch != NULL && ch->ch_copyID != copyID)
5371 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005372 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005373 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005374 {
5375 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5376 jq = jq->jq_next)
5377 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5378 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5379 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005380 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005381 {
5382 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005383 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005384 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5385 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005386 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005387 {
5388 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005389 dtv.vval.v_partial =
5390 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005391 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5392 }
5393 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005394 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005395 {
5396 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005397 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005398 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5399 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005400 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005401 {
5402 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005403 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005404 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5405 }
5406 }
5407 }
5408#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005409 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005410}
5411
Bram Moolenaar8c711452005-01-14 21:53:12 +00005412/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005413 * Return a string with the string representation of a variable.
5414 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005415 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005416 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005417 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005418 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005419 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005420 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5421 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005422 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005423 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005424 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005425echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005426 typval_T *tv,
5427 char_u **tofree,
5428 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005429 int copyID,
5430 int echo_style,
5431 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005432 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005434 static int recurse = 0;
5435 char_u *r = NULL;
5436
Bram Moolenaar33570922005-01-25 22:26:29 +00005437 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005438 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005439 if (!did_echo_string_emsg)
5440 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005441 // Only give this message once for a recursive call to avoid
5442 // flooding the user with errors. And stop iterating over lists
5443 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005444 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005445 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005446 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005447 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005448 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005449 }
5450 ++recurse;
5451
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005452 switch (tv->v_type)
5453 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005454 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005455 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005456 {
5457 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005458 r = tv->vval.v_string;
5459 if (r == NULL)
5460 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005461 }
5462 else
5463 {
5464 *tofree = string_quote(tv->vval.v_string, FALSE);
5465 r = *tofree;
5466 }
5467 break;
5468
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005469 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005470 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005471 char_u buf[MAX_FUNC_NAME_LEN];
5472
5473 if (echo_style)
5474 {
LemonBoya5d35902022-04-29 21:15:02 +01005475 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5476 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005477 buf, MAX_FUNC_NAME_LEN);
5478 if (r == buf)
5479 {
5480 r = vim_strsave(buf);
5481 *tofree = r;
5482 }
5483 else
5484 *tofree = NULL;
5485 }
5486 else
5487 {
5488 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5489 : make_ufunc_name_readable(
5490 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5491 TRUE);
5492 r = *tofree;
5493 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005494 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005495 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005496
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005497 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005498 {
5499 partial_T *pt = tv->vval.v_partial;
5500 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005501 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005502 garray_T ga;
5503 int i;
5504 char_u *tf;
5505
5506 ga_init2(&ga, 1, 100);
5507 ga_concat(&ga, (char_u *)"function(");
5508 if (fname != NULL)
5509 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005510 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005511 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005512 && vim_isupper(fname[1]))
5513 {
5514 ga_concat(&ga, (char_u *)"'g:");
5515 ga_concat(&ga, fname + 1);
5516 }
5517 else
5518 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005519 vim_free(fname);
5520 }
5521 if (pt != NULL && pt->pt_argc > 0)
5522 {
5523 ga_concat(&ga, (char_u *)", [");
5524 for (i = 0; i < pt->pt_argc; ++i)
5525 {
5526 if (i > 0)
5527 ga_concat(&ga, (char_u *)", ");
5528 ga_concat(&ga,
5529 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5530 vim_free(tf);
5531 }
5532 ga_concat(&ga, (char_u *)"]");
5533 }
5534 if (pt != NULL && pt->pt_dict != NULL)
5535 {
5536 typval_T dtv;
5537
5538 ga_concat(&ga, (char_u *)", ");
5539 dtv.v_type = VAR_DICT;
5540 dtv.vval.v_dict = pt->pt_dict;
5541 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5542 vim_free(tf);
5543 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005544 // terminate with ')' and a NUL
5545 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005546
5547 *tofree = ga.ga_data;
5548 r = *tofree;
5549 break;
5550 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005551
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005552 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005553 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005554 break;
5555
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005556 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005557 if (tv->vval.v_list == NULL)
5558 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005559 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005560 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +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_list->lv_copyID == copyID
5564 && tv->vval.v_list->lv_len > 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_list->lv_copyID;
5572
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005573 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005574 *tofree = list2string(tv, copyID, restore_copyID);
5575 if (restore_copyID)
5576 tv->vval.v_list->lv_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 Moolenaar8c711452005-01-14 21:53:12 +00005581 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005582 if (tv->vval.v_dict == NULL)
5583 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005584 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005585 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005586 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005587 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005588 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5589 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005590 {
5591 *tofree = NULL;
5592 r = (char_u *)"{...}";
5593 }
5594 else
5595 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005596 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005597
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005598 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005599 *tofree = dict2string(tv, copyID, restore_copyID);
5600 if (restore_copyID)
5601 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005602 r = *tofree;
5603 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005604 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005605
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005606 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005607 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005608 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005609 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005610 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005611 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005612 break;
5613
Bram Moolenaar835dc632016-02-07 14:27:38 +01005614 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005615 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005616#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005617 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005618 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5619 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005620 if (composite_val)
5621 {
5622 *tofree = string_quote(r, FALSE);
5623 r = *tofree;
5624 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005625#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005626 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005627
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005628 case VAR_INSTR:
5629 *tofree = NULL;
5630 r = (char_u *)"instructions";
5631 break;
5632
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005633 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005634#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005635 *tofree = NULL;
5636 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5637 r = numbuf;
5638 break;
5639#endif
5640
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005641 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005642 case VAR_SPECIAL:
5643 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005644 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005645 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005646 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005647
Bram Moolenaar8502c702014-06-17 12:51:16 +02005648 if (--recurse == 0)
5649 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005650 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005651}
5652
5653/*
5654 * Return a string with the string representation of a variable.
5655 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5656 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005657 * Does not put quotes around strings, as ":echo" displays values.
5658 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5659 * May return NULL.
5660 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005661 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005662echo_string(
5663 typval_T *tv,
5664 char_u **tofree,
5665 char_u *numbuf,
5666 int copyID)
5667{
5668 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5669}
5670
5671/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005672 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5673 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005674 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005675 */
5676 int
5677buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5678{
5679 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005680 char_u *t;
5681 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005682
5683 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5684 return -1;
5685
5686 if (lnum > buf->b_ml.ml_line_count)
5687 lnum = buf->b_ml.ml_line_count;
5688
5689 str = ml_get_buf(buf, lnum, FALSE);
5690 if (str == NULL)
5691 return -1;
5692
5693 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005694 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005695
Bram Moolenaar91458462021-01-13 20:08:38 +01005696 // count the number of characters
5697 t = str;
5698 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5699 t += mb_ptr2len(t);
5700
5701 // In insert mode, when the cursor is at the end of a non-empty line,
5702 // byteidx points to the NUL character immediately past the end of the
5703 // string. In this case, add one to the character count.
5704 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5705 count++;
5706
5707 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005708}
5709
5710/*
5711 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005712 * byte index. Works only for loaded buffers. Returns -1 on failure.
5713 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005714 */
5715 int
5716buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5717{
5718 char_u *str;
5719 char_u *t;
5720
5721 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5722 return -1;
5723
5724 if (lnum > buf->b_ml.ml_line_count)
5725 lnum = buf->b_ml.ml_line_count;
5726
5727 str = ml_get_buf(buf, lnum, FALSE);
5728 if (str == NULL)
5729 return -1;
5730
5731 // Convert the character offset to a byte offset
5732 t = str;
5733 while (*t != NUL && --charidx > 0)
5734 t += mb_ptr2len(t);
5735
Bram Moolenaar91458462021-01-13 20:08:38 +01005736 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005737}
5738
5739/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005741 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005743 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005744var2fpos(
5745 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005746 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005747 int *fnum, // set to fnum for '0, 'A, etc.
5748 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005750 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005752 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005754 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005755 if (varp->v_type == VAR_LIST)
5756 {
5757 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005758 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005759 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005760 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005761
5762 l = varp->vval.v_list;
5763 if (l == NULL)
5764 return NULL;
5765
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005766 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005767 pos.lnum = list_find_nr(l, 0L, &error);
5768 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005769 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005770 if (charcol)
5771 len = (long)mb_charlen(ml_get(pos.lnum));
5772 else
5773 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005774
Bram Moolenaarec65d772020-08-20 22:29:12 +02005775 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005776 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005777 li = list_find(l, 1L);
5778 if (li != NULL && li->li_tv.v_type == VAR_STRING
5779 && li->li_tv.vval.v_string != NULL
5780 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005781 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005782 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005783 }
5784 else
5785 {
5786 pos.col = list_find_nr(l, 1L, &error);
5787 if (error)
5788 return NULL;
5789 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005790
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005791 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005792 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005793 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005794 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005795
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005796 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005797 pos.coladd = list_find_nr(l, 2L, &error);
5798 if (error)
5799 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005800
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005801 return &pos;
5802 }
5803
Bram Moolenaarc5809432021-03-27 21:23:30 +01005804 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5805 return NULL;
5806
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005807 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005808 if (name == NULL)
5809 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005810
5811 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005812 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005813 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005814 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005815 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005816 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005817 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005818 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005819 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005820 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005821 pos = VIsual;
5822 else
5823 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005824 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005825 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005826 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005828 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005829 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5831 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005832 pos = *pp;
5833 }
5834 if (pos.lnum != 0)
5835 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005836 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005837 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5838 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005840
Bram Moolenaara5525202006-03-02 22:52:09 +00005841 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005842
Bram Moolenaar477933c2007-07-17 14:32:23 +00005843 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005844 {
5845 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005846 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005847 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005848 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005849 // In silent Ex mode topline is zero, but that's not a valid line
5850 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005851 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005852 return &pos;
5853 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005854 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005855 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005856 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005857 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005858 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005859 return &pos;
5860 }
5861 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005862 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005864 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 {
5866 pos.lnum = curbuf->b_ml.ml_line_count;
5867 pos.col = 0;
5868 }
5869 else
5870 {
5871 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005872 if (charcol)
5873 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5874 else
5875 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 }
5877 return &pos;
5878 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005879 if (in_vim9script())
5880 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 return NULL;
5882}
5883
5884/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005885 * Convert list in "arg" into a position and optional file number.
5886 * When "fnump" is NULL there is no file number, only 3 items.
5887 * Note that the column is passed on as-is, the caller may want to decrement
5888 * it to use 1 for the first column.
5889 * Return FAIL when conversion is not possible, doesn't check the position for
5890 * validity.
5891 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005892 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005893list2fpos(
5894 typval_T *arg,
5895 pos_T *posp,
5896 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005897 colnr_T *curswantp,
5898 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005899{
5900 list_T *l = arg->vval.v_list;
5901 long i = 0;
5902 long n;
5903
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005904 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5905 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005906 if (arg->v_type != VAR_LIST
5907 || l == NULL
5908 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005909 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005910 return FAIL;
5911
5912 if (fnump != NULL)
5913 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005914 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005915 if (n < 0)
5916 return FAIL;
5917 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005918 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005919 *fnump = n;
5920 }
5921
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005922 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005923 if (n < 0)
5924 return FAIL;
5925 posp->lnum = n;
5926
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005927 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005928 if (n < 0)
5929 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005930 // If character position is specified, then convert to byte position
5931 if (charcol)
5932 {
5933 buf_T *buf;
5934
5935 // Get the text for the specified line in a loaded buffer
5936 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5937 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5938 return FAIL;
5939
Bram Moolenaar91458462021-01-13 20:08:38 +01005940 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005941 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005942 posp->col = n;
5943
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005944 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005945 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005946 posp->coladd = 0;
5947 else
5948 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005949
Bram Moolenaar493c1782014-05-28 14:34:46 +02005950 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005951 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005952
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005953 return OK;
5954}
5955
5956/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 * Get the length of an environment variable name.
5958 * Advance "arg" to the first character after the name.
5959 * Return 0 for error.
5960 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005961 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005962get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963{
5964 char_u *p;
5965 int len;
5966
5967 for (p = *arg; vim_isIDc(*p); ++p)
5968 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005969 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 return 0;
5971
5972 len = (int)(p - *arg);
5973 *arg = p;
5974 return len;
5975}
5976
5977/*
5978 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005979 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 * Return 0 if something is wrong.
5981 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005982 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005983get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984{
5985 char_u *p;
5986 int len;
5987
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005988 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005990 {
5991 if (*p == ':')
5992 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005993 // "s:" is start of "s:var", but "n:" is not and can be used in
5994 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005995 len = (int)(p - *arg);
5996 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5997 || len > 1)
5998 break;
5999 }
6000 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006001 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 return 0;
6003
6004 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006005 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006
6007 return len;
6008}
6009
6010/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006011 * Get the length of the name of a variable or function.
6012 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006014 * Return -1 if curly braces expansion failed.
6015 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 * If the name contains 'magic' {}'s, expand them and return the
6017 * expanded name in an allocated string via 'alias' - caller must free.
6018 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006019 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006020get_name_len(
6021 char_u **arg,
6022 char_u **alias,
6023 int evaluate,
6024 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025{
6026 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 char_u *p;
6028 char_u *expr_start;
6029 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006031 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032
6033 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6034 && (*arg)[2] == (int)KE_SNR)
6035 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006036 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 *arg += 3;
6038 return get_id_len(arg) + 3;
6039 }
6040 len = eval_fname_script(*arg);
6041 if (len > 0)
6042 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006043 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 *arg += len;
6045 }
6046
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006048 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006050 p = find_name_end(*arg, &expr_start, &expr_end,
6051 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 if (expr_start != NULL)
6053 {
6054 char_u *temp_string;
6055
6056 if (!evaluate)
6057 {
6058 len += (int)(p - *arg);
6059 *arg = skipwhite(p);
6060 return len;
6061 }
6062
6063 /*
6064 * Include any <SID> etc in the expanded string:
6065 * Thus the -len here.
6066 */
6067 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6068 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006069 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 *alias = temp_string;
6071 *arg = skipwhite(p);
6072 return (int)STRLEN(temp_string);
6073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074
6075 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006076 // Only give an error when there is something, otherwise it will be
6077 // reported at a higher level.
6078 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006079 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080
6081 return len;
6082}
6083
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006084/*
6085 * Find the end of a variable or function name, taking care of magic braces.
6086 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6087 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006088 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006089 * Return a pointer to just after the name. Equal to "arg" if there is no
6090 * valid name.
6091 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006092 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006093find_name_end(
6094 char_u *arg,
6095 char_u **expr_start,
6096 char_u **expr_end,
6097 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006099 int mb_nest = 0;
6100 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006102 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02006103 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006105 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006107 *expr_start = NULL;
6108 *expr_end = NULL;
6109 }
6110
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006111 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006112 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
6113 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006114 return arg;
6115
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006116 for (p = arg; *p != NUL
6117 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006118 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006119 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006120 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006121 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006122 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006123 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006124 if (*p == '\'')
6125 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006126 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006127 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006128 ;
6129 if (*p == NUL)
6130 break;
6131 }
6132 else if (*p == '"')
6133 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006134 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006135 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006136 if (*p == '\\' && p[1] != NUL)
6137 ++p;
6138 if (*p == NUL)
6139 break;
6140 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006141 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6142 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006143 // "s:" is start of "s:var", but "n:" is not and can be used in
6144 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006145 len = (int)(p - arg);
6146 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006147 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006148 break;
6149 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006150
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006151 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006153 if (*p == '[')
6154 ++br_nest;
6155 else if (*p == ']')
6156 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006158
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006159 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006161 if (*p == '{')
6162 {
6163 mb_nest++;
6164 if (expr_start != NULL && *expr_start == NULL)
6165 *expr_start = p;
6166 }
6167 else if (*p == '}')
6168 {
6169 mb_nest--;
6170 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6171 *expr_end = p;
6172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 }
6175
6176 return p;
6177}
6178
6179/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006180 * Expands out the 'magic' {}'s in a variable/function name.
6181 * Note that this can call itself recursively, to deal with
6182 * constructs like foo{bar}{baz}{bam}
6183 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6184 * "in_start" ^
6185 * "expr_start" ^
6186 * "expr_end" ^
6187 * "in_end" ^
6188 *
6189 * Returns a new allocated string, which the caller must free.
6190 * Returns NULL for failure.
6191 */
6192 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006193make_expanded_name(
6194 char_u *in_start,
6195 char_u *expr_start,
6196 char_u *expr_end,
6197 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006198{
6199 char_u c1;
6200 char_u *retval = NULL;
6201 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006202
6203 if (expr_end == NULL || in_end == NULL)
6204 return NULL;
6205 *expr_start = NUL;
6206 *expr_end = NUL;
6207 c1 = *in_end;
6208 *in_end = NUL;
6209
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006210 temp_result = eval_to_string(expr_start + 1, FALSE);
6211 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006212 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006213 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6214 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006215 if (retval != NULL)
6216 {
6217 STRCPY(retval, in_start);
6218 STRCAT(retval, temp_result);
6219 STRCAT(retval, expr_end + 1);
6220 }
6221 }
6222 vim_free(temp_result);
6223
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006224 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006225 *expr_start = '{';
6226 *expr_end = '}';
6227
6228 if (retval != NULL)
6229 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006230 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006231 if (expr_start != NULL)
6232 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006233 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006234 temp_result = make_expanded_name(retval, expr_start,
6235 expr_end, temp_result);
6236 vim_free(retval);
6237 retval = temp_result;
6238 }
6239 }
6240
6241 return retval;
6242}
6243
6244/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006246 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006248 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006249eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006251 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006252}
6253
6254/*
6255 * Return TRUE if character "c" can be used as the first character in a
6256 * variable or function name (excluding '{' and '}').
6257 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006258 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006259eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006260{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006261 return ASCII_ISALPHA(c) || c == '_';
6262}
6263
6264/*
6265 * Return TRUE if character "c" can be used as the first character of a
6266 * dictionary key.
6267 */
6268 int
6269eval_isdictc(int c)
6270{
6271 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272}
6273
6274/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006275 * Handle:
6276 * - expr[expr], expr[expr:expr] subscript
6277 * - ".name" lookup
6278 * - function call with Funcref variable: func(expr)
6279 * - method call: var->method()
6280 *
6281 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006282 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006283 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006284 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006285handle_subscript(
6286 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006287 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006288 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006289 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006290 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006291{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006292 int evaluate = evalarg != NULL
6293 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006294 int ret = OK;
6295 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006296 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006297 int getnext;
6298 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006299
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006300 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006301 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006302 // When at the end of the line and ".name" or "->{" or "->X" follows in
6303 // the next line then consume the line break.
6304 p = eval_next_non_blank(*arg, evalarg, &getnext);
6305 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006306 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006307 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6308 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6309 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006310 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006311 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006312 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006313 check_white = FALSE;
6314 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006315
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006316 if (rettv->v_type == VAR_ANY)
6317 {
6318 char_u *exp_name;
6319 int cc;
6320 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006321 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006322 type_T *type;
6323
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006324 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006325 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006326 if (**arg != '.')
6327 {
6328 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006329 semsg(_(e_expected_dot_after_name_str),
6330 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006331 ret = FAIL;
6332 break;
6333 }
6334 ++*arg;
6335 if (IS_WHITE_OR_NUL(**arg))
6336 {
6337 if (verbose)
6338 emsg(_(e_no_white_space_allowed_after_dot));
6339 ret = FAIL;
6340 break;
6341 }
6342
6343 // isolate the name
6344 exp_name = *arg;
6345 while (eval_isnamec(**arg))
6346 ++*arg;
6347 cc = **arg;
6348 **arg = NUL;
6349
6350 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00006351 evalarg->eval_cctx, evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006352 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006353
6354 if (idx < 0 && ufunc == NULL)
6355 {
6356 ret = FAIL;
6357 break;
6358 }
6359 if (idx >= 0)
6360 {
6361 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6362 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6363
6364 copy_tv(sv->sv_tv, rettv);
6365 }
6366 else
6367 {
6368 rettv->v_type = VAR_FUNC;
6369 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6370 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006371 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006372 }
6373
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006374 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6375 || rettv->v_type == VAR_PARTIAL))
6376 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006377 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006378 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6379 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006380
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006381 // Stop the expression evaluation when immediately aborting on
6382 // error, or when an interrupt occurred or an exception was thrown
6383 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006384 if (aborting())
6385 {
6386 if (ret == OK)
6387 clear_tv(rettv);
6388 ret = FAIL;
6389 }
6390 dict_unref(selfdict);
6391 selfdict = NULL;
6392 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006393 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006394 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006395 if (in_vim9script())
6396 *arg = skipwhite(p + 2);
6397 else
6398 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006399 if (ret == OK)
6400 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006401 if (VIM_ISWHITE(**arg))
6402 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006403 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006404 ret = FAIL;
6405 }
6406 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006407 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006408 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006409 else
6410 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006411 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006412 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006413 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006414 // "." is ".name" lookup when we found a dict or when evaluating and
6415 // scriptversion is at least 2, where string concatenation is "..".
6416 else if (**arg == '['
6417 || (**arg == '.' && (rettv->v_type == VAR_DICT
6418 || (!evaluate
6419 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006420 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006421 {
6422 dict_unref(selfdict);
6423 if (rettv->v_type == VAR_DICT)
6424 {
6425 selfdict = rettv->vval.v_dict;
6426 if (selfdict != NULL)
6427 ++selfdict->dv_refcount;
6428 }
6429 else
6430 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006431 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006432 {
6433 clear_tv(rettv);
6434 ret = FAIL;
6435 }
6436 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006437 else
6438 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006439 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006440
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006441 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6442 // Don't do this when "Func" is already a partial that was bound
6443 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006444 if (selfdict != NULL
6445 && (rettv->v_type == VAR_FUNC
6446 || (rettv->v_type == VAR_PARTIAL
6447 && (rettv->vval.v_partial->pt_auto
6448 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006449 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006450
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006451 dict_unref(selfdict);
6452 return ret;
6453}
6454
6455/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006456 * Make a copy of an item.
6457 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006458 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006459 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6460 * reference to an already copied list/dict can be used.
6461 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006462 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006463 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006464item_copy(
6465 typval_T *from,
6466 typval_T *to,
6467 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006468 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006469 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006470{
6471 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006472 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006473
Bram Moolenaar33570922005-01-25 22:26:29 +00006474 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006475 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006476 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006477 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006478 }
6479 ++recurse;
6480
6481 switch (from->v_type)
6482 {
6483 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006484 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006485 case VAR_STRING:
6486 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006487 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006488 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006489 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006490 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006491 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006492 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006493 copy_tv(from, to);
6494 break;
6495 case VAR_LIST:
6496 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006497 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006498 if (from->vval.v_list == NULL)
6499 to->vval.v_list = NULL;
6500 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6501 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006502 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006503 to->vval.v_list = from->vval.v_list->lv_copylist;
6504 ++to->vval.v_list->lv_refcount;
6505 }
6506 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006507 to->vval.v_list = list_copy(from->vval.v_list,
6508 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006509 if (to->vval.v_list == NULL)
6510 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006511 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006512 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006513 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006514 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006515 case VAR_DICT:
6516 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006517 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006518 if (from->vval.v_dict == NULL)
6519 to->vval.v_dict = NULL;
6520 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6521 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006522 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006523 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6524 ++to->vval.v_dict->dv_refcount;
6525 }
6526 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006527 to->vval.v_dict = dict_copy(from->vval.v_dict,
6528 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006529 if (to->vval.v_dict == NULL)
6530 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006531 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006532 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006533 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006534 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006535 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006536 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006537 }
6538 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006539 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006540}
6541
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006542 void
6543echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6544{
6545 char_u *tofree;
6546 char_u numbuf[NUMBUFLEN];
6547 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6548
6549 if (*atstart)
6550 {
6551 *atstart = FALSE;
6552 // Call msg_start() after eval1(), evaluating the expression
6553 // may cause a message to appear.
6554 if (with_space)
6555 {
6556 // Mark the saved text as finishing the line, so that what
6557 // follows is displayed on a new line when scrolling back
6558 // at the more prompt.
6559 msg_sb_eol();
6560 msg_start();
6561 }
6562 }
6563 else if (with_space)
6564 msg_puts_attr(" ", echo_attr);
6565
6566 if (p != NULL)
6567 for ( ; *p != NUL && !got_int; ++p)
6568 {
6569 if (*p == '\n' || *p == '\r' || *p == TAB)
6570 {
6571 if (*p != TAB && *needclr)
6572 {
6573 // remove any text still there from the command
6574 msg_clr_eos();
6575 *needclr = FALSE;
6576 }
6577 msg_putchar_attr(*p, echo_attr);
6578 }
6579 else
6580 {
6581 if (has_mbyte)
6582 {
6583 int i = (*mb_ptr2len)(p);
6584
6585 (void)msg_outtrans_len_attr(p, i, echo_attr);
6586 p += i - 1;
6587 }
6588 else
6589 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6590 }
6591 }
6592 vim_free(tofree);
6593}
6594
Bram Moolenaare9a41262005-01-15 22:18:47 +00006595/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 * ":echo expr1 ..." print each argument separated with a space, add a
6597 * newline at the end.
6598 * ":echon expr1 ..." print each argument plain.
6599 */
6600 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006601ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602{
6603 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006604 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006605 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 int needclr = TRUE;
6607 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006608 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006609 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006610 evalarg_T evalarg;
6611
Bram Moolenaare707c882020-07-01 13:07:37 +02006612 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613
6614 if (eap->skip)
6615 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006616 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006618 // If eval1() causes an error message the text from the command may
6619 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006620 need_clr_eos = needclr;
6621
Bram Moolenaar68db9962021-05-09 23:19:22 +02006622 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006623 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 {
6625 /*
6626 * Report the invalid expression unless the expression evaluation
6627 * has been cancelled due to an aborting error, an interrupt, or an
6628 * exception.
6629 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006630 if (!aborting() && did_emsg == did_emsg_before
6631 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006632 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006633 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 break;
6635 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006636 need_clr_eos = FALSE;
6637
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006639 {
6640 if (rettv.v_type == VAR_VOID)
6641 {
6642 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6643 break;
6644 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006645 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006646 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006647
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006648 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 arg = skipwhite(arg);
6650 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006651 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006652 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653
6654 if (eap->skip)
6655 --emsg_skip;
6656 else
6657 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006658 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 if (needclr)
6660 msg_clr_eos();
6661 if (eap->cmdidx == CMD_echo)
6662 msg_end();
6663 }
6664}
6665
6666/*
6667 * ":echohl {name}".
6668 */
6669 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006670ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006672 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673}
6674
6675/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006676 * Returns the :echo attribute
6677 */
6678 int
6679get_echo_attr(void)
6680{
6681 return echo_attr;
6682}
6683
6684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 * ":execute expr1 ..." execute the result of an expression.
6686 * ":echomsg expr1 ..." Print a message
6687 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006688 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 * Each gets spaces around each argument and a newline at the end for
6690 * echo commands
6691 */
6692 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006693ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694{
6695 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006696 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 int ret = OK;
6698 char_u *p;
6699 garray_T ga;
6700 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006701 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702
6703 ga_init2(&ga, 1, 80);
6704
6705 if (eap->skip)
6706 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006707 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006709 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006710 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712
6713 if (!eap->skip)
6714 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006715 char_u buf[NUMBUFLEN];
6716
6717 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006718 {
6719 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6720 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006721 semsg(_(e_using_invalid_value_as_string_str),
6722 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006723 p = NULL;
6724 }
6725 else
6726 p = tv_get_string_buf(&rettv, buf);
6727 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006728 else
6729 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006730 if (p == NULL)
6731 {
6732 clear_tv(&rettv);
6733 ret = FAIL;
6734 break;
6735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 len = (int)STRLEN(p);
6737 if (ga_grow(&ga, len + 2) == FAIL)
6738 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006739 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 ret = FAIL;
6741 break;
6742 }
6743 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 ga.ga_len += len;
6747 }
6748
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006749 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 arg = skipwhite(arg);
6751 }
6752
6753 if (ret != FAIL && ga.ga_data != NULL)
6754 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006755 // use the first line of continuation lines for messages
6756 SOURCING_LNUM = start_lnum;
6757
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006758 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6759 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006760 // Mark the already saved text as finishing the line, so that what
6761 // follows is displayed on a new line when scrolling back at the
6762 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006763 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006764 }
6765
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006767 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006768 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006769 out_flush();
6770 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006771 else if (eap->cmdidx == CMD_echoconsole)
6772 {
6773 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6774 ui_write((char_u *)"\r\n", 2, TRUE);
6775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 else if (eap->cmdidx == CMD_echoerr)
6777 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006778 int save_did_emsg = did_emsg;
6779
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006780 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006781 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 if (!force_abort)
6783 did_emsg = save_did_emsg;
6784 }
6785 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006786 {
6787 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6788
6789 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6790 sticky_cmdmod_flags = cmdmod.cmod_flags
6791 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 do_cmdline((char_u *)ga.ga_data,
6793 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006794 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 }
6797
6798 ga_clear(&ga);
6799
6800 if (eap->skip)
6801 --emsg_skip;
6802
Bram Moolenaar63b91732021-08-05 20:40:03 +02006803 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804}
6805
6806/*
6807 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6808 * "arg" points to the "&" or '+' when called, to "option" when returning.
6809 * Returns NULL when no option name found. Otherwise pointer to the char
6810 * after the option name.
6811 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006812 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006813find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814{
6815 char_u *p = *arg;
6816
6817 ++p;
6818 if (*p == 'g' && p[1] == ':')
6819 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006820 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 p += 2;
6822 }
6823 else if (*p == 'l' && p[1] == ':')
6824 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006825 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 p += 2;
6827 }
6828 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006829 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830
6831 if (!ASCII_ISALPHA(*p))
6832 return NULL;
6833 *arg = p;
6834
6835 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006836 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 else
6838 while (ASCII_ISALPHA(*p))
6839 ++p;
6840 return p;
6841}
6842
6843/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006844 * Display script name where an item was last set.
6845 * Should only be invoked when 'verbose' is non-zero.
6846 */
6847 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006848last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006849{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006850 char_u *p;
6851
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006852 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006853 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006854 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006855 if (p != NULL)
6856 {
6857 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006858 msg_puts(_("\n\tLast set from "));
6859 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006860 if (script_ctx.sc_lnum > 0)
6861 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006862 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006863 msg_outnum((long)script_ctx.sc_lnum);
6864 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006865 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006866 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006867 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006868 }
6869}
6870
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006871#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872
6873/*
6874 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006875 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 * "flags" can be "g" to do a global substitute.
6877 * Returns an allocated string, NULL for error.
6878 */
6879 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006880do_string_sub(
6881 char_u *str,
6882 char_u *pat,
6883 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006884 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006885 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886{
6887 int sublen;
6888 regmatch_T regmatch;
6889 int i;
6890 int do_all;
6891 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006892 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 garray_T ga;
6894 char_u *ret;
6895 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006896 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006898 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006900 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901
6902 ga_init2(&ga, 1, 200);
6903
6904 do_all = (flags[0] == 'g');
6905
6906 regmatch.rm_ic = p_ic;
6907 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6908 if (regmatch.regprog != NULL)
6909 {
6910 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006911 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6913 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006914 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006915 if (regmatch.startp[0] == regmatch.endp[0])
6916 {
6917 if (zero_width == regmatch.startp[0])
6918 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006919 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006920 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006921 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6922 (size_t)i);
6923 ga.ga_len += i;
6924 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006925 continue;
6926 }
6927 zero_width = regmatch.startp[0];
6928 }
6929
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 /*
6931 * Get some space for a temporary buffer to do the substitution
6932 * into. It will contain:
6933 * - The text up to where the match is.
6934 * - The substituted text.
6935 * - The text after the match.
6936 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01006937 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006938 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6940 {
6941 ga_clear(&ga);
6942 break;
6943 }
6944
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006945 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 i = (int)(regmatch.startp[0] - tail);
6947 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006948 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01006949 (void)vim_regsub(&regmatch, sub, expr,
6950 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
6951 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006953 tail = regmatch.endp[0];
6954 if (*tail == NUL)
6955 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 if (!do_all)
6957 break;
6958 }
6959
6960 if (ga.ga_data != NULL)
6961 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6962
Bram Moolenaar473de612013-06-08 18:19:48 +02006963 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 }
6965
6966 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6967 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006968 if (p_cpo == empty_option)
6969 p_cpo = save_cpo;
6970 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006971 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006972 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006973 // If it's still empty it was changed and restored, need to restore in
6974 // the complicated way.
6975 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01006976 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006977 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979
6980 return ret;
6981}