blob: 42b883e9b00bd3e86af7d06d3c192781c84fe051 [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 Moolenaarb171fb12020-06-24 20:34:03 +02002390
Bram Moolenaar79481362022-06-27 20:15:10 +01002391 if (ret != FAIL)
2392 {
2393 expr_end = p;
2394 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002395
Bram Moolenaar79481362022-06-27 20:15:10 +01002396 // In Vim9 script a command block is not split at NL characters for
2397 // commands using an expression argument. Skip over a '#' comment to
2398 // check for a following NL. Require white space before the '#'.
2399 if (in_vim9script() && p > expr_end && retarg == NULL)
2400 while (*p == '#')
2401 {
2402 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002403
Bram Moolenaar79481362022-06-27 20:15:10 +01002404 if (nl == NULL)
2405 break;
2406 p = skipwhite(nl + 1);
2407 if (eap != NULL && *p != NUL)
2408 eap->nextcmd = p;
2409 check_for_end = FALSE;
2410 }
2411
2412 if (check_for_end)
2413 end_error = !ends_excmd2(arg, p);
2414 }
2415
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002416 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 {
2418 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002419 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 /*
2421 * Report the invalid expression unless the expression evaluation has
2422 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002423 * exception, or we already gave a more specific error.
2424 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002426 if (!aborting()
2427 && did_emsg == did_emsg_before
2428 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002429 && (flags & EVAL_CONSTANT) == 0
2430 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002431 {
2432 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002433 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002434 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002435 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002436 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002437
2438 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002439 // a next command to avoid more errors, unless "|" is following, which
2440 // could only be a command separator.
Bram Moolenaar79481362022-06-27 20:15:10 +01002441 if (eap != NULL && p != NULL
2442 && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
Bram Moolenaar8143a532020-12-13 20:26:29 +01002443 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002444 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002446
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002447 if (retarg != NULL)
2448 *retarg = p;
2449 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002450 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002451
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 return ret;
2453}
2454
2455/*
2456 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002457 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002458 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 *
2460 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002461 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002463 * Note: "rettv.v_lock" is not set.
2464 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 * Return OK or FAIL.
2466 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002467 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002468eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002470 char_u *p;
2471 int getnext;
2472
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002473 CLEAR_POINTER(rettv);
2474
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 /*
2476 * Get the first variable.
2477 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002478 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 return FAIL;
2480
Bram Moolenaar793648f2020-06-26 21:28:25 +02002481 p = eval_next_non_blank(*arg, evalarg, &getnext);
2482 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002484 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002485 int result;
2486 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002487 evalarg_T *evalarg_used = evalarg;
2488 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002489 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002490 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002491 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002492
2493 if (evalarg == NULL)
2494 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002495 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002496 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002497 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002498 orig_flags = evalarg_used->eval_flags;
2499 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002500
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002501 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002502 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002503 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002504 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002505 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002506 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002507 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002508 clear_tv(rettv);
2509 return FAIL;
2510 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002511 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002512 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002513
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002515 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002517 int error = FALSE;
2518
Bram Moolenaar13106602020-10-04 16:06:05 +02002519 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002520 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002521 else if (vim9script)
2522 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002523 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002525 if (error || !op_falsy || !result)
2526 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002527 if (error)
2528 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 }
2530
2531 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002532 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002534 if (op_falsy)
2535 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002536 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002537 {
mityu4ac198c2021-05-28 17:52:40 +02002538 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002539 clear_tv(rettv);
2540 return FAIL;
2541 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002542 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002543 evalarg_used->eval_flags = (op_falsy ? !result : result)
2544 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2545 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002546 {
2547 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002549 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002550 if (!op_falsy || !result)
2551 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002553 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002555 /*
2556 * Check for the ":".
2557 */
2558 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2559 if (*p != ':')
2560 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002561 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002562 if (evaluate && result)
2563 clear_tv(rettv);
2564 evalarg_used->eval_flags = orig_flags;
2565 return FAIL;
2566 }
2567 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002568 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002569 else
2570 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002571 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002572 {
2573 error_white_both(p, 1);
2574 clear_tv(rettv);
2575 evalarg_used->eval_flags = orig_flags;
2576 return FAIL;
2577 }
2578 *arg = p;
2579 }
2580
2581 /*
2582 * Get the third variable. Recursive!
2583 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002584 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002585 {
mityu4ac198c2021-05-28 17:52:40 +02002586 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002587 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002588 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002589 return FAIL;
2590 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002591 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2592 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002593 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002594 if (eval1(arg, &var2, evalarg_used) == FAIL)
2595 {
2596 if (evaluate && result)
2597 clear_tv(rettv);
2598 evalarg_used->eval_flags = orig_flags;
2599 return FAIL;
2600 }
2601 if (evaluate && !result)
2602 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002604
2605 if (evalarg == NULL)
2606 clear_evalarg(&local_evalarg, NULL);
2607 else
2608 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 }
2610
2611 return OK;
2612}
2613
2614/*
2615 * Handle first level expression:
2616 * expr2 || expr2 || expr2 logical OR
2617 *
2618 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002619 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 *
2621 * Return OK or FAIL.
2622 */
2623 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002624eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002626 char_u *p;
2627 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628
2629 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002630 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002632 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 return FAIL;
2634
2635 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002636 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002638 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002639 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002641 evalarg_T *evalarg_used = evalarg;
2642 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002643 int evaluate;
2644 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002645 long result = FALSE;
2646 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002647 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002648 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002649
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002650 if (evalarg == NULL)
2651 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002652 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002653 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002654 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002655 orig_flags = evalarg_used->eval_flags;
2656 evaluate = orig_flags & EVAL_EVALUATE;
2657 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002658 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002659 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002660 result = tv_get_bool_chk(rettv, &error);
2661 else if (tv_get_number_chk(rettv, &error) != 0)
2662 result = TRUE;
2663 clear_tv(rettv);
2664 if (error)
2665 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 }
2667
2668 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002669 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002671 while (p[0] == '|' && p[1] == '|')
2672 {
2673 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002674 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002675 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002676 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002677 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002678 {
2679 error_white_both(p, 2);
2680 clear_tv(rettv);
2681 return FAIL;
2682 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002683 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002684 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002685
2686 /*
2687 * Get the second variable.
2688 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002689 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002690 {
mityu4ac198c2021-05-28 17:52:40 +02002691 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002692 clear_tv(rettv);
2693 return FAIL;
2694 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002695 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2696 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002697 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002698 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002699 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002700
2701 /*
2702 * Compute the result.
2703 */
2704 if (evaluate && !result)
2705 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002706 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002707 result = tv_get_bool_chk(&var2, &error);
2708 else if (tv_get_number_chk(&var2, &error) != 0)
2709 result = TRUE;
2710 clear_tv(&var2);
2711 if (error)
2712 return FAIL;
2713 }
2714 if (evaluate)
2715 {
2716 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002717 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002718 rettv->v_type = VAR_BOOL;
2719 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002720 }
2721 else
2722 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002723 rettv->v_type = VAR_NUMBER;
2724 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002725 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002726 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002727
2728 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002730
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002731 if (evalarg == NULL)
2732 clear_evalarg(&local_evalarg, NULL);
2733 else
2734 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 }
2736
2737 return OK;
2738}
2739
2740/*
2741 * Handle second level expression:
2742 * expr3 && expr3 && expr3 logical AND
2743 *
2744 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002745 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 *
2747 * Return OK or FAIL.
2748 */
2749 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002750eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002752 char_u *p;
2753 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754
2755 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002756 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002758 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 return FAIL;
2760
2761 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002762 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002764 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002765 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002767 evalarg_T *evalarg_used = evalarg;
2768 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002769 int orig_flags;
2770 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002771 long result = TRUE;
2772 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002773 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002774 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002775
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002776 if (evalarg == NULL)
2777 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002778 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002779 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002780 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002781 orig_flags = evalarg_used->eval_flags;
2782 evaluate = orig_flags & EVAL_EVALUATE;
2783 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002784 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002785 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002786 result = tv_get_bool_chk(rettv, &error);
2787 else if (tv_get_number_chk(rettv, &error) == 0)
2788 result = FALSE;
2789 clear_tv(rettv);
2790 if (error)
2791 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 }
2793
2794 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002795 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002797 while (p[0] == '&' && p[1] == '&')
2798 {
2799 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002800 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002801 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002802 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002803 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002804 {
2805 error_white_both(p, 2);
2806 clear_tv(rettv);
2807 return FAIL;
2808 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002809 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002810 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002811
2812 /*
2813 * Get the second variable.
2814 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002815 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002816 {
mityu4ac198c2021-05-28 17:52:40 +02002817 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002818 clear_tv(rettv);
2819 return FAIL;
2820 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002821 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2822 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002823 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002824 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002825 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002826 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002827
2828 /*
2829 * Compute the result.
2830 */
2831 if (evaluate && result)
2832 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002833 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002834 result = tv_get_bool_chk(&var2, &error);
2835 else if (tv_get_number_chk(&var2, &error) == 0)
2836 result = FALSE;
2837 clear_tv(&var2);
2838 if (error)
2839 return FAIL;
2840 }
2841 if (evaluate)
2842 {
2843 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002844 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002845 rettv->v_type = VAR_BOOL;
2846 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002847 }
2848 else
2849 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002850 rettv->v_type = VAR_NUMBER;
2851 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002852 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002853 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002854
2855 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002857
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002858 if (evalarg == NULL)
2859 clear_evalarg(&local_evalarg, NULL);
2860 else
2861 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 }
2863
2864 return OK;
2865}
2866
2867/*
2868 * Handle third level expression:
2869 * var1 == var2
2870 * var1 =~ var2
2871 * var1 != var2
2872 * var1 !~ var2
2873 * var1 > var2
2874 * var1 >= var2
2875 * var1 < var2
2876 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002877 * var1 is var2
2878 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 *
2880 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002881 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 *
2883 * Return OK or FAIL.
2884 */
2885 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002886eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002889 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002890 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002892 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893
2894 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002895 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002897 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 return FAIL;
2899
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002900 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002901
Bram Moolenaar696ba232020-07-29 21:20:41 +02002902 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903
2904 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002905 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002907 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002909 typval_T var2;
2910 int ic;
2911 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002912 int evaluate = evalarg == NULL
2913 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002914 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002915
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002916 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002917 {
Bram Moolenaare442d592022-05-05 12:20:28 +01002918 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002919 p = *arg;
2920 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002921 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2922 {
mityu4ac198c2021-05-28 17:52:40 +02002923 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002924 clear_tv(rettv);
2925 return FAIL;
2926 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002927
Bram Moolenaar696ba232020-07-29 21:20:41 +02002928 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2929 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002930 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002931 clear_tv(rettv);
2932 return FAIL;
2933 }
2934
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002935 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 if (p[len] == '?')
2937 {
2938 ic = TRUE;
2939 ++len;
2940 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002941 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 else if (p[len] == '#')
2943 {
2944 ic = FALSE;
2945 ++len;
2946 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002947 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002949 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950
2951 /*
2952 * Get the second variable.
2953 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002954 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2955 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002956 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002957 clear_tv(rettv);
2958 return FAIL;
2959 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002960 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002961 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002963 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 return FAIL;
2965 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002966 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002967 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002968 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002969
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002970 // use the line of the comparison for messages
2971 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002972 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002973 {
2974 ret = FAIL;
2975 clear_tv(rettv);
2976 }
2977 else
2978 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002979 clear_tv(&var2);
2980 return ret;
2981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 }
2983
2984 return OK;
2985}
2986
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002987/*
2988 * Make a copy of blob "tv1" and append blob "tv2".
2989 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 void
2991eval_addblob(typval_T *tv1, typval_T *tv2)
2992{
2993 blob_T *b1 = tv1->vval.v_blob;
2994 blob_T *b2 = tv2->vval.v_blob;
2995 blob_T *b = blob_alloc();
2996 int i;
2997
2998 if (b != NULL)
2999 {
3000 for (i = 0; i < blob_len(b1); i++)
3001 ga_append(&b->bv_ga, blob_get(b1, i));
3002 for (i = 0; i < blob_len(b2); i++)
3003 ga_append(&b->bv_ga, blob_get(b2, i));
3004
3005 clear_tv(tv1);
3006 rettv_blob_set(tv1, b);
3007 }
3008}
3009
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003010/*
3011 * Make a copy of list "tv1" and append list "tv2".
3012 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013 int
3014eval_addlist(typval_T *tv1, typval_T *tv2)
3015{
3016 typval_T var3;
3017
3018 // concatenate Lists
3019 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3020 {
3021 clear_tv(tv1);
3022 clear_tv(tv2);
3023 return FAIL;
3024 }
3025 clear_tv(tv1);
3026 *tv1 = var3;
3027 return OK;
3028}
3029
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003031 * Handle the bitwise left/right shift operator expression:
3032 * var1 << var2
3033 * var1 >> var2
3034 *
3035 * "arg" must point to the first non-white of the expression.
3036 * "arg" is advanced to just after the recognized expression.
3037 *
3038 * Return OK or FAIL.
3039 */
3040 static int
3041eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3042{
3043 /*
3044 * Get the first expression.
3045 */
3046 if (eval6(arg, rettv, evalarg) == FAIL)
3047 return FAIL;
3048
3049 /*
3050 * Repeat computing, until no '<<' or '>>' is following.
3051 */
3052 for (;;)
3053 {
3054 char_u *p;
3055 int getnext;
3056 exprtype_T type;
3057 int evaluate;
3058 typval_T var2;
3059 int vim9script;
3060
3061 p = eval_next_non_blank(*arg, evalarg, &getnext);
3062 if (p[0] == '<' && p[1] == '<')
3063 type = EXPR_LSHIFT;
3064 else if (p[0] == '>' && p[1] == '>')
3065 type = EXPR_RSHIFT;
3066 else
3067 return OK;
3068
3069 // Handle a bitwise left or right shift operator
3070 if (rettv->v_type != VAR_NUMBER)
3071 {
3072 // left operand should be a number
3073 emsg(_(e_bitshift_ops_must_be_number));
3074 clear_tv(rettv);
3075 return FAIL;
3076 }
3077
3078 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3079 vim9script = in_vim9script();
3080 if (getnext)
3081 {
3082 *arg = eval_next_line(*arg, evalarg);
3083 p = *arg;
3084 }
3085 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3086 {
3087 error_white_both(*arg, 2);
3088 clear_tv(rettv);
3089 return FAIL;
3090 }
3091
3092 /*
3093 * Get the second variable.
3094 */
3095 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3096 {
3097 error_white_both(p, 2);
3098 clear_tv(rettv);
3099 return FAIL;
3100 }
3101 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3102 if (eval6(arg, &var2, evalarg) == FAIL)
3103 {
3104 clear_tv(rettv);
3105 return FAIL;
3106 }
3107
3108 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3109 {
3110 // right operand should be a positive number
3111 if (var2.v_type != VAR_NUMBER)
3112 emsg(_(e_bitshift_ops_must_be_number));
3113 else
3114 emsg(_(e_bitshift_ops_must_be_postive));
3115 clear_tv(rettv);
3116 clear_tv(&var2);
3117 return FAIL;
3118 }
3119
3120 if (evaluate)
3121 {
3122 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3123 // shifting more bits than we have always results in zero
3124 rettv->vval.v_number = 0;
3125 else if (type == EXPR_LSHIFT)
3126 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003127 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003128 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003129 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003130 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003131 }
3132
3133 clear_tv(&var2);
3134 }
3135
3136 return OK;
3137}
3138
3139/*
3140 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003141 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003143 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003144 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 *
3146 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003147 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 *
3149 * Return OK or FAIL.
3150 */
3151 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003152eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003155 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003157 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 return FAIL;
3159
3160 /*
3161 * Repeat computing, until no '+', '-' or '.' is following.
3162 */
3163 for (;;)
3164 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003165 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003166 int getnext;
3167 char_u *p;
3168 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003169 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003170 int concat;
3171 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003172 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003173
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003174 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003175 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003176 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003177 p = eval_next_non_blank(*arg, evalarg, &getnext);
3178 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003179 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003180 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3181 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003183 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3184 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003185
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003186 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003187 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003188 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003189 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003190 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003191 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003192 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003193 {
mityu4ac198c2021-05-28 17:52:40 +02003194 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003195 clear_tv(rettv);
3196 return FAIL;
3197 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003198 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003199 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003200 if ((op != '+' || (rettv->v_type != VAR_LIST
3201 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003202#ifdef FEAT_FLOAT
3203 && (op == '.' || rettv->v_type != VAR_FLOAT)
3204#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003205 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003206 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003207 int error = FALSE;
3208
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003209 // For "list + ...", an illegal use of the first operand as
3210 // a number cannot be determined before evaluating the 2nd
3211 // operand: if this is also a list, all is ok.
3212 // For "something . ...", "something - ..." or "non-list + ...",
3213 // we know that the first operand needs to be a string or number
3214 // without evaluating the 2nd operand. So check before to avoid
3215 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003216 if (op != '.')
3217 tv_get_number_chk(rettv, &error);
3218 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003219 {
3220 clear_tv(rettv);
3221 return FAIL;
3222 }
3223 }
3224
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 /*
3226 * Get the second variable.
3227 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003228 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003229 {
mityu89dcb4d2021-05-28 13:50:17 +02003230 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003231 clear_tv(rettv);
3232 return FAIL;
3233 }
3234 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003235 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003237 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 return FAIL;
3239 }
3240
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003241 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 {
3243 /*
3244 * Compute the result.
3245 */
3246 if (op == '.')
3247 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003248 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3249 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003250 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003251
Bram Moolenaar2e086612020-08-25 22:37:48 +02003252 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003253 || var2.v_type == VAR_CHANNEL
3254 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003255 semsg(_(e_using_invalid_value_as_string_str),
3256 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003257#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003258 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003259 {
3260 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3261 var2.vval.v_float);
3262 s2 = buf2;
3263 }
3264#endif
3265 else
3266 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003267 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003268 {
3269 clear_tv(rettv);
3270 clear_tv(&var2);
3271 return FAIL;
3272 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003273 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003274 clear_tv(rettv);
3275 rettv->v_type = VAR_STRING;
3276 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003278 else if (op == '+' && rettv->v_type == VAR_BLOB
3279 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003280 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003281 else if (op == '+' && rettv->v_type == VAR_LIST
3282 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003283 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003285 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 else
3288 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003289 int error = FALSE;
3290 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003291#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003292 float_T f1 = 0, f2 = 0;
3293
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003294 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003295 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003296 f1 = rettv->vval.v_float;
3297 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003298 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003299 else
3300#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003301 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003302 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003303 if (error)
3304 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003305 // This can only happen for "list + non-list" or
3306 // "blob + non-blob". For "non-list + ..." or
3307 // "something - ...", we returned before evaluating the
3308 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003309 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003310 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003311 return FAIL;
3312 }
3313#ifdef FEAT_FLOAT
3314 if (var2.v_type == VAR_FLOAT)
3315 f1 = n1;
3316#endif
3317 }
3318#ifdef FEAT_FLOAT
3319 if (var2.v_type == VAR_FLOAT)
3320 {
3321 f2 = var2.vval.v_float;
3322 n2 = 0;
3323 }
3324 else
3325#endif
3326 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003327 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003328 if (error)
3329 {
3330 clear_tv(rettv);
3331 clear_tv(&var2);
3332 return FAIL;
3333 }
3334#ifdef FEAT_FLOAT
3335 if (rettv->v_type == VAR_FLOAT)
3336 f2 = n2;
3337#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003338 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003339 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003340
3341#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003342 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003343 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3344 {
3345 if (op == '+')
3346 f1 = f1 + f2;
3347 else
3348 f1 = f1 - f2;
3349 rettv->v_type = VAR_FLOAT;
3350 rettv->vval.v_float = f1;
3351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003353#endif
3354 {
3355 if (op == '+')
3356 n1 = n1 + n2;
3357 else
3358 n1 = n1 - n2;
3359 rettv->v_type = VAR_NUMBER;
3360 rettv->vval.v_number = n1;
3361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003363 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 }
3365 }
3366 return OK;
3367}
3368
3369/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003370 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 * * number multiplication
3372 * / number division
3373 * % number modulo
3374 *
3375 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003376 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 *
3378 * Return OK or FAIL.
3379 */
3380 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003381eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003382 char_u **arg,
3383 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003384 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003385 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003387#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003388 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390
3391 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003392 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003394 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 return FAIL;
3396
3397 /*
3398 * Repeat computing, until no '*', '/' or '%' is following.
3399 */
3400 for (;;)
3401 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003402 int evaluate;
3403 int getnext;
3404 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003405 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003406 int op;
3407 varnumber_T n1, n2;
3408#ifdef FEAT_FLOAT
3409 float_T f1, f2;
3410#endif
3411 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003412
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003413 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003414 p = eval_next_non_blank(*arg, evalarg, &getnext);
3415 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003416 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003418
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003419 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003420 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003421 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003422 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003423 {
3424 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3425 {
mityu4ac198c2021-05-28 17:52:40 +02003426 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003427 clear_tv(rettv);
3428 return FAIL;
3429 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003430 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003433#ifdef FEAT_FLOAT
3434 f1 = 0;
3435 f2 = 0;
3436#endif
3437 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003438 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003440#ifdef FEAT_FLOAT
3441 if (rettv->v_type == VAR_FLOAT)
3442 {
3443 f1 = rettv->vval.v_float;
3444 use_float = TRUE;
3445 n1 = 0;
3446 }
3447 else
3448#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003449 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003450 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003451 if (error)
3452 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 }
3454 else
3455 n1 = 0;
3456
3457 /*
3458 * Get the second variable.
3459 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003460 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3461 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003462 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003463 clear_tv(rettv);
3464 return FAIL;
3465 }
3466 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003467 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 return FAIL;
3469
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003470 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003472#ifdef FEAT_FLOAT
3473 if (var2.v_type == VAR_FLOAT)
3474 {
3475 if (!use_float)
3476 {
3477 f1 = n1;
3478 use_float = TRUE;
3479 }
3480 f2 = var2.vval.v_float;
3481 n2 = 0;
3482 }
3483 else
3484#endif
3485 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003486 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003487 clear_tv(&var2);
3488 if (error)
3489 return FAIL;
3490#ifdef FEAT_FLOAT
3491 if (use_float)
3492 f2 = n2;
3493#endif
3494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495
3496 /*
3497 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003498 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003500#ifdef FEAT_FLOAT
3501 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003503 if (op == '*')
3504 f1 = f1 * f2;
3505 else if (op == '/')
3506 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003507# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003508 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003509 if (f2 == 0.0)
3510 {
3511 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003512 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003513 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003514 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003515 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003516 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003517 }
3518 else
3519 f1 = f1 / f2;
3520# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003521 // We rely on the floating point library to handle divide
3522 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003523 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003524# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003527 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003528 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003529 return FAIL;
3530 }
3531 rettv->v_type = VAR_FLOAT;
3532 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 }
3534 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003535#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003537 int failed = FALSE;
3538
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003539 if (op == '*')
3540 n1 = n1 * n2;
3541 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003542 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003544 n1 = num_modulus(n1, n2, &failed);
3545 if (failed)
3546 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003547
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003548 rettv->v_type = VAR_NUMBER;
3549 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 }
3552 }
3553
3554 return OK;
3555}
3556
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003557/*
3558 * Handle a type cast before a base level expression.
3559 * "arg" must point to the first non-white of the expression.
3560 * "arg" is advanced to just after the recognized expression.
3561 * Return OK or FAIL.
3562 */
3563 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003564eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003565 char_u **arg,
3566 typval_T *rettv,
3567 evalarg_T *evalarg,
3568 int want_string) // after "." operator
3569{
3570 type_T *want_type = NULL;
3571 garray_T type_list; // list of pointers to allocated types
3572 int res;
3573 int evaluate = evalarg == NULL ? 0
3574 : (evalarg->eval_flags & EVAL_EVALUATE);
3575
3576 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003577 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3578 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003579 {
3580 ++*arg;
3581 ga_init2(&type_list, sizeof(type_T *), 10);
3582 want_type = parse_type(arg, &type_list, TRUE);
3583 if (want_type == NULL && (evaluate || **arg != '>'))
3584 {
3585 clear_type_list(&type_list);
3586 return FAIL;
3587 }
3588
3589 if (**arg != '>')
3590 {
3591 if (*skipwhite(*arg) == '>')
3592 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3593 else
3594 emsg(_(e_missing_gt));
3595 clear_type_list(&type_list);
3596 return FAIL;
3597 }
3598 ++*arg;
3599 *arg = skipwhite_and_linebreak(*arg, evalarg);
3600 }
3601
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003602 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003603
3604 if (want_type != NULL && evaluate)
3605 {
3606 if (res == OK)
3607 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003608 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3609 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003610
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003611 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003612 {
3613 if (want_type == &t_bool && actual != &t_bool
3614 && (actual->tt_flags & TTFLAG_BOOL_OK))
3615 {
3616 int n = tv2bool(rettv);
3617
3618 // can use "0" and "1" for boolean in some places
3619 clear_tv(rettv);
3620 rettv->v_type = VAR_BOOL;
3621 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3622 }
3623 else
3624 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003625 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003626
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003627 where.wt_variable = TRUE;
3628 res = check_type(want_type, actual, TRUE, where);
3629 }
3630 }
3631 }
3632 clear_type_list(&type_list);
3633 }
3634
3635 return res;
3636}
3637
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003638 int
3639eval_leader(char_u **arg, int vim9)
3640{
3641 char_u *s = *arg;
3642 char_u *p = *arg;
3643
3644 while (*p == '!' || *p == '-' || *p == '+')
3645 {
3646 char_u *n = skipwhite(p + 1);
3647
3648 // ++, --, -+ and +- are not accepted in Vim9 script
3649 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3650 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003651 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003652 return FAIL;
3653 }
3654 p = n;
3655 }
3656 *arg = p;
3657 return OK;
3658}
3659
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003661 * Check for a predefined value "true", "false" and "null.*".
3662 * Return OK when recognized.
3663 */
3664 int
3665handle_predefined(char_u *s, int len, typval_T *rettv)
3666{
3667 switch (len)
3668 {
3669 case 4: if (STRNCMP(s, "true", 4) == 0)
3670 {
3671 rettv->v_type = VAR_BOOL;
3672 rettv->vval.v_number = VVAL_TRUE;
3673 return OK;
3674 }
3675 if (STRNCMP(s, "null", 4) == 0)
3676 {
3677 rettv->v_type = VAR_SPECIAL;
3678 rettv->vval.v_number = VVAL_NULL;
3679 return OK;
3680 }
3681 break;
3682 case 5: if (STRNCMP(s, "false", 5) == 0)
3683 {
3684 rettv->v_type = VAR_BOOL;
3685 rettv->vval.v_number = VVAL_FALSE;
3686 return OK;
3687 }
3688 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003689 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3690 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003691#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003692 rettv->v_type = VAR_JOB;
3693 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003694#else
3695 rettv->v_type = VAR_SPECIAL;
3696 rettv->vval.v_number = VVAL_NULL;
3697#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003698 return OK;
3699 }
3700 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003701 case 9:
3702 if (STRNCMP(s, "null_", 5) != 0)
3703 break;
3704 if (STRNCMP(s + 5, "list", 4) == 0)
3705 {
3706 rettv->v_type = VAR_LIST;
3707 rettv->vval.v_list = NULL;
3708 return OK;
3709 }
3710 if (STRNCMP(s + 5, "dict", 4) == 0)
3711 {
3712 rettv->v_type = VAR_DICT;
3713 rettv->vval.v_dict = NULL;
3714 return OK;
3715 }
3716 if (STRNCMP(s + 5, "blob", 4) == 0)
3717 {
3718 rettv->v_type = VAR_BLOB;
3719 rettv->vval.v_blob = NULL;
3720 return OK;
3721 }
3722 break;
3723 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3724 {
3725 rettv->v_type = VAR_STRING;
3726 rettv->vval.v_string = NULL;
3727 return OK;
3728 }
3729 break;
3730 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003731 if (STRNCMP(s, "null_channel", 12) == 0)
3732 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003733#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003734 rettv->v_type = VAR_CHANNEL;
3735 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003736#else
3737 rettv->v_type = VAR_SPECIAL;
3738 rettv->vval.v_number = VVAL_NULL;
3739#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003740 return OK;
3741 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003742 if (STRNCMP(s, "null_partial", 12) == 0)
3743 {
3744 rettv->v_type = VAR_PARTIAL;
3745 rettv->vval.v_partial = NULL;
3746 return OK;
3747 }
3748 break;
3749 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3750 {
3751 rettv->v_type = VAR_FUNC;
3752 rettv->vval.v_string = NULL;
3753 return OK;
3754 }
3755 break;
3756 }
3757 return FAIL;
3758}
3759
3760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 * Handle sixth level expression:
3762 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003763 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003764 * "string" string constant
3765 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 * &option-name option value
3767 * @r register contents
3768 * identifier variable value
3769 * function() function call
3770 * $VAR environment variable
3771 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003772 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003774 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003775 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 *
3777 * Also handle:
3778 * ! in front logical NOT
3779 * - in front unary minus
3780 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003781 * trailing [] subscript in String or List
3782 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003783 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 *
3785 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003786 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 *
3788 * Return OK or FAIL.
3789 */
3790 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003791eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003792 char_u **arg,
3793 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003794 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003797 int evaluate = evalarg != NULL
3798 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 int len;
3800 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003801 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 char_u *start_leader, *end_leader;
3803 int ret = OK;
3804 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003805 static int recurse = 0;
3806 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807
3808 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003809 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003810 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003812 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813
3814 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003815 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 */
3817 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003818 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003819 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 end_leader = *arg;
3821
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003822 if (**arg == '.' && (!isdigit(*(*arg + 1))
3823#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003824 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003825#endif
3826 ))
3827 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003828 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003829 ++*arg;
3830 return FAIL;
3831 }
3832
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003833 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003834 // and crash. With MSVC the stack is smaller.
3835 if (recurse ==
3836#ifdef _MSC_VER
3837 300
3838#else
3839 1000
3840#endif
3841 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003842 {
3843 semsg(_(e_expression_too_recursive_str), *arg);
3844 return FAIL;
3845 }
3846 ++recurse;
3847
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 switch (**arg)
3849 {
3850 /*
3851 * Number constant.
3852 */
3853 case '0':
3854 case '1':
3855 case '2':
3856 case '3':
3857 case '4':
3858 case '5':
3859 case '6':
3860 case '7':
3861 case '8':
3862 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003863 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003864
3865 // Apply prefixed "-" and "+" now. Matters especially when
3866 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003867 if (ret == OK && evaluate && end_leader > start_leader
3868 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003869 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 break;
3871
3872 /*
3873 * String constant: "string".
3874 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003875 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 break;
3877
3878 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003879 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003881 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003882 break;
3883
3884 /*
3885 * List: [expr, expr]
3886 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003887 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 break;
3889
3890 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003891 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003892 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003893 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003894 {
3895 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3896 }
3897 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003898 {
3899 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003900 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003901 }
3902 else
3903 ret = NOTDONE;
3904 break;
3905
3906 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003907 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003908 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003909 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003910 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003911 ret = NOTDONE;
3912 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00003913 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003914 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003915 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003916 break;
3917
3918 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003919 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003921 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 break;
3923
3924 /*
3925 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01003926 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 */
LemonBoy2eaef102022-05-06 13:14:50 +01003928 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
3929 ret = eval_interp_string(arg, rettv, evaluate);
3930 else
3931 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 break;
3933
3934 /*
3935 * Register contents: @r.
3936 */
3937 case '@': ++*arg;
3938 if (evaluate)
3939 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003940 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003941 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003942 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003943 emsg_invreg(**arg);
3944 else
3945 {
3946 rettv->v_type = VAR_STRING;
3947 rettv->vval.v_string = get_reg_contents(**arg,
3948 GREG_EXPR_SRC);
3949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 }
3951 if (**arg != NUL)
3952 ++*arg;
3953 break;
3954
3955 /*
3956 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003957 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003959 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003960 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01003961 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003962 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003963 if (ret == OK && evaluate)
3964 {
3965 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3966
Bram Moolenaara9931532021-06-12 15:58:16 +02003967 // Compile it here to get the return type. The return
3968 // type is optional, when it's missing use t_unknown.
3969 // This is recognized in compile_return().
3970 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3971 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00003972 if (compile_def_function(ufunc, FALSE,
3973 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003974 {
3975 clear_tv(rettv);
3976 ret = FAIL;
3977 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003978 }
3979 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003980 if (ret == NOTDONE)
3981 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003982 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003983 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003984
Bram Moolenaar9215f012020-06-27 21:18:00 +02003985 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003986 if (**arg == ')')
3987 ++*arg;
3988 else if (ret == OK)
3989 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003990 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003991 clear_tv(rettv);
3992 ret = FAIL;
3993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 }
3995 break;
3996
Bram Moolenaar8c711452005-01-14 21:53:12 +00003997 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 break;
3999 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004000
4001 if (ret == NOTDONE)
4002 {
4003 /*
4004 * Must be a variable or function name.
4005 * Can also be a curly-braces kind of name: {expr}.
4006 */
4007 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004008 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004009 if (alias != NULL)
4010 s = alias;
4011
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004012 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004013 ret = FAIL;
4014 else
4015 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004016 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4017
Bram Moolenaar4525a572022-02-13 11:57:33 +00004018 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004019 {
4020 emsg(_(e_cannot_use_underscore_here));
4021 ret = FAIL;
4022 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004023 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004024 && s[0] == 's' && s[1] == ':')
4025 {
4026 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4027 ret = FAIL;
4028 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004029 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004030 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004031 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004032 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004033 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004034 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02004035 else if (flags & EVAL_CONSTANT)
4036 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004037 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004038 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004039 // get the value of "true", "false", etc. or a variable
4040 ret = FAIL;
4041 if (vim9script)
4042 ret = handle_predefined(s, len, rettv);
4043 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004044 {
4045 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004046 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004047 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004048 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004049 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004050 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004051 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004052 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004053 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004054 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004055 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004056 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004057 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004058 }
4059
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004060 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4061 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004062 if (ret == OK)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004063 ret = handle_subscript(arg, name_start, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064
4065 /*
4066 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4067 */
4068 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004069 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004070
4071 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004072 return ret;
4073}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004074
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004075/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004076 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004077 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004078 * Adjusts "end_leaderp" until it is at "start_leader".
4079 */
4080 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004081eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004082 typval_T *rettv,
4083 int numeric_only,
4084 char_u *start_leader,
4085 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004086{
4087 char_u *end_leader = *end_leaderp;
4088 int ret = OK;
4089 int error = FALSE;
4090 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004091 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004092 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004093#ifdef FEAT_FLOAT
4094 float_T f = 0.0;
4095
4096 if (rettv->v_type == VAR_FLOAT)
4097 f = rettv->vval.v_float;
4098 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004099#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004100 {
4101 while (VIM_ISWHITE(end_leader[-1]))
4102 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004103 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004104 val = tv2bool(rettv);
4105 else
4106 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004107 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004108 if (error)
4109 {
4110 clear_tv(rettv);
4111 ret = FAIL;
4112 }
4113 else
4114 {
4115 while (end_leader > start_leader)
4116 {
4117 --end_leader;
4118 if (*end_leader == '!')
4119 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004120 if (numeric_only)
4121 {
4122 ++end_leader;
4123 break;
4124 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004125#ifdef FEAT_FLOAT
4126 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004127 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004128 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004129 {
4130 rettv->v_type = VAR_BOOL;
4131 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4132 }
4133 else
4134 f = !f;
4135 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004136 else
4137#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004138 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004139 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004140 type = VAR_BOOL;
4141 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004142 }
4143 else if (*end_leader == '-')
4144 {
4145#ifdef FEAT_FLOAT
4146 if (rettv->v_type == VAR_FLOAT)
4147 f = -f;
4148 else
4149#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004150 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004151 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004152 type = VAR_NUMBER;
4153 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004154 }
4155 }
4156#ifdef FEAT_FLOAT
4157 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004159 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004160 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004162 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004163#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004164 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004165 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004166 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004167 rettv->v_type = type;
4168 else
4169 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004170 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004173 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 return ret;
4175}
4176
4177/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004178 * Call the function referred to in "rettv".
4179 */
4180 static int
4181call_func_rettv(
4182 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004183 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004184 typval_T *rettv,
4185 int evaluate,
4186 dict_T *selfdict,
4187 typval_T *basetv)
4188{
4189 partial_T *pt = NULL;
4190 funcexe_T funcexe;
4191 typval_T functv;
4192 char_u *s;
4193 int ret;
4194
4195 // need to copy the funcref so that we can clear rettv
4196 if (evaluate)
4197 {
4198 functv = *rettv;
4199 rettv->v_type = VAR_UNKNOWN;
4200
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004201 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004202 if (functv.v_type == VAR_PARTIAL)
4203 {
4204 pt = functv.vval.v_partial;
4205 s = partial_name(pt);
4206 }
4207 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004208 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004209 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004210 if (s == NULL || *s == NUL)
4211 {
4212 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004213 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004214 goto theend;
4215 }
4216 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004217 }
4218 else
4219 s = (char_u *)"";
4220
Bram Moolenaara80faa82020-04-12 19:37:17 +02004221 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004222 funcexe.fe_firstline = curwin->w_cursor.lnum;
4223 funcexe.fe_lastline = curwin->w_cursor.lnum;
4224 funcexe.fe_evaluate = evaluate;
4225 funcexe.fe_partial = pt;
4226 funcexe.fe_selfdict = selfdict;
4227 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004228 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004229
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004230theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004231 // Clear the funcref afterwards, so that deleting it while
4232 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004233 if (evaluate)
4234 clear_tv(&functv);
4235
4236 return ret;
4237}
4238
4239/*
4240 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004241 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004242 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4243 */
4244 static int
4245eval_lambda(
4246 char_u **arg,
4247 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004248 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004249 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004250{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004251 int evaluate = evalarg != NULL
4252 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004253 typval_T base = *rettv;
4254 int ret;
4255
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004256 rettv->v_type = VAR_UNKNOWN;
4257
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004258 if (**arg == '{')
4259 {
4260 // ->{lambda}()
4261 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4262 }
4263 else
4264 {
4265 // ->(lambda)()
4266 ++*arg;
4267 ret = eval1(arg, rettv, evalarg);
4268 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004269 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004270 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004271 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004272 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004273 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004274 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4275 && rettv->v_type != VAR_PARTIAL)
4276 {
4277 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4278 return FAIL;
4279 }
4280 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004281 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004282 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004283 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004284
4285 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004286 {
4287 if (verbose)
4288 {
4289 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004290 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004291 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004292 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004293 }
4294 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004295 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004296 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004297 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004298 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004299
4300 // Clear the funcref afterwards, so that deleting it while
4301 // evaluating the arguments is possible (see test55).
4302 if (evaluate)
4303 clear_tv(&base);
4304
4305 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004306}
4307
4308/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004309 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004310 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004311 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4312 */
4313 static int
4314eval_method(
4315 char_u **arg,
4316 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004317 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004318 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004319{
4320 char_u *name;
4321 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004322 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004323 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004324 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004325 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004326 int evaluate = evalarg != NULL
4327 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004328
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004329 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004330
Bram Moolenaarac92e252019-08-03 21:58:38 +02004331 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004332 len = get_name_len(arg, &alias, evaluate, TRUE);
4333 if (alias != NULL)
4334 name = alias;
4335
4336 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004337 {
4338 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004339 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004340 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004341 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004342 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004343 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004344 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004345
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004346 // If there is no "(" immediately following, but there is further on,
4347 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4348 // Does not handle anything where "(" is part of the expression.
4349 *arg = skipwhite(*arg);
4350
4351 if (**arg != '(' && alias == NULL
4352 && (paren = vim_strchr(*arg, '(')) != NULL)
4353 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004354 char_u *deref;
4355
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004356 *arg = name;
4357 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004358 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4359 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004360 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004361 *arg = name + len;
4362 ret = FAIL;
4363 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004364 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004365 {
4366 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004367 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004368 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004369 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004370 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004371
4372 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004373 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004374 *arg = skipwhite(*arg);
4375
4376 if (**arg != '(')
4377 {
4378 if (verbose)
4379 semsg(_(e_missing_parenthesis_str), name);
4380 ret = FAIL;
4381 }
4382 else if (VIM_ISWHITE((*arg)[-1]))
4383 {
4384 if (verbose)
4385 emsg(_(e_no_white_space_allowed_before_parenthesis));
4386 ret = FAIL;
4387 }
4388 else
4389 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004390 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004391 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004392 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004393
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004394 // Clear the funcref afterwards, so that deleting it while
4395 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004396 if (evaluate)
4397 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004398 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004399
Bram Moolenaarac92e252019-08-03 21:58:38 +02004400 return ret;
4401}
4402
4403/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004404 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4405 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004406 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4407 */
4408 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004409eval_index(
4410 char_u **arg,
4411 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004412 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004413 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004414{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004415 int evaluate = evalarg != NULL
4416 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004417 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004418 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004419 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004420 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004421 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004422 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004423
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004424 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4425 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004426
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004427 init_tv(&var1);
4428 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004429 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004430 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004431 /*
4432 * dict.name
4433 */
4434 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004435 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004436 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004437 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004438 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004439 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004440 }
4441 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004442 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004443 /*
4444 * something[idx]
4445 *
4446 * Get the (first) variable from inside the [].
4447 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004448 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004449 if (**arg == ':')
4450 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004451 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004452 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004453 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004454 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004455 semsg(_(e_white_space_required_before_and_after_str_at_str),
4456 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004457 clear_tv(&var1);
4458 return FAIL;
4459 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004460 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004461 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004462 int error = FALSE;
4463
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004464#ifdef FEAT_FLOAT
4465 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004466 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004467 && var1.v_type == VAR_FLOAT)
4468 {
4469 var1.vval.v_string = typval_tostring(&var1, TRUE);
4470 var1.v_type = VAR_STRING;
4471 }
4472#endif
Bram Moolenaar4525a572022-02-13 11:57:33 +00004473 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004474 tv_get_number_chk(&var1, &error);
4475 else
4476 error = tv_get_string_chk(&var1) == NULL;
4477 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004478 {
4479 // not a number or string
4480 clear_tv(&var1);
4481 return FAIL;
4482 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004483 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004484
4485 /*
4486 * Get the second variable from inside the [:].
4487 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004488 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004489 if (**arg == ':')
4490 {
4491 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004492 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004493 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004494 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004495 semsg(_(e_white_space_required_before_and_after_str_at_str),
4496 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004497 if (!empty1)
4498 clear_tv(&var1);
4499 return FAIL;
4500 }
4501 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004502 if (**arg == ']')
4503 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004504 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004505 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004506 if (!empty1)
4507 clear_tv(&var1);
4508 return FAIL;
4509 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004510 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004511 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004512 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004513 if (!empty1)
4514 clear_tv(&var1);
4515 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004516 return FAIL;
4517 }
4518 }
4519
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004520 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004521 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004522 if (**arg != ']')
4523 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004524 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004525 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004526 clear_tv(&var1);
4527 if (range)
4528 clear_tv(&var2);
4529 return FAIL;
4530 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004531 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004532 }
4533
4534 if (evaluate)
4535 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004536 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004537 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004538 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004539
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004540 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004541 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004542 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004543 clear_tv(&var2);
4544 return res;
4545 }
4546 return OK;
4547}
4548
4549/*
4550 * Check if "rettv" can have an [index] or [sli:ce]
4551 */
4552 int
4553check_can_index(typval_T *rettv, int evaluate, int verbose)
4554{
4555 switch (rettv->v_type)
4556 {
4557 case VAR_FUNC:
4558 case VAR_PARTIAL:
4559 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004560 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004561 return FAIL;
4562 case VAR_FLOAT:
4563#ifdef FEAT_FLOAT
4564 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004565 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004566 return FAIL;
4567#endif
4568 case VAR_BOOL:
4569 case VAR_SPECIAL:
4570 case VAR_JOB:
4571 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004572 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004573 if (verbose)
4574 emsg(_(e_cannot_index_special_variable));
4575 return FAIL;
4576 case VAR_UNKNOWN:
4577 case VAR_ANY:
4578 case VAR_VOID:
4579 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004580 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004581 emsg(_(e_cannot_index_special_variable));
4582 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004583 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004584 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004585
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004586 case VAR_STRING:
4587 case VAR_LIST:
4588 case VAR_DICT:
4589 case VAR_BLOB:
4590 break;
4591 case VAR_NUMBER:
4592 if (in_vim9script())
4593 emsg(_(e_cannot_index_number));
4594 break;
4595 }
4596 return OK;
4597}
4598
4599/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004600 * slice() function
4601 */
4602 void
4603f_slice(typval_T *argvars, typval_T *rettv)
4604{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004605 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004606 && ((argvars[0].v_type != VAR_STRING
4607 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004608 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004609 && check_for_list_arg(argvars, 0) == FAIL)
4610 || check_for_number_arg(argvars, 1) == FAIL
4611 || check_for_opt_number_arg(argvars, 2) == FAIL))
4612 return;
4613
Bram Moolenaar6601b622021-01-13 21:47:15 +01004614 if (check_can_index(argvars, TRUE, FALSE) == OK)
4615 {
4616 copy_tv(argvars, rettv);
4617 eval_index_inner(rettv, TRUE, argvars + 1,
4618 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4619 TRUE, NULL, 0, FALSE);
4620 }
4621}
4622
4623/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004624 * Apply index or range to "rettv".
4625 * "var1" is the first index, NULL for [:expr].
4626 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004627 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4628 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004629 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4630 */
4631 int
4632eval_index_inner(
4633 typval_T *rettv,
4634 int is_range,
4635 typval_T *var1,
4636 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004637 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004638 char_u *key,
4639 int keylen,
4640 int verbose)
4641{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004642 varnumber_T n1, n2 = 0;
4643 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004644
4645 n1 = 0;
4646 if (var1 != NULL && rettv->v_type != VAR_DICT)
4647 n1 = tv_get_number(var1);
4648
4649 if (is_range)
4650 {
4651 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004652 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004653 if (verbose)
4654 emsg(_(e_cannot_slice_dictionary));
4655 return FAIL;
4656 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004657 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004658 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004659 else
4660 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004661 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004662
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004663 switch (rettv->v_type)
4664 {
4665 case VAR_UNKNOWN:
4666 case VAR_ANY:
4667 case VAR_VOID:
4668 case VAR_FUNC:
4669 case VAR_PARTIAL:
4670 case VAR_FLOAT:
4671 case VAR_BOOL:
4672 case VAR_SPECIAL:
4673 case VAR_JOB:
4674 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004675 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004676 break; // not evaluating, skipping over subscript
4677
4678 case VAR_NUMBER:
4679 case VAR_STRING:
4680 {
4681 char_u *s = tv_get_string(rettv);
4682
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004683 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004684 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004685 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004686 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004687 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004688 else
4689 s = char_from_string(s, n1);
4690 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004691 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004692 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004693 // The resulting variable is a substring. If the indexes
4694 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004695 if (n1 < 0)
4696 {
4697 n1 = len + n1;
4698 if (n1 < 0)
4699 n1 = 0;
4700 }
4701 if (n2 < 0)
4702 n2 = len + n2;
4703 else if (n2 >= len)
4704 n2 = len;
4705 if (n1 >= len || n2 < 0 || n1 > n2)
4706 s = NULL;
4707 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004708 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004709 }
4710 else
4711 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004712 // The resulting variable is a string of a single
4713 // character. If the index is too big or negative the
4714 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004715 if (n1 >= len || n1 < 0)
4716 s = NULL;
4717 else
4718 s = vim_strnsave(s + n1, 1);
4719 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004720 clear_tv(rettv);
4721 rettv->v_type = VAR_STRING;
4722 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004723 }
4724 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004725
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004726 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004727 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4728 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004729 break;
4730
4731 case VAR_LIST:
4732 if (var1 == NULL)
4733 n1 = 0;
4734 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004735 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004736 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004737 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004738 return FAIL;
4739 break;
4740
4741 case VAR_DICT:
4742 {
4743 dictitem_T *item;
4744 typval_T tmp;
4745
4746 if (key == NULL)
4747 {
4748 key = tv_get_string_chk(var1);
4749 if (key == NULL)
4750 return FAIL;
4751 }
4752
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004753 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004754
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004755 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004756 {
4757 if (verbose)
4758 {
4759 if (keylen > 0)
4760 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004761 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004762 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004763 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004764 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004765
4766 copy_tv(&item->di_tv, &tmp);
4767 clear_tv(rettv);
4768 *rettv = tmp;
4769 }
4770 break;
4771 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004772 return OK;
4773}
4774
4775/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004776 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004777 */
4778 char_u *
4779partial_name(partial_T *pt)
4780{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004781 if (pt != NULL)
4782 {
4783 if (pt->pt_name != NULL)
4784 return pt->pt_name;
4785 if (pt->pt_func != NULL)
4786 return pt->pt_func->uf_name;
4787 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004788 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004789}
4790
Bram Moolenaarddecc252016-04-06 22:59:37 +02004791 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004792partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004793{
4794 int i;
4795
4796 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004797 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004798 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004799 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004800 if (pt->pt_name != NULL)
4801 {
4802 func_unref(pt->pt_name);
4803 vim_free(pt->pt_name);
4804 }
4805 else
4806 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004807
Bram Moolenaar54656012021-06-09 20:50:46 +02004808 // "out_up" is no longer used, decrement refcount on partial that owns it.
4809 partial_unref(pt->pt_outer.out_up_partial);
4810
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004811 // Using pt_outer from another partial.
4812 partial_unref(pt->pt_outer_partial);
4813
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004814 // Decrease the reference count for the context of a closure. If down
4815 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004816 if (pt->pt_funcstack != NULL)
4817 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004818 --pt->pt_funcstack->fs_refcount;
4819 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004820 }
4821
Bram Moolenaarddecc252016-04-06 22:59:37 +02004822 vim_free(pt);
4823}
4824
4825/*
4826 * Unreference a closure: decrement the reference count and free it when it
4827 * becomes zero.
4828 */
4829 void
4830partial_unref(partial_T *pt)
4831{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004832 if (pt != NULL)
4833 {
4834 if (--pt->pt_refcount <= 0)
4835 partial_free(pt);
4836
4837 // If the reference count goes down to one, the funcstack may be the
4838 // only reference and can be freed if no other partials reference it.
4839 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4840 funcstack_check_refcount(pt->pt_funcstack);
4841 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004842}
4843
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004844/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004845 * Return the next (unique) copy ID.
4846 * Used for serializing nested structures.
4847 */
4848 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004849get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004850{
4851 current_copyID += COPYID_INC;
4852 return current_copyID;
4853}
4854
4855/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004856 * Garbage collection for lists and dictionaries.
4857 *
4858 * We use reference counts to be able to free most items right away when they
4859 * are no longer used. But for composite items it's possible that it becomes
4860 * unused while the reference count is > 0: When there is a recursive
4861 * reference. Example:
4862 * :let l = [1, 2, 3]
4863 * :let d = {9: l}
4864 * :let l[1] = d
4865 *
4866 * Since this is quite unusual we handle this with garbage collection: every
4867 * once in a while find out which lists and dicts are not referenced from any
4868 * variable.
4869 *
4870 * Here is a good reference text about garbage collection (refers to Python
4871 * but it applies to all reference-counting mechanisms):
4872 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004873 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004874
4875/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004876 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004877 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004878 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004879 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004880 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004881garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004882{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004883 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004884 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004885 buf_T *buf;
4886 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004887 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004888 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004889
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004890 if (!testing)
4891 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004892 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004893 want_garbage_collect = FALSE;
4894 may_garbage_collect = FALSE;
4895 garbage_collect_at_exit = FALSE;
4896 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004897
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004898 // The execution stack can grow big, limit the size.
4899 if (exestack.ga_maxlen - exestack.ga_len > 500)
4900 {
4901 size_t new_len;
4902 char_u *pp;
4903 int n;
4904
4905 // Keep 150% of the current size, with a minimum of the growth size.
4906 n = exestack.ga_len / 2;
4907 if (n < exestack.ga_growsize)
4908 n = exestack.ga_growsize;
4909
4910 // Don't make it bigger though.
4911 if (exestack.ga_len + n < exestack.ga_maxlen)
4912 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004913 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004914 pp = vim_realloc(exestack.ga_data, new_len);
4915 if (pp == NULL)
4916 return FAIL;
4917 exestack.ga_maxlen = exestack.ga_len + n;
4918 exestack.ga_data = pp;
4919 }
4920 }
4921
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004922 // We advance by two because we add one for items referenced through
4923 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004924 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004925
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004926 /*
4927 * 1. Go through all accessible variables and mark all lists and dicts
4928 * with copyID.
4929 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004930
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004931 // Don't free variables in the previous_funccal list unless they are only
4932 // referenced through previous_funccal. This must be first, because if
4933 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004934 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004935
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004936 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004937 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004938
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004939 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004940 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004941 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4942 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004943
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004944 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004945 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004946 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4947 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004948 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004949 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4950 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004951#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004952 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004953 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4954 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004955 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004956 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004957 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4958 NULL, NULL);
4959#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004960
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004961 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004962 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004963 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4964 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004965 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004966 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004967
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004968 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004969 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004970
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004971 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004972 abort = abort || set_ref_in_functions(copyID);
4973
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004974 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004975 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004976
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004977 // funcstacks keep variables for closures
4978 abort = abort || set_ref_in_funcstacks(copyID);
4979
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004980 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004981 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004982
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004983 // callbacks in buffers
4984 abort = abort || set_ref_in_buffers(copyID);
4985
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004986 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
4987 abort = abort || set_ref_in_insexpand_funcs(copyID);
4988
4989 // 'operatorfunc' callback
4990 abort = abort || set_ref_in_opfunc(copyID);
4991
4992 // 'tagfunc' callback
4993 abort = abort || set_ref_in_tagfunc(copyID);
4994
4995 // 'imactivatefunc' and 'imstatusfunc' callbacks
4996 abort = abort || set_ref_in_im_funcs(copyID);
4997
Bram Moolenaar1dced572012-04-05 16:54:08 +02004998#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004999 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005000#endif
5001
Bram Moolenaardb913952012-06-29 12:54:53 +02005002#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005003 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005004#endif
5005
5006#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005007 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005008#endif
5009
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005010#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005011 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005012 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005013#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005014#ifdef FEAT_NETBEANS_INTG
5015 abort = abort || set_ref_in_nb_channel(copyID);
5016#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005017
Bram Moolenaare3188e22016-05-31 21:13:04 +02005018#ifdef FEAT_TIMERS
5019 abort = abort || set_ref_in_timer(copyID);
5020#endif
5021
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005022#ifdef FEAT_QUICKFIX
5023 abort = abort || set_ref_in_quickfix(copyID);
5024#endif
5025
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005026#ifdef FEAT_TERMINAL
5027 abort = abort || set_ref_in_term(copyID);
5028#endif
5029
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005030#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005031 abort = abort || set_ref_in_popups(copyID);
5032#endif
5033
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005034 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005035 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005036 /*
5037 * 2. Free lists and dictionaries that are not referenced.
5038 */
5039 did_free = free_unref_items(copyID);
5040
5041 /*
5042 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005043 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005044 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005045 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005046 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005047 else if (p_verbose > 0)
5048 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005049 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005050 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005051
5052 return did_free;
5053}
5054
5055/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005056 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005057 */
5058 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005059free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005060{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005061 int did_free = FALSE;
5062
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005063 // Let all "free" functions know that we are here. This means no
5064 // dictionaries, lists, channels or jobs are to be freed, because we will
5065 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005066 in_free_unref_items = TRUE;
5067
5068 /*
5069 * PASS 1: free the contents of the items. We don't free the items
5070 * themselves yet, so that it is possible to decrement refcount counters
5071 */
5072
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005073 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005074 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005075
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005076 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005077 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005078
5079#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005080 // Go through the list of jobs and free items without the copyID. This
5081 // must happen before doing channels, because jobs refer to channels, but
5082 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005083 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5084
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005085 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005086 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5087#endif
5088
5089 /*
5090 * PASS 2: free the items themselves.
5091 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005092 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005093 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005094
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005095#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005096 // Go through the list of jobs and free items without the copyID. This
5097 // must happen before doing channels, because jobs refer to channels, but
5098 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005099 free_unused_jobs(copyID, COPYID_MASK);
5100
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005101 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005102 free_unused_channels(copyID, COPYID_MASK);
5103#endif
5104
5105 in_free_unref_items = FALSE;
5106
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005107 return did_free;
5108}
5109
5110/*
5111 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005112 * "list_stack" is used to add lists to be marked. Can be NULL.
5113 *
5114 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005115 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005116 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005117set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005118{
5119 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005120 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005121 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005122 hashtab_T *cur_ht;
5123 ht_stack_T *ht_stack = NULL;
5124 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005125
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005126 cur_ht = ht;
5127 for (;;)
5128 {
5129 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005130 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005131 // Mark each item in the hashtab. If the item contains a hashtab
5132 // it is added to ht_stack, if it contains a list it is added to
5133 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005134 todo = (int)cur_ht->ht_used;
5135 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5136 if (!HASHITEM_EMPTY(hi))
5137 {
5138 --todo;
5139 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5140 &ht_stack, list_stack);
5141 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005142 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005143
5144 if (ht_stack == NULL)
5145 break;
5146
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005147 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005148 cur_ht = ht_stack->ht;
5149 tempitem = ht_stack;
5150 ht_stack = ht_stack->prev;
5151 free(tempitem);
5152 }
5153
5154 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005155}
5156
Dominique Pelle748b3082022-01-08 12:41:16 +00005157#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5158 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005159/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005160 * Mark a dict and its items with "copyID".
5161 * Returns TRUE if setting references failed somehow.
5162 */
5163 int
5164set_ref_in_dict(dict_T *d, int copyID)
5165{
5166 if (d != NULL && d->dv_copyID != copyID)
5167 {
5168 d->dv_copyID = copyID;
5169 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5170 }
5171 return FALSE;
5172}
Dominique Pelle748b3082022-01-08 12:41:16 +00005173#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005174
5175/*
5176 * Mark a list and its items with "copyID".
5177 * Returns TRUE if setting references failed somehow.
5178 */
5179 int
5180set_ref_in_list(list_T *ll, int copyID)
5181{
5182 if (ll != NULL && ll->lv_copyID != copyID)
5183 {
5184 ll->lv_copyID = copyID;
5185 return set_ref_in_list_items(ll, copyID, NULL);
5186 }
5187 return FALSE;
5188}
5189
5190/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005191 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005192 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5193 *
5194 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005195 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005196 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005197set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005198{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005199 listitem_T *li;
5200 int abort = FALSE;
5201 list_T *cur_l;
5202 list_stack_T *list_stack = NULL;
5203 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005204
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005205 cur_l = l;
5206 for (;;)
5207 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005208 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005209 // Mark each item in the list. If the item contains a hashtab
5210 // it is added to ht_stack, if it contains a list it is added to
5211 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005212 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5213 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5214 ht_stack, &list_stack);
5215 if (list_stack == NULL)
5216 break;
5217
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005218 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005219 cur_l = list_stack->list;
5220 tempitem = list_stack;
5221 list_stack = list_stack->prev;
5222 free(tempitem);
5223 }
5224
5225 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005226}
5227
5228/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005229 * Mark the partial in callback 'cb' with "copyID".
5230 */
5231 int
5232set_ref_in_callback(callback_T *cb, int copyID)
5233{
5234 typval_T tv;
5235
5236 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5237 return FALSE;
5238
5239 tv.v_type = VAR_PARTIAL;
5240 tv.vval.v_partial = cb->cb_partial;
5241 return set_ref_in_item(&tv, copyID, NULL, NULL);
5242}
5243
5244/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005245 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005246 * "list_stack" is used to add lists to be marked. Can be NULL.
5247 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5248 *
5249 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005250 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005251 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005252set_ref_in_item(
5253 typval_T *tv,
5254 int copyID,
5255 ht_stack_T **ht_stack,
5256 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005257{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005258 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005259
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005260 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005261 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005262 dict_T *dd = tv->vval.v_dict;
5263
Bram Moolenaara03f2332016-02-06 18:09:59 +01005264 if (dd != NULL && dd->dv_copyID != copyID)
5265 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005266 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005267 dd->dv_copyID = copyID;
5268 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005269 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005270 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5271 }
5272 else
5273 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005274 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5275
Bram Moolenaara03f2332016-02-06 18:09:59 +01005276 if (newitem == NULL)
5277 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005278 else
5279 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005280 newitem->ht = &dd->dv_hashtab;
5281 newitem->prev = *ht_stack;
5282 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005283 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005284 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005285 }
5286 }
5287 else if (tv->v_type == VAR_LIST)
5288 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005289 list_T *ll = tv->vval.v_list;
5290
Bram Moolenaara03f2332016-02-06 18:09:59 +01005291 if (ll != NULL && ll->lv_copyID != copyID)
5292 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005293 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005294 ll->lv_copyID = copyID;
5295 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005296 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005297 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005298 }
5299 else
5300 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005301 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5302
Bram Moolenaara03f2332016-02-06 18:09:59 +01005303 if (newitem == NULL)
5304 abort = TRUE;
5305 else
5306 {
5307 newitem->list = ll;
5308 newitem->prev = *list_stack;
5309 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005310 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005311 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005312 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005313 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005314 else if (tv->v_type == VAR_FUNC)
5315 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005316 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005317 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005318 else if (tv->v_type == VAR_PARTIAL)
5319 {
5320 partial_T *pt = tv->vval.v_partial;
5321 int i;
5322
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005323 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005324 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005325 // Didn't see this partial yet.
5326 pt->pt_copyID = copyID;
5327
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005328 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005329
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005330 if (pt->pt_dict != NULL)
5331 {
5332 typval_T dtv;
5333
5334 dtv.v_type = VAR_DICT;
5335 dtv.vval.v_dict = pt->pt_dict;
5336 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5337 }
5338
5339 for (i = 0; i < pt->pt_argc; ++i)
5340 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5341 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005342 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005343 }
5344 }
5345#ifdef FEAT_JOB_CHANNEL
5346 else if (tv->v_type == VAR_JOB)
5347 {
5348 job_T *job = tv->vval.v_job;
5349 typval_T dtv;
5350
5351 if (job != NULL && job->jv_copyID != copyID)
5352 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005353 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005354 if (job->jv_channel != NULL)
5355 {
5356 dtv.v_type = VAR_CHANNEL;
5357 dtv.vval.v_channel = job->jv_channel;
5358 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5359 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005360 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005361 {
5362 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005363 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005364 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5365 }
5366 }
5367 }
5368 else if (tv->v_type == VAR_CHANNEL)
5369 {
5370 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005371 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005372 typval_T dtv;
5373 jsonq_T *jq;
5374 cbq_T *cq;
5375
5376 if (ch != NULL && ch->ch_copyID != copyID)
5377 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005378 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005379 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005380 {
5381 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5382 jq = jq->jq_next)
5383 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5384 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5385 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005386 if (cq->cq_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 = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005390 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5391 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005392 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005393 {
5394 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005395 dtv.vval.v_partial =
5396 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005397 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5398 }
5399 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005400 if (ch->ch_callback.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_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005404 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5405 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005406 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005407 {
5408 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005409 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005410 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5411 }
5412 }
5413 }
5414#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005415 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005416}
5417
Bram Moolenaar8c711452005-01-14 21:53:12 +00005418/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005419 * Return a string with the string representation of a variable.
5420 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005421 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005422 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005423 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005424 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005425 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005426 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5427 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005428 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005430 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005431echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005432 typval_T *tv,
5433 char_u **tofree,
5434 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005435 int copyID,
5436 int echo_style,
5437 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005438 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005440 static int recurse = 0;
5441 char_u *r = NULL;
5442
Bram Moolenaar33570922005-01-25 22:26:29 +00005443 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005444 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005445 if (!did_echo_string_emsg)
5446 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005447 // Only give this message once for a recursive call to avoid
5448 // flooding the user with errors. And stop iterating over lists
5449 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005450 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005451 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005452 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005453 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005454 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005455 }
5456 ++recurse;
5457
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 switch (tv->v_type)
5459 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005460 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005461 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005462 {
5463 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005464 r = tv->vval.v_string;
5465 if (r == NULL)
5466 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005467 }
5468 else
5469 {
5470 *tofree = string_quote(tv->vval.v_string, FALSE);
5471 r = *tofree;
5472 }
5473 break;
5474
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005475 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005476 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005477 char_u buf[MAX_FUNC_NAME_LEN];
5478
5479 if (echo_style)
5480 {
LemonBoya5d35902022-04-29 21:15:02 +01005481 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5482 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005483 buf, MAX_FUNC_NAME_LEN);
5484 if (r == buf)
5485 {
5486 r = vim_strsave(buf);
5487 *tofree = r;
5488 }
5489 else
5490 *tofree = NULL;
5491 }
5492 else
5493 {
5494 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5495 : make_ufunc_name_readable(
5496 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5497 TRUE);
5498 r = *tofree;
5499 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005500 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005501 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005502
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005503 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005504 {
5505 partial_T *pt = tv->vval.v_partial;
5506 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005507 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005508 garray_T ga;
5509 int i;
5510 char_u *tf;
5511
5512 ga_init2(&ga, 1, 100);
5513 ga_concat(&ga, (char_u *)"function(");
5514 if (fname != NULL)
5515 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005516 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005517 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005518 && vim_isupper(fname[1]))
5519 {
5520 ga_concat(&ga, (char_u *)"'g:");
5521 ga_concat(&ga, fname + 1);
5522 }
5523 else
5524 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005525 vim_free(fname);
5526 }
5527 if (pt != NULL && pt->pt_argc > 0)
5528 {
5529 ga_concat(&ga, (char_u *)", [");
5530 for (i = 0; i < pt->pt_argc; ++i)
5531 {
5532 if (i > 0)
5533 ga_concat(&ga, (char_u *)", ");
5534 ga_concat(&ga,
5535 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5536 vim_free(tf);
5537 }
5538 ga_concat(&ga, (char_u *)"]");
5539 }
5540 if (pt != NULL && pt->pt_dict != NULL)
5541 {
5542 typval_T dtv;
5543
5544 ga_concat(&ga, (char_u *)", ");
5545 dtv.v_type = VAR_DICT;
5546 dtv.vval.v_dict = pt->pt_dict;
5547 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5548 vim_free(tf);
5549 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005550 // terminate with ')' and a NUL
5551 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005552
5553 *tofree = ga.ga_data;
5554 r = *tofree;
5555 break;
5556 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005557
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005558 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005559 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005560 break;
5561
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005562 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005563 if (tv->vval.v_list == NULL)
5564 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005565 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005566 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005567 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005568 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005569 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5570 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005571 {
5572 *tofree = NULL;
5573 r = (char_u *)"[...]";
5574 }
5575 else
5576 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005577 int old_copyID = tv->vval.v_list->lv_copyID;
5578
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005579 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005580 *tofree = list2string(tv, copyID, restore_copyID);
5581 if (restore_copyID)
5582 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005583 r = *tofree;
5584 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005585 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005586
Bram Moolenaar8c711452005-01-14 21:53:12 +00005587 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005588 if (tv->vval.v_dict == NULL)
5589 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005590 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005591 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005592 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005593 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005594 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5595 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005596 {
5597 *tofree = NULL;
5598 r = (char_u *)"{...}";
5599 }
5600 else
5601 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005602 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005603
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005604 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005605 *tofree = dict2string(tv, copyID, restore_copyID);
5606 if (restore_copyID)
5607 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005608 r = *tofree;
5609 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005610 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005611
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005612 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005613 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005614 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005615 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005616 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005617 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005618 break;
5619
Bram Moolenaar835dc632016-02-07 14:27:38 +01005620 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005621 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005622#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005623 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005624 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5625 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005626 if (composite_val)
5627 {
5628 *tofree = string_quote(r, FALSE);
5629 r = *tofree;
5630 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005631#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005632 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005633
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005634 case VAR_INSTR:
5635 *tofree = NULL;
5636 r = (char_u *)"instructions";
5637 break;
5638
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005639 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005640#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005641 *tofree = NULL;
5642 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5643 r = numbuf;
5644 break;
5645#endif
5646
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005647 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005648 case VAR_SPECIAL:
5649 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005650 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005651 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005652 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005653
Bram Moolenaar8502c702014-06-17 12:51:16 +02005654 if (--recurse == 0)
5655 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005656 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005657}
5658
5659/*
5660 * Return a string with the string representation of a variable.
5661 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5662 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005663 * Does not put quotes around strings, as ":echo" displays values.
5664 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5665 * May return NULL.
5666 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005667 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005668echo_string(
5669 typval_T *tv,
5670 char_u **tofree,
5671 char_u *numbuf,
5672 int copyID)
5673{
5674 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5675}
5676
5677/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005678 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5679 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005680 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005681 */
5682 int
5683buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5684{
5685 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005686 char_u *t;
5687 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005688
5689 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5690 return -1;
5691
5692 if (lnum > buf->b_ml.ml_line_count)
5693 lnum = buf->b_ml.ml_line_count;
5694
5695 str = ml_get_buf(buf, lnum, FALSE);
5696 if (str == NULL)
5697 return -1;
5698
5699 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005700 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005701
Bram Moolenaar91458462021-01-13 20:08:38 +01005702 // count the number of characters
5703 t = str;
5704 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5705 t += mb_ptr2len(t);
5706
5707 // In insert mode, when the cursor is at the end of a non-empty line,
5708 // byteidx points to the NUL character immediately past the end of the
5709 // string. In this case, add one to the character count.
5710 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5711 count++;
5712
5713 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005714}
5715
5716/*
5717 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005718 * byte index. Works only for loaded buffers. Returns -1 on failure.
5719 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005720 */
5721 int
5722buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5723{
5724 char_u *str;
5725 char_u *t;
5726
5727 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5728 return -1;
5729
5730 if (lnum > buf->b_ml.ml_line_count)
5731 lnum = buf->b_ml.ml_line_count;
5732
5733 str = ml_get_buf(buf, lnum, FALSE);
5734 if (str == NULL)
5735 return -1;
5736
5737 // Convert the character offset to a byte offset
5738 t = str;
5739 while (*t != NUL && --charidx > 0)
5740 t += mb_ptr2len(t);
5741
Bram Moolenaar91458462021-01-13 20:08:38 +01005742 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005743}
5744
5745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005747 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005749 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005750var2fpos(
5751 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005752 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005753 int *fnum, // set to fnum for '0, 'A, etc.
5754 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005756 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005758 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005760 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005761 if (varp->v_type == VAR_LIST)
5762 {
5763 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005764 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005765 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005766 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005767
5768 l = varp->vval.v_list;
5769 if (l == NULL)
5770 return NULL;
5771
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005772 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005773 pos.lnum = list_find_nr(l, 0L, &error);
5774 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005775 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005776 if (charcol)
5777 len = (long)mb_charlen(ml_get(pos.lnum));
5778 else
5779 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005780
Bram Moolenaarec65d772020-08-20 22:29:12 +02005781 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005782 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005783 li = list_find(l, 1L);
5784 if (li != NULL && li->li_tv.v_type == VAR_STRING
5785 && li->li_tv.vval.v_string != NULL
5786 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005787 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005788 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005789 }
5790 else
5791 {
5792 pos.col = list_find_nr(l, 1L, &error);
5793 if (error)
5794 return NULL;
5795 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005796
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005797 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005798 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005799 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005800 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005801
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005802 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005803 pos.coladd = list_find_nr(l, 2L, &error);
5804 if (error)
5805 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005806
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005807 return &pos;
5808 }
5809
Bram Moolenaarc5809432021-03-27 21:23:30 +01005810 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5811 return NULL;
5812
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005813 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005814 if (name == NULL)
5815 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005816
5817 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005818 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005819 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005820 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005821 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005822 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005823 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005824 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005825 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005826 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005827 pos = VIsual;
5828 else
5829 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005830 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005831 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005832 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005834 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005835 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5837 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005838 pos = *pp;
5839 }
5840 if (pos.lnum != 0)
5841 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005842 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005843 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5844 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005846
Bram Moolenaara5525202006-03-02 22:52:09 +00005847 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005848
Bram Moolenaar477933c2007-07-17 14:32:23 +00005849 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005850 {
5851 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005852 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005853 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005854 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005855 // In silent Ex mode topline is zero, but that's not a valid line
5856 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005857 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005858 return &pos;
5859 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005860 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005861 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005862 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005863 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005864 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005865 return &pos;
5866 }
5867 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005868 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005870 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 {
5872 pos.lnum = curbuf->b_ml.ml_line_count;
5873 pos.col = 0;
5874 }
5875 else
5876 {
5877 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005878 if (charcol)
5879 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5880 else
5881 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 }
5883 return &pos;
5884 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005885 if (in_vim9script())
5886 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 return NULL;
5888}
5889
5890/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005891 * Convert list in "arg" into a position and optional file number.
5892 * When "fnump" is NULL there is no file number, only 3 items.
5893 * Note that the column is passed on as-is, the caller may want to decrement
5894 * it to use 1 for the first column.
5895 * Return FAIL when conversion is not possible, doesn't check the position for
5896 * validity.
5897 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005898 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005899list2fpos(
5900 typval_T *arg,
5901 pos_T *posp,
5902 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005903 colnr_T *curswantp,
5904 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005905{
5906 list_T *l = arg->vval.v_list;
5907 long i = 0;
5908 long n;
5909
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005910 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5911 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005912 if (arg->v_type != VAR_LIST
5913 || l == NULL
5914 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005915 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005916 return FAIL;
5917
5918 if (fnump != NULL)
5919 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005920 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005921 if (n < 0)
5922 return FAIL;
5923 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005924 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005925 *fnump = n;
5926 }
5927
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005928 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005929 if (n < 0)
5930 return FAIL;
5931 posp->lnum = n;
5932
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005933 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005934 if (n < 0)
5935 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005936 // If character position is specified, then convert to byte position
5937 if (charcol)
5938 {
5939 buf_T *buf;
5940
5941 // Get the text for the specified line in a loaded buffer
5942 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5943 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5944 return FAIL;
5945
Bram Moolenaar91458462021-01-13 20:08:38 +01005946 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005947 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005948 posp->col = n;
5949
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005950 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005951 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005952 posp->coladd = 0;
5953 else
5954 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005955
Bram Moolenaar493c1782014-05-28 14:34:46 +02005956 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005957 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005958
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005959 return OK;
5960}
5961
5962/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 * Get the length of an environment variable name.
5964 * Advance "arg" to the first character after the name.
5965 * Return 0 for error.
5966 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005967 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005968get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969{
5970 char_u *p;
5971 int len;
5972
5973 for (p = *arg; vim_isIDc(*p); ++p)
5974 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005975 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 return 0;
5977
5978 len = (int)(p - *arg);
5979 *arg = p;
5980 return len;
5981}
5982
5983/*
5984 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005985 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 * Return 0 if something is wrong.
5987 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005988 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005989get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990{
5991 char_u *p;
5992 int len;
5993
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005994 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005996 {
5997 if (*p == ':')
5998 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005999 // "s:" is start of "s:var", but "n:" is not and can be used in
6000 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006001 len = (int)(p - *arg);
6002 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6003 || len > 1)
6004 break;
6005 }
6006 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006007 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 return 0;
6009
6010 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006011 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012
6013 return len;
6014}
6015
6016/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006017 * Get the length of the name of a variable or function.
6018 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006020 * Return -1 if curly braces expansion failed.
6021 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 * If the name contains 'magic' {}'s, expand them and return the
6023 * expanded name in an allocated string via 'alias' - caller must free.
6024 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006025 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006026get_name_len(
6027 char_u **arg,
6028 char_u **alias,
6029 int evaluate,
6030 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031{
6032 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 char_u *p;
6034 char_u *expr_start;
6035 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006037 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038
6039 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6040 && (*arg)[2] == (int)KE_SNR)
6041 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006042 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 *arg += 3;
6044 return get_id_len(arg) + 3;
6045 }
6046 len = eval_fname_script(*arg);
6047 if (len > 0)
6048 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006049 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 *arg += len;
6051 }
6052
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006054 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006056 p = find_name_end(*arg, &expr_start, &expr_end,
6057 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 if (expr_start != NULL)
6059 {
6060 char_u *temp_string;
6061
6062 if (!evaluate)
6063 {
6064 len += (int)(p - *arg);
6065 *arg = skipwhite(p);
6066 return len;
6067 }
6068
6069 /*
6070 * Include any <SID> etc in the expanded string:
6071 * Thus the -len here.
6072 */
6073 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6074 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006075 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 *alias = temp_string;
6077 *arg = skipwhite(p);
6078 return (int)STRLEN(temp_string);
6079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080
6081 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006082 // Only give an error when there is something, otherwise it will be
6083 // reported at a higher level.
6084 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006085 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086
6087 return len;
6088}
6089
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006090/*
6091 * Find the end of a variable or function name, taking care of magic braces.
6092 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6093 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006094 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006095 * Return a pointer to just after the name. Equal to "arg" if there is no
6096 * valid name.
6097 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006098 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006099find_name_end(
6100 char_u *arg,
6101 char_u **expr_start,
6102 char_u **expr_end,
6103 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006105 int mb_nest = 0;
6106 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006108 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02006109 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006111 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006113 *expr_start = NULL;
6114 *expr_end = NULL;
6115 }
6116
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006117 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006118 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
6119 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006120 return arg;
6121
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006122 for (p = arg; *p != NUL
6123 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006124 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006125 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006126 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006127 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006128 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006129 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006130 if (*p == '\'')
6131 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006132 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006133 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006134 ;
6135 if (*p == NUL)
6136 break;
6137 }
6138 else if (*p == '"')
6139 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006140 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006141 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006142 if (*p == '\\' && p[1] != NUL)
6143 ++p;
6144 if (*p == NUL)
6145 break;
6146 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006147 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6148 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006149 // "s:" is start of "s:var", but "n:" is not and can be used in
6150 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006151 len = (int)(p - arg);
6152 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006153 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006154 break;
6155 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006156
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006157 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006159 if (*p == '[')
6160 ++br_nest;
6161 else if (*p == ']')
6162 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006164
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006165 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006167 if (*p == '{')
6168 {
6169 mb_nest++;
6170 if (expr_start != NULL && *expr_start == NULL)
6171 *expr_start = p;
6172 }
6173 else if (*p == '}')
6174 {
6175 mb_nest--;
6176 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6177 *expr_end = p;
6178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 }
6181
6182 return p;
6183}
6184
6185/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006186 * Expands out the 'magic' {}'s in a variable/function name.
6187 * Note that this can call itself recursively, to deal with
6188 * constructs like foo{bar}{baz}{bam}
6189 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6190 * "in_start" ^
6191 * "expr_start" ^
6192 * "expr_end" ^
6193 * "in_end" ^
6194 *
6195 * Returns a new allocated string, which the caller must free.
6196 * Returns NULL for failure.
6197 */
6198 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006199make_expanded_name(
6200 char_u *in_start,
6201 char_u *expr_start,
6202 char_u *expr_end,
6203 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006204{
6205 char_u c1;
6206 char_u *retval = NULL;
6207 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006208
6209 if (expr_end == NULL || in_end == NULL)
6210 return NULL;
6211 *expr_start = NUL;
6212 *expr_end = NUL;
6213 c1 = *in_end;
6214 *in_end = NUL;
6215
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006216 temp_result = eval_to_string(expr_start + 1, FALSE);
6217 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006218 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006219 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6220 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006221 if (retval != NULL)
6222 {
6223 STRCPY(retval, in_start);
6224 STRCAT(retval, temp_result);
6225 STRCAT(retval, expr_end + 1);
6226 }
6227 }
6228 vim_free(temp_result);
6229
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006230 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006231 *expr_start = '{';
6232 *expr_end = '}';
6233
6234 if (retval != NULL)
6235 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006236 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006237 if (expr_start != NULL)
6238 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006239 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006240 temp_result = make_expanded_name(retval, expr_start,
6241 expr_end, temp_result);
6242 vim_free(retval);
6243 retval = temp_result;
6244 }
6245 }
6246
6247 return retval;
6248}
6249
6250/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006252 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006254 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006255eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006257 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006258}
6259
6260/*
6261 * Return TRUE if character "c" can be used as the first character in a
6262 * variable or function name (excluding '{' and '}').
6263 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006264 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006265eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006266{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006267 return ASCII_ISALPHA(c) || c == '_';
6268}
6269
6270/*
6271 * Return TRUE if character "c" can be used as the first character of a
6272 * dictionary key.
6273 */
6274 int
6275eval_isdictc(int c)
6276{
6277 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278}
6279
6280/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006281 * Handle:
6282 * - expr[expr], expr[expr:expr] subscript
6283 * - ".name" lookup
6284 * - function call with Funcref variable: func(expr)
6285 * - method call: var->method()
6286 *
6287 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006288 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006289 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006290 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006291handle_subscript(
6292 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006293 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006294 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006295 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006296 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006297{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006298 int evaluate = evalarg != NULL
6299 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006300 int ret = OK;
6301 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006302 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006303 int getnext;
6304 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006305
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006306 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006307 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006308 // When at the end of the line and ".name" or "->{" or "->X" follows in
6309 // the next line then consume the line break.
6310 p = eval_next_non_blank(*arg, evalarg, &getnext);
6311 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006312 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006313 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6314 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6315 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006316 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006317 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006318 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006319 check_white = FALSE;
6320 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006321
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006322 if (rettv->v_type == VAR_ANY)
6323 {
6324 char_u *exp_name;
6325 int cc;
6326 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006327 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006328 type_T *type;
6329
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006330 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006331 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006332 if (**arg != '.')
6333 {
6334 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006335 semsg(_(e_expected_dot_after_name_str),
6336 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006337 ret = FAIL;
6338 break;
6339 }
6340 ++*arg;
6341 if (IS_WHITE_OR_NUL(**arg))
6342 {
6343 if (verbose)
6344 emsg(_(e_no_white_space_allowed_after_dot));
6345 ret = FAIL;
6346 break;
6347 }
6348
6349 // isolate the name
6350 exp_name = *arg;
6351 while (eval_isnamec(**arg))
6352 ++*arg;
6353 cc = **arg;
6354 **arg = NUL;
6355
6356 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00006357 evalarg->eval_cctx, evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006358 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006359
6360 if (idx < 0 && ufunc == NULL)
6361 {
6362 ret = FAIL;
6363 break;
6364 }
6365 if (idx >= 0)
6366 {
6367 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6368 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6369
6370 copy_tv(sv->sv_tv, rettv);
6371 }
6372 else
6373 {
6374 rettv->v_type = VAR_FUNC;
6375 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6376 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006377 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006378 }
6379
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006380 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6381 || rettv->v_type == VAR_PARTIAL))
6382 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006383 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006384 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6385 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006386
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006387 // Stop the expression evaluation when immediately aborting on
6388 // error, or when an interrupt occurred or an exception was thrown
6389 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006390 if (aborting())
6391 {
6392 if (ret == OK)
6393 clear_tv(rettv);
6394 ret = FAIL;
6395 }
6396 dict_unref(selfdict);
6397 selfdict = NULL;
6398 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006399 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006400 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006401 if (in_vim9script())
6402 *arg = skipwhite(p + 2);
6403 else
6404 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006405 if (ret == OK)
6406 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006407 if (VIM_ISWHITE(**arg))
6408 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006409 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006410 ret = FAIL;
6411 }
6412 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006413 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006414 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006415 else
6416 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006417 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006418 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006419 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006420 // "." is ".name" lookup when we found a dict or when evaluating and
6421 // scriptversion is at least 2, where string concatenation is "..".
6422 else if (**arg == '['
6423 || (**arg == '.' && (rettv->v_type == VAR_DICT
6424 || (!evaluate
6425 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006426 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006427 {
6428 dict_unref(selfdict);
6429 if (rettv->v_type == VAR_DICT)
6430 {
6431 selfdict = rettv->vval.v_dict;
6432 if (selfdict != NULL)
6433 ++selfdict->dv_refcount;
6434 }
6435 else
6436 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006437 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006438 {
6439 clear_tv(rettv);
6440 ret = FAIL;
6441 }
6442 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006443 else
6444 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006445 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006446
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006447 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6448 // Don't do this when "Func" is already a partial that was bound
6449 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006450 if (selfdict != NULL
6451 && (rettv->v_type == VAR_FUNC
6452 || (rettv->v_type == VAR_PARTIAL
6453 && (rettv->vval.v_partial->pt_auto
6454 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006455 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006456
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006457 dict_unref(selfdict);
6458 return ret;
6459}
6460
6461/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006462 * Make a copy of an item.
6463 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006464 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006465 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6466 * reference to an already copied list/dict can be used.
6467 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006468 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006469 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006470item_copy(
6471 typval_T *from,
6472 typval_T *to,
6473 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006474 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006475 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006476{
6477 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006478 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006479
Bram Moolenaar33570922005-01-25 22:26:29 +00006480 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006481 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006482 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006483 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006484 }
6485 ++recurse;
6486
6487 switch (from->v_type)
6488 {
6489 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006490 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006491 case VAR_STRING:
6492 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006493 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006494 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006495 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006496 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006497 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006498 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006499 copy_tv(from, to);
6500 break;
6501 case VAR_LIST:
6502 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006503 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006504 if (from->vval.v_list == NULL)
6505 to->vval.v_list = NULL;
6506 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6507 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006508 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006509 to->vval.v_list = from->vval.v_list->lv_copylist;
6510 ++to->vval.v_list->lv_refcount;
6511 }
6512 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006513 to->vval.v_list = list_copy(from->vval.v_list,
6514 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006515 if (to->vval.v_list == NULL)
6516 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006517 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006518 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006519 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006520 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006521 case VAR_DICT:
6522 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006523 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006524 if (from->vval.v_dict == NULL)
6525 to->vval.v_dict = NULL;
6526 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6527 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006528 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006529 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6530 ++to->vval.v_dict->dv_refcount;
6531 }
6532 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006533 to->vval.v_dict = dict_copy(from->vval.v_dict,
6534 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006535 if (to->vval.v_dict == NULL)
6536 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006537 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006538 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006539 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006540 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006541 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006542 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006543 }
6544 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006545 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006546}
6547
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006548 void
6549echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6550{
6551 char_u *tofree;
6552 char_u numbuf[NUMBUFLEN];
6553 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6554
6555 if (*atstart)
6556 {
6557 *atstart = FALSE;
6558 // Call msg_start() after eval1(), evaluating the expression
6559 // may cause a message to appear.
6560 if (with_space)
6561 {
6562 // Mark the saved text as finishing the line, so that what
6563 // follows is displayed on a new line when scrolling back
6564 // at the more prompt.
6565 msg_sb_eol();
6566 msg_start();
6567 }
6568 }
6569 else if (with_space)
6570 msg_puts_attr(" ", echo_attr);
6571
6572 if (p != NULL)
6573 for ( ; *p != NUL && !got_int; ++p)
6574 {
6575 if (*p == '\n' || *p == '\r' || *p == TAB)
6576 {
6577 if (*p != TAB && *needclr)
6578 {
6579 // remove any text still there from the command
6580 msg_clr_eos();
6581 *needclr = FALSE;
6582 }
6583 msg_putchar_attr(*p, echo_attr);
6584 }
6585 else
6586 {
6587 if (has_mbyte)
6588 {
6589 int i = (*mb_ptr2len)(p);
6590
6591 (void)msg_outtrans_len_attr(p, i, echo_attr);
6592 p += i - 1;
6593 }
6594 else
6595 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6596 }
6597 }
6598 vim_free(tofree);
6599}
6600
Bram Moolenaare9a41262005-01-15 22:18:47 +00006601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 * ":echo expr1 ..." print each argument separated with a space, add a
6603 * newline at the end.
6604 * ":echon expr1 ..." print each argument plain.
6605 */
6606 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006607ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608{
6609 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006610 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006611 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 int needclr = TRUE;
6613 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006614 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006615 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006616 evalarg_T evalarg;
6617
Bram Moolenaare707c882020-07-01 13:07:37 +02006618 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619
6620 if (eap->skip)
6621 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006622 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006624 // If eval1() causes an error message the text from the command may
6625 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006626 need_clr_eos = needclr;
6627
Bram Moolenaar68db9962021-05-09 23:19:22 +02006628 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006629 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 {
6631 /*
6632 * Report the invalid expression unless the expression evaluation
6633 * has been cancelled due to an aborting error, an interrupt, or an
6634 * exception.
6635 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006636 if (!aborting() && did_emsg == did_emsg_before
6637 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006638 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006639 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 break;
6641 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006642 need_clr_eos = FALSE;
6643
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006645 {
6646 if (rettv.v_type == VAR_VOID)
6647 {
6648 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6649 break;
6650 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006651 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006652 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006653
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006654 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 arg = skipwhite(arg);
6656 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006657 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006658 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659
6660 if (eap->skip)
6661 --emsg_skip;
6662 else
6663 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006664 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 if (needclr)
6666 msg_clr_eos();
6667 if (eap->cmdidx == CMD_echo)
6668 msg_end();
6669 }
6670}
6671
6672/*
6673 * ":echohl {name}".
6674 */
6675 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006676ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006678 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679}
6680
6681/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006682 * Returns the :echo attribute
6683 */
6684 int
6685get_echo_attr(void)
6686{
6687 return echo_attr;
6688}
6689
6690/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 * ":execute expr1 ..." execute the result of an expression.
6692 * ":echomsg expr1 ..." Print a message
6693 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006694 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 * Each gets spaces around each argument and a newline at the end for
6696 * echo commands
6697 */
6698 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006699ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700{
6701 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006702 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 int ret = OK;
6704 char_u *p;
6705 garray_T ga;
6706 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006707 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708
6709 ga_init2(&ga, 1, 80);
6710
6711 if (eap->skip)
6712 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006713 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006715 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006716 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718
6719 if (!eap->skip)
6720 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006721 char_u buf[NUMBUFLEN];
6722
6723 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006724 {
6725 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6726 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006727 semsg(_(e_using_invalid_value_as_string_str),
6728 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006729 p = NULL;
6730 }
6731 else
6732 p = tv_get_string_buf(&rettv, buf);
6733 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006734 else
6735 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006736 if (p == NULL)
6737 {
6738 clear_tv(&rettv);
6739 ret = FAIL;
6740 break;
6741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 len = (int)STRLEN(p);
6743 if (ga_grow(&ga, len + 2) == FAIL)
6744 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006745 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 ret = FAIL;
6747 break;
6748 }
6749 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 ga.ga_len += len;
6753 }
6754
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006755 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 arg = skipwhite(arg);
6757 }
6758
6759 if (ret != FAIL && ga.ga_data != NULL)
6760 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006761 // use the first line of continuation lines for messages
6762 SOURCING_LNUM = start_lnum;
6763
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006764 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6765 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006766 // Mark the already saved text as finishing the line, so that what
6767 // follows is displayed on a new line when scrolling back at the
6768 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006769 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006770 }
6771
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006773 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006774 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006775 out_flush();
6776 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006777 else if (eap->cmdidx == CMD_echoconsole)
6778 {
6779 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6780 ui_write((char_u *)"\r\n", 2, TRUE);
6781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 else if (eap->cmdidx == CMD_echoerr)
6783 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006784 int save_did_emsg = did_emsg;
6785
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006786 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006787 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 if (!force_abort)
6789 did_emsg = save_did_emsg;
6790 }
6791 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006792 {
6793 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6794
6795 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6796 sticky_cmdmod_flags = cmdmod.cmod_flags
6797 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 do_cmdline((char_u *)ga.ga_data,
6799 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006800 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 }
6803
6804 ga_clear(&ga);
6805
6806 if (eap->skip)
6807 --emsg_skip;
6808
Bram Moolenaar63b91732021-08-05 20:40:03 +02006809 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810}
6811
6812/*
6813 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6814 * "arg" points to the "&" or '+' when called, to "option" when returning.
6815 * Returns NULL when no option name found. Otherwise pointer to the char
6816 * after the option name.
6817 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006818 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006819find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820{
6821 char_u *p = *arg;
6822
6823 ++p;
6824 if (*p == 'g' && p[1] == ':')
6825 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006826 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 p += 2;
6828 }
6829 else if (*p == 'l' && p[1] == ':')
6830 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006831 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 p += 2;
6833 }
6834 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006835 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836
6837 if (!ASCII_ISALPHA(*p))
6838 return NULL;
6839 *arg = p;
6840
6841 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006842 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 else
6844 while (ASCII_ISALPHA(*p))
6845 ++p;
6846 return p;
6847}
6848
6849/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006850 * Display script name where an item was last set.
6851 * Should only be invoked when 'verbose' is non-zero.
6852 */
6853 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006854last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006855{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006856 char_u *p;
6857
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006858 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006859 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006860 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006861 if (p != NULL)
6862 {
6863 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006864 msg_puts(_("\n\tLast set from "));
6865 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006866 if (script_ctx.sc_lnum > 0)
6867 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006868 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006869 msg_outnum((long)script_ctx.sc_lnum);
6870 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006871 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006872 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006873 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006874 }
6875}
6876
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006877#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878
6879/*
6880 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006881 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 * "flags" can be "g" to do a global substitute.
6883 * Returns an allocated string, NULL for error.
6884 */
6885 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006886do_string_sub(
6887 char_u *str,
6888 char_u *pat,
6889 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006890 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006891 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892{
6893 int sublen;
6894 regmatch_T regmatch;
6895 int i;
6896 int do_all;
6897 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006898 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 garray_T ga;
6900 char_u *ret;
6901 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006902 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006904 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006906 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907
6908 ga_init2(&ga, 1, 200);
6909
6910 do_all = (flags[0] == 'g');
6911
6912 regmatch.rm_ic = p_ic;
6913 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6914 if (regmatch.regprog != NULL)
6915 {
6916 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006917 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6919 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006920 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006921 if (regmatch.startp[0] == regmatch.endp[0])
6922 {
6923 if (zero_width == regmatch.startp[0])
6924 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006925 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006926 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006927 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6928 (size_t)i);
6929 ga.ga_len += i;
6930 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006931 continue;
6932 }
6933 zero_width = regmatch.startp[0];
6934 }
6935
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 /*
6937 * Get some space for a temporary buffer to do the substitution
6938 * into. It will contain:
6939 * - The text up to where the match is.
6940 * - The substituted text.
6941 * - The text after the match.
6942 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01006943 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006944 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6946 {
6947 ga_clear(&ga);
6948 break;
6949 }
6950
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006951 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 i = (int)(regmatch.startp[0] - tail);
6953 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006954 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01006955 (void)vim_regsub(&regmatch, sub, expr,
6956 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
6957 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006959 tail = regmatch.endp[0];
6960 if (*tail == NUL)
6961 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 if (!do_all)
6963 break;
6964 }
6965
6966 if (ga.ga_data != NULL)
6967 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6968
Bram Moolenaar473de612013-06-08 18:19:48 +02006969 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 }
6971
6972 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6973 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006974 if (p_cpo == empty_option)
6975 p_cpo = save_cpo;
6976 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006977 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006978 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006979 // If it's still empty it was changed and restored, need to restore in
6980 // the complicated way.
6981 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01006982 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006983 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985
6986 return ret;
6987}