blob: 60daca51ce9de279ceb77a8bcf7a42c30b862c54 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar071d4272004-06-13 20:20:40 +000032/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000033 * Info used by a ":for" loop.
34 */
Bram Moolenaar33570922005-01-25 22:26:29 +000035typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000036{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010037 int fi_semicolon; // TRUE if ending in '; var]'
38 int fi_varcount; // nr of variables in the list
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020039 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010040 listwatch_T fi_lw; // keep an eye on the item used.
41 list_T *fi_list; // list being used
42 int fi_bi; // index of blob
43 blob_T *fi_blob; // blob being used
Bram Moolenaar74e54fc2021-03-26 20:41:29 +010044 char_u *fi_string; // copy of string being used
45 int fi_byte_idx; // byte index in fi_string
Bram Moolenaar33570922005-01-25 22:26:29 +000046} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000047
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020048static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
49static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010052static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020053static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010054static int eval8(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
55static int eval9(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
56static int eval9_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000057
Bram Moolenaar48e697e2016-01-23 22:17:30 +010058static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020059static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020060
Bram Moolenaare21c1582019-03-02 11:57:09 +010061/*
62 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010063 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010064 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020065 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010066num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010067{
68 varnumber_T result;
69
Bram Moolenaar99880f92021-01-20 21:23:14 +010070 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010071 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010072 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010073 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010074 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010075 if (failed != NULL)
76 *failed = TRUE;
77 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010078 if (n1 == 0)
79 result = VARNUM_MIN; // similar to NaN
80 else if (n1 < 0)
81 result = -VARNUM_MAX;
82 else
83 result = VARNUM_MAX;
84 }
85 else
86 result = n1 / n2;
87
88 return result;
89}
90
91/*
92 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010093 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010094 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020095 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010096num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010097{
Bram Moolenaar99880f92021-01-20 21:23:14 +010098 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010099 {
Bram Moolenaar99880f92021-01-20 21:23:14 +0100100 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +0100101 if (failed != NULL)
102 *failed = TRUE;
103 }
Bram Moolenaare21c1582019-03-02 11:57:09 +0100104 return (n2 == 0) ? 0 : (n1 % n2);
105}
106
Bram Moolenaar33570922005-01-25 22:26:29 +0000107/*
108 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000109 */
110 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100111eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000112{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200113 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200114 func_init();
Bram Moolenaara7043832005-01-21 11:56:39 +0000115}
116
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000117#if defined(EXITFREE) || defined(PROTO)
118 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100119eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000120{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200121 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100122 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200123 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000124
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200125 // autoloaded script names
126 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000127
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200128 // unreferenced lists and dicts
129 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200130
131 // functions not garbage collected
132 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000133}
134#endif
135
Bram Moolenaare6b53242020-07-01 17:28:33 +0200136 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200137fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
138{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100139 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200140 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200141 if (eap != NULL)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200142 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200143 evalarg->eval_cstack = eap->cstack;
Bram Moolenaara7583c42022-05-07 21:14:05 +0100144 if (sourcing_a_script(eap) || eap->getline == get_list_line)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200145 {
146 evalarg->eval_getline = eap->getline;
147 evalarg->eval_cookie = eap->cookie;
148 }
Bram Moolenaar37c83712020-06-30 21:18:36 +0200149 }
150}
151
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152/*
153 * Top level evaluation function, returning a boolean.
154 * Sets "error" to TRUE if there was an error.
155 * Return TRUE or FALSE.
156 */
157 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100158eval_to_bool(
159 char_u *arg,
160 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200161 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100162 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163{
Bram Moolenaar33570922005-01-25 22:26:29 +0000164 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200165 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200166 evalarg_T evalarg;
167
Bram Moolenaar37c83712020-06-30 21:18:36 +0200168 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169
170 if (skip)
171 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200172 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 else
175 {
176 *error = FALSE;
177 if (!skip)
178 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200179 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200180 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200181 else
182 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000183 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 }
185 }
186 if (skip)
187 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200188 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200190 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191}
192
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100193/*
194 * Call eval1() and give an error message if not done at a lower level.
195 */
196 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200197eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100198{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100199 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100200 int ret;
201 int did_emsg_before = did_emsg;
202 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200203 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100204
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200205 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
206
207 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100208 if (ret == FAIL)
209 {
210 // Report the invalid expression unless the expression evaluation has
211 // been cancelled due to an aborting error, an interrupt, or an
212 // exception, or we already gave a more specific error.
213 // Also check called_emsg for when using assert_fails().
214 if (!aborting() && did_emsg == did_emsg_before
215 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200216 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100217 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200218 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100219 return ret;
220}
221
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100222/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200223 * Return whether a typval is a valid expression to pass to eval_expr_typval()
224 * or eval_expr_to_bool(). An empty string returns FALSE;
225 */
226 int
227eval_expr_valid_arg(typval_T *tv)
228{
229 return tv->v_type != VAR_UNKNOWN
230 && (tv->v_type != VAR_STRING
231 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
232}
233
234/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100235 * Evaluate an expression, which can be a function, partial or string.
236 * Pass arguments "argv[argc]".
237 * Return the result in "rettv" and OK or FAIL.
238 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200239 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100240eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
241{
242 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100243 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200244 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100245
246 if (expr->v_type == VAR_FUNC)
247 {
248 s = expr->vval.v_string;
249 if (s == NULL || *s == NUL)
250 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200251 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000252 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200253 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100254 return FAIL;
255 }
256 else if (expr->v_type == VAR_PARTIAL)
257 {
258 partial_T *partial = expr->vval.v_partial;
259
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200260 if (partial == NULL)
261 return FAIL;
262
Bram Moolenaar822ba242020-05-24 23:00:18 +0200263 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200264 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100265 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200266 if (call_def_function(partial->pt_func, argc, argv,
267 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100268 return FAIL;
269 }
270 else
271 {
272 s = partial_name(partial);
273 if (s == NULL || *s == NUL)
274 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200275 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000276 funcexe.fe_evaluate = TRUE;
277 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
279 return FAIL;
280 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100281 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200282 else if (expr->v_type == VAR_INSTR)
283 {
284 return exe_typval_instr(expr, rettv);
285 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100286 else
287 {
Bram Moolenaarfc4c4482022-01-26 21:17:04 +0000288 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
Bram Moolenaar48570482017-10-30 21:48:41 +0100289 if (s == NULL)
290 return FAIL;
291 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200292 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100293 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200294 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100295 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200296 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200297 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100298 return FAIL;
299 }
300 }
301 return OK;
302}
303
304/*
305 * Like eval_to_bool() but using a typval_T instead of a string.
306 * Works for string, funcref and partial.
307 */
308 int
309eval_expr_to_bool(typval_T *expr, int *error)
310{
311 typval_T rettv;
312 int res;
313
314 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
315 {
316 *error = TRUE;
317 return FALSE;
318 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200319 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100320 clear_tv(&rettv);
321 return res;
322}
323
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324/*
325 * Top level evaluation function, returning a string. If "skip" is TRUE,
326 * only parsing to "nextcmd" is done, without reporting errors. Return
327 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
328 */
329 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100330eval_to_string_skip(
331 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200332 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100333 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334{
Bram Moolenaar33570922005-01-25 22:26:29 +0000335 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200337 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338
Bram Moolenaar37c83712020-06-30 21:18:36 +0200339 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 if (skip)
341 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200342 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 retval = NULL;
344 else
345 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100346 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000347 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 }
349 if (skip)
350 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200351 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352
353 return retval;
354}
355
356/*
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100357 * Initialize "evalarg" for use.
358 */
359 void
360init_evalarg(evalarg_T *evalarg)
361{
362 CLEAR_POINTER(evalarg);
363 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
364}
365
366/*
367 * If "evalarg->eval_tofree" is not NULL free it later.
368 * Caller is expected to overwrite "evalarg->eval_tofree" next.
369 */
370 static void
371free_eval_tofree_later(evalarg_T *evalarg)
372{
373 if (evalarg->eval_tofree != NULL)
374 {
375 if (ga_grow(&evalarg->eval_tofree_ga, 1) == OK)
376 ((char_u **)evalarg->eval_tofree_ga.ga_data)
377 [evalarg->eval_tofree_ga.ga_len++]
378 = evalarg->eval_tofree;
379 else
380 vim_free(evalarg->eval_tofree);
381 }
382}
383
384/*
385 * After using "evalarg" filled from "eap": free the memory.
386 */
387 void
388clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
389{
390 if (evalarg != NULL)
391 {
392 if (evalarg->eval_tofree != NULL)
393 {
394 if (eap != NULL)
395 {
396 // We may need to keep the original command line, e.g. for
397 // ":let" it has the variable names. But we may also need the
398 // new one, "nextcmd" points into it. Keep both.
399 vim_free(eap->cmdline_tofree);
400 eap->cmdline_tofree = *eap->cmdlinep;
401 *eap->cmdlinep = evalarg->eval_tofree;
402 }
403 else
404 vim_free(evalarg->eval_tofree);
405 evalarg->eval_tofree = NULL;
406 }
407
408 ga_clear_strings(&evalarg->eval_tofree_ga);
409 VIM_CLEAR(evalarg->eval_tofree_lambda);
410 }
411}
412
413/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000414 * Skip over an expression at "*pp".
415 * Return FAIL for an error, OK otherwise.
416 */
417 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200418skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000419{
Bram Moolenaar33570922005-01-25 22:26:29 +0000420 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000421
422 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200423 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000424}
425
426/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200427 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200428 * If in Vim9 script and line breaks are encountered, the lines are
429 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200430 * "arg" is advanced to just after the expression.
431 * "start" is set to the start of the expression, "end" to just after the end.
432 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200433 * Return FAIL for an error, OK otherwise.
434 */
435 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200436skip_expr_concatenate(
437 char_u **arg,
438 char_u **start,
439 char_u **end,
440 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200441{
442 typval_T rettv;
443 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200444 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200445 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200446 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200447 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200448 int evaluate = evalarg == NULL
449 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200450
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200451 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200452 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200453 {
454 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200455 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200456 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200457 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200458 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200459 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200460 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200461
462 // Don't evaluate the expression.
463 if (evalarg != NULL)
464 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200465 *arg = skipwhite(*arg);
466 res = eval1(arg, &rettv, evalarg);
467 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200468 if (evalarg != NULL)
469 evalarg->eval_flags = save_flags;
470
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200471 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200472 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200473 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200474 if (evalarg->eval_ga.ga_len == 1)
475 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200476 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200477 ga_clear(gap);
478 gap->ga_itemsize = 0;
479 }
480 else
481 {
482 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200483 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200484
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200485 // Line breaks encountered, concatenate all the lines.
486 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200487 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200488
489 // free the lines only when using getsourceline()
490 if (evalarg->eval_cookie != NULL)
491 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200492 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200493 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200494 // Do not free the last line, "arg" points into it, free it
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100495 // later. Also free "eval_tofree" later if needed.
496 free_eval_tofree_later(evalarg);
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200497 evalarg->eval_tofree =
498 ((char_u **)gap->ga_data)[gap->ga_len - 1];
499 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200500 ga_clear_strings(gap);
501 }
502 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200503 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200504 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200505
506 // free lines that were explicitly marked for freeing
507 ga_clear_strings(freegap);
508 }
509
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200510 gap->ga_itemsize = 0;
511 if (p == NULL)
512 return FAIL;
513 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200514 vim_free(evalarg->eval_tofree_lambda);
515 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200516 // Compute "end" relative to the end.
517 *end = *start + STRLEN(*start) - endoff;
518 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200519 }
520
521 return res;
522}
523
524/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100525 * Convert "tv" to a string.
526 * When "convert" is TRUE convert a List into a sequence of lines and convert
527 * a Float to a String.
528 * Returns an allocated string (NULL when out of memory).
529 */
530 char_u *
531typval2string(typval_T *tv, int convert)
532{
533 garray_T ga;
534 char_u *retval;
535#ifdef FEAT_FLOAT
536 char_u numbuf[NUMBUFLEN];
537#endif
538
539 if (convert && tv->v_type == VAR_LIST)
540 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000541 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100542 if (tv->vval.v_list != NULL)
543 {
544 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
545 if (tv->vval.v_list->lv_len > 0)
546 ga_append(&ga, NL);
547 }
548 ga_append(&ga, NUL);
549 retval = (char_u *)ga.ga_data;
550 }
551#ifdef FEAT_FLOAT
552 else if (convert && tv->v_type == VAR_FLOAT)
553 {
554 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
555 retval = vim_strsave(numbuf);
556 }
557#endif
558 else
559 retval = vim_strsave(tv_get_string(tv));
560 return retval;
561}
562
563/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200564 * Top level evaluation function, returning a string. Does not handle line
565 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000566 * When "convert" is TRUE convert a List into a sequence of lines and convert
567 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 * Return pointer to allocated memory, or NULL for failure.
569 */
570 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100571eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100572 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100573 int convert,
574 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575{
Bram Moolenaar33570922005-01-25 22:26:29 +0000576 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100578 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100580 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
581 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 retval = NULL;
583 else
584 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100585 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000586 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100588 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589
590 return retval;
591}
592
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100593 char_u *
594eval_to_string(
595 char_u *arg,
596 int convert)
597{
598 return eval_to_string_eap(arg, convert, NULL);
599}
600
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000602 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100603 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200604 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 */
606 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100607eval_to_string_safe(
608 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000609 int use_sandbox,
610 int keep_script_version)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611{
612 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200613 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200614 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200615 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616
Bram Moolenaar9530b582022-01-22 13:39:08 +0000617 if (!keep_script_version)
618 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200619 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000620 if (use_sandbox)
621 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100622 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200623 may_garbage_collect = FALSE;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200624 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000625 if (use_sandbox)
626 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100627 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200628 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200629 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200630 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 return retval;
632}
633
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634/*
635 * Top level evaluation function, returning a number.
636 * Evaluates "expr" silently.
637 * Returns -1 for an error.
638 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200639 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100640eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
Bram Moolenaar33570922005-01-25 22:26:29 +0000642 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200643 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000644 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645
646 ++emsg_off;
647
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200648 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 retval = -1;
650 else
651 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100652 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000653 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 }
655 --emsg_off;
656
657 return retval;
658}
659
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000660/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000661 * Top level evaluation function.
662 * Returns an allocated typval_T with the result.
663 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000664 */
665 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200666eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000667{
668 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200669 evalarg_T evalarg;
670
671 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000672
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200673 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200674 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100675 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000676
Bram Moolenaar37c83712020-06-30 21:18:36 +0200677 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000678 return tv;
679}
680
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000682 * "*arg" points to what can be a function name in the form of "import.Name" or
683 * "Funcref". Return the name of the function. Set "tofree" to something that
684 * was allocated.
685 * If "verbose" is FALSE no errors are given.
686 * Return NULL for any failure.
687 */
688 static char_u *
689deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000690 char_u **arg,
691 char_u **tofree,
692 evalarg_T *evalarg,
693 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000694{
695 typval_T ref;
696 char_u *name = *arg;
697
698 ref.v_type = VAR_UNKNOWN;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100699 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100700 {
701 dictitem_T *v;
702
703 // If <SID>VarName was used it would not be found, try another way.
704 v = find_var_also_in_script(name, NULL, FALSE);
705 if (v == NULL)
706 return NULL;
707 copy_tv(&v->di_tv, &ref);
708 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000709 if (*skipwhite(*arg) != NUL)
710 {
711 if (verbose)
712 semsg(_(e_trailing_characters_str), *arg);
713 name = NULL;
714 }
715 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
716 {
717 name = ref.vval.v_string;
718 ref.vval.v_string = NULL;
719 *tofree = name;
720 }
721 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
722 {
723 if (ref.vval.v_partial->pt_argc > 0
724 || ref.vval.v_partial->pt_dict != NULL)
725 {
726 if (verbose)
727 emsg(_(e_cannot_use_partial_here));
728 name = NULL;
729 }
730 else
731 {
732 name = vim_strsave(partial_name(ref.vval.v_partial));
733 *tofree = name;
734 }
735 }
736 else
737 {
738 if (verbose)
739 semsg(_(e_not_callable_type_str), name);
740 name = NULL;
741 }
742 clear_tv(&ref);
743 return name;
744}
745
746/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100747 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200748 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
749 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000750 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200752 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100753call_vim_function(
754 char_u *func,
755 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200756 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200757 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000759 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200760 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000761 char_u *arg;
762 char_u *name;
763 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000764 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100766 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200767 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000768 funcexe.fe_firstline = curwin->w_cursor.lnum;
769 funcexe.fe_lastline = curwin->w_cursor.lnum;
770 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000771
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000772 // The name might be "import.Func" or "Funcref". We don't know, we need to
773 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000774 // autoload script has errors. Guess that when there is a dot in the name
775 // showing errors is the right choice.
776 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000777 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000778 if (ignore_errors)
779 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000780 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000781 if (ignore_errors)
782 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000783 if (name == NULL)
784 name = func;
785
786 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
787
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000788 if (ret == FAIL)
789 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000790 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000791
792 return ret;
793}
794
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100795/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100796 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000797 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
798 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000799 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000800 */
801 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100802call_func_retstr(
803 char_u *func,
804 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200805 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000806{
807 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000808 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000809
Bram Moolenaarded27a12018-08-01 19:06:03 +0200810 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000811 return NULL;
812
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100813 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000814 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 return retval;
816}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000817
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000818/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100819 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000820 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000821 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000822 */
823 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100824call_func_retlist(
825 char_u *func,
826 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200827 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000828{
829 typval_T rettv;
830
Bram Moolenaarded27a12018-08-01 19:06:03 +0200831 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000832 return NULL;
833
834 if (rettv.v_type != VAR_LIST)
835 {
836 clear_tv(&rettv);
837 return NULL;
838 }
839
840 return rettv.vval.v_list;
841}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842
Bram Moolenaare70dd112022-01-21 16:31:11 +0000843#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100845 * Evaluate "arg", which is 'foldexpr'.
846 * Note: caller must set "curwin" to match "arg".
847 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
848 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 */
850 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000851eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000853 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000854 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200855 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000857 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000858 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
859 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860
Bram Moolenaare70dd112022-01-21 16:31:11 +0000861 arg = wp->w_p_fde;
862 current_sctx = wp->w_p_script_ctx[WV_FDE];
863
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000865 if (use_sandbox)
866 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100867 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200869 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 retval = 0;
871 else
872 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100873 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000874 if (tv.v_type == VAR_NUMBER)
875 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000876 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 retval = 0;
878 else
879 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100880 // If the result is a string, check if there is a non-digit before
881 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000882 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 if (!VIM_ISDIGIT(*s) && *s != '-')
884 *cp = *s++;
885 retval = atol((char *)s);
886 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000887 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 }
889 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000890 if (use_sandbox)
891 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100892 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200893 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000894 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200896 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897}
898#endif
899
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000901 * Get an lval: variable, Dict item or List item that can be assigned a value
902 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
903 * "name.key", "name.key[expr]" etc.
904 * Indexing only works if "name" is an existing List or Dictionary.
905 * "name" points to the start of the name.
906 * If "rettv" is not NULL it points to the value to be assigned.
907 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
908 * wrong; must end in space or cmd separator.
909 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100910 * flags:
911 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100912 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100913 * GLV_NO_AUTOLOAD: do not use script autoloading
914 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000915 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000916 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000917 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000918 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200919 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100920get_lval(
921 char_u *name,
922 typval_T *rettv,
923 lval_T *lp,
924 int unlet,
925 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100926 int flags, // GLV_ values
927 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000928{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000929 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000930 char_u *expr_start, *expr_end;
931 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000932 dictitem_T *v;
933 typval_T var1;
934 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000935 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000936 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000937 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100938 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100939 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100940 int writing;
Bram Moolenaar4525a572022-02-13 11:57:33 +0000941 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000942
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100943 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200944 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000945
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100946 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000947 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100948 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000949 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200950 lp->ll_name_end = find_name_end(name, NULL, NULL,
951 FNE_INCL_BR | fne_flags);
952 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000953 }
954
Bram Moolenaara749a422022-02-12 19:52:25 +0000955 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +0000956 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +0000957 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
958 {
959 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
960 return NULL;
961 }
962
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100963 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000964 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100965 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000966 if (expr_start != NULL)
967 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100968 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100969 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000970 && *p != '[' && *p != '.')
971 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000972 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000973 return NULL;
974 }
975
976 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
977 if (lp->ll_exp_name == NULL)
978 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100979 // Report an invalid expression in braces, unless the
980 // expression evaluation has been cancelled due to an
981 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000982 if (!aborting() && !quiet)
983 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000984 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000985 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000986 return NULL;
987 }
988 }
989 lp->ll_name = lp->ll_exp_name;
990 }
991 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100992 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000993 lp->ll_name = name;
994
Bram Moolenaar4525a572022-02-13 11:57:33 +0000995 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100996 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200997 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +0000998 // However, "g:[key]" is indexing a dictionary.
999 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001000 {
1001 --p;
1002 lp->ll_name_end = p;
1003 }
1004 if (*p == ':')
1005 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001006 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001007
1008 if (tp == p + 1 && !quiet)
1009 {
1010 semsg(_(e_white_space_required_after_str_str), ":", p);
1011 return NULL;
1012 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001013
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001014 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001015 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001016 semsg(_(e_using_type_not_in_script_context_str), p);
1017 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001018 }
1019
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001020 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001021 lp->ll_type = parse_type(&tp,
1022 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1023 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001024 if (lp->ll_type == NULL && !quiet)
1025 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001026 lp->ll_name_end = tp;
1027 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001028 }
1029 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001030 if (lp->ll_name == NULL)
1031 return p;
1032
Bram Moolenaar62aec932022-01-29 21:45:34 +00001033 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001034 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001035 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001036
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001037 if (import != NULL)
1038 {
1039 ufunc_T *ufunc;
1040 type_T *type;
1041
1042 lp->ll_sid = import->imp_sid;
1043 lp->ll_name = skipwhite(p + 1);
1044 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1045 lp->ll_name_end = p;
1046
1047 // check the item is exported
1048 cc = *p;
1049 *p = NUL;
1050 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001051 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001052 {
1053 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001054 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001055 }
1056 *p = cc;
1057 }
1058 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001060 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001061 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001062 return p;
1063
Bram Moolenaar4525a572022-02-13 11:57:33 +00001064 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001065 {
1066 // using local variable
1067 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001068 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001069 }
1070 else
1071 {
1072 cc = *p;
1073 *p = NUL;
1074 // When we would write to the variable pass &ht and prevent autoload.
1075 writing = !(flags & GLV_READ_ONLY);
1076 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001077 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001078 if (v == NULL && !quiet)
1079 semsg(_(e_undefined_variable_str), lp->ll_name);
1080 *p = cc;
1081 if (v == NULL)
1082 return NULL;
1083 lp->ll_tv = &v->di_tv;
1084 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001085
Bram Moolenaar4525a572022-02-13 11:57:33 +00001086 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001087 {
1088 if (!quiet)
1089 semsg(_(e_variable_already_declared), lp->ll_name);
1090 return NULL;
1091 }
1092
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001093 /*
1094 * Loop until no more [idx] or .key is following.
1095 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001096 var1.v_type = VAR_UNKNOWN;
1097 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001098 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001099 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001100 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1101 {
1102 if (!quiet)
1103 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1104 return NULL;
1105 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001106 if (lp->ll_tv->v_type != VAR_LIST
1107 && lp->ll_tv->v_type != VAR_DICT
1108 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001109 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001110 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001111 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001112 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001113 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001114
1115 // a NULL list/blob works like an empty list/blob, allocate one now.
1116 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1117 rettv_list_alloc(lp->ll_tv);
1118 else if (lp->ll_tv->v_type == VAR_BLOB
1119 && lp->ll_tv->vval.v_blob == NULL)
1120 rettv_blob_alloc(lp->ll_tv);
1121
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001122 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001123 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001124 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001125 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001126 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001127 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001128
Bram Moolenaar4525a572022-02-13 11:57:33 +00001129 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001130 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001131 && lp->ll_tv == &v->di_tv
1132 && ht != NULL && ht == get_script_local_ht())
1133 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001134 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001135
1136 // Vim9 script local variable: get the type
1137 if (sv != NULL)
1138 lp->ll_valtype = sv->sv_type;
1139 }
1140
Bram Moolenaar8c711452005-01-14 21:53:12 +00001141 len = -1;
1142 if (*p == '.')
1143 {
1144 key = p + 1;
1145 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1146 ;
1147 if (len == 0)
1148 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001149 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001150 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001151 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001152 }
1153 p = key + len;
1154 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001155 else
1156 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001157 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001158 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001159 if (*p == ':')
1160 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001161 else
1162 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001163 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001164 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001165 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001166 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001167 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001168 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001169 clear_tv(&var1);
1170 return NULL;
1171 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001172 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001173 }
1174
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001175 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001176 if (*p == ':')
1177 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001178 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001179 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001180 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001181 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001182 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001183 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001184 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001185 if (rettv != NULL
1186 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001187 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001188 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001189 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001190 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001191 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001192 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001193 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001194 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001195 }
1196 p = skipwhite(p + 1);
1197 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001198 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001199 else
1200 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001201 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001202 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001203 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001204 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001205 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001206 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001207 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001208 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001209 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001210 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001211 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001212 clear_tv(&var2);
1213 return NULL;
1214 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001215 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001216 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001217 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001218 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001219 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001220
Bram Moolenaar8c711452005-01-14 21:53:12 +00001221 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001222 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001223 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001224 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001225 clear_tv(&var1);
1226 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001227 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001228 }
1229
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001230 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001231 ++p;
1232 }
1233
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001234 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001235 {
1236 if (len == -1)
1237 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001238 // "[key]": get key from "var1"
1239 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001240 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001241 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001242 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001243 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001244 }
1245 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001246 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001247
1248 // a NULL dict is equivalent with an empty dict
1249 if (lp->ll_tv->vval.v_dict == NULL)
1250 {
1251 lp->ll_tv->vval.v_dict = dict_alloc();
1252 if (lp->ll_tv->vval.v_dict == NULL)
1253 {
1254 clear_tv(&var1);
1255 return NULL;
1256 }
1257 ++lp->ll_tv->vval.v_dict->dv_refcount;
1258 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001259 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001260
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001261 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001262
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001263 // When assigning to a scope dictionary check that a function and
1264 // variable name is valid (only variable name unless it is l: or
1265 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001266 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001267 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001268 int prevval;
1269 int wrong;
1270
1271 if (len != -1)
1272 {
1273 prevval = key[len];
1274 key[len] = NUL;
1275 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001276 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001277 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001278 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1279 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001280 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001281 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001282 if (len != -1)
1283 key[len] = prevval;
1284 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001285 {
1286 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001287 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001288 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001289 }
1290
Bram Moolenaar10c65862020-10-08 21:16:42 +02001291 if (lp->ll_valtype != NULL)
1292 // use the type of the member
1293 lp->ll_valtype = lp->ll_valtype->tt_member;
1294
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001295 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001296 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001297 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001298 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001299 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001300 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001301 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001302 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001303 return NULL;
1304 }
1305
Bram Moolenaar31b81602019-02-10 22:14:27 +01001306 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001307 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001308 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001309 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001310 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001311 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001312 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001313 }
1314 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001315 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001316 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001317 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001318 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001319 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001320 p = NULL;
1321 break;
1322 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001323 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001324 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001325 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1326 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001327 {
1328 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001329 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001330 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001331
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001332 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001333 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001334 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001335 else if (lp->ll_tv->v_type == VAR_BLOB)
1336 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001337 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1338
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001339 /*
1340 * Get the number and item for the only or first index of the List.
1341 */
1342 if (empty1)
1343 lp->ll_n1 = 0;
1344 else
1345 // is number or string
1346 lp->ll_n1 = (long)tv_get_number(&var1);
1347 clear_tv(&var1);
1348
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001349 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001350 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001351 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001352 return NULL;
1353 }
1354 if (lp->ll_range && !lp->ll_empty2)
1355 {
1356 lp->ll_n2 = (long)tv_get_number(&var2);
1357 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001358 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1359 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001360 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001361 }
1362 lp->ll_blob = lp->ll_tv->vval.v_blob;
1363 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001364 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001365 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001366 else
1367 {
1368 /*
1369 * Get the number and item for the only or first index of the List.
1370 */
1371 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001372 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001373 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001374 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001375 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001376 clear_tv(&var1);
1377
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001378 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001379 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001380 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1381 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001382 if (lp->ll_li == NULL)
1383 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001384 clear_tv(&var2);
1385 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001386 }
1387
Bram Moolenaar10c65862020-10-08 21:16:42 +02001388 if (lp->ll_valtype != NULL)
1389 // use the type of the member
1390 lp->ll_valtype = lp->ll_valtype->tt_member;
1391
Bram Moolenaar8c711452005-01-14 21:53:12 +00001392 /*
1393 * May need to find the item or absolute index for the second
1394 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001395 * When no index given: "lp->ll_empty2" is TRUE.
1396 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001397 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001398 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001399 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001400 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001401 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001402 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001403 if (check_range_index_two(lp->ll_list,
1404 &lp->ll_n1, lp->ll_li,
1405 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001406 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001407 }
1408
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001409 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001410 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001411 }
1412
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001413 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001414 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001415 return p;
1416}
1417
1418/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001419 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001420 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001421 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001422clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001423{
1424 vim_free(lp->ll_exp_name);
1425 vim_free(lp->ll_newkey);
1426}
1427
1428/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001429 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001430 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001431 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1432 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001433 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001434 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001435set_var_lval(
1436 lval_T *lp,
1437 char_u *endp,
1438 typval_T *rettv,
1439 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001440 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1441 char_u *op,
1442 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001443{
1444 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001445 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001446
1447 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001448 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001449 cc = *endp;
1450 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001451 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1452 return;
1453
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001454 if (lp->ll_blob != NULL)
1455 {
1456 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001457
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001458 if (op != NULL && *op != '=')
1459 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001460 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001461 return;
1462 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001463 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001464 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001465
1466 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1467 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001468 if (lp->ll_empty2)
1469 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1470
Bram Moolenaar68452172021-04-12 21:21:02 +02001471 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1472 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001473 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001474 }
1475 else
1476 {
1477 val = (int)tv_get_number_chk(rettv, &error);
1478 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001479 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001480 }
1481 }
1482 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001483 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001484 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001485
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001486 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1487 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001488 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001489 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001490 *endp = cc;
1491 return;
1492 }
1493
Bram Moolenaarff697e62019-02-12 22:28:33 +01001494 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001495 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001496 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001497 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001498 {
1499 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001500 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1501 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001502 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001503 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001504 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001505 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001506 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001507 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001508 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001509 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001510 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1511 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001512 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001513 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001514 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001515 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001516 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001517 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001518 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001519 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001520 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001521 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001522 else if (lp->ll_range)
1523 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001524 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1525 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001526 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001527 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001528 return;
1529 }
1530
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001531 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1532 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001533 }
1534 else
1535 {
1536 /*
1537 * Assign to a List or Dictionary item.
1538 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001539 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1540 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001541 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001542 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001543 return;
1544 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001545
1546 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001547 && check_typval_arg_type(lp->ll_valtype, rettv,
1548 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001549 return;
1550
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001551 if (lp->ll_newkey != NULL)
1552 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001553 if (op != NULL && *op != '=')
1554 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001555 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001556 return;
1557 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001558 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1559 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001560 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001561
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001562 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001563 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001564 if (di == NULL)
1565 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001566 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1567 {
1568 vim_free(di);
1569 return;
1570 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001571 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001572 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001573 else if (op != NULL && *op != '=')
1574 {
1575 tv_op(lp->ll_tv, rettv, op);
1576 return;
1577 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001578 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001579 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001580
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001581 /*
1582 * Assign the value to the variable or list item.
1583 */
1584 if (copy)
1585 copy_tv(rettv, lp->ll_tv);
1586 else
1587 {
1588 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001589 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001590 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001591 }
1592 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001593}
1594
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001595/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001596 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1597 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001598 * Returns OK or FAIL.
1599 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001600 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001601tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001602{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001603 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001604 char_u numbuf[NUMBUFLEN];
1605 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001606 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001607
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001608 // Can't do anything with a Funcref or Dict on the right.
1609 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001610 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001611 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1612 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001613 {
1614 switch (tv1->v_type)
1615 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001616 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001617 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001618 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001619 case VAR_DICT:
1620 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001621 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001622 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001623 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001624 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001625 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001626 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001627 break;
1628
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001629 case VAR_BLOB:
1630 if (*op != '+' || tv2->v_type != VAR_BLOB)
1631 break;
1632 // BLOB += BLOB
1633 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1634 {
1635 blob_T *b1 = tv1->vval.v_blob;
1636 blob_T *b2 = tv2->vval.v_blob;
1637 int i, len = blob_len(b2);
1638 for (i = 0; i < len; i++)
1639 ga_append(&b1->bv_ga, blob_get(b2, i));
1640 }
1641 return OK;
1642
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001643 case VAR_LIST:
1644 if (*op != '+' || tv2->v_type != VAR_LIST)
1645 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001646 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001647 if (tv2->vval.v_list != NULL)
1648 {
1649 if (tv1->vval.v_list == NULL)
1650 {
1651 tv1->vval.v_list = tv2->vval.v_list;
1652 ++tv1->vval.v_list->lv_refcount;
1653 }
1654 else
1655 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1656 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001657 return OK;
1658
1659 case VAR_NUMBER:
1660 case VAR_STRING:
1661 if (tv2->v_type == VAR_LIST)
1662 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001663 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001664 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001665 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001666 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001667#ifdef FEAT_FLOAT
1668 if (tv2->v_type == VAR_FLOAT)
1669 {
1670 float_T f = n;
1671
Bram Moolenaarff697e62019-02-12 22:28:33 +01001672 if (*op == '%')
1673 break;
1674 switch (*op)
1675 {
1676 case '+': f += tv2->vval.v_float; break;
1677 case '-': f -= tv2->vval.v_float; break;
1678 case '*': f *= tv2->vval.v_float; break;
1679 case '/': f /= tv2->vval.v_float; break;
1680 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001681 clear_tv(tv1);
1682 tv1->v_type = VAR_FLOAT;
1683 tv1->vval.v_float = f;
1684 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001685 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001686#endif
1687 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001688 switch (*op)
1689 {
1690 case '+': n += tv_get_number(tv2); break;
1691 case '-': n -= tv_get_number(tv2); break;
1692 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001693 case '/': n = num_divide(n, tv_get_number(tv2),
1694 &failed); break;
1695 case '%': n = num_modulus(n, tv_get_number(tv2),
1696 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001697 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001698 clear_tv(tv1);
1699 tv1->v_type = VAR_NUMBER;
1700 tv1->vval.v_number = n;
1701 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001702 }
1703 else
1704 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001705 if (tv2->v_type == VAR_FLOAT)
1706 break;
1707
Bram Moolenaarff697e62019-02-12 22:28:33 +01001708 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001709 s = tv_get_string(tv1);
1710 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001711 clear_tv(tv1);
1712 tv1->v_type = VAR_STRING;
1713 tv1->vval.v_string = s;
1714 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001715 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001716
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001717 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001718#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001719 {
1720 float_T f;
1721
Bram Moolenaarff697e62019-02-12 22:28:33 +01001722 if (*op == '%' || *op == '.'
1723 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001724 && tv2->v_type != VAR_NUMBER
1725 && tv2->v_type != VAR_STRING))
1726 break;
1727 if (tv2->v_type == VAR_FLOAT)
1728 f = tv2->vval.v_float;
1729 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001730 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001731 switch (*op)
1732 {
1733 case '+': tv1->vval.v_float += f; break;
1734 case '-': tv1->vval.v_float -= f; break;
1735 case '*': tv1->vval.v_float *= f; break;
1736 case '/': tv1->vval.v_float /= f; break;
1737 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001738 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001739#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001740 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001741 }
1742 }
1743
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001744 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001745 return FAIL;
1746}
1747
1748/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001749 * Evaluate the expression used in a ":for var in expr" command.
1750 * "arg" points to "var".
1751 * Set "*errp" to TRUE for an error, FALSE otherwise;
1752 * Return a pointer that holds the info. Null when there is an error.
1753 */
1754 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001755eval_for_line(
1756 char_u *arg,
1757 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001758 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001759 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001760{
Bram Moolenaar33570922005-01-25 22:26:29 +00001761 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001762 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001763 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001764 typval_T tv;
1765 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001766 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001767
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001768 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001769
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001770 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001771 if (fi == NULL)
1772 return NULL;
1773
Bram Moolenaar404557e2021-07-05 21:41:48 +02001774 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1775 &fi->fi_semicolon, FALSE);
1776 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001777 return fi;
1778
Bram Moolenaar404557e2021-07-05 21:41:48 +02001779 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001780 if (expr[0] != 'i' || expr[1] != 'n'
1781 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001782 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001783 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1784 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1785 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001786 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001787 return fi;
1788 }
1789
1790 if (skip)
1791 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001792 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1793 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001794 {
1795 *errp = FALSE;
1796 if (!skip)
1797 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001798 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001799 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001800 l = tv.vval.v_list;
1801 if (l == NULL)
1802 {
1803 // a null list is like an empty list: do nothing
1804 clear_tv(&tv);
1805 }
1806 else
1807 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001809 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001810
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001811 // No need to increment the refcount, it's already set for
1812 // the list being used in "tv".
1813 fi->fi_list = l;
1814 list_add_watch(l, &fi->fi_lw);
1815 fi->fi_lw.lw_item = l->lv_first;
1816 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001817 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001818 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001819 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001820 fi->fi_bi = 0;
1821 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001822 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001823 typval_T btv;
1824
1825 // Make a copy, so that the iteration still works when the
1826 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001827 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001828 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001829 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001830 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001831 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001832 else if (tv.v_type == VAR_STRING)
1833 {
1834 fi->fi_byte_idx = 0;
1835 fi->fi_string = tv.vval.v_string;
1836 tv.vval.v_string = NULL;
1837 if (fi->fi_string == NULL)
1838 fi->fi_string = vim_strsave((char_u *)"");
1839 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001840 else
1841 {
Sean Dewar80d73952021-08-04 19:25:54 +02001842 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001843 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001844 }
1845 }
1846 }
1847 if (skip)
1848 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001849 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001850
1851 return fi;
1852}
1853
1854/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001855 * Used when looping over a :for line, skip the "in expr" part.
1856 */
1857 void
1858skip_for_lines(void *fi_void, evalarg_T *evalarg)
1859{
1860 forinfo_T *fi = (forinfo_T *)fi_void;
1861 int i;
1862
1863 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01001864 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001865}
1866
1867/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001868 * Use the first item in a ":for" list. Advance to the next.
1869 * Assign the values to the variable (list). "arg" points to the first one.
1870 * Return TRUE when a valid item was found, FALSE when at end of list or
1871 * something wrong.
1872 */
1873 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001874next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001875{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001876 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001877 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001878 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001879 ? (ASSIGN_FINAL
1880 // first round: error if variable exists
1881 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1882 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001883 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001884 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001885 int skip_assign = in_vim9script() && arg[0] == '_'
1886 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001887
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001888 if (fi->fi_blob != NULL)
1889 {
1890 typval_T tv;
1891
1892 if (fi->fi_bi >= blob_len(fi->fi_blob))
1893 return FALSE;
1894 tv.v_type = VAR_NUMBER;
1895 tv.v_lock = VAR_FIXED;
1896 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1897 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001898 if (skip_assign)
1899 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001900 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001901 fi->fi_varcount, flag, NULL) == OK;
1902 }
1903
1904 if (fi->fi_string != NULL)
1905 {
1906 typval_T tv;
1907 int len;
1908
1909 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1910 if (len == 0)
1911 return FALSE;
1912 tv.v_type = VAR_STRING;
1913 tv.v_lock = VAR_FIXED;
1914 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1915 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001916 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001917 if (skip_assign)
1918 result = TRUE;
1919 else
1920 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001921 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001922 vim_free(tv.vval.v_string);
1923 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001924 }
1925
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001926 item = fi->fi_lw.lw_item;
1927 if (item == NULL)
1928 result = FALSE;
1929 else
1930 {
1931 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001932 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001933 if (skip_assign)
1934 result = TRUE;
1935 else
1936 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001937 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938 }
1939 return result;
1940}
1941
1942/*
1943 * Free the structure used to store info used by ":for".
1944 */
1945 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001946free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001947{
Bram Moolenaar33570922005-01-25 22:26:29 +00001948 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001949
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001950 if (fi == NULL)
1951 return;
1952 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001953 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001954 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001955 list_unref(fi->fi_list);
1956 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001957 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001958 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001959 else
1960 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001961 vim_free(fi);
1962}
1963
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001965set_context_for_expression(
1966 expand_T *xp,
1967 char_u *arg,
1968 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001970 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001972 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001974 if (cmdidx == CMD_let || cmdidx == CMD_var
1975 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001976 {
1977 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001978 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001979 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001980 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001981 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001982 {
1983 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001984 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001985 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986 break;
1987 }
1988 return;
1989 }
1990 }
1991 else
1992 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1993 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 while ((xp->xp_pattern = vim_strpbrk(arg,
1995 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1996 {
1997 c = *xp->xp_pattern;
1998 if (c == '&')
1999 {
2000 c = xp->xp_pattern[1];
2001 if (c == '&')
2002 {
2003 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002004 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 }
2006 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002007 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002009 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2010 xp->xp_pattern += 2;
2011
2012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 }
2014 else if (c == '$')
2015 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002016 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 xp->xp_context = EXPAND_ENV_VARS;
2018 }
2019 else if (c == '=')
2020 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002021 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 xp->xp_context = EXPAND_EXPRESSION;
2023 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002024 else if (c == '#'
2025 && xp->xp_context == EXPAND_EXPRESSION)
2026 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002027 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002028 break;
2029 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002030 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 && xp->xp_context == EXPAND_FUNCTIONS
2032 && vim_strchr(xp->xp_pattern, '(') == NULL)
2033 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002034 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 break;
2036 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002037 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002039 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 {
2041 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2042 if (c == '\\' && xp->xp_pattern[1] != NUL)
2043 ++xp->xp_pattern;
2044 xp->xp_context = EXPAND_NOTHING;
2045 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002046 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002048 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2050 /* skip */ ;
2051 xp->xp_context = EXPAND_NOTHING;
2052 }
2053 else if (c == '|')
2054 {
2055 if (xp->xp_pattern[1] == '|')
2056 {
2057 ++xp->xp_pattern;
2058 xp->xp_context = EXPAND_EXPRESSION;
2059 }
2060 else
2061 xp->xp_context = EXPAND_COMMANDS;
2062 }
2063 else
2064 xp->xp_context = EXPAND_EXPRESSION;
2065 }
2066 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002067 // Doesn't look like something valid, expand as an expression
2068 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002069 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 arg = xp->xp_pattern;
2071 if (*arg != NUL)
2072 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2073 /* skip */ ;
2074 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002075
2076 // ":exe one two" completes "two"
2077 if ((cmdidx == CMD_execute
2078 || cmdidx == CMD_echo
2079 || cmdidx == CMD_echon
2080 || cmdidx == CMD_echomsg)
2081 && xp->xp_context == EXPAND_EXPRESSION)
2082 {
2083 for (;;)
2084 {
2085 char_u *n = skiptowhite(arg);
2086
2087 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2088 break;
2089 arg = skipwhite(n);
2090 }
2091 }
2092
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 xp->xp_pattern = arg;
2094}
2095
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002097 * Return TRUE if "pat" matches "text".
2098 * Does not use 'cpo' and always uses 'magic'.
2099 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002100 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002101pattern_match(char_u *pat, char_u *text, int ic)
2102{
2103 int matches = FALSE;
2104 char_u *save_cpo;
2105 regmatch_T regmatch;
2106
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002107 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002108 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002109 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002110 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2111 if (regmatch.regprog != NULL)
2112 {
2113 regmatch.rm_ic = ic;
2114 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2115 vim_regfree(regmatch.regprog);
2116 }
2117 p_cpo = save_cpo;
2118 return matches;
2119}
2120
2121/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002122 * Handle a name followed by "(". Both for just "name(arg)" and for
2123 * "expr->name(arg)".
2124 * Returns OK or FAIL.
2125 */
2126 static int
2127eval_func(
2128 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002129 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002130 char_u *name,
2131 int name_len,
2132 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002133 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002134 typval_T *basetv) // "expr" for "expr->name(arg)"
2135{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002136 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002137 char_u *s = name;
2138 int len = name_len;
2139 partial_T *partial;
2140 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002141 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002142 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002143
2144 if (!evaluate)
2145 check_vars(s, len);
2146
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002147 // If "s" is the name of a variable of type VAR_FUNC
2148 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002149 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002150 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002151
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002152 // Need to make a copy, in case evaluating the arguments makes
2153 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002154 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002155 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002156 ret = FAIL;
2157 else
2158 {
2159 funcexe_T funcexe;
2160
2161 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002162 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002163 funcexe.fe_firstline = curwin->w_cursor.lnum;
2164 funcexe.fe_lastline = curwin->w_cursor.lnum;
2165 funcexe.fe_evaluate = evaluate;
2166 funcexe.fe_partial = partial;
2167 funcexe.fe_basetv = basetv;
2168 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002169 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002170 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002171 }
2172 vim_free(s);
2173
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002174 // If evaluate is FALSE rettv->v_type was not set in
2175 // get_func_tv, but it's needed in handle_subscript() to parse
2176 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002177 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2178 {
2179 rettv->vval.v_string = NULL;
2180 rettv->v_type = VAR_FUNC;
2181 }
2182
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002183 // Stop the expression evaluation when immediately
2184 // aborting on error, or when an interrupt occurred or
2185 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002186 if (evaluate && aborting())
2187 {
2188 if (ret == OK)
2189 clear_tv(rettv);
2190 ret = FAIL;
2191 }
2192 return ret;
2193}
2194
2195/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002196 * After a NL, skip over empty lines and comment-only lines.
2197 */
2198 static char_u *
2199newline_skip_comments(char_u *arg)
2200{
2201 char_u *p = arg + 1;
2202
2203 for (;;)
2204 {
2205 p = skipwhite(p);
2206
2207 if (*p == NUL)
2208 break;
2209 if (vim9_comment_start(p))
2210 {
2211 char_u *nl = vim_strchr(p, NL);
2212
2213 if (nl == NULL)
2214 break;
2215 p = nl;
2216 }
2217 if (*p != NL)
2218 break;
2219 ++p; // skip another NL
2220 }
2221 return p;
2222}
2223
2224/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002225 * Get the next line source line without advancing. But do skip over comment
2226 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002227 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002228 */
2229 static char_u *
2230getline_peek_skip_comments(evalarg_T *evalarg)
2231{
2232 for (;;)
2233 {
2234 char_u *next = getline_peek(evalarg->eval_getline,
2235 evalarg->eval_cookie);
2236 char_u *p;
2237
2238 if (next == NULL)
2239 break;
2240 p = skipwhite(next);
2241 if (*p != NUL && !vim9_comment_start(p))
2242 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002243 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002244 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002245 }
2246 return NULL;
2247}
2248
2249/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002250 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2251 * comment) and there is a next line, return the next line (skipping blanks)
2252 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002253 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2254 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002255 * "arg" must point somewhere inside a line, not at the start.
2256 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002257 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002258eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2259{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002260 char_u *p = skipwhite(arg);
2261
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002262 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002263 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002264 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002265 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2266 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002267 && (*p == NUL || *p == NL
2268 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002269 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002270 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002271
Bram Moolenaare442d592022-05-05 12:20:28 +01002272 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002273 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002274 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002275 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002276 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002277 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002278
Bram Moolenaar9d489562020-07-30 20:08:50 +02002279 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002280 {
2281 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002282 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002283 }
2284 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002285 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002286}
2287
2288/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002289 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002290 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002291 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002292 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002293eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002294{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002295 garray_T *gap = &evalarg->eval_ga;
2296 char_u *line;
2297
Bram Moolenaar39be4982022-05-06 21:51:50 +01002298 if (arg != NULL)
2299 {
2300 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002301 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002302 // Truncate before a trailing comment, so that concatenating the lines
2303 // won't turn the rest into a comment.
2304 if (*skipwhite(arg) == '#')
2305 *arg = NUL;
2306 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002307
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002308 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002309 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2310 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002311 else
2312 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002313 if (line == NULL)
2314 return NULL;
2315
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002316 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002317 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2318 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002319 char_u *p = skipwhite(line);
2320
2321 // Going to concatenate the lines after parsing. For an empty or
2322 // comment line use an empty string.
2323 if (*p == NUL || vim9_comment_start(p))
2324 {
2325 vim_free(line);
2326 line = vim_strsave((char_u *)"");
2327 }
2328
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002329 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2330 ++gap->ga_len;
2331 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002332 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002333 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002334 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002335 evalarg->eval_tofree = line;
2336 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002337
2338 // Advanced to the next line, "arg" no longer points into the previous
2339 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002340 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002341 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002342}
2343
Bram Moolenaare6b53242020-07-01 17:28:33 +02002344/*
2345 * Call eval_next_non_blank() and get the next line if needed.
2346 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002347 char_u *
2348skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2349{
2350 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002351 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002352
Bram Moolenaar962d7212020-07-04 14:15:00 +02002353 if (evalarg == NULL)
2354 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002355 eval_next_non_blank(p, evalarg, &getnext);
2356 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002357 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002358 return p;
2359}
2360
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002361/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002363 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2365 */
2366
2367/*
2368 * Handle zero level expression.
2369 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002370 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002371 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002372 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 * Return OK or FAIL.
2374 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002375 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002376eval0(
2377 char_u *arg,
2378 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002379 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002380 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002382 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2383}
2384
2385/*
2386 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2387 * expression and don't check what comes after the expression.
2388 */
2389 int
2390eval0_retarg(
2391 char_u *arg,
2392 typval_T *rettv,
2393 exarg_T *eap,
2394 evalarg_T *evalarg,
2395 char_u **retarg)
2396{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 int ret;
2398 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002399 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002400 int did_emsg_before = did_emsg;
2401 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002402 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002403 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002404 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
2406 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002407 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002408
Bram Moolenaar79481362022-06-27 20:15:10 +01002409 if (ret != FAIL)
2410 {
2411 expr_end = p;
2412 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002413
Bram Moolenaar79481362022-06-27 20:15:10 +01002414 // In Vim9 script a command block is not split at NL characters for
2415 // commands using an expression argument. Skip over a '#' comment to
2416 // check for a following NL. Require white space before the '#'.
2417 if (in_vim9script() && p > expr_end && retarg == NULL)
2418 while (*p == '#')
2419 {
2420 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002421
Bram Moolenaar79481362022-06-27 20:15:10 +01002422 if (nl == NULL)
2423 break;
2424 p = skipwhite(nl + 1);
2425 if (eap != NULL && *p != NUL)
2426 eap->nextcmd = p;
2427 check_for_end = FALSE;
2428 }
2429
2430 if (check_for_end)
2431 end_error = !ends_excmd2(arg, p);
2432 }
2433
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002434 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 {
2436 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002437 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 /*
2439 * Report the invalid expression unless the expression evaluation has
2440 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002441 * exception, or we already gave a more specific error.
2442 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002444 if (!aborting()
2445 && did_emsg == did_emsg_before
2446 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002447 && (flags & EVAL_CONSTANT) == 0
2448 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002449 {
2450 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002451 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002452 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002453 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002454 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002455
2456 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002457 // a next command to avoid more errors, unless "|" is following, which
2458 // could only be a command separator.
Bram Moolenaar79481362022-06-27 20:15:10 +01002459 if (eap != NULL && p != NULL
2460 && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
Bram Moolenaar8143a532020-12-13 20:26:29 +01002461 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002462 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002464
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002465 if (retarg != NULL)
2466 *retarg = p;
2467 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002468 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002469
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 return ret;
2471}
2472
2473/*
2474 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002475 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002476 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 *
2478 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002479 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002481 * Note: "rettv.v_lock" is not set.
2482 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 * Return OK or FAIL.
2484 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002485 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002486eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002488 char_u *p;
2489 int getnext;
2490
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002491 CLEAR_POINTER(rettv);
2492
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 /*
2494 * Get the first variable.
2495 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002496 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 return FAIL;
2498
Bram Moolenaar793648f2020-06-26 21:28:25 +02002499 p = eval_next_non_blank(*arg, evalarg, &getnext);
2500 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002502 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002503 int result;
2504 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002505 evalarg_T *evalarg_used = evalarg;
2506 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002507 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002508 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002509 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002510
2511 if (evalarg == NULL)
2512 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002513 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002514 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002515 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002516 orig_flags = evalarg_used->eval_flags;
2517 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002518
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002519 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002520 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002521 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002522 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002523 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002524 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002525 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002526 clear_tv(rettv);
2527 return FAIL;
2528 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002529 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002530 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002531
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002533 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002535 int error = FALSE;
2536
Bram Moolenaar13106602020-10-04 16:06:05 +02002537 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002538 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002539 else if (vim9script)
2540 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002541 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002543 if (error || !op_falsy || !result)
2544 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002545 if (error)
2546 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 }
2548
2549 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002550 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002552 if (op_falsy)
2553 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002554 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002555 {
mityu4ac198c2021-05-28 17:52:40 +02002556 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002557 clear_tv(rettv);
2558 return FAIL;
2559 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002560 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002561 evalarg_used->eval_flags = (op_falsy ? !result : result)
2562 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2563 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002564 {
2565 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002567 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002568 if (!op_falsy || !result)
2569 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002571 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002573 /*
2574 * Check for the ":".
2575 */
2576 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2577 if (*p != ':')
2578 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002579 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002580 if (evaluate && result)
2581 clear_tv(rettv);
2582 evalarg_used->eval_flags = orig_flags;
2583 return FAIL;
2584 }
2585 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002586 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002587 else
2588 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002589 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002590 {
2591 error_white_both(p, 1);
2592 clear_tv(rettv);
2593 evalarg_used->eval_flags = orig_flags;
2594 return FAIL;
2595 }
2596 *arg = p;
2597 }
2598
2599 /*
2600 * Get the third variable. Recursive!
2601 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002602 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002603 {
mityu4ac198c2021-05-28 17:52:40 +02002604 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002605 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002606 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002607 return FAIL;
2608 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002609 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2610 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002611 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002612 if (eval1(arg, &var2, evalarg_used) == FAIL)
2613 {
2614 if (evaluate && result)
2615 clear_tv(rettv);
2616 evalarg_used->eval_flags = orig_flags;
2617 return FAIL;
2618 }
2619 if (evaluate && !result)
2620 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002622
2623 if (evalarg == NULL)
2624 clear_evalarg(&local_evalarg, NULL);
2625 else
2626 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 }
2628
2629 return OK;
2630}
2631
2632/*
2633 * Handle first level expression:
2634 * expr2 || expr2 || expr2 logical OR
2635 *
2636 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002637 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 *
2639 * Return OK or FAIL.
2640 */
2641 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002642eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002644 char_u *p;
2645 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646
2647 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002648 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002650 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 return FAIL;
2652
2653 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002654 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002656 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002657 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002659 evalarg_T *evalarg_used = evalarg;
2660 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002661 int evaluate;
2662 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002663 long result = FALSE;
2664 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002665 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002666 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002667
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002668 if (evalarg == NULL)
2669 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002670 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002671 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002672 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002673 orig_flags = evalarg_used->eval_flags;
2674 evaluate = orig_flags & EVAL_EVALUATE;
2675 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002676 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002677 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002678 result = tv_get_bool_chk(rettv, &error);
2679 else if (tv_get_number_chk(rettv, &error) != 0)
2680 result = TRUE;
2681 clear_tv(rettv);
2682 if (error)
2683 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 }
2685
2686 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002687 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002689 while (p[0] == '|' && p[1] == '|')
2690 {
2691 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002692 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002693 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002694 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002695 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002696 {
2697 error_white_both(p, 2);
2698 clear_tv(rettv);
2699 return FAIL;
2700 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002701 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002702 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002703
2704 /*
2705 * Get the second variable.
2706 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002707 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002708 {
mityu4ac198c2021-05-28 17:52:40 +02002709 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002710 clear_tv(rettv);
2711 return FAIL;
2712 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002713 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2714 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002715 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002716 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002717 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002718
2719 /*
2720 * Compute the result.
2721 */
2722 if (evaluate && !result)
2723 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002724 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002725 result = tv_get_bool_chk(&var2, &error);
2726 else if (tv_get_number_chk(&var2, &error) != 0)
2727 result = TRUE;
2728 clear_tv(&var2);
2729 if (error)
2730 return FAIL;
2731 }
2732 if (evaluate)
2733 {
2734 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002735 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002736 rettv->v_type = VAR_BOOL;
2737 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002738 }
2739 else
2740 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002741 rettv->v_type = VAR_NUMBER;
2742 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002743 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002744 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002745
2746 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002748
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002749 if (evalarg == NULL)
2750 clear_evalarg(&local_evalarg, NULL);
2751 else
2752 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 }
2754
2755 return OK;
2756}
2757
2758/*
2759 * Handle second level expression:
2760 * expr3 && expr3 && expr3 logical AND
2761 *
2762 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002763 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 *
2765 * Return OK or FAIL.
2766 */
2767 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002768eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002770 char_u *p;
2771 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772
2773 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002774 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002776 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 return FAIL;
2778
2779 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002780 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002782 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002783 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002785 evalarg_T *evalarg_used = evalarg;
2786 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002787 int orig_flags;
2788 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002789 long result = TRUE;
2790 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002791 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002792 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002793
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002794 if (evalarg == NULL)
2795 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002796 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002797 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002798 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002799 orig_flags = evalarg_used->eval_flags;
2800 evaluate = orig_flags & EVAL_EVALUATE;
2801 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002802 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002803 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002804 result = tv_get_bool_chk(rettv, &error);
2805 else if (tv_get_number_chk(rettv, &error) == 0)
2806 result = FALSE;
2807 clear_tv(rettv);
2808 if (error)
2809 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 }
2811
2812 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002813 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002815 while (p[0] == '&' && p[1] == '&')
2816 {
2817 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002818 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002819 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002820 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002821 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002822 {
2823 error_white_both(p, 2);
2824 clear_tv(rettv);
2825 return FAIL;
2826 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002827 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002828 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002829
2830 /*
2831 * Get the second variable.
2832 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002833 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002834 {
mityu4ac198c2021-05-28 17:52:40 +02002835 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002836 clear_tv(rettv);
2837 return FAIL;
2838 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002839 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2840 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002841 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002842 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002843 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002844 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002845
2846 /*
2847 * Compute the result.
2848 */
2849 if (evaluate && result)
2850 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002851 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002852 result = tv_get_bool_chk(&var2, &error);
2853 else if (tv_get_number_chk(&var2, &error) == 0)
2854 result = FALSE;
2855 clear_tv(&var2);
2856 if (error)
2857 return FAIL;
2858 }
2859 if (evaluate)
2860 {
2861 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002862 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002863 rettv->v_type = VAR_BOOL;
2864 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002865 }
2866 else
2867 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002868 rettv->v_type = VAR_NUMBER;
2869 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002870 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002871 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002872
2873 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002875
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002876 if (evalarg == NULL)
2877 clear_evalarg(&local_evalarg, NULL);
2878 else
2879 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 }
2881
2882 return OK;
2883}
2884
2885/*
2886 * Handle third level expression:
2887 * var1 == var2
2888 * var1 =~ var2
2889 * var1 != var2
2890 * var1 !~ var2
2891 * var1 > var2
2892 * var1 >= var2
2893 * var1 < var2
2894 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002895 * var1 is var2
2896 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 *
2898 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002899 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 *
2901 * Return OK or FAIL.
2902 */
2903 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002904eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002907 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002908 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002910 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911
2912 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002913 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002915 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 return FAIL;
2917
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002918 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002919
Bram Moolenaar696ba232020-07-29 21:20:41 +02002920 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921
2922 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002923 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002925 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002927 typval_T var2;
2928 int ic;
2929 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002930 int evaluate = evalarg == NULL
2931 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002932 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002933
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002934 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002935 {
Bram Moolenaare442d592022-05-05 12:20:28 +01002936 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002937 p = *arg;
2938 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002939 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2940 {
mityu4ac198c2021-05-28 17:52:40 +02002941 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002942 clear_tv(rettv);
2943 return FAIL;
2944 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002945
Bram Moolenaar696ba232020-07-29 21:20:41 +02002946 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2947 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002948 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002949 clear_tv(rettv);
2950 return FAIL;
2951 }
2952
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002953 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 if (p[len] == '?')
2955 {
2956 ic = TRUE;
2957 ++len;
2958 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002959 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 else if (p[len] == '#')
2961 {
2962 ic = FALSE;
2963 ++len;
2964 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002965 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002967 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968
2969 /*
2970 * Get the second variable.
2971 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002972 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2973 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002974 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002975 clear_tv(rettv);
2976 return FAIL;
2977 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002978 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002979 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002981 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 return FAIL;
2983 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002984 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002985 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002986 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002987
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002988 // use the line of the comparison for messages
2989 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002990 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002991 {
2992 ret = FAIL;
2993 clear_tv(rettv);
2994 }
2995 else
2996 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002997 clear_tv(&var2);
2998 return ret;
2999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 }
3001
3002 return OK;
3003}
3004
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003005/*
3006 * Make a copy of blob "tv1" and append blob "tv2".
3007 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008 void
3009eval_addblob(typval_T *tv1, typval_T *tv2)
3010{
3011 blob_T *b1 = tv1->vval.v_blob;
3012 blob_T *b2 = tv2->vval.v_blob;
3013 blob_T *b = blob_alloc();
3014 int i;
3015
3016 if (b != NULL)
3017 {
3018 for (i = 0; i < blob_len(b1); i++)
3019 ga_append(&b->bv_ga, blob_get(b1, i));
3020 for (i = 0; i < blob_len(b2); i++)
3021 ga_append(&b->bv_ga, blob_get(b2, i));
3022
3023 clear_tv(tv1);
3024 rettv_blob_set(tv1, b);
3025 }
3026}
3027
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003028/*
3029 * Make a copy of list "tv1" and append list "tv2".
3030 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 int
3032eval_addlist(typval_T *tv1, typval_T *tv2)
3033{
3034 typval_T var3;
3035
3036 // concatenate Lists
3037 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3038 {
3039 clear_tv(tv1);
3040 clear_tv(tv2);
3041 return FAIL;
3042 }
3043 clear_tv(tv1);
3044 *tv1 = var3;
3045 return OK;
3046}
3047
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003049 * Handle the bitwise left/right shift operator expression:
3050 * var1 << var2
3051 * var1 >> var2
3052 *
3053 * "arg" must point to the first non-white of the expression.
3054 * "arg" is advanced to just after the recognized expression.
3055 *
3056 * Return OK or FAIL.
3057 */
3058 static int
3059eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3060{
3061 /*
3062 * Get the first expression.
3063 */
3064 if (eval6(arg, rettv, evalarg) == FAIL)
3065 return FAIL;
3066
3067 /*
3068 * Repeat computing, until no '<<' or '>>' is following.
3069 */
3070 for (;;)
3071 {
3072 char_u *p;
3073 int getnext;
3074 exprtype_T type;
3075 int evaluate;
3076 typval_T var2;
3077 int vim9script;
3078
3079 p = eval_next_non_blank(*arg, evalarg, &getnext);
3080 if (p[0] == '<' && p[1] == '<')
3081 type = EXPR_LSHIFT;
3082 else if (p[0] == '>' && p[1] == '>')
3083 type = EXPR_RSHIFT;
3084 else
3085 return OK;
3086
3087 // Handle a bitwise left or right shift operator
3088 if (rettv->v_type != VAR_NUMBER)
3089 {
3090 // left operand should be a number
3091 emsg(_(e_bitshift_ops_must_be_number));
3092 clear_tv(rettv);
3093 return FAIL;
3094 }
3095
3096 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3097 vim9script = in_vim9script();
3098 if (getnext)
3099 {
3100 *arg = eval_next_line(*arg, evalarg);
3101 p = *arg;
3102 }
3103 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3104 {
3105 error_white_both(*arg, 2);
3106 clear_tv(rettv);
3107 return FAIL;
3108 }
3109
3110 /*
3111 * Get the second variable.
3112 */
3113 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3114 {
3115 error_white_both(p, 2);
3116 clear_tv(rettv);
3117 return FAIL;
3118 }
3119 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3120 if (eval6(arg, &var2, evalarg) == FAIL)
3121 {
3122 clear_tv(rettv);
3123 return FAIL;
3124 }
3125
3126 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3127 {
3128 // right operand should be a positive number
3129 if (var2.v_type != VAR_NUMBER)
3130 emsg(_(e_bitshift_ops_must_be_number));
3131 else
3132 emsg(_(e_bitshift_ops_must_be_postive));
3133 clear_tv(rettv);
3134 clear_tv(&var2);
3135 return FAIL;
3136 }
3137
3138 if (evaluate)
3139 {
3140 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3141 // shifting more bits than we have always results in zero
3142 rettv->vval.v_number = 0;
3143 else if (type == EXPR_LSHIFT)
3144 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003145 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003146 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003147 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003148 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003149 }
3150
3151 clear_tv(&var2);
3152 }
3153
3154 return OK;
3155}
3156
3157/*
3158 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003159 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003161 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003162 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 *
3164 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003165 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 *
3167 * Return OK or FAIL.
3168 */
3169 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003170eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003173 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003175 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 return FAIL;
3177
3178 /*
3179 * Repeat computing, until no '+', '-' or '.' is following.
3180 */
3181 for (;;)
3182 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003183 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003184 int getnext;
3185 char_u *p;
3186 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003187 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003188 int concat;
3189 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003190 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003191
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003192 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003193 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003194 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003195 p = eval_next_non_blank(*arg, evalarg, &getnext);
3196 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003197 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003198 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3199 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003201 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3202 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003203
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003204 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003205 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003206 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003207 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003208 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003209 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003210 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003211 {
mityu4ac198c2021-05-28 17:52:40 +02003212 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003213 clear_tv(rettv);
3214 return FAIL;
3215 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003216 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003217 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003218 if ((op != '+' || (rettv->v_type != VAR_LIST
3219 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003220#ifdef FEAT_FLOAT
3221 && (op == '.' || rettv->v_type != VAR_FLOAT)
3222#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003223 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003224 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003225 int error = FALSE;
3226
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003227 // For "list + ...", an illegal use of the first operand as
3228 // a number cannot be determined before evaluating the 2nd
3229 // operand: if this is also a list, all is ok.
3230 // For "something . ...", "something - ..." or "non-list + ...",
3231 // we know that the first operand needs to be a string or number
3232 // without evaluating the 2nd operand. So check before to avoid
3233 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003234 if (op != '.')
3235 tv_get_number_chk(rettv, &error);
3236 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003237 {
3238 clear_tv(rettv);
3239 return FAIL;
3240 }
3241 }
3242
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 /*
3244 * Get the second variable.
3245 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003246 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003247 {
mityu89dcb4d2021-05-28 13:50:17 +02003248 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003249 clear_tv(rettv);
3250 return FAIL;
3251 }
3252 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003253 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003255 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 return FAIL;
3257 }
3258
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003259 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 {
3261 /*
3262 * Compute the result.
3263 */
3264 if (op == '.')
3265 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003266 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3267 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003268 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003269
Bram Moolenaar2e086612020-08-25 22:37:48 +02003270 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003271 || var2.v_type == VAR_CHANNEL
3272 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003273 semsg(_(e_using_invalid_value_as_string_str),
3274 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003275#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003276 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003277 {
3278 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3279 var2.vval.v_float);
3280 s2 = buf2;
3281 }
3282#endif
3283 else
3284 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003285 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003286 {
3287 clear_tv(rettv);
3288 clear_tv(&var2);
3289 return FAIL;
3290 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003291 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003292 clear_tv(rettv);
3293 rettv->v_type = VAR_STRING;
3294 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003296 else if (op == '+' && rettv->v_type == VAR_BLOB
3297 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003299 else if (op == '+' && rettv->v_type == VAR_LIST
3300 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003301 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003303 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 else
3306 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003307 int error = FALSE;
3308 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003309#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003310 float_T f1 = 0, f2 = 0;
3311
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003312 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003313 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003314 f1 = rettv->vval.v_float;
3315 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003316 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003317 else
3318#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003319 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003320 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003321 if (error)
3322 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003323 // This can only happen for "list + non-list" or
3324 // "blob + non-blob". For "non-list + ..." or
3325 // "something - ...", we returned before evaluating the
3326 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003327 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003328 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003329 return FAIL;
3330 }
3331#ifdef FEAT_FLOAT
3332 if (var2.v_type == VAR_FLOAT)
3333 f1 = n1;
3334#endif
3335 }
3336#ifdef FEAT_FLOAT
3337 if (var2.v_type == VAR_FLOAT)
3338 {
3339 f2 = var2.vval.v_float;
3340 n2 = 0;
3341 }
3342 else
3343#endif
3344 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003345 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003346 if (error)
3347 {
3348 clear_tv(rettv);
3349 clear_tv(&var2);
3350 return FAIL;
3351 }
3352#ifdef FEAT_FLOAT
3353 if (rettv->v_type == VAR_FLOAT)
3354 f2 = n2;
3355#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003356 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003357 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003358
3359#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003360 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003361 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3362 {
3363 if (op == '+')
3364 f1 = f1 + f2;
3365 else
3366 f1 = f1 - f2;
3367 rettv->v_type = VAR_FLOAT;
3368 rettv->vval.v_float = f1;
3369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003371#endif
3372 {
3373 if (op == '+')
3374 n1 = n1 + n2;
3375 else
3376 n1 = n1 - n2;
3377 rettv->v_type = VAR_NUMBER;
3378 rettv->vval.v_number = n1;
3379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003381 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 }
3383 }
3384 return OK;
3385}
3386
3387/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003388 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 * * number multiplication
3390 * / number division
3391 * % number modulo
3392 *
3393 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003394 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 *
3396 * Return OK or FAIL.
3397 */
3398 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003399eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003400 char_u **arg,
3401 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003402 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003403 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003405#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003406 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408
3409 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003410 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003412 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 return FAIL;
3414
3415 /*
3416 * Repeat computing, until no '*', '/' or '%' is following.
3417 */
3418 for (;;)
3419 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003420 int evaluate;
3421 int getnext;
3422 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003423 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003424 int op;
3425 varnumber_T n1, n2;
3426#ifdef FEAT_FLOAT
3427 float_T f1, f2;
3428#endif
3429 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003430
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003431 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003432 p = eval_next_non_blank(*arg, evalarg, &getnext);
3433 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003434 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003436
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003437 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003438 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003439 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003440 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003441 {
3442 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3443 {
mityu4ac198c2021-05-28 17:52:40 +02003444 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003445 clear_tv(rettv);
3446 return FAIL;
3447 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003448 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003451#ifdef FEAT_FLOAT
3452 f1 = 0;
3453 f2 = 0;
3454#endif
3455 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003456 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003458#ifdef FEAT_FLOAT
3459 if (rettv->v_type == VAR_FLOAT)
3460 {
3461 f1 = rettv->vval.v_float;
3462 use_float = TRUE;
3463 n1 = 0;
3464 }
3465 else
3466#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003467 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003468 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003469 if (error)
3470 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 }
3472 else
3473 n1 = 0;
3474
3475 /*
3476 * Get the second variable.
3477 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003478 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3479 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003480 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003481 clear_tv(rettv);
3482 return FAIL;
3483 }
3484 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003485 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 return FAIL;
3487
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003488 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003490#ifdef FEAT_FLOAT
3491 if (var2.v_type == VAR_FLOAT)
3492 {
3493 if (!use_float)
3494 {
3495 f1 = n1;
3496 use_float = TRUE;
3497 }
3498 f2 = var2.vval.v_float;
3499 n2 = 0;
3500 }
3501 else
3502#endif
3503 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003504 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003505 clear_tv(&var2);
3506 if (error)
3507 return FAIL;
3508#ifdef FEAT_FLOAT
3509 if (use_float)
3510 f2 = n2;
3511#endif
3512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513
3514 /*
3515 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003516 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003518#ifdef FEAT_FLOAT
3519 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003521 if (op == '*')
3522 f1 = f1 * f2;
3523 else if (op == '/')
3524 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003525# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003526 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003527 if (f2 == 0.0)
3528 {
3529 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003530 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003531 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003532 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003533 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003534 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003535 }
3536 else
3537 f1 = f1 / f2;
3538# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003539 // We rely on the floating point library to handle divide
3540 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003541 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003542# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003545 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003546 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003547 return FAIL;
3548 }
3549 rettv->v_type = VAR_FLOAT;
3550 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 }
3552 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003555 int failed = FALSE;
3556
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003557 if (op == '*')
3558 n1 = n1 * n2;
3559 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003560 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003562 n1 = num_modulus(n1, n2, &failed);
3563 if (failed)
3564 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003565
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003566 rettv->v_type = VAR_NUMBER;
3567 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 }
3570 }
3571
3572 return OK;
3573}
3574
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003575/*
3576 * Handle a type cast before a base level expression.
3577 * "arg" must point to the first non-white of the expression.
3578 * "arg" is advanced to just after the recognized expression.
3579 * Return OK or FAIL.
3580 */
3581 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003582eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003583 char_u **arg,
3584 typval_T *rettv,
3585 evalarg_T *evalarg,
3586 int want_string) // after "." operator
3587{
3588 type_T *want_type = NULL;
3589 garray_T type_list; // list of pointers to allocated types
3590 int res;
3591 int evaluate = evalarg == NULL ? 0
3592 : (evalarg->eval_flags & EVAL_EVALUATE);
3593
3594 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003595 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3596 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003597 {
3598 ++*arg;
3599 ga_init2(&type_list, sizeof(type_T *), 10);
3600 want_type = parse_type(arg, &type_list, TRUE);
3601 if (want_type == NULL && (evaluate || **arg != '>'))
3602 {
3603 clear_type_list(&type_list);
3604 return FAIL;
3605 }
3606
3607 if (**arg != '>')
3608 {
3609 if (*skipwhite(*arg) == '>')
3610 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3611 else
3612 emsg(_(e_missing_gt));
3613 clear_type_list(&type_list);
3614 return FAIL;
3615 }
3616 ++*arg;
3617 *arg = skipwhite_and_linebreak(*arg, evalarg);
3618 }
3619
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003620 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003621
3622 if (want_type != NULL && evaluate)
3623 {
3624 if (res == OK)
3625 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003626 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3627 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003628
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003629 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003630 {
3631 if (want_type == &t_bool && actual != &t_bool
3632 && (actual->tt_flags & TTFLAG_BOOL_OK))
3633 {
3634 int n = tv2bool(rettv);
3635
3636 // can use "0" and "1" for boolean in some places
3637 clear_tv(rettv);
3638 rettv->v_type = VAR_BOOL;
3639 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3640 }
3641 else
3642 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003643 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003644
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003645 where.wt_variable = TRUE;
3646 res = check_type(want_type, actual, TRUE, where);
3647 }
3648 }
3649 }
3650 clear_type_list(&type_list);
3651 }
3652
3653 return res;
3654}
3655
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003656 int
3657eval_leader(char_u **arg, int vim9)
3658{
3659 char_u *s = *arg;
3660 char_u *p = *arg;
3661
3662 while (*p == '!' || *p == '-' || *p == '+')
3663 {
3664 char_u *n = skipwhite(p + 1);
3665
3666 // ++, --, -+ and +- are not accepted in Vim9 script
3667 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3668 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003669 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003670 return FAIL;
3671 }
3672 p = n;
3673 }
3674 *arg = p;
3675 return OK;
3676}
3677
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003679 * Check for a predefined value "true", "false" and "null.*".
3680 * Return OK when recognized.
3681 */
3682 int
3683handle_predefined(char_u *s, int len, typval_T *rettv)
3684{
3685 switch (len)
3686 {
3687 case 4: if (STRNCMP(s, "true", 4) == 0)
3688 {
3689 rettv->v_type = VAR_BOOL;
3690 rettv->vval.v_number = VVAL_TRUE;
3691 return OK;
3692 }
3693 if (STRNCMP(s, "null", 4) == 0)
3694 {
3695 rettv->v_type = VAR_SPECIAL;
3696 rettv->vval.v_number = VVAL_NULL;
3697 return OK;
3698 }
3699 break;
3700 case 5: if (STRNCMP(s, "false", 5) == 0)
3701 {
3702 rettv->v_type = VAR_BOOL;
3703 rettv->vval.v_number = VVAL_FALSE;
3704 return OK;
3705 }
3706 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003707 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3708 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003709#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003710 rettv->v_type = VAR_JOB;
3711 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003712#else
3713 rettv->v_type = VAR_SPECIAL;
3714 rettv->vval.v_number = VVAL_NULL;
3715#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003716 return OK;
3717 }
3718 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003719 case 9:
3720 if (STRNCMP(s, "null_", 5) != 0)
3721 break;
3722 if (STRNCMP(s + 5, "list", 4) == 0)
3723 {
3724 rettv->v_type = VAR_LIST;
3725 rettv->vval.v_list = NULL;
3726 return OK;
3727 }
3728 if (STRNCMP(s + 5, "dict", 4) == 0)
3729 {
3730 rettv->v_type = VAR_DICT;
3731 rettv->vval.v_dict = NULL;
3732 return OK;
3733 }
3734 if (STRNCMP(s + 5, "blob", 4) == 0)
3735 {
3736 rettv->v_type = VAR_BLOB;
3737 rettv->vval.v_blob = NULL;
3738 return OK;
3739 }
3740 break;
3741 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3742 {
3743 rettv->v_type = VAR_STRING;
3744 rettv->vval.v_string = NULL;
3745 return OK;
3746 }
3747 break;
3748 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003749 if (STRNCMP(s, "null_channel", 12) == 0)
3750 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003751#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003752 rettv->v_type = VAR_CHANNEL;
3753 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003754#else
3755 rettv->v_type = VAR_SPECIAL;
3756 rettv->vval.v_number = VVAL_NULL;
3757#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003758 return OK;
3759 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003760 if (STRNCMP(s, "null_partial", 12) == 0)
3761 {
3762 rettv->v_type = VAR_PARTIAL;
3763 rettv->vval.v_partial = NULL;
3764 return OK;
3765 }
3766 break;
3767 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3768 {
3769 rettv->v_type = VAR_FUNC;
3770 rettv->vval.v_string = NULL;
3771 return OK;
3772 }
3773 break;
3774 }
3775 return FAIL;
3776}
3777
3778/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 * Handle sixth level expression:
3780 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003781 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003782 * "string" string constant
3783 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 * &option-name option value
3785 * @r register contents
3786 * identifier variable value
3787 * function() function call
3788 * $VAR environment variable
3789 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003790 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003792 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003793 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 *
3795 * Also handle:
3796 * ! in front logical NOT
3797 * - in front unary minus
3798 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003799 * trailing [] subscript in String or List
3800 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003801 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 *
3803 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003804 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 *
3806 * Return OK or FAIL.
3807 */
3808 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003809eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003810 char_u **arg,
3811 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003812 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003815 int evaluate = evalarg != NULL
3816 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 int len;
3818 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003819 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 char_u *start_leader, *end_leader;
3821 int ret = OK;
3822 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003823 static int recurse = 0;
3824 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825
3826 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003827 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003828 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003830 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831
3832 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003833 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 */
3835 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003836 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003837 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 end_leader = *arg;
3839
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003840 if (**arg == '.' && (!isdigit(*(*arg + 1))
3841#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003842 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003843#endif
3844 ))
3845 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003846 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003847 ++*arg;
3848 return FAIL;
3849 }
3850
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003851 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003852 // and crash. With MSVC the stack is smaller.
3853 if (recurse ==
3854#ifdef _MSC_VER
3855 300
3856#else
3857 1000
3858#endif
3859 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003860 {
3861 semsg(_(e_expression_too_recursive_str), *arg);
3862 return FAIL;
3863 }
3864 ++recurse;
3865
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 switch (**arg)
3867 {
3868 /*
3869 * Number constant.
3870 */
3871 case '0':
3872 case '1':
3873 case '2':
3874 case '3':
3875 case '4':
3876 case '5':
3877 case '6':
3878 case '7':
3879 case '8':
3880 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003881 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003882
3883 // Apply prefixed "-" and "+" now. Matters especially when
3884 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003885 if (ret == OK && evaluate && end_leader > start_leader
3886 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003887 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 break;
3889
3890 /*
3891 * String constant: "string".
3892 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003893 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 break;
3895
3896 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003897 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003899 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003900 break;
3901
3902 /*
3903 * List: [expr, expr]
3904 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003905 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 break;
3907
3908 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003909 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003910 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003911 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003912 {
3913 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3914 }
3915 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003916 {
3917 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003918 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003919 }
3920 else
3921 ret = NOTDONE;
3922 break;
3923
3924 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003925 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003926 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003927 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003928 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003929 ret = NOTDONE;
3930 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00003931 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003932 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003933 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003934 break;
3935
3936 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003937 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003939 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 break;
3941
3942 /*
3943 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01003944 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 */
LemonBoy2eaef102022-05-06 13:14:50 +01003946 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
3947 ret = eval_interp_string(arg, rettv, evaluate);
3948 else
3949 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 break;
3951
3952 /*
3953 * Register contents: @r.
3954 */
3955 case '@': ++*arg;
3956 if (evaluate)
3957 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003958 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003959 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003960 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003961 emsg_invreg(**arg);
3962 else
3963 {
3964 rettv->v_type = VAR_STRING;
3965 rettv->vval.v_string = get_reg_contents(**arg,
3966 GREG_EXPR_SRC);
3967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
3969 if (**arg != NUL)
3970 ++*arg;
3971 break;
3972
3973 /*
3974 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003975 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003977 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003978 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01003979 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003980 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003981 if (ret == OK && evaluate)
3982 {
3983 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3984
Bram Moolenaara9931532021-06-12 15:58:16 +02003985 // Compile it here to get the return type. The return
3986 // type is optional, when it's missing use t_unknown.
3987 // This is recognized in compile_return().
3988 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3989 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00003990 if (compile_def_function(ufunc, FALSE,
3991 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003992 {
3993 clear_tv(rettv);
3994 ret = FAIL;
3995 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003996 }
3997 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003998 if (ret == NOTDONE)
3999 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004000 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004001 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004002
Bram Moolenaar9215f012020-06-27 21:18:00 +02004003 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004004 if (**arg == ')')
4005 ++*arg;
4006 else if (ret == OK)
4007 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004008 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004009 clear_tv(rettv);
4010 ret = FAIL;
4011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 }
4013 break;
4014
Bram Moolenaar8c711452005-01-14 21:53:12 +00004015 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 break;
4017 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004018
4019 if (ret == NOTDONE)
4020 {
4021 /*
4022 * Must be a variable or function name.
4023 * Can also be a curly-braces kind of name: {expr}.
4024 */
4025 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004026 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004027 if (alias != NULL)
4028 s = alias;
4029
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004030 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004031 ret = FAIL;
4032 else
4033 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004034 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4035
Bram Moolenaar4525a572022-02-13 11:57:33 +00004036 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004037 {
4038 emsg(_(e_cannot_use_underscore_here));
4039 ret = FAIL;
4040 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004041 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004042 && s[0] == 's' && s[1] == ':')
4043 {
4044 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4045 ret = FAIL;
4046 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004047 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004048 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004049 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004050 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004051 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004052 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02004053 else if (flags & EVAL_CONSTANT)
4054 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004055 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004056 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004057 // get the value of "true", "false", etc. or a variable
4058 ret = FAIL;
4059 if (vim9script)
4060 ret = handle_predefined(s, len, rettv);
4061 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004062 {
4063 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004064 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004065 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004066 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004067 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004068 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004069 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004070 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004071 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004072 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004073 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004074 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004075 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004076 }
4077
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004078 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4079 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004080 if (ret == OK)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004081 ret = handle_subscript(arg, name_start, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082
4083 /*
4084 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4085 */
4086 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004087 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004088
4089 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004090 return ret;
4091}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004092
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004093/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004094 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004095 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004096 * Adjusts "end_leaderp" until it is at "start_leader".
4097 */
4098 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004099eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004100 typval_T *rettv,
4101 int numeric_only,
4102 char_u *start_leader,
4103 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004104{
4105 char_u *end_leader = *end_leaderp;
4106 int ret = OK;
4107 int error = FALSE;
4108 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004109 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004110 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004111#ifdef FEAT_FLOAT
4112 float_T f = 0.0;
4113
4114 if (rettv->v_type == VAR_FLOAT)
4115 f = rettv->vval.v_float;
4116 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004117#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004118 {
4119 while (VIM_ISWHITE(end_leader[-1]))
4120 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004121 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004122 val = tv2bool(rettv);
4123 else
4124 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004125 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004126 if (error)
4127 {
4128 clear_tv(rettv);
4129 ret = FAIL;
4130 }
4131 else
4132 {
4133 while (end_leader > start_leader)
4134 {
4135 --end_leader;
4136 if (*end_leader == '!')
4137 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004138 if (numeric_only)
4139 {
4140 ++end_leader;
4141 break;
4142 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004143#ifdef FEAT_FLOAT
4144 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004145 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004146 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004147 {
4148 rettv->v_type = VAR_BOOL;
4149 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4150 }
4151 else
4152 f = !f;
4153 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004154 else
4155#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004156 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004157 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004158 type = VAR_BOOL;
4159 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004160 }
4161 else if (*end_leader == '-')
4162 {
4163#ifdef FEAT_FLOAT
4164 if (rettv->v_type == VAR_FLOAT)
4165 f = -f;
4166 else
4167#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004168 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004169 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004170 type = VAR_NUMBER;
4171 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004172 }
4173 }
4174#ifdef FEAT_FLOAT
4175 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004177 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004178 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004180 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004181#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004182 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004183 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004184 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004185 rettv->v_type = type;
4186 else
4187 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004188 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004191 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 return ret;
4193}
4194
4195/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004196 * Call the function referred to in "rettv".
4197 */
4198 static int
4199call_func_rettv(
4200 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004201 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004202 typval_T *rettv,
4203 int evaluate,
4204 dict_T *selfdict,
4205 typval_T *basetv)
4206{
4207 partial_T *pt = NULL;
4208 funcexe_T funcexe;
4209 typval_T functv;
4210 char_u *s;
4211 int ret;
4212
4213 // need to copy the funcref so that we can clear rettv
4214 if (evaluate)
4215 {
4216 functv = *rettv;
4217 rettv->v_type = VAR_UNKNOWN;
4218
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004219 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004220 if (functv.v_type == VAR_PARTIAL)
4221 {
4222 pt = functv.vval.v_partial;
4223 s = partial_name(pt);
4224 }
4225 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004226 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004227 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004228 if (s == NULL || *s == NUL)
4229 {
4230 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004231 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004232 goto theend;
4233 }
4234 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004235 }
4236 else
4237 s = (char_u *)"";
4238
Bram Moolenaara80faa82020-04-12 19:37:17 +02004239 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004240 funcexe.fe_firstline = curwin->w_cursor.lnum;
4241 funcexe.fe_lastline = curwin->w_cursor.lnum;
4242 funcexe.fe_evaluate = evaluate;
4243 funcexe.fe_partial = pt;
4244 funcexe.fe_selfdict = selfdict;
4245 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004246 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004247
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004248theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004249 // Clear the funcref afterwards, so that deleting it while
4250 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004251 if (evaluate)
4252 clear_tv(&functv);
4253
4254 return ret;
4255}
4256
4257/*
4258 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004259 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004260 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4261 */
4262 static int
4263eval_lambda(
4264 char_u **arg,
4265 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004266 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004267 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004268{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004269 int evaluate = evalarg != NULL
4270 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004271 typval_T base = *rettv;
4272 int ret;
4273
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004274 rettv->v_type = VAR_UNKNOWN;
4275
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004276 if (**arg == '{')
4277 {
4278 // ->{lambda}()
4279 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4280 }
4281 else
4282 {
4283 // ->(lambda)()
4284 ++*arg;
4285 ret = eval1(arg, rettv, evalarg);
4286 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004287 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004288 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004289 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004290 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004291 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004292 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4293 && rettv->v_type != VAR_PARTIAL)
4294 {
4295 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4296 return FAIL;
4297 }
4298 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004299 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004300 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004301 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004302
4303 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004304 {
4305 if (verbose)
4306 {
4307 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004308 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004309 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004310 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004311 }
4312 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004313 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004314 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004315 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004316 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004317
4318 // Clear the funcref afterwards, so that deleting it while
4319 // evaluating the arguments is possible (see test55).
4320 if (evaluate)
4321 clear_tv(&base);
4322
4323 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004324}
4325
4326/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004327 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004328 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004329 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4330 */
4331 static int
4332eval_method(
4333 char_u **arg,
4334 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004335 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004336 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004337{
4338 char_u *name;
4339 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004340 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004341 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004342 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004343 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004344 int evaluate = evalarg != NULL
4345 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004346
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004347 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004348
Bram Moolenaarac92e252019-08-03 21:58:38 +02004349 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004350 len = get_name_len(arg, &alias, evaluate, TRUE);
4351 if (alias != NULL)
4352 name = alias;
4353
4354 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004355 {
4356 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004357 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004358 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004359 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004360 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004361 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004362 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004363
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004364 // If there is no "(" immediately following, but there is further on,
4365 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4366 // Does not handle anything where "(" is part of the expression.
4367 *arg = skipwhite(*arg);
4368
4369 if (**arg != '(' && alias == NULL
4370 && (paren = vim_strchr(*arg, '(')) != NULL)
4371 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004372 char_u *deref;
4373
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004374 *arg = name;
4375 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004376 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4377 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004378 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004379 *arg = name + len;
4380 ret = FAIL;
4381 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004382 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004383 {
4384 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004385 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004386 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004387 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004388 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004389
4390 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004391 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004392 *arg = skipwhite(*arg);
4393
4394 if (**arg != '(')
4395 {
4396 if (verbose)
4397 semsg(_(e_missing_parenthesis_str), name);
4398 ret = FAIL;
4399 }
4400 else if (VIM_ISWHITE((*arg)[-1]))
4401 {
4402 if (verbose)
4403 emsg(_(e_no_white_space_allowed_before_parenthesis));
4404 ret = FAIL;
4405 }
4406 else
4407 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004408 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004409 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004410 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004411
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004412 // Clear the funcref afterwards, so that deleting it while
4413 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004414 if (evaluate)
4415 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004416 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004417
Bram Moolenaarac92e252019-08-03 21:58:38 +02004418 return ret;
4419}
4420
4421/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004422 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4423 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004424 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4425 */
4426 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004427eval_index(
4428 char_u **arg,
4429 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004430 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004431 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004432{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004433 int evaluate = evalarg != NULL
4434 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004435 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004436 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004437 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004438 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004439 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004440 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004441
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004442 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4443 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004444
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004445 init_tv(&var1);
4446 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004447 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004448 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004449 /*
4450 * dict.name
4451 */
4452 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004453 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004454 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004455 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004456 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004457 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004458 }
4459 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004460 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004461 /*
4462 * something[idx]
4463 *
4464 * Get the (first) variable from inside the [].
4465 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004466 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004467 if (**arg == ':')
4468 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004469 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004470 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004471 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004472 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004473 semsg(_(e_white_space_required_before_and_after_str_at_str),
4474 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004475 clear_tv(&var1);
4476 return FAIL;
4477 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004478 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004479 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004480 int error = FALSE;
4481
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004482#ifdef FEAT_FLOAT
4483 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004484 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004485 && var1.v_type == VAR_FLOAT)
4486 {
4487 var1.vval.v_string = typval_tostring(&var1, TRUE);
4488 var1.v_type = VAR_STRING;
4489 }
4490#endif
Bram Moolenaar4525a572022-02-13 11:57:33 +00004491 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004492 tv_get_number_chk(&var1, &error);
4493 else
4494 error = tv_get_string_chk(&var1) == NULL;
4495 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004496 {
4497 // not a number or string
4498 clear_tv(&var1);
4499 return FAIL;
4500 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004501 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004502
4503 /*
4504 * Get the second variable from inside the [:].
4505 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004506 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004507 if (**arg == ':')
4508 {
4509 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004510 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004511 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004512 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004513 semsg(_(e_white_space_required_before_and_after_str_at_str),
4514 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004515 if (!empty1)
4516 clear_tv(&var1);
4517 return FAIL;
4518 }
4519 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004520 if (**arg == ']')
4521 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004522 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004523 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004524 if (!empty1)
4525 clear_tv(&var1);
4526 return FAIL;
4527 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004528 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004529 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004530 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004531 if (!empty1)
4532 clear_tv(&var1);
4533 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004534 return FAIL;
4535 }
4536 }
4537
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004538 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004539 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004540 if (**arg != ']')
4541 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004542 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004543 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004544 clear_tv(&var1);
4545 if (range)
4546 clear_tv(&var2);
4547 return FAIL;
4548 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004549 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004550 }
4551
4552 if (evaluate)
4553 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004554 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004555 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004556 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004557
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004558 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004559 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004560 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004561 clear_tv(&var2);
4562 return res;
4563 }
4564 return OK;
4565}
4566
4567/*
4568 * Check if "rettv" can have an [index] or [sli:ce]
4569 */
4570 int
4571check_can_index(typval_T *rettv, int evaluate, int verbose)
4572{
4573 switch (rettv->v_type)
4574 {
4575 case VAR_FUNC:
4576 case VAR_PARTIAL:
4577 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004578 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004579 return FAIL;
4580 case VAR_FLOAT:
4581#ifdef FEAT_FLOAT
4582 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004583 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004584 return FAIL;
4585#endif
4586 case VAR_BOOL:
4587 case VAR_SPECIAL:
4588 case VAR_JOB:
4589 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004590 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004591 if (verbose)
4592 emsg(_(e_cannot_index_special_variable));
4593 return FAIL;
4594 case VAR_UNKNOWN:
4595 case VAR_ANY:
4596 case VAR_VOID:
4597 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004598 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004599 emsg(_(e_cannot_index_special_variable));
4600 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004601 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004602 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004603
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004604 case VAR_STRING:
4605 case VAR_LIST:
4606 case VAR_DICT:
4607 case VAR_BLOB:
4608 break;
4609 case VAR_NUMBER:
4610 if (in_vim9script())
4611 emsg(_(e_cannot_index_number));
4612 break;
4613 }
4614 return OK;
4615}
4616
4617/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004618 * slice() function
4619 */
4620 void
4621f_slice(typval_T *argvars, typval_T *rettv)
4622{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004623 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004624 && ((argvars[0].v_type != VAR_STRING
4625 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004626 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004627 && check_for_list_arg(argvars, 0) == FAIL)
4628 || check_for_number_arg(argvars, 1) == FAIL
4629 || check_for_opt_number_arg(argvars, 2) == FAIL))
4630 return;
4631
Bram Moolenaar6601b622021-01-13 21:47:15 +01004632 if (check_can_index(argvars, TRUE, FALSE) == OK)
4633 {
4634 copy_tv(argvars, rettv);
4635 eval_index_inner(rettv, TRUE, argvars + 1,
4636 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4637 TRUE, NULL, 0, FALSE);
4638 }
4639}
4640
4641/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004642 * Apply index or range to "rettv".
4643 * "var1" is the first index, NULL for [:expr].
4644 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004645 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4646 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004647 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4648 */
4649 int
4650eval_index_inner(
4651 typval_T *rettv,
4652 int is_range,
4653 typval_T *var1,
4654 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004655 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004656 char_u *key,
4657 int keylen,
4658 int verbose)
4659{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004660 varnumber_T n1, n2 = 0;
4661 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004662
4663 n1 = 0;
4664 if (var1 != NULL && rettv->v_type != VAR_DICT)
4665 n1 = tv_get_number(var1);
4666
4667 if (is_range)
4668 {
4669 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004670 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004671 if (verbose)
4672 emsg(_(e_cannot_slice_dictionary));
4673 return FAIL;
4674 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004675 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004676 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004677 else
4678 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004679 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004680
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004681 switch (rettv->v_type)
4682 {
4683 case VAR_UNKNOWN:
4684 case VAR_ANY:
4685 case VAR_VOID:
4686 case VAR_FUNC:
4687 case VAR_PARTIAL:
4688 case VAR_FLOAT:
4689 case VAR_BOOL:
4690 case VAR_SPECIAL:
4691 case VAR_JOB:
4692 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004693 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004694 break; // not evaluating, skipping over subscript
4695
4696 case VAR_NUMBER:
4697 case VAR_STRING:
4698 {
4699 char_u *s = tv_get_string(rettv);
4700
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004701 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004702 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004703 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004704 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004705 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004706 else
4707 s = char_from_string(s, n1);
4708 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004709 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004710 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004711 // The resulting variable is a substring. If the indexes
4712 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004713 if (n1 < 0)
4714 {
4715 n1 = len + n1;
4716 if (n1 < 0)
4717 n1 = 0;
4718 }
4719 if (n2 < 0)
4720 n2 = len + n2;
4721 else if (n2 >= len)
4722 n2 = len;
4723 if (n1 >= len || n2 < 0 || n1 > n2)
4724 s = NULL;
4725 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004726 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004727 }
4728 else
4729 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004730 // The resulting variable is a string of a single
4731 // character. If the index is too big or negative the
4732 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004733 if (n1 >= len || n1 < 0)
4734 s = NULL;
4735 else
4736 s = vim_strnsave(s + n1, 1);
4737 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004738 clear_tv(rettv);
4739 rettv->v_type = VAR_STRING;
4740 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004741 }
4742 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004743
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004744 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004745 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4746 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004747 break;
4748
4749 case VAR_LIST:
4750 if (var1 == NULL)
4751 n1 = 0;
4752 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004753 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004754 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004755 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004756 return FAIL;
4757 break;
4758
4759 case VAR_DICT:
4760 {
4761 dictitem_T *item;
4762 typval_T tmp;
4763
4764 if (key == NULL)
4765 {
4766 key = tv_get_string_chk(var1);
4767 if (key == NULL)
4768 return FAIL;
4769 }
4770
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004771 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004772
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004773 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004774 {
4775 if (verbose)
4776 {
4777 if (keylen > 0)
4778 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004779 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004780 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004781 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004782 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004783
4784 copy_tv(&item->di_tv, &tmp);
4785 clear_tv(rettv);
4786 *rettv = tmp;
4787 }
4788 break;
4789 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004790 return OK;
4791}
4792
4793/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004794 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004795 */
4796 char_u *
4797partial_name(partial_T *pt)
4798{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004799 if (pt != NULL)
4800 {
4801 if (pt->pt_name != NULL)
4802 return pt->pt_name;
4803 if (pt->pt_func != NULL)
4804 return pt->pt_func->uf_name;
4805 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004806 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004807}
4808
Bram Moolenaarddecc252016-04-06 22:59:37 +02004809 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004810partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004811{
4812 int i;
4813
4814 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004815 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004816 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004817 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004818 if (pt->pt_name != NULL)
4819 {
4820 func_unref(pt->pt_name);
4821 vim_free(pt->pt_name);
4822 }
4823 else
4824 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004825
Bram Moolenaar54656012021-06-09 20:50:46 +02004826 // "out_up" is no longer used, decrement refcount on partial that owns it.
4827 partial_unref(pt->pt_outer.out_up_partial);
4828
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004829 // Using pt_outer from another partial.
4830 partial_unref(pt->pt_outer_partial);
4831
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004832 // Decrease the reference count for the context of a closure. If down
4833 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004834 if (pt->pt_funcstack != NULL)
4835 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004836 --pt->pt_funcstack->fs_refcount;
4837 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004838 }
4839
Bram Moolenaarddecc252016-04-06 22:59:37 +02004840 vim_free(pt);
4841}
4842
4843/*
4844 * Unreference a closure: decrement the reference count and free it when it
4845 * becomes zero.
4846 */
4847 void
4848partial_unref(partial_T *pt)
4849{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004850 if (pt != NULL)
4851 {
4852 if (--pt->pt_refcount <= 0)
4853 partial_free(pt);
4854
4855 // If the reference count goes down to one, the funcstack may be the
4856 // only reference and can be freed if no other partials reference it.
4857 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4858 funcstack_check_refcount(pt->pt_funcstack);
4859 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004860}
4861
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004862/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004863 * Return the next (unique) copy ID.
4864 * Used for serializing nested structures.
4865 */
4866 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004867get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004868{
4869 current_copyID += COPYID_INC;
4870 return current_copyID;
4871}
4872
4873/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004874 * Garbage collection for lists and dictionaries.
4875 *
4876 * We use reference counts to be able to free most items right away when they
4877 * are no longer used. But for composite items it's possible that it becomes
4878 * unused while the reference count is > 0: When there is a recursive
4879 * reference. Example:
4880 * :let l = [1, 2, 3]
4881 * :let d = {9: l}
4882 * :let l[1] = d
4883 *
4884 * Since this is quite unusual we handle this with garbage collection: every
4885 * once in a while find out which lists and dicts are not referenced from any
4886 * variable.
4887 *
4888 * Here is a good reference text about garbage collection (refers to Python
4889 * but it applies to all reference-counting mechanisms):
4890 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004891 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004892
4893/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004894 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004895 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004896 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004897 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004898 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004899garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004900{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004901 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004902 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004903 buf_T *buf;
4904 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004905 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004906 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004907
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004908 if (!testing)
4909 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004910 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004911 want_garbage_collect = FALSE;
4912 may_garbage_collect = FALSE;
4913 garbage_collect_at_exit = FALSE;
4914 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004915
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004916 // The execution stack can grow big, limit the size.
4917 if (exestack.ga_maxlen - exestack.ga_len > 500)
4918 {
4919 size_t new_len;
4920 char_u *pp;
4921 int n;
4922
4923 // Keep 150% of the current size, with a minimum of the growth size.
4924 n = exestack.ga_len / 2;
4925 if (n < exestack.ga_growsize)
4926 n = exestack.ga_growsize;
4927
4928 // Don't make it bigger though.
4929 if (exestack.ga_len + n < exestack.ga_maxlen)
4930 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004931 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004932 pp = vim_realloc(exestack.ga_data, new_len);
4933 if (pp == NULL)
4934 return FAIL;
4935 exestack.ga_maxlen = exestack.ga_len + n;
4936 exestack.ga_data = pp;
4937 }
4938 }
4939
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004940 // We advance by two because we add one for items referenced through
4941 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004942 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004943
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004944 /*
4945 * 1. Go through all accessible variables and mark all lists and dicts
4946 * with copyID.
4947 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004948
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004949 // Don't free variables in the previous_funccal list unless they are only
4950 // referenced through previous_funccal. This must be first, because if
4951 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004952 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004953
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004954 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004955 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004956
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004957 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004958 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004959 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4960 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004961
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004962 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004963 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004964 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4965 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004966 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004967 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4968 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004969#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004970 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004971 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4972 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004973 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004974 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004975 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4976 NULL, NULL);
4977#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004978
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004979 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004980 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004981 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4982 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004983 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004984 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004985
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004986 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004987 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004988
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004989 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004990 abort = abort || set_ref_in_functions(copyID);
4991
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004992 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004993 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004994
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004995 // funcstacks keep variables for closures
4996 abort = abort || set_ref_in_funcstacks(copyID);
4997
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004998 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004999 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005000
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005001 // callbacks in buffers
5002 abort = abort || set_ref_in_buffers(copyID);
5003
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005004 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5005 abort = abort || set_ref_in_insexpand_funcs(copyID);
5006
5007 // 'operatorfunc' callback
5008 abort = abort || set_ref_in_opfunc(copyID);
5009
5010 // 'tagfunc' callback
5011 abort = abort || set_ref_in_tagfunc(copyID);
5012
5013 // 'imactivatefunc' and 'imstatusfunc' callbacks
5014 abort = abort || set_ref_in_im_funcs(copyID);
5015
Bram Moolenaar1dced572012-04-05 16:54:08 +02005016#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005017 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005018#endif
5019
Bram Moolenaardb913952012-06-29 12:54:53 +02005020#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005021 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005022#endif
5023
5024#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005025 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005026#endif
5027
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005028#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005029 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005030 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005031#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005032#ifdef FEAT_NETBEANS_INTG
5033 abort = abort || set_ref_in_nb_channel(copyID);
5034#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005035
Bram Moolenaare3188e22016-05-31 21:13:04 +02005036#ifdef FEAT_TIMERS
5037 abort = abort || set_ref_in_timer(copyID);
5038#endif
5039
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005040#ifdef FEAT_QUICKFIX
5041 abort = abort || set_ref_in_quickfix(copyID);
5042#endif
5043
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005044#ifdef FEAT_TERMINAL
5045 abort = abort || set_ref_in_term(copyID);
5046#endif
5047
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005048#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005049 abort = abort || set_ref_in_popups(copyID);
5050#endif
5051
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005052 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005053 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005054 /*
5055 * 2. Free lists and dictionaries that are not referenced.
5056 */
5057 did_free = free_unref_items(copyID);
5058
5059 /*
5060 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005061 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005062 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005063 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005064 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005065 else if (p_verbose > 0)
5066 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005067 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005068 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005069
5070 return did_free;
5071}
5072
5073/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005074 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005075 */
5076 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005077free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005078{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005079 int did_free = FALSE;
5080
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005081 // Let all "free" functions know that we are here. This means no
5082 // dictionaries, lists, channels or jobs are to be freed, because we will
5083 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005084 in_free_unref_items = TRUE;
5085
5086 /*
5087 * PASS 1: free the contents of the items. We don't free the items
5088 * themselves yet, so that it is possible to decrement refcount counters
5089 */
5090
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005091 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005092 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005093
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005094 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005095 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005096
5097#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005098 // Go through the list of jobs and free items without the copyID. This
5099 // must happen before doing channels, because jobs refer to channels, but
5100 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005101 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5102
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005103 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005104 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5105#endif
5106
5107 /*
5108 * PASS 2: free the items themselves.
5109 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005110 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005111 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005112
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005113#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005114 // Go through the list of jobs and free items without the copyID. This
5115 // must happen before doing channels, because jobs refer to channels, but
5116 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005117 free_unused_jobs(copyID, COPYID_MASK);
5118
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005119 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005120 free_unused_channels(copyID, COPYID_MASK);
5121#endif
5122
5123 in_free_unref_items = FALSE;
5124
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005125 return did_free;
5126}
5127
5128/*
5129 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005130 * "list_stack" is used to add lists to be marked. Can be NULL.
5131 *
5132 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005133 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005134 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005135set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005136{
5137 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005138 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005139 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005140 hashtab_T *cur_ht;
5141 ht_stack_T *ht_stack = NULL;
5142 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005143
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005144 cur_ht = ht;
5145 for (;;)
5146 {
5147 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005148 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005149 // Mark each item in the hashtab. If the item contains a hashtab
5150 // it is added to ht_stack, if it contains a list it is added to
5151 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005152 todo = (int)cur_ht->ht_used;
5153 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5154 if (!HASHITEM_EMPTY(hi))
5155 {
5156 --todo;
5157 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5158 &ht_stack, list_stack);
5159 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005160 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005161
5162 if (ht_stack == NULL)
5163 break;
5164
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005165 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005166 cur_ht = ht_stack->ht;
5167 tempitem = ht_stack;
5168 ht_stack = ht_stack->prev;
5169 free(tempitem);
5170 }
5171
5172 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005173}
5174
Dominique Pelle748b3082022-01-08 12:41:16 +00005175#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5176 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005177/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005178 * Mark a dict and its items with "copyID".
5179 * Returns TRUE if setting references failed somehow.
5180 */
5181 int
5182set_ref_in_dict(dict_T *d, int copyID)
5183{
5184 if (d != NULL && d->dv_copyID != copyID)
5185 {
5186 d->dv_copyID = copyID;
5187 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5188 }
5189 return FALSE;
5190}
Dominique Pelle748b3082022-01-08 12:41:16 +00005191#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005192
5193/*
5194 * Mark a list and its items with "copyID".
5195 * Returns TRUE if setting references failed somehow.
5196 */
5197 int
5198set_ref_in_list(list_T *ll, int copyID)
5199{
5200 if (ll != NULL && ll->lv_copyID != copyID)
5201 {
5202 ll->lv_copyID = copyID;
5203 return set_ref_in_list_items(ll, copyID, NULL);
5204 }
5205 return FALSE;
5206}
5207
5208/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005209 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005210 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5211 *
5212 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005213 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005214 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005215set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005216{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005217 listitem_T *li;
5218 int abort = FALSE;
5219 list_T *cur_l;
5220 list_stack_T *list_stack = NULL;
5221 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005222
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005223 cur_l = l;
5224 for (;;)
5225 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005226 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005227 // Mark each item in the list. If the item contains a hashtab
5228 // it is added to ht_stack, if it contains a list it is added to
5229 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005230 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5231 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5232 ht_stack, &list_stack);
5233 if (list_stack == NULL)
5234 break;
5235
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005236 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005237 cur_l = list_stack->list;
5238 tempitem = list_stack;
5239 list_stack = list_stack->prev;
5240 free(tempitem);
5241 }
5242
5243 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005244}
5245
5246/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005247 * Mark the partial in callback 'cb' with "copyID".
5248 */
5249 int
5250set_ref_in_callback(callback_T *cb, int copyID)
5251{
5252 typval_T tv;
5253
5254 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5255 return FALSE;
5256
5257 tv.v_type = VAR_PARTIAL;
5258 tv.vval.v_partial = cb->cb_partial;
5259 return set_ref_in_item(&tv, copyID, NULL, NULL);
5260}
5261
5262/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005263 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005264 * "list_stack" is used to add lists to be marked. Can be NULL.
5265 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5266 *
5267 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005268 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005269 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005270set_ref_in_item(
5271 typval_T *tv,
5272 int copyID,
5273 ht_stack_T **ht_stack,
5274 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005275{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005276 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005277
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005278 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005279 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005280 dict_T *dd = tv->vval.v_dict;
5281
Bram Moolenaara03f2332016-02-06 18:09:59 +01005282 if (dd != NULL && dd->dv_copyID != copyID)
5283 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005284 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005285 dd->dv_copyID = copyID;
5286 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005287 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005288 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5289 }
5290 else
5291 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005292 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5293
Bram Moolenaara03f2332016-02-06 18:09:59 +01005294 if (newitem == NULL)
5295 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005296 else
5297 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005298 newitem->ht = &dd->dv_hashtab;
5299 newitem->prev = *ht_stack;
5300 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005301 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005302 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005303 }
5304 }
5305 else if (tv->v_type == VAR_LIST)
5306 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005307 list_T *ll = tv->vval.v_list;
5308
Bram Moolenaara03f2332016-02-06 18:09:59 +01005309 if (ll != NULL && ll->lv_copyID != copyID)
5310 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005311 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005312 ll->lv_copyID = copyID;
5313 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005314 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005315 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005316 }
5317 else
5318 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005319 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5320
Bram Moolenaara03f2332016-02-06 18:09:59 +01005321 if (newitem == NULL)
5322 abort = TRUE;
5323 else
5324 {
5325 newitem->list = ll;
5326 newitem->prev = *list_stack;
5327 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005328 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005329 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005330 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005331 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005332 else if (tv->v_type == VAR_FUNC)
5333 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005334 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005335 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005336 else if (tv->v_type == VAR_PARTIAL)
5337 {
5338 partial_T *pt = tv->vval.v_partial;
5339 int i;
5340
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005341 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005342 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005343 // Didn't see this partial yet.
5344 pt->pt_copyID = copyID;
5345
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005346 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005347
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005348 if (pt->pt_dict != NULL)
5349 {
5350 typval_T dtv;
5351
5352 dtv.v_type = VAR_DICT;
5353 dtv.vval.v_dict = pt->pt_dict;
5354 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5355 }
5356
5357 for (i = 0; i < pt->pt_argc; ++i)
5358 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5359 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005360 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005361 }
5362 }
5363#ifdef FEAT_JOB_CHANNEL
5364 else if (tv->v_type == VAR_JOB)
5365 {
5366 job_T *job = tv->vval.v_job;
5367 typval_T dtv;
5368
5369 if (job != NULL && job->jv_copyID != copyID)
5370 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005371 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005372 if (job->jv_channel != NULL)
5373 {
5374 dtv.v_type = VAR_CHANNEL;
5375 dtv.vval.v_channel = job->jv_channel;
5376 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5377 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005378 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005379 {
5380 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005381 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005382 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5383 }
5384 }
5385 }
5386 else if (tv->v_type == VAR_CHANNEL)
5387 {
5388 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005389 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005390 typval_T dtv;
5391 jsonq_T *jq;
5392 cbq_T *cq;
5393
5394 if (ch != NULL && ch->ch_copyID != copyID)
5395 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005396 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005397 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005398 {
5399 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5400 jq = jq->jq_next)
5401 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5402 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5403 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005404 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005405 {
5406 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005407 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005408 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5409 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005410 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005411 {
5412 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005413 dtv.vval.v_partial =
5414 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005415 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5416 }
5417 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005418 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005419 {
5420 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005421 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005422 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5423 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005424 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005425 {
5426 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005427 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005428 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5429 }
5430 }
5431 }
5432#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005433 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005434}
5435
Bram Moolenaar8c711452005-01-14 21:53:12 +00005436/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 * Return a string with the string representation of a variable.
5438 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005439 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005440 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005441 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005442 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005443 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005444 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5445 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005446 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005447 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005448 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005449echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005450 typval_T *tv,
5451 char_u **tofree,
5452 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005453 int copyID,
5454 int echo_style,
5455 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005456 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005457{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005458 static int recurse = 0;
5459 char_u *r = NULL;
5460
Bram Moolenaar33570922005-01-25 22:26:29 +00005461 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005462 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005463 if (!did_echo_string_emsg)
5464 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005465 // Only give this message once for a recursive call to avoid
5466 // flooding the user with errors. And stop iterating over lists
5467 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005468 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005469 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005470 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005471 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005472 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005473 }
5474 ++recurse;
5475
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005476 switch (tv->v_type)
5477 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005478 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005479 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005480 {
5481 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005482 r = tv->vval.v_string;
5483 if (r == NULL)
5484 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005485 }
5486 else
5487 {
5488 *tofree = string_quote(tv->vval.v_string, FALSE);
5489 r = *tofree;
5490 }
5491 break;
5492
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005494 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005495 char_u buf[MAX_FUNC_NAME_LEN];
5496
5497 if (echo_style)
5498 {
LemonBoya5d35902022-04-29 21:15:02 +01005499 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5500 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005501 buf, MAX_FUNC_NAME_LEN);
5502 if (r == buf)
5503 {
5504 r = vim_strsave(buf);
5505 *tofree = r;
5506 }
5507 else
5508 *tofree = NULL;
5509 }
5510 else
5511 {
5512 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5513 : make_ufunc_name_readable(
5514 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5515 TRUE);
5516 r = *tofree;
5517 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005518 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005519 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005520
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005521 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005522 {
5523 partial_T *pt = tv->vval.v_partial;
5524 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005525 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005526 garray_T ga;
5527 int i;
5528 char_u *tf;
5529
5530 ga_init2(&ga, 1, 100);
5531 ga_concat(&ga, (char_u *)"function(");
5532 if (fname != NULL)
5533 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005534 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005535 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005536 && vim_isupper(fname[1]))
5537 {
5538 ga_concat(&ga, (char_u *)"'g:");
5539 ga_concat(&ga, fname + 1);
5540 }
5541 else
5542 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005543 vim_free(fname);
5544 }
5545 if (pt != NULL && pt->pt_argc > 0)
5546 {
5547 ga_concat(&ga, (char_u *)", [");
5548 for (i = 0; i < pt->pt_argc; ++i)
5549 {
5550 if (i > 0)
5551 ga_concat(&ga, (char_u *)", ");
5552 ga_concat(&ga,
5553 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5554 vim_free(tf);
5555 }
5556 ga_concat(&ga, (char_u *)"]");
5557 }
5558 if (pt != NULL && pt->pt_dict != NULL)
5559 {
5560 typval_T dtv;
5561
5562 ga_concat(&ga, (char_u *)", ");
5563 dtv.v_type = VAR_DICT;
5564 dtv.vval.v_dict = pt->pt_dict;
5565 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5566 vim_free(tf);
5567 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005568 // terminate with ')' and a NUL
5569 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005570
5571 *tofree = ga.ga_data;
5572 r = *tofree;
5573 break;
5574 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005575
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005576 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005577 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005578 break;
5579
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005580 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005581 if (tv->vval.v_list == NULL)
5582 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005583 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005584 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005585 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005586 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005587 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5588 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005589 {
5590 *tofree = NULL;
5591 r = (char_u *)"[...]";
5592 }
5593 else
5594 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005595 int old_copyID = tv->vval.v_list->lv_copyID;
5596
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005597 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005598 *tofree = list2string(tv, copyID, restore_copyID);
5599 if (restore_copyID)
5600 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005601 r = *tofree;
5602 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005603 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005604
Bram Moolenaar8c711452005-01-14 21:53:12 +00005605 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005606 if (tv->vval.v_dict == NULL)
5607 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005608 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005609 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005610 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005611 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005612 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5613 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005614 {
5615 *tofree = NULL;
5616 r = (char_u *)"{...}";
5617 }
5618 else
5619 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005620 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005621
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005622 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005623 *tofree = dict2string(tv, copyID, restore_copyID);
5624 if (restore_copyID)
5625 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005626 r = *tofree;
5627 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005628 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005629
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005630 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005631 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005632 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005633 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005634 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005635 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005636 break;
5637
Bram Moolenaar835dc632016-02-07 14:27:38 +01005638 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005639 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005640#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005641 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005642 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5643 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005644 if (composite_val)
5645 {
5646 *tofree = string_quote(r, FALSE);
5647 r = *tofree;
5648 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005649#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005650 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005651
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005652 case VAR_INSTR:
5653 *tofree = NULL;
5654 r = (char_u *)"instructions";
5655 break;
5656
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005657 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005658#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005659 *tofree = NULL;
5660 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5661 r = numbuf;
5662 break;
5663#endif
5664
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005665 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005666 case VAR_SPECIAL:
5667 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005668 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005669 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005670 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005671
Bram Moolenaar8502c702014-06-17 12:51:16 +02005672 if (--recurse == 0)
5673 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005674 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005675}
5676
5677/*
5678 * Return a string with the string representation of a variable.
5679 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5680 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005681 * Does not put quotes around strings, as ":echo" displays values.
5682 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5683 * May return NULL.
5684 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005685 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005686echo_string(
5687 typval_T *tv,
5688 char_u **tofree,
5689 char_u *numbuf,
5690 int copyID)
5691{
5692 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5693}
5694
5695/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005696 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5697 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005698 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005699 */
5700 int
5701buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5702{
5703 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005704 char_u *t;
5705 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005706
5707 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5708 return -1;
5709
5710 if (lnum > buf->b_ml.ml_line_count)
5711 lnum = buf->b_ml.ml_line_count;
5712
5713 str = ml_get_buf(buf, lnum, FALSE);
5714 if (str == NULL)
5715 return -1;
5716
5717 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005718 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005719
Bram Moolenaar91458462021-01-13 20:08:38 +01005720 // count the number of characters
5721 t = str;
5722 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5723 t += mb_ptr2len(t);
5724
5725 // In insert mode, when the cursor is at the end of a non-empty line,
5726 // byteidx points to the NUL character immediately past the end of the
5727 // string. In this case, add one to the character count.
5728 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5729 count++;
5730
5731 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005732}
5733
5734/*
5735 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005736 * byte index. Works only for loaded buffers. Returns -1 on failure.
5737 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005738 */
5739 int
5740buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5741{
5742 char_u *str;
5743 char_u *t;
5744
5745 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5746 return -1;
5747
5748 if (lnum > buf->b_ml.ml_line_count)
5749 lnum = buf->b_ml.ml_line_count;
5750
5751 str = ml_get_buf(buf, lnum, FALSE);
5752 if (str == NULL)
5753 return -1;
5754
5755 // Convert the character offset to a byte offset
5756 t = str;
5757 while (*t != NUL && --charidx > 0)
5758 t += mb_ptr2len(t);
5759
Bram Moolenaar91458462021-01-13 20:08:38 +01005760 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005761}
5762
5763/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005765 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005767 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005768var2fpos(
5769 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005770 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005771 int *fnum, // set to fnum for '0, 'A, etc.
5772 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005774 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005776 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005778 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005779 if (varp->v_type == VAR_LIST)
5780 {
5781 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005782 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005783 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005784 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005785
5786 l = varp->vval.v_list;
5787 if (l == NULL)
5788 return NULL;
5789
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005790 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005791 pos.lnum = list_find_nr(l, 0L, &error);
5792 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005793 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005794 if (charcol)
5795 len = (long)mb_charlen(ml_get(pos.lnum));
5796 else
5797 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005798
Bram Moolenaarec65d772020-08-20 22:29:12 +02005799 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005800 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005801 li = list_find(l, 1L);
5802 if (li != NULL && li->li_tv.v_type == VAR_STRING
5803 && li->li_tv.vval.v_string != NULL
5804 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005805 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005806 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005807 }
5808 else
5809 {
5810 pos.col = list_find_nr(l, 1L, &error);
5811 if (error)
5812 return NULL;
5813 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005814
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005815 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005816 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005817 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005818 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005819
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005820 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005821 pos.coladd = list_find_nr(l, 2L, &error);
5822 if (error)
5823 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005824
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005825 return &pos;
5826 }
5827
Bram Moolenaarc5809432021-03-27 21:23:30 +01005828 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5829 return NULL;
5830
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005831 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005832 if (name == NULL)
5833 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005834
5835 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005836 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005837 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005838 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005839 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005840 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005841 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005842 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005843 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005844 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005845 pos = VIsual;
5846 else
5847 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005848 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005849 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005850 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005852 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005853 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5855 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005856 pos = *pp;
5857 }
5858 if (pos.lnum != 0)
5859 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005860 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005861 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5862 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005864
Bram Moolenaara5525202006-03-02 22:52:09 +00005865 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005866
Bram Moolenaar477933c2007-07-17 14:32:23 +00005867 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005868 {
5869 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005870 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005871 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005872 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005873 // In silent Ex mode topline is zero, but that's not a valid line
5874 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005875 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005876 return &pos;
5877 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005878 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005879 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005880 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005881 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005882 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005883 return &pos;
5884 }
5885 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005886 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005888 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 {
5890 pos.lnum = curbuf->b_ml.ml_line_count;
5891 pos.col = 0;
5892 }
5893 else
5894 {
5895 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005896 if (charcol)
5897 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5898 else
5899 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 }
5901 return &pos;
5902 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005903 if (in_vim9script())
5904 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 return NULL;
5906}
5907
5908/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005909 * Convert list in "arg" into a position and optional file number.
5910 * When "fnump" is NULL there is no file number, only 3 items.
5911 * Note that the column is passed on as-is, the caller may want to decrement
5912 * it to use 1 for the first column.
5913 * Return FAIL when conversion is not possible, doesn't check the position for
5914 * validity.
5915 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005916 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005917list2fpos(
5918 typval_T *arg,
5919 pos_T *posp,
5920 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005921 colnr_T *curswantp,
5922 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005923{
5924 list_T *l = arg->vval.v_list;
5925 long i = 0;
5926 long n;
5927
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005928 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5929 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005930 if (arg->v_type != VAR_LIST
5931 || l == NULL
5932 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005933 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005934 return FAIL;
5935
5936 if (fnump != NULL)
5937 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005938 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005939 if (n < 0)
5940 return FAIL;
5941 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005942 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005943 *fnump = n;
5944 }
5945
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005946 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005947 if (n < 0)
5948 return FAIL;
5949 posp->lnum = n;
5950
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005951 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005952 if (n < 0)
5953 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005954 // If character position is specified, then convert to byte position
5955 if (charcol)
5956 {
5957 buf_T *buf;
5958
5959 // Get the text for the specified line in a loaded buffer
5960 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5961 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5962 return FAIL;
5963
Bram Moolenaar91458462021-01-13 20:08:38 +01005964 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005965 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005966 posp->col = n;
5967
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005968 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005969 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005970 posp->coladd = 0;
5971 else
5972 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005973
Bram Moolenaar493c1782014-05-28 14:34:46 +02005974 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005975 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005976
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005977 return OK;
5978}
5979
5980/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 * Get the length of an environment variable name.
5982 * Advance "arg" to the first character after the name.
5983 * Return 0 for error.
5984 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005985 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005986get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987{
5988 char_u *p;
5989 int len;
5990
5991 for (p = *arg; vim_isIDc(*p); ++p)
5992 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005993 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 return 0;
5995
5996 len = (int)(p - *arg);
5997 *arg = p;
5998 return len;
5999}
6000
6001/*
6002 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006003 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 * Return 0 if something is wrong.
6005 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006006 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006007get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008{
6009 char_u *p;
6010 int len;
6011
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006012 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006014 {
6015 if (*p == ':')
6016 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006017 // "s:" is start of "s:var", but "n:" is not and can be used in
6018 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006019 len = (int)(p - *arg);
6020 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6021 || len > 1)
6022 break;
6023 }
6024 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006025 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 return 0;
6027
6028 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006029 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030
6031 return len;
6032}
6033
6034/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006035 * Get the length of the name of a variable or function.
6036 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006038 * Return -1 if curly braces expansion failed.
6039 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 * If the name contains 'magic' {}'s, expand them and return the
6041 * expanded name in an allocated string via 'alias' - caller must free.
6042 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006043 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006044get_name_len(
6045 char_u **arg,
6046 char_u **alias,
6047 int evaluate,
6048 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049{
6050 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 char_u *p;
6052 char_u *expr_start;
6053 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006055 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056
6057 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6058 && (*arg)[2] == (int)KE_SNR)
6059 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006060 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 *arg += 3;
6062 return get_id_len(arg) + 3;
6063 }
6064 len = eval_fname_script(*arg);
6065 if (len > 0)
6066 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006067 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 *arg += len;
6069 }
6070
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006072 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006074 p = find_name_end(*arg, &expr_start, &expr_end,
6075 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 if (expr_start != NULL)
6077 {
6078 char_u *temp_string;
6079
6080 if (!evaluate)
6081 {
6082 len += (int)(p - *arg);
6083 *arg = skipwhite(p);
6084 return len;
6085 }
6086
6087 /*
6088 * Include any <SID> etc in the expanded string:
6089 * Thus the -len here.
6090 */
6091 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6092 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006093 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 *alias = temp_string;
6095 *arg = skipwhite(p);
6096 return (int)STRLEN(temp_string);
6097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098
6099 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006100 // Only give an error when there is something, otherwise it will be
6101 // reported at a higher level.
6102 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006103 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104
6105 return len;
6106}
6107
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006108/*
6109 * Find the end of a variable or function name, taking care of magic braces.
6110 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6111 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006112 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006113 * Return a pointer to just after the name. Equal to "arg" if there is no
6114 * valid name.
6115 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006116 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006117find_name_end(
6118 char_u *arg,
6119 char_u **expr_start,
6120 char_u **expr_end,
6121 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006123 int mb_nest = 0;
6124 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006126 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02006127 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006129 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006131 *expr_start = NULL;
6132 *expr_end = NULL;
6133 }
6134
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006135 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006136 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
6137 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006138 return arg;
6139
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006140 for (p = arg; *p != NUL
6141 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006142 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006143 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006144 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006145 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006146 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006147 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006148 if (*p == '\'')
6149 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006150 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006151 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006152 ;
6153 if (*p == NUL)
6154 break;
6155 }
6156 else if (*p == '"')
6157 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006158 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006159 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006160 if (*p == '\\' && p[1] != NUL)
6161 ++p;
6162 if (*p == NUL)
6163 break;
6164 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006165 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6166 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006167 // "s:" is start of "s:var", but "n:" is not and can be used in
6168 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006169 len = (int)(p - arg);
6170 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006171 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006172 break;
6173 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006174
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006175 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006177 if (*p == '[')
6178 ++br_nest;
6179 else if (*p == ']')
6180 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006182
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006183 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006185 if (*p == '{')
6186 {
6187 mb_nest++;
6188 if (expr_start != NULL && *expr_start == NULL)
6189 *expr_start = p;
6190 }
6191 else if (*p == '}')
6192 {
6193 mb_nest--;
6194 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6195 *expr_end = p;
6196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 }
6199
6200 return p;
6201}
6202
6203/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006204 * Expands out the 'magic' {}'s in a variable/function name.
6205 * Note that this can call itself recursively, to deal with
6206 * constructs like foo{bar}{baz}{bam}
6207 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6208 * "in_start" ^
6209 * "expr_start" ^
6210 * "expr_end" ^
6211 * "in_end" ^
6212 *
6213 * Returns a new allocated string, which the caller must free.
6214 * Returns NULL for failure.
6215 */
6216 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006217make_expanded_name(
6218 char_u *in_start,
6219 char_u *expr_start,
6220 char_u *expr_end,
6221 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006222{
6223 char_u c1;
6224 char_u *retval = NULL;
6225 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006226
6227 if (expr_end == NULL || in_end == NULL)
6228 return NULL;
6229 *expr_start = NUL;
6230 *expr_end = NUL;
6231 c1 = *in_end;
6232 *in_end = NUL;
6233
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006234 temp_result = eval_to_string(expr_start + 1, FALSE);
6235 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006236 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006237 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6238 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006239 if (retval != NULL)
6240 {
6241 STRCPY(retval, in_start);
6242 STRCAT(retval, temp_result);
6243 STRCAT(retval, expr_end + 1);
6244 }
6245 }
6246 vim_free(temp_result);
6247
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006248 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006249 *expr_start = '{';
6250 *expr_end = '}';
6251
6252 if (retval != NULL)
6253 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006254 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006255 if (expr_start != NULL)
6256 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006257 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006258 temp_result = make_expanded_name(retval, expr_start,
6259 expr_end, temp_result);
6260 vim_free(retval);
6261 retval = temp_result;
6262 }
6263 }
6264
6265 return retval;
6266}
6267
6268/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006270 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006272 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006273eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006275 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006276}
6277
6278/*
6279 * Return TRUE if character "c" can be used as the first character in a
6280 * variable or function name (excluding '{' and '}').
6281 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006282 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006283eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006284{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006285 return ASCII_ISALPHA(c) || c == '_';
6286}
6287
6288/*
6289 * Return TRUE if character "c" can be used as the first character of a
6290 * dictionary key.
6291 */
6292 int
6293eval_isdictc(int c)
6294{
6295 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296}
6297
6298/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006299 * Handle:
6300 * - expr[expr], expr[expr:expr] subscript
6301 * - ".name" lookup
6302 * - function call with Funcref variable: func(expr)
6303 * - method call: var->method()
6304 *
6305 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006306 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006307 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006308 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006309handle_subscript(
6310 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006311 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006312 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006313 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006314 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006315{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006316 int evaluate = evalarg != NULL
6317 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006318 int ret = OK;
6319 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006320 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006321 int getnext;
6322 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006323
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006324 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006325 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006326 // When at the end of the line and ".name" or "->{" or "->X" follows in
6327 // the next line then consume the line break.
6328 p = eval_next_non_blank(*arg, evalarg, &getnext);
6329 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006330 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006331 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6332 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6333 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006334 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006335 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006336 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006337 check_white = FALSE;
6338 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006339
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006340 if (rettv->v_type == VAR_ANY)
6341 {
6342 char_u *exp_name;
6343 int cc;
6344 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006345 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006346 type_T *type;
6347
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006348 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006349 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006350 if (**arg != '.')
6351 {
6352 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006353 semsg(_(e_expected_dot_after_name_str),
6354 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006355 ret = FAIL;
6356 break;
6357 }
6358 ++*arg;
6359 if (IS_WHITE_OR_NUL(**arg))
6360 {
6361 if (verbose)
6362 emsg(_(e_no_white_space_allowed_after_dot));
6363 ret = FAIL;
6364 break;
6365 }
6366
6367 // isolate the name
6368 exp_name = *arg;
6369 while (eval_isnamec(**arg))
6370 ++*arg;
6371 cc = **arg;
6372 **arg = NUL;
6373
6374 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00006375 evalarg->eval_cctx, evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006376 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006377
6378 if (idx < 0 && ufunc == NULL)
6379 {
6380 ret = FAIL;
6381 break;
6382 }
6383 if (idx >= 0)
6384 {
6385 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6386 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6387
6388 copy_tv(sv->sv_tv, rettv);
6389 }
6390 else
6391 {
6392 rettv->v_type = VAR_FUNC;
6393 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6394 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006395 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006396 }
6397
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006398 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6399 || rettv->v_type == VAR_PARTIAL))
6400 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006401 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006402 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6403 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006404
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006405 // Stop the expression evaluation when immediately aborting on
6406 // error, or when an interrupt occurred or an exception was thrown
6407 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006408 if (aborting())
6409 {
6410 if (ret == OK)
6411 clear_tv(rettv);
6412 ret = FAIL;
6413 }
6414 dict_unref(selfdict);
6415 selfdict = NULL;
6416 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006417 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006418 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006419 if (in_vim9script())
6420 *arg = skipwhite(p + 2);
6421 else
6422 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006423 if (ret == OK)
6424 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006425 if (VIM_ISWHITE(**arg))
6426 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006427 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006428 ret = FAIL;
6429 }
6430 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006431 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006432 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006433 else
6434 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006435 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006436 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006437 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006438 // "." is ".name" lookup when we found a dict or when evaluating and
6439 // scriptversion is at least 2, where string concatenation is "..".
6440 else if (**arg == '['
6441 || (**arg == '.' && (rettv->v_type == VAR_DICT
6442 || (!evaluate
6443 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006444 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006445 {
6446 dict_unref(selfdict);
6447 if (rettv->v_type == VAR_DICT)
6448 {
6449 selfdict = rettv->vval.v_dict;
6450 if (selfdict != NULL)
6451 ++selfdict->dv_refcount;
6452 }
6453 else
6454 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006455 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006456 {
6457 clear_tv(rettv);
6458 ret = FAIL;
6459 }
6460 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006461 else
6462 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006463 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006464
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006465 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6466 // Don't do this when "Func" is already a partial that was bound
6467 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006468 if (selfdict != NULL
6469 && (rettv->v_type == VAR_FUNC
6470 || (rettv->v_type == VAR_PARTIAL
6471 && (rettv->vval.v_partial->pt_auto
6472 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006473 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006474
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006475 dict_unref(selfdict);
6476 return ret;
6477}
6478
6479/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006480 * Make a copy of an item.
6481 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006482 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006483 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6484 * reference to an already copied list/dict can be used.
6485 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006486 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006487 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006488item_copy(
6489 typval_T *from,
6490 typval_T *to,
6491 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006492 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006493 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006494{
6495 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006496 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006497
Bram Moolenaar33570922005-01-25 22:26:29 +00006498 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006499 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006500 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006501 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006502 }
6503 ++recurse;
6504
6505 switch (from->v_type)
6506 {
6507 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006508 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006509 case VAR_STRING:
6510 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006511 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006512 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006513 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006514 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006515 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006516 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006517 copy_tv(from, to);
6518 break;
6519 case VAR_LIST:
6520 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006521 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006522 if (from->vval.v_list == NULL)
6523 to->vval.v_list = NULL;
6524 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6525 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006526 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006527 to->vval.v_list = from->vval.v_list->lv_copylist;
6528 ++to->vval.v_list->lv_refcount;
6529 }
6530 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006531 to->vval.v_list = list_copy(from->vval.v_list,
6532 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006533 if (to->vval.v_list == NULL)
6534 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006535 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006536 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006537 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006538 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006539 case VAR_DICT:
6540 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006541 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006542 if (from->vval.v_dict == NULL)
6543 to->vval.v_dict = NULL;
6544 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6545 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006546 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006547 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6548 ++to->vval.v_dict->dv_refcount;
6549 }
6550 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006551 to->vval.v_dict = dict_copy(from->vval.v_dict,
6552 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006553 if (to->vval.v_dict == NULL)
6554 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006555 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006556 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006557 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006558 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006559 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006560 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006561 }
6562 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006563 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006564}
6565
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006566 void
6567echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6568{
6569 char_u *tofree;
6570 char_u numbuf[NUMBUFLEN];
6571 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6572
6573 if (*atstart)
6574 {
6575 *atstart = FALSE;
6576 // Call msg_start() after eval1(), evaluating the expression
6577 // may cause a message to appear.
6578 if (with_space)
6579 {
6580 // Mark the saved text as finishing the line, so that what
6581 // follows is displayed on a new line when scrolling back
6582 // at the more prompt.
6583 msg_sb_eol();
6584 msg_start();
6585 }
6586 }
6587 else if (with_space)
6588 msg_puts_attr(" ", echo_attr);
6589
6590 if (p != NULL)
6591 for ( ; *p != NUL && !got_int; ++p)
6592 {
6593 if (*p == '\n' || *p == '\r' || *p == TAB)
6594 {
6595 if (*p != TAB && *needclr)
6596 {
6597 // remove any text still there from the command
6598 msg_clr_eos();
6599 *needclr = FALSE;
6600 }
6601 msg_putchar_attr(*p, echo_attr);
6602 }
6603 else
6604 {
6605 if (has_mbyte)
6606 {
6607 int i = (*mb_ptr2len)(p);
6608
6609 (void)msg_outtrans_len_attr(p, i, echo_attr);
6610 p += i - 1;
6611 }
6612 else
6613 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6614 }
6615 }
6616 vim_free(tofree);
6617}
6618
Bram Moolenaare9a41262005-01-15 22:18:47 +00006619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 * ":echo expr1 ..." print each argument separated with a space, add a
6621 * newline at the end.
6622 * ":echon expr1 ..." print each argument plain.
6623 */
6624 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006625ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626{
6627 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006628 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006629 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 int needclr = TRUE;
6631 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006632 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006633 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006634 evalarg_T evalarg;
6635
Bram Moolenaare707c882020-07-01 13:07:37 +02006636 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637
6638 if (eap->skip)
6639 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006640 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006642 // If eval1() causes an error message the text from the command may
6643 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006644 need_clr_eos = needclr;
6645
Bram Moolenaar68db9962021-05-09 23:19:22 +02006646 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006647 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 {
6649 /*
6650 * Report the invalid expression unless the expression evaluation
6651 * has been cancelled due to an aborting error, an interrupt, or an
6652 * exception.
6653 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006654 if (!aborting() && did_emsg == did_emsg_before
6655 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006656 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006657 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 break;
6659 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006660 need_clr_eos = FALSE;
6661
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006663 {
6664 if (rettv.v_type == VAR_VOID)
6665 {
6666 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6667 break;
6668 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006669 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006670 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006671
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006672 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 arg = skipwhite(arg);
6674 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006675 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006676 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677
6678 if (eap->skip)
6679 --emsg_skip;
6680 else
6681 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006682 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 if (needclr)
6684 msg_clr_eos();
6685 if (eap->cmdidx == CMD_echo)
6686 msg_end();
6687 }
6688}
6689
6690/*
6691 * ":echohl {name}".
6692 */
6693 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006694ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006696 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697}
6698
6699/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006700 * Returns the :echo attribute
6701 */
6702 int
6703get_echo_attr(void)
6704{
6705 return echo_attr;
6706}
6707
6708/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 * ":execute expr1 ..." execute the result of an expression.
6710 * ":echomsg expr1 ..." Print a message
6711 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006712 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 * Each gets spaces around each argument and a newline at the end for
6714 * echo commands
6715 */
6716 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006717ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718{
6719 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006720 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 int ret = OK;
6722 char_u *p;
6723 garray_T ga;
6724 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006725 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726
6727 ga_init2(&ga, 1, 80);
6728
6729 if (eap->skip)
6730 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006731 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006733 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006734 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736
6737 if (!eap->skip)
6738 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006739 char_u buf[NUMBUFLEN];
6740
6741 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006742 {
6743 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6744 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006745 semsg(_(e_using_invalid_value_as_string_str),
6746 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006747 p = NULL;
6748 }
6749 else
6750 p = tv_get_string_buf(&rettv, buf);
6751 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006752 else
6753 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006754 if (p == NULL)
6755 {
6756 clear_tv(&rettv);
6757 ret = FAIL;
6758 break;
6759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 len = (int)STRLEN(p);
6761 if (ga_grow(&ga, len + 2) == FAIL)
6762 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006763 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 ret = FAIL;
6765 break;
6766 }
6767 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 ga.ga_len += len;
6771 }
6772
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006773 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 arg = skipwhite(arg);
6775 }
6776
6777 if (ret != FAIL && ga.ga_data != NULL)
6778 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006779 // use the first line of continuation lines for messages
6780 SOURCING_LNUM = start_lnum;
6781
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006782 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6783 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006784 // Mark the already saved text as finishing the line, so that what
6785 // follows is displayed on a new line when scrolling back at the
6786 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006787 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006788 }
6789
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006791 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006792 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006793 out_flush();
6794 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006795 else if (eap->cmdidx == CMD_echoconsole)
6796 {
6797 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6798 ui_write((char_u *)"\r\n", 2, TRUE);
6799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 else if (eap->cmdidx == CMD_echoerr)
6801 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006802 int save_did_emsg = did_emsg;
6803
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006804 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006805 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 if (!force_abort)
6807 did_emsg = save_did_emsg;
6808 }
6809 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006810 {
6811 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6812
6813 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6814 sticky_cmdmod_flags = cmdmod.cmod_flags
6815 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 do_cmdline((char_u *)ga.ga_data,
6817 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006818 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 }
6821
6822 ga_clear(&ga);
6823
6824 if (eap->skip)
6825 --emsg_skip;
6826
Bram Moolenaar63b91732021-08-05 20:40:03 +02006827 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828}
6829
6830/*
6831 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6832 * "arg" points to the "&" or '+' when called, to "option" when returning.
6833 * Returns NULL when no option name found. Otherwise pointer to the char
6834 * after the option name.
6835 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006836 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006837find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838{
6839 char_u *p = *arg;
6840
6841 ++p;
6842 if (*p == 'g' && p[1] == ':')
6843 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006844 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 p += 2;
6846 }
6847 else if (*p == 'l' && p[1] == ':')
6848 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006849 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 p += 2;
6851 }
6852 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006853 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854
6855 if (!ASCII_ISALPHA(*p))
6856 return NULL;
6857 *arg = p;
6858
6859 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006860 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 else
6862 while (ASCII_ISALPHA(*p))
6863 ++p;
6864 return p;
6865}
6866
6867/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006868 * Display script name where an item was last set.
6869 * Should only be invoked when 'verbose' is non-zero.
6870 */
6871 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006872last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006873{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006874 char_u *p;
6875
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006876 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006877 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006878 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006879 if (p != NULL)
6880 {
6881 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006882 msg_puts(_("\n\tLast set from "));
6883 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006884 if (script_ctx.sc_lnum > 0)
6885 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006886 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006887 msg_outnum((long)script_ctx.sc_lnum);
6888 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006889 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006890 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006891 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006892 }
6893}
6894
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006895#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896
6897/*
6898 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006899 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 * "flags" can be "g" to do a global substitute.
6901 * Returns an allocated string, NULL for error.
6902 */
6903 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006904do_string_sub(
6905 char_u *str,
6906 char_u *pat,
6907 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006908 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006909 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910{
6911 int sublen;
6912 regmatch_T regmatch;
6913 int i;
6914 int do_all;
6915 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006916 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 garray_T ga;
6918 char_u *ret;
6919 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006920 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006922 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006924 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925
6926 ga_init2(&ga, 1, 200);
6927
6928 do_all = (flags[0] == 'g');
6929
6930 regmatch.rm_ic = p_ic;
6931 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6932 if (regmatch.regprog != NULL)
6933 {
6934 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006935 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6937 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006938 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006939 if (regmatch.startp[0] == regmatch.endp[0])
6940 {
6941 if (zero_width == regmatch.startp[0])
6942 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006943 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006944 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006945 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6946 (size_t)i);
6947 ga.ga_len += i;
6948 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006949 continue;
6950 }
6951 zero_width = regmatch.startp[0];
6952 }
6953
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 /*
6955 * Get some space for a temporary buffer to do the substitution
6956 * into. It will contain:
6957 * - The text up to where the match is.
6958 * - The substituted text.
6959 * - The text after the match.
6960 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01006961 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006962 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6964 {
6965 ga_clear(&ga);
6966 break;
6967 }
6968
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006969 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 i = (int)(regmatch.startp[0] - tail);
6971 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006972 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01006973 (void)vim_regsub(&regmatch, sub, expr,
6974 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
6975 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006977 tail = regmatch.endp[0];
6978 if (*tail == NUL)
6979 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 if (!do_all)
6981 break;
6982 }
6983
6984 if (ga.ga_data != NULL)
6985 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6986
Bram Moolenaar473de612013-06-08 18:19:48 +02006987 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 }
6989
6990 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6991 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006992 if (p_cpo == empty_option)
6993 p_cpo = save_cpo;
6994 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006995 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006996 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006997 // If it's still empty it was changed and restored, need to restore in
6998 // the complicated way.
6999 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007000 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007001 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003
7004 return ret;
7005}