blob: 3ecbf6546fd66ea3a9a9491df3b6a9ea223191ed [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);
52static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +020053static int eval7t(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020054static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020055static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000056
Bram Moolenaar48e697e2016-01-23 22:17:30 +010057static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020058static 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 +020059
Bram Moolenaare21c1582019-03-02 11:57:09 +010060/*
61 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010062 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010063 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020064 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010065num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010066{
67 varnumber_T result;
68
Bram Moolenaar99880f92021-01-20 21:23:14 +010069 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010070 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010071 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010072 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010073 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010074 if (failed != NULL)
75 *failed = TRUE;
76 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010077 if (n1 == 0)
78 result = VARNUM_MIN; // similar to NaN
79 else if (n1 < 0)
80 result = -VARNUM_MAX;
81 else
82 result = VARNUM_MAX;
83 }
84 else
85 result = n1 / n2;
86
87 return result;
88}
89
90/*
91 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010092 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010093 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020094 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010095num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010096{
Bram Moolenaar99880f92021-01-20 21:23:14 +010097 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010098 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010099 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +0100100 if (failed != NULL)
101 *failed = TRUE;
102 }
Bram Moolenaare21c1582019-03-02 11:57:09 +0100103 return (n2 == 0) ? 0 : (n1 % n2);
104}
105
Bram Moolenaar33570922005-01-25 22:26:29 +0000106/*
107 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000108 */
109 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100110eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000111{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200112 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200113 func_init();
Bram Moolenaara7043832005-01-21 11:56:39 +0000114}
115
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000116#if defined(EXITFREE) || defined(PROTO)
117 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100118eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000119{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200120 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100121 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200122 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000123
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200124 // autoloaded script names
125 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000126
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200127 // unreferenced lists and dicts
128 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200129
130 // functions not garbage collected
131 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000132}
133#endif
134
Bram Moolenaare6b53242020-07-01 17:28:33 +0200135 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200136fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
137{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100138 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200139 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200140 if (eap != NULL)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200141 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200142 evalarg->eval_cstack = eap->cstack;
143 if (getline_equal(eap->getline, eap->cookie, getsourceline))
144 {
145 evalarg->eval_getline = eap->getline;
146 evalarg->eval_cookie = eap->cookie;
147 }
Bram Moolenaar37c83712020-06-30 21:18:36 +0200148 }
149}
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
152 * Top level evaluation function, returning a boolean.
153 * Sets "error" to TRUE if there was an error.
154 * Return TRUE or FALSE.
155 */
156 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100157eval_to_bool(
158 char_u *arg,
159 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200160 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100161 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162{
Bram Moolenaar33570922005-01-25 22:26:29 +0000163 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200164 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200165 evalarg_T evalarg;
166
Bram Moolenaar37c83712020-06-30 21:18:36 +0200167 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168
169 if (skip)
170 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200171 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 else
174 {
175 *error = FALSE;
176 if (!skip)
177 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200178 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200179 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200180 else
181 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000182 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 }
184 }
185 if (skip)
186 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200187 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200189 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190}
191
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100192/*
193 * Call eval1() and give an error message if not done at a lower level.
194 */
195 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200196eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100197{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100198 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100199 int ret;
200 int did_emsg_before = did_emsg;
201 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200202 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100203
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200204 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
205
206 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100207 if (ret == FAIL)
208 {
209 // Report the invalid expression unless the expression evaluation has
210 // been cancelled due to an aborting error, an interrupt, or an
211 // exception, or we already gave a more specific error.
212 // Also check called_emsg for when using assert_fails().
213 if (!aborting() && did_emsg == did_emsg_before
214 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200215 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100216 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200217 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100218 return ret;
219}
220
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100221/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200222 * Return whether a typval is a valid expression to pass to eval_expr_typval()
223 * or eval_expr_to_bool(). An empty string returns FALSE;
224 */
225 int
226eval_expr_valid_arg(typval_T *tv)
227{
228 return tv->v_type != VAR_UNKNOWN
229 && (tv->v_type != VAR_STRING
230 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
231}
232
233/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100234 * Evaluate an expression, which can be a function, partial or string.
235 * Pass arguments "argv[argc]".
236 * Return the result in "rettv" and OK or FAIL.
237 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200238 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100239eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
240{
241 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100242 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200243 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100244
245 if (expr->v_type == VAR_FUNC)
246 {
247 s = expr->vval.v_string;
248 if (s == NULL || *s == NUL)
249 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200250 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000251 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200252 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100253 return FAIL;
254 }
255 else if (expr->v_type == VAR_PARTIAL)
256 {
257 partial_T *partial = expr->vval.v_partial;
258
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200259 if (partial == NULL)
260 return FAIL;
261
Bram Moolenaar822ba242020-05-24 23:00:18 +0200262 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200263 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100264 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200265 if (call_def_function(partial->pt_func, argc, argv,
266 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100267 return FAIL;
268 }
269 else
270 {
271 s = partial_name(partial);
272 if (s == NULL || *s == NUL)
273 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200274 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000275 funcexe.fe_evaluate = TRUE;
276 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
278 return FAIL;
279 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100280 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200281 else if (expr->v_type == VAR_INSTR)
282 {
283 return exe_typval_instr(expr, rettv);
284 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100285 else
286 {
Bram Moolenaarfc4c4482022-01-26 21:17:04 +0000287 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
Bram Moolenaar48570482017-10-30 21:48:41 +0100288 if (s == NULL)
289 return FAIL;
290 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200291 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100292 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200293 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100294 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200295 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200296 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100297 return FAIL;
298 }
299 }
300 return OK;
301}
302
303/*
304 * Like eval_to_bool() but using a typval_T instead of a string.
305 * Works for string, funcref and partial.
306 */
307 int
308eval_expr_to_bool(typval_T *expr, int *error)
309{
310 typval_T rettv;
311 int res;
312
313 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
314 {
315 *error = TRUE;
316 return FALSE;
317 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200318 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100319 clear_tv(&rettv);
320 return res;
321}
322
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323/*
324 * Top level evaluation function, returning a string. If "skip" is TRUE,
325 * only parsing to "nextcmd" is done, without reporting errors. Return
326 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
327 */
328 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100329eval_to_string_skip(
330 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200331 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100332 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333{
Bram Moolenaar33570922005-01-25 22:26:29 +0000334 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200336 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337
Bram Moolenaar37c83712020-06-30 21:18:36 +0200338 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 if (skip)
340 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200341 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 retval = NULL;
343 else
344 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100345 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000346 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 }
348 if (skip)
349 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200350 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351
352 return retval;
353}
354
355/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000356 * Skip over an expression at "*pp".
357 * Return FAIL for an error, OK otherwise.
358 */
359 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200360skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000361{
Bram Moolenaar33570922005-01-25 22:26:29 +0000362 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000363
364 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200365 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000366}
367
368/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200369 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200370 * If in Vim9 script and line breaks are encountered, the lines are
371 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200372 * "arg" is advanced to just after the expression.
373 * "start" is set to the start of the expression, "end" to just after the end.
374 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200375 * Return FAIL for an error, OK otherwise.
376 */
377 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200378skip_expr_concatenate(
379 char_u **arg,
380 char_u **start,
381 char_u **end,
382 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200383{
384 typval_T rettv;
385 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200386 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200387 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200388 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200389 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200390 int evaluate = evalarg == NULL
391 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200392
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200393 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200394 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200395 {
396 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200397 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200398 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200399 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200400 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200401 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200402 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200403
404 // Don't evaluate the expression.
405 if (evalarg != NULL)
406 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200407 *arg = skipwhite(*arg);
408 res = eval1(arg, &rettv, evalarg);
409 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200410 if (evalarg != NULL)
411 evalarg->eval_flags = save_flags;
412
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200413 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200414 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200415 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200416 if (evalarg->eval_ga.ga_len == 1)
417 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200418 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200419 ga_clear(gap);
420 gap->ga_itemsize = 0;
421 }
422 else
423 {
424 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200425 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200426
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200427 // Line breaks encountered, concatenate all the lines.
428 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200429 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200430
431 // free the lines only when using getsourceline()
432 if (evalarg->eval_cookie != NULL)
433 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200434 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200435 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200436 // Do not free the last line, "arg" points into it, free it
437 // later.
438 vim_free(evalarg->eval_tofree);
439 evalarg->eval_tofree =
440 ((char_u **)gap->ga_data)[gap->ga_len - 1];
441 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200442 ga_clear_strings(gap);
443 }
444 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200445 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200446 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200447
448 // free lines that were explicitly marked for freeing
449 ga_clear_strings(freegap);
450 }
451
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200452 gap->ga_itemsize = 0;
453 if (p == NULL)
454 return FAIL;
455 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200456 vim_free(evalarg->eval_tofree_lambda);
457 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200458 // Compute "end" relative to the end.
459 *end = *start + STRLEN(*start) - endoff;
460 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200461 }
462
463 return res;
464}
465
466/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100467 * Convert "tv" to a string.
468 * When "convert" is TRUE convert a List into a sequence of lines and convert
469 * a Float to a String.
470 * Returns an allocated string (NULL when out of memory).
471 */
472 char_u *
473typval2string(typval_T *tv, int convert)
474{
475 garray_T ga;
476 char_u *retval;
477#ifdef FEAT_FLOAT
478 char_u numbuf[NUMBUFLEN];
479#endif
480
481 if (convert && tv->v_type == VAR_LIST)
482 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000483 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100484 if (tv->vval.v_list != NULL)
485 {
486 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
487 if (tv->vval.v_list->lv_len > 0)
488 ga_append(&ga, NL);
489 }
490 ga_append(&ga, NUL);
491 retval = (char_u *)ga.ga_data;
492 }
493#ifdef FEAT_FLOAT
494 else if (convert && tv->v_type == VAR_FLOAT)
495 {
496 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
497 retval = vim_strsave(numbuf);
498 }
499#endif
500 else
501 retval = vim_strsave(tv_get_string(tv));
502 return retval;
503}
504
505/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200506 * Top level evaluation function, returning a string. Does not handle line
507 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000508 * When "convert" is TRUE convert a List into a sequence of lines and convert
509 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 * Return pointer to allocated memory, or NULL for failure.
511 */
512 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100513eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100514 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100515 int convert,
516 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517{
Bram Moolenaar33570922005-01-25 22:26:29 +0000518 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100520 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100522 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
523 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 retval = NULL;
525 else
526 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100527 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000528 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100530 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531
532 return retval;
533}
534
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100535 char_u *
536eval_to_string(
537 char_u *arg,
538 int convert)
539{
540 return eval_to_string_eap(arg, convert, NULL);
541}
542
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000544 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200545 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200546 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 */
548 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100549eval_to_string_safe(
550 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000551 int use_sandbox,
552 int keep_script_version)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553{
554 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200555 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200556 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200557 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558
Bram Moolenaar9530b582022-01-22 13:39:08 +0000559 if (!keep_script_version)
560 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200561 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000562 if (use_sandbox)
563 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200564 ++textwinlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200565 may_garbage_collect = FALSE;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200566 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000567 if (use_sandbox)
568 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200569 --textwinlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200570 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200571 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200572 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 return retval;
574}
575
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576/*
577 * Top level evaluation function, returning a number.
578 * Evaluates "expr" silently.
579 * Returns -1 for an error.
580 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200581 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100582eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583{
Bram Moolenaar33570922005-01-25 22:26:29 +0000584 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200585 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000586 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587
588 ++emsg_off;
589
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200590 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 retval = -1;
592 else
593 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100594 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000595 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 }
597 --emsg_off;
598
599 return retval;
600}
601
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000602/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000603 * Top level evaluation function.
604 * Returns an allocated typval_T with the result.
605 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000606 */
607 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200608eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000609{
610 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200611 evalarg_T evalarg;
612
613 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000614
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200615 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200616 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100617 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000618
Bram Moolenaar37c83712020-06-30 21:18:36 +0200619 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000620 return tv;
621}
622
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000624 * "*arg" points to what can be a function name in the form of "import.Name" or
625 * "Funcref". Return the name of the function. Set "tofree" to something that
626 * was allocated.
627 * If "verbose" is FALSE no errors are given.
628 * Return NULL for any failure.
629 */
630 static char_u *
631deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000632 char_u **arg,
633 char_u **tofree,
634 evalarg_T *evalarg,
635 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000636{
637 typval_T ref;
638 char_u *name = *arg;
639
640 ref.v_type = VAR_UNKNOWN;
641 if (eval7(arg, &ref, evalarg, FALSE) == FAIL)
642 return NULL;
643 if (*skipwhite(*arg) != NUL)
644 {
645 if (verbose)
646 semsg(_(e_trailing_characters_str), *arg);
647 name = NULL;
648 }
649 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
650 {
651 name = ref.vval.v_string;
652 ref.vval.v_string = NULL;
653 *tofree = name;
654 }
655 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
656 {
657 if (ref.vval.v_partial->pt_argc > 0
658 || ref.vval.v_partial->pt_dict != NULL)
659 {
660 if (verbose)
661 emsg(_(e_cannot_use_partial_here));
662 name = NULL;
663 }
664 else
665 {
666 name = vim_strsave(partial_name(ref.vval.v_partial));
667 *tofree = name;
668 }
669 }
670 else
671 {
672 if (verbose)
673 semsg(_(e_not_callable_type_str), name);
674 name = NULL;
675 }
676 clear_tv(&ref);
677 return name;
678}
679
680/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100681 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200682 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
683 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000684 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200686 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100687call_vim_function(
688 char_u *func,
689 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200690 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200691 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000693 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200694 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000695 char_u *arg;
696 char_u *name;
697 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100699 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200700 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000701 funcexe.fe_firstline = curwin->w_cursor.lnum;
702 funcexe.fe_lastline = curwin->w_cursor.lnum;
703 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000704
705 // The name might be "import.Func" or "Funcref".
706 arg = func;
Bram Moolenaarb3d9cee2022-01-17 21:13:28 +0000707 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000708 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarb3d9cee2022-01-17 21:13:28 +0000709 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000710 if (name == NULL)
711 name = func;
712
713 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
714
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000715 if (ret == FAIL)
716 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000717 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000718
719 return ret;
720}
721
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100722/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100723 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000724 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
725 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000726 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000727 */
728 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100729call_func_retstr(
730 char_u *func,
731 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200732 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000733{
734 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000735 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000736
Bram Moolenaarded27a12018-08-01 19:06:03 +0200737 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000738 return NULL;
739
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100740 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000741 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 return retval;
743}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000744
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000745/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100746 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000747 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000748 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000749 */
750 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100751call_func_retlist(
752 char_u *func,
753 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200754 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000755{
756 typval_T rettv;
757
Bram Moolenaarded27a12018-08-01 19:06:03 +0200758 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000759 return NULL;
760
761 if (rettv.v_type != VAR_LIST)
762 {
763 clear_tv(&rettv);
764 return NULL;
765 }
766
767 return rettv.vval.v_list;
768}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769
Bram Moolenaare70dd112022-01-21 16:31:11 +0000770#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100772 * Evaluate "arg", which is 'foldexpr'.
773 * Note: caller must set "curwin" to match "arg".
774 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
775 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 */
777 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000778eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000780 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000781 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200782 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000784 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000785 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
786 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787
Bram Moolenaare70dd112022-01-21 16:31:11 +0000788 arg = wp->w_p_fde;
789 current_sctx = wp->w_p_script_ctx[WV_FDE];
790
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000792 if (use_sandbox)
793 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200794 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200796 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 retval = 0;
798 else
799 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100800 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000801 if (tv.v_type == VAR_NUMBER)
802 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000803 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 retval = 0;
805 else
806 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100807 // If the result is a string, check if there is a non-digit before
808 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000809 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 if (!VIM_ISDIGIT(*s) && *s != '-')
811 *cp = *s++;
812 retval = atol((char *)s);
813 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000814 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 }
816 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000817 if (use_sandbox)
818 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200819 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200820 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000821 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200823 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824}
825#endif
826
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000828 * Get an lval: variable, Dict item or List item that can be assigned a value
829 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
830 * "name.key", "name.key[expr]" etc.
831 * Indexing only works if "name" is an existing List or Dictionary.
832 * "name" points to the start of the name.
833 * If "rettv" is not NULL it points to the value to be assigned.
834 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
835 * wrong; must end in space or cmd separator.
836 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100837 * flags:
838 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100839 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100840 * GLV_NO_AUTOLOAD: do not use script autoloading
841 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000842 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000843 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000844 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000845 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200846 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100847get_lval(
848 char_u *name,
849 typval_T *rettv,
850 lval_T *lp,
851 int unlet,
852 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100853 int flags, // GLV_ values
854 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000855{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000856 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000857 char_u *expr_start, *expr_end;
858 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000859 dictitem_T *v;
860 typval_T var1;
861 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000862 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000863 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000864 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100865 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100866 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100867 int writing;
Bram Moolenaar4525a572022-02-13 11:57:33 +0000868 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000869
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100870 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200871 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000872
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100873 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000874 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100875 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000876 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200877 lp->ll_name_end = find_name_end(name, NULL, NULL,
878 FNE_INCL_BR | fne_flags);
879 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000880 }
881
Bram Moolenaara749a422022-02-12 19:52:25 +0000882 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +0000883 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +0000884 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
885 {
886 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
887 return NULL;
888 }
889
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100890 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000891 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100892 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000893 if (expr_start != NULL)
894 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100895 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100896 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000897 && *p != '[' && *p != '.')
898 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000899 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000900 return NULL;
901 }
902
903 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
904 if (lp->ll_exp_name == NULL)
905 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100906 // Report an invalid expression in braces, unless the
907 // expression evaluation has been cancelled due to an
908 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000909 if (!aborting() && !quiet)
910 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000911 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000912 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000913 return NULL;
914 }
915 }
916 lp->ll_name = lp->ll_exp_name;
917 }
918 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100919 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000920 lp->ll_name = name;
921
Bram Moolenaar4525a572022-02-13 11:57:33 +0000922 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100923 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200924 // "a: type" is declaring variable "a" with a type, not "a:".
925 if (p == name + 2 && p[-1] == ':')
926 {
927 --p;
928 lp->ll_name_end = p;
929 }
930 if (*p == ':')
931 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000932 garray_T tmp_type_list;
933 garray_T *type_list;
934 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +0200935
936 if (tp == p + 1 && !quiet)
937 {
938 semsg(_(e_white_space_required_after_str_str), ":", p);
939 return NULL;
940 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100941
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000942 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
943 type_list = &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list;
944 else
945 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000946 // TODO: should we give an error here?
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000947 type_list = &tmp_type_list;
948 ga_init2(type_list, sizeof(type_T), 10);
949 }
950
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200951 // parse the type after the name
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000952 lp->ll_type = parse_type(&tp, type_list, !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100953 if (lp->ll_type == NULL && !quiet)
954 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200955 lp->ll_name_end = tp;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000956
957 // drop the type when not in a script
958 if (type_list == &tmp_type_list)
959 {
960 lp->ll_type = NULL;
961 clear_type_list(type_list);
962 }
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200963 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100964 }
965 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000966 if (lp->ll_name == NULL)
967 return p;
968
Bram Moolenaar62aec932022-01-29 21:45:34 +0000969 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000970 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000971 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000972
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000973 if (import != NULL)
974 {
975 ufunc_T *ufunc;
976 type_T *type;
977
978 lp->ll_sid = import->imp_sid;
979 lp->ll_name = skipwhite(p + 1);
980 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
981 lp->ll_name_end = p;
982
983 // check the item is exported
984 cc = *p;
985 *p = NUL;
986 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000987 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000988 {
989 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +0000990 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000991 }
992 *p = cc;
993 }
994 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100995
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100996 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000997 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000998 return p;
999
Bram Moolenaar4525a572022-02-13 11:57:33 +00001000 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001001 {
1002 // using local variable
1003 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001004 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001005 }
1006 else
1007 {
1008 cc = *p;
1009 *p = NUL;
1010 // When we would write to the variable pass &ht and prevent autoload.
1011 writing = !(flags & GLV_READ_ONLY);
1012 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001013 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001014 if (v == NULL && !quiet)
1015 semsg(_(e_undefined_variable_str), lp->ll_name);
1016 *p = cc;
1017 if (v == NULL)
1018 return NULL;
1019 lp->ll_tv = &v->di_tv;
1020 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001021
Bram Moolenaar4525a572022-02-13 11:57:33 +00001022 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001023 {
1024 if (!quiet)
1025 semsg(_(e_variable_already_declared), lp->ll_name);
1026 return NULL;
1027 }
1028
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001029 /*
1030 * Loop until no more [idx] or .key is following.
1031 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001032 var1.v_type = VAR_UNKNOWN;
1033 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001034 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001035 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001036 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1037 {
1038 if (!quiet)
1039 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1040 return NULL;
1041 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001042 if (lp->ll_tv->v_type != VAR_LIST
1043 && lp->ll_tv->v_type != VAR_DICT
1044 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001045 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001046 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001047 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001048 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001049 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001050
1051 // a NULL list/blob works like an empty list/blob, allocate one now.
1052 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1053 rettv_list_alloc(lp->ll_tv);
1054 else if (lp->ll_tv->v_type == VAR_BLOB
1055 && lp->ll_tv->vval.v_blob == NULL)
1056 rettv_blob_alloc(lp->ll_tv);
1057
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001058 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001059 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001060 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001061 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001062 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001063 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001064
Bram Moolenaar4525a572022-02-13 11:57:33 +00001065 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001066 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001067 && lp->ll_tv == &v->di_tv
1068 && ht != NULL && ht == get_script_local_ht())
1069 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001070 svar_T *sv = find_typval_in_script(lp->ll_tv, 0);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001071
1072 // Vim9 script local variable: get the type
1073 if (sv != NULL)
1074 lp->ll_valtype = sv->sv_type;
1075 }
1076
Bram Moolenaar8c711452005-01-14 21:53:12 +00001077 len = -1;
1078 if (*p == '.')
1079 {
1080 key = p + 1;
1081 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1082 ;
1083 if (len == 0)
1084 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001085 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001086 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001087 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001088 }
1089 p = key + len;
1090 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001091 else
1092 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001093 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001094 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001095 if (*p == ':')
1096 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001097 else
1098 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001099 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001100 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001101 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001102 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001103 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001104 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001105 clear_tv(&var1);
1106 return NULL;
1107 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001108 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001109 }
1110
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001111 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001112 if (*p == ':')
1113 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001114 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001115 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001116 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001117 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001118 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001119 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001120 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001121 if (rettv != NULL
1122 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001123 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001124 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001125 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001126 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001127 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001128 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001129 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001130 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001131 }
1132 p = skipwhite(p + 1);
1133 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001134 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001135 else
1136 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001137 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001138 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001139 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001140 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001141 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001142 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001143 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001144 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001145 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001146 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001147 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001148 clear_tv(&var2);
1149 return NULL;
1150 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001151 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001152 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001153 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001154 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001155 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001156
Bram Moolenaar8c711452005-01-14 21:53:12 +00001157 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001158 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001159 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001160 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001161 clear_tv(&var1);
1162 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001163 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001164 }
1165
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001166 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001167 ++p;
1168 }
1169
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001170 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001171 {
1172 if (len == -1)
1173 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001174 // "[key]": get key from "var1"
1175 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001176 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001177 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001178 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001179 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001180 }
1181 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001182 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001183
1184 // a NULL dict is equivalent with an empty dict
1185 if (lp->ll_tv->vval.v_dict == NULL)
1186 {
1187 lp->ll_tv->vval.v_dict = dict_alloc();
1188 if (lp->ll_tv->vval.v_dict == NULL)
1189 {
1190 clear_tv(&var1);
1191 return NULL;
1192 }
1193 ++lp->ll_tv->vval.v_dict->dv_refcount;
1194 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001195 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001196
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001197 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001198
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001199 // When assigning to a scope dictionary check that a function and
1200 // variable name is valid (only variable name unless it is l: or
1201 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001202 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001203 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001204 int prevval;
1205 int wrong;
1206
1207 if (len != -1)
1208 {
1209 prevval = key[len];
1210 key[len] = NUL;
1211 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001212 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001213 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001214 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1215 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001216 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001217 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001218 if (len != -1)
1219 key[len] = prevval;
1220 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001221 {
1222 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001223 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001224 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001225 }
1226
Bram Moolenaar10c65862020-10-08 21:16:42 +02001227 if (lp->ll_valtype != NULL)
1228 // use the type of the member
1229 lp->ll_valtype = lp->ll_valtype->tt_member;
1230
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001231 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001232 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001233 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001234 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001235 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001236 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001237 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001238 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001239 return NULL;
1240 }
1241
Bram Moolenaar31b81602019-02-10 22:14:27 +01001242 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001243 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001244 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001245 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001246 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001247 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001248 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001249 }
1250 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001251 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001252 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001253 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001254 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001255 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001256 p = NULL;
1257 break;
1258 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001259 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001260 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001261 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1262 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001263 {
1264 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001265 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001266 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001267
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001268 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001269 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001270 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001271 else if (lp->ll_tv->v_type == VAR_BLOB)
1272 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001273 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1274
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001275 /*
1276 * Get the number and item for the only or first index of the List.
1277 */
1278 if (empty1)
1279 lp->ll_n1 = 0;
1280 else
1281 // is number or string
1282 lp->ll_n1 = (long)tv_get_number(&var1);
1283 clear_tv(&var1);
1284
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001285 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001286 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001287 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001288 return NULL;
1289 }
1290 if (lp->ll_range && !lp->ll_empty2)
1291 {
1292 lp->ll_n2 = (long)tv_get_number(&var2);
1293 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001294 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1295 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001296 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001297 }
1298 lp->ll_blob = lp->ll_tv->vval.v_blob;
1299 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001300 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001301 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001302 else
1303 {
1304 /*
1305 * Get the number and item for the only or first index of the List.
1306 */
1307 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001308 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001309 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001310 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001311 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001312 clear_tv(&var1);
1313
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001314 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001315 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001316 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001317 if (lp->ll_li == NULL)
1318 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001319 clear_tv(&var2);
1320 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001321 }
1322
Bram Moolenaar10c65862020-10-08 21:16:42 +02001323 if (lp->ll_valtype != NULL)
1324 // use the type of the member
1325 lp->ll_valtype = lp->ll_valtype->tt_member;
1326
Bram Moolenaar8c711452005-01-14 21:53:12 +00001327 /*
1328 * May need to find the item or absolute index for the second
1329 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001330 * When no index given: "lp->ll_empty2" is TRUE.
1331 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001332 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001333 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001334 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001335 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001336 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001337 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001338 if (check_range_index_two(lp->ll_list,
1339 &lp->ll_n1, lp->ll_li,
1340 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001341 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001342 }
1343
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001344 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001345 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001346 }
1347
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001348 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001350 return p;
1351}
1352
1353/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001354 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001355 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001356 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001357clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001358{
1359 vim_free(lp->ll_exp_name);
1360 vim_free(lp->ll_newkey);
1361}
1362
1363/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001364 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001365 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001366 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1367 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001368 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001369 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001370set_var_lval(
1371 lval_T *lp,
1372 char_u *endp,
1373 typval_T *rettv,
1374 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001375 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1376 char_u *op,
1377 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001378{
1379 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001380 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001381
1382 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001383 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001384 cc = *endp;
1385 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001386 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1387 return;
1388
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001389 if (lp->ll_blob != NULL)
1390 {
1391 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001392
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001393 if (op != NULL && *op != '=')
1394 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001395 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001396 return;
1397 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001398 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001399 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001400
1401 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1402 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001403 if (lp->ll_empty2)
1404 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1405
Bram Moolenaar68452172021-04-12 21:21:02 +02001406 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1407 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001408 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001409 }
1410 else
1411 {
1412 val = (int)tv_get_number_chk(rettv, &error);
1413 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001414 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001415 }
1416 }
1417 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001418 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001419 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001420
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001421 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1422 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001423 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001424 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001425 *endp = cc;
1426 return;
1427 }
1428
Bram Moolenaarff697e62019-02-12 22:28:33 +01001429 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001430 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001431 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001432 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001433 {
1434 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001435 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1436 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001437 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001438 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001439 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001440 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001441 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001442 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001443 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001444 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001445 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1446 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001447 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001448 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001449 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001450 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001451 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001452 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001453 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001454 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001455 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001456 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001457 else if (lp->ll_range)
1458 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001459 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1460 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001461 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001462 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001463 return;
1464 }
1465
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001466 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1467 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001468 }
1469 else
1470 {
1471 /*
1472 * Assign to a List or Dictionary item.
1473 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001474 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1475 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001476 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001477 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001478 return;
1479 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001480
1481 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001482 && check_typval_arg_type(lp->ll_valtype, rettv,
1483 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001484 return;
1485
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001486 if (lp->ll_newkey != NULL)
1487 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001488 if (op != NULL && *op != '=')
1489 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001490 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001491 return;
1492 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001493 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1494 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001495 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001496
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001497 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001498 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001499 if (di == NULL)
1500 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001501 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1502 {
1503 vim_free(di);
1504 return;
1505 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001506 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001507 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001508 else if (op != NULL && *op != '=')
1509 {
1510 tv_op(lp->ll_tv, rettv, op);
1511 return;
1512 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001513 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001514 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001515
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001516 /*
1517 * Assign the value to the variable or list item.
1518 */
1519 if (copy)
1520 copy_tv(rettv, lp->ll_tv);
1521 else
1522 {
1523 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001524 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001525 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001526 }
1527 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001528}
1529
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001530/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001531 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1532 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001533 * Returns OK or FAIL.
1534 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001535 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001536tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001537{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001538 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001539 char_u numbuf[NUMBUFLEN];
1540 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001541 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001542
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001543 // Can't do anything with a Funcref or Dict on the right.
1544 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001545 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001546 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1547 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001548 {
1549 switch (tv1->v_type)
1550 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001551 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001552 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001553 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001554 case VAR_DICT:
1555 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001556 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001557 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001558 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001559 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001560 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001561 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001562 break;
1563
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001564 case VAR_BLOB:
1565 if (*op != '+' || tv2->v_type != VAR_BLOB)
1566 break;
1567 // BLOB += BLOB
1568 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1569 {
1570 blob_T *b1 = tv1->vval.v_blob;
1571 blob_T *b2 = tv2->vval.v_blob;
1572 int i, len = blob_len(b2);
1573 for (i = 0; i < len; i++)
1574 ga_append(&b1->bv_ga, blob_get(b2, i));
1575 }
1576 return OK;
1577
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001578 case VAR_LIST:
1579 if (*op != '+' || tv2->v_type != VAR_LIST)
1580 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001581 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001582 if (tv2->vval.v_list != NULL)
1583 {
1584 if (tv1->vval.v_list == NULL)
1585 {
1586 tv1->vval.v_list = tv2->vval.v_list;
1587 ++tv1->vval.v_list->lv_refcount;
1588 }
1589 else
1590 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1591 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001592 return OK;
1593
1594 case VAR_NUMBER:
1595 case VAR_STRING:
1596 if (tv2->v_type == VAR_LIST)
1597 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001598 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001599 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001600 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001601 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001602#ifdef FEAT_FLOAT
1603 if (tv2->v_type == VAR_FLOAT)
1604 {
1605 float_T f = n;
1606
Bram Moolenaarff697e62019-02-12 22:28:33 +01001607 if (*op == '%')
1608 break;
1609 switch (*op)
1610 {
1611 case '+': f += tv2->vval.v_float; break;
1612 case '-': f -= tv2->vval.v_float; break;
1613 case '*': f *= tv2->vval.v_float; break;
1614 case '/': f /= tv2->vval.v_float; break;
1615 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001616 clear_tv(tv1);
1617 tv1->v_type = VAR_FLOAT;
1618 tv1->vval.v_float = f;
1619 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001620 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001621#endif
1622 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001623 switch (*op)
1624 {
1625 case '+': n += tv_get_number(tv2); break;
1626 case '-': n -= tv_get_number(tv2); break;
1627 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001628 case '/': n = num_divide(n, tv_get_number(tv2),
1629 &failed); break;
1630 case '%': n = num_modulus(n, tv_get_number(tv2),
1631 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001632 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001633 clear_tv(tv1);
1634 tv1->v_type = VAR_NUMBER;
1635 tv1->vval.v_number = n;
1636 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001637 }
1638 else
1639 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001640 if (tv2->v_type == VAR_FLOAT)
1641 break;
1642
Bram Moolenaarff697e62019-02-12 22:28:33 +01001643 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001644 s = tv_get_string(tv1);
1645 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001646 clear_tv(tv1);
1647 tv1->v_type = VAR_STRING;
1648 tv1->vval.v_string = s;
1649 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001650 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001651
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001652 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001653#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001654 {
1655 float_T f;
1656
Bram Moolenaarff697e62019-02-12 22:28:33 +01001657 if (*op == '%' || *op == '.'
1658 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001659 && tv2->v_type != VAR_NUMBER
1660 && tv2->v_type != VAR_STRING))
1661 break;
1662 if (tv2->v_type == VAR_FLOAT)
1663 f = tv2->vval.v_float;
1664 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001665 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001666 switch (*op)
1667 {
1668 case '+': tv1->vval.v_float += f; break;
1669 case '-': tv1->vval.v_float -= f; break;
1670 case '*': tv1->vval.v_float *= f; break;
1671 case '/': tv1->vval.v_float /= f; break;
1672 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001673 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001674#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001675 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001676 }
1677 }
1678
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001679 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001680 return FAIL;
1681}
1682
1683/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001684 * Evaluate the expression used in a ":for var in expr" command.
1685 * "arg" points to "var".
1686 * Set "*errp" to TRUE for an error, FALSE otherwise;
1687 * Return a pointer that holds the info. Null when there is an error.
1688 */
1689 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001690eval_for_line(
1691 char_u *arg,
1692 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001693 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001694 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001695{
Bram Moolenaar33570922005-01-25 22:26:29 +00001696 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001697 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001698 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001699 typval_T tv;
1700 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001701 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001702
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001703 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001704
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001705 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001706 if (fi == NULL)
1707 return NULL;
1708
Bram Moolenaar404557e2021-07-05 21:41:48 +02001709 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1710 &fi->fi_semicolon, FALSE);
1711 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001712 return fi;
1713
Bram Moolenaar404557e2021-07-05 21:41:48 +02001714 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001715 if (expr[0] != 'i' || expr[1] != 'n'
1716 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001717 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001718 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1719 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1720 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001721 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001722 return fi;
1723 }
1724
1725 if (skip)
1726 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001727 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1728 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001729 {
1730 *errp = FALSE;
1731 if (!skip)
1732 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001733 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001734 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001735 l = tv.vval.v_list;
1736 if (l == NULL)
1737 {
1738 // a null list is like an empty list: do nothing
1739 clear_tv(&tv);
1740 }
1741 else
1742 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001743 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001744 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001745
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001746 // No need to increment the refcount, it's already set for
1747 // the list being used in "tv".
1748 fi->fi_list = l;
1749 list_add_watch(l, &fi->fi_lw);
1750 fi->fi_lw.lw_item = l->lv_first;
1751 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001752 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001753 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001754 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001755 fi->fi_bi = 0;
1756 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001757 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001758 typval_T btv;
1759
1760 // Make a copy, so that the iteration still works when the
1761 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001762 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001763 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001764 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001765 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001766 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001767 else if (tv.v_type == VAR_STRING)
1768 {
1769 fi->fi_byte_idx = 0;
1770 fi->fi_string = tv.vval.v_string;
1771 tv.vval.v_string = NULL;
1772 if (fi->fi_string == NULL)
1773 fi->fi_string = vim_strsave((char_u *)"");
1774 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001775 else
1776 {
Sean Dewar80d73952021-08-04 19:25:54 +02001777 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001778 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001779 }
1780 }
1781 }
1782 if (skip)
1783 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001784 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001785
1786 return fi;
1787}
1788
1789/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001790 * Used when looping over a :for line, skip the "in expr" part.
1791 */
1792 void
1793skip_for_lines(void *fi_void, evalarg_T *evalarg)
1794{
1795 forinfo_T *fi = (forinfo_T *)fi_void;
1796 int i;
1797
1798 for (i = 0; i < fi->fi_break_count; ++i)
1799 eval_next_line(evalarg);
1800}
1801
1802/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001803 * Use the first item in a ":for" list. Advance to the next.
1804 * Assign the values to the variable (list). "arg" points to the first one.
1805 * Return TRUE when a valid item was found, FALSE when at end of list or
1806 * something wrong.
1807 */
1808 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001809next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001810{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001811 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001812 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001813 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001814 ? (ASSIGN_FINAL
1815 // first round: error if variable exists
1816 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1817 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001818 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001819 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001820 int skip_assign = in_vim9script() && arg[0] == '_'
1821 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001822
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001823 if (fi->fi_blob != NULL)
1824 {
1825 typval_T tv;
1826
1827 if (fi->fi_bi >= blob_len(fi->fi_blob))
1828 return FALSE;
1829 tv.v_type = VAR_NUMBER;
1830 tv.v_lock = VAR_FIXED;
1831 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1832 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001833 if (skip_assign)
1834 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001835 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001836 fi->fi_varcount, flag, NULL) == OK;
1837 }
1838
1839 if (fi->fi_string != NULL)
1840 {
1841 typval_T tv;
1842 int len;
1843
1844 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1845 if (len == 0)
1846 return FALSE;
1847 tv.v_type = VAR_STRING;
1848 tv.v_lock = VAR_FIXED;
1849 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1850 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001851 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001852 if (skip_assign)
1853 result = TRUE;
1854 else
1855 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001856 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001857 vim_free(tv.vval.v_string);
1858 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001859 }
1860
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001861 item = fi->fi_lw.lw_item;
1862 if (item == NULL)
1863 result = FALSE;
1864 else
1865 {
1866 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001867 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001868 if (skip_assign)
1869 result = TRUE;
1870 else
1871 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001872 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001873 }
1874 return result;
1875}
1876
1877/*
1878 * Free the structure used to store info used by ":for".
1879 */
1880 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001881free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001882{
Bram Moolenaar33570922005-01-25 22:26:29 +00001883 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001884
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001885 if (fi == NULL)
1886 return;
1887 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001888 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001889 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001890 list_unref(fi->fi_list);
1891 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001892 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001893 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001894 else
1895 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001896 vim_free(fi);
1897}
1898
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001900set_context_for_expression(
1901 expand_T *xp,
1902 char_u *arg,
1903 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001905 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001907 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001909 if (cmdidx == CMD_let || cmdidx == CMD_var
1910 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001911 {
1912 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001913 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001914 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001915 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001916 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001917 {
1918 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001919 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001920 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921 break;
1922 }
1923 return;
1924 }
1925 }
1926 else
1927 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1928 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 while ((xp->xp_pattern = vim_strpbrk(arg,
1930 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1931 {
1932 c = *xp->xp_pattern;
1933 if (c == '&')
1934 {
1935 c = xp->xp_pattern[1];
1936 if (c == '&')
1937 {
1938 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001939 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 }
1941 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001942 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001944 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1945 xp->xp_pattern += 2;
1946
1947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 }
1949 else if (c == '$')
1950 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001951 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 xp->xp_context = EXPAND_ENV_VARS;
1953 }
1954 else if (c == '=')
1955 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001956 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 xp->xp_context = EXPAND_EXPRESSION;
1958 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001959 else if (c == '#'
1960 && xp->xp_context == EXPAND_EXPRESSION)
1961 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001962 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001963 break;
1964 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001965 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 && xp->xp_context == EXPAND_FUNCTIONS
1967 && vim_strchr(xp->xp_pattern, '(') == NULL)
1968 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001969 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 break;
1971 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001972 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001974 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 {
1976 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1977 if (c == '\\' && xp->xp_pattern[1] != NUL)
1978 ++xp->xp_pattern;
1979 xp->xp_context = EXPAND_NOTHING;
1980 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001981 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001983 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1985 /* skip */ ;
1986 xp->xp_context = EXPAND_NOTHING;
1987 }
1988 else if (c == '|')
1989 {
1990 if (xp->xp_pattern[1] == '|')
1991 {
1992 ++xp->xp_pattern;
1993 xp->xp_context = EXPAND_EXPRESSION;
1994 }
1995 else
1996 xp->xp_context = EXPAND_COMMANDS;
1997 }
1998 else
1999 xp->xp_context = EXPAND_EXPRESSION;
2000 }
2001 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002002 // Doesn't look like something valid, expand as an expression
2003 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002004 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 arg = xp->xp_pattern;
2006 if (*arg != NUL)
2007 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2008 /* skip */ ;
2009 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002010
2011 // ":exe one two" completes "two"
2012 if ((cmdidx == CMD_execute
2013 || cmdidx == CMD_echo
2014 || cmdidx == CMD_echon
2015 || cmdidx == CMD_echomsg)
2016 && xp->xp_context == EXPAND_EXPRESSION)
2017 {
2018 for (;;)
2019 {
2020 char_u *n = skiptowhite(arg);
2021
2022 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2023 break;
2024 arg = skipwhite(n);
2025 }
2026 }
2027
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 xp->xp_pattern = arg;
2029}
2030
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002032 * Return TRUE if "pat" matches "text".
2033 * Does not use 'cpo' and always uses 'magic'.
2034 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002035 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002036pattern_match(char_u *pat, char_u *text, int ic)
2037{
2038 int matches = FALSE;
2039 char_u *save_cpo;
2040 regmatch_T regmatch;
2041
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002042 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002043 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002044 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002045 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2046 if (regmatch.regprog != NULL)
2047 {
2048 regmatch.rm_ic = ic;
2049 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2050 vim_regfree(regmatch.regprog);
2051 }
2052 p_cpo = save_cpo;
2053 return matches;
2054}
2055
2056/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002057 * Handle a name followed by "(". Both for just "name(arg)" and for
2058 * "expr->name(arg)".
2059 * Returns OK or FAIL.
2060 */
2061 static int
2062eval_func(
2063 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002064 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002065 char_u *name,
2066 int name_len,
2067 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002068 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002069 typval_T *basetv) // "expr" for "expr->name(arg)"
2070{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002071 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002072 char_u *s = name;
2073 int len = name_len;
2074 partial_T *partial;
2075 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002076 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002077 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002078
2079 if (!evaluate)
2080 check_vars(s, len);
2081
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002082 // If "s" is the name of a variable of type VAR_FUNC
2083 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002084 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002085 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002086
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002087 // Need to make a copy, in case evaluating the arguments makes
2088 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002089 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002090 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002091 ret = FAIL;
2092 else
2093 {
2094 funcexe_T funcexe;
2095
2096 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002097 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002098 funcexe.fe_firstline = curwin->w_cursor.lnum;
2099 funcexe.fe_lastline = curwin->w_cursor.lnum;
2100 funcexe.fe_evaluate = evaluate;
2101 funcexe.fe_partial = partial;
2102 funcexe.fe_basetv = basetv;
2103 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002104 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002105 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002106 }
2107 vim_free(s);
2108
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002109 // If evaluate is FALSE rettv->v_type was not set in
2110 // get_func_tv, but it's needed in handle_subscript() to parse
2111 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002112 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2113 {
2114 rettv->vval.v_string = NULL;
2115 rettv->v_type = VAR_FUNC;
2116 }
2117
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002118 // Stop the expression evaluation when immediately
2119 // aborting on error, or when an interrupt occurred or
2120 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002121 if (evaluate && aborting())
2122 {
2123 if (ret == OK)
2124 clear_tv(rettv);
2125 ret = FAIL;
2126 }
2127 return ret;
2128}
2129
2130/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002131 * Get the next line source line without advancing. But do skip over comment
2132 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002133 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002134 */
2135 static char_u *
2136getline_peek_skip_comments(evalarg_T *evalarg)
2137{
2138 for (;;)
2139 {
2140 char_u *next = getline_peek(evalarg->eval_getline,
2141 evalarg->eval_cookie);
2142 char_u *p;
2143
2144 if (next == NULL)
2145 break;
2146 p = skipwhite(next);
2147 if (*p != NUL && !vim9_comment_start(p))
2148 return next;
2149 (void)eval_next_line(evalarg);
2150 }
2151 return NULL;
2152}
2153
2154/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002155 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2156 * comment) and there is a next line, return the next line (skipping blanks)
2157 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002158 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2159 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002160 * "arg" must point somewhere inside a line, not at the start.
2161 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002162 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002163eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2164{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002165 char_u *p = skipwhite(arg);
2166
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002167 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002168 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002169 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002170 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002171 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002172 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002173 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002174
2175 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002176 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002177 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002178 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002179
Bram Moolenaar9d489562020-07-30 20:08:50 +02002180 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002181 {
2182 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002183 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002184 }
2185 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002186 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002187}
2188
2189/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002190 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002191 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002192 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002193 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002194eval_next_line(evalarg_T *evalarg)
2195{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002196 garray_T *gap = &evalarg->eval_ga;
2197 char_u *line;
2198
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002199 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002200 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2201 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002202 else
2203 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002204 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002205 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2206 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002207 char_u *p = skipwhite(line);
2208
2209 // Going to concatenate the lines after parsing. For an empty or
2210 // comment line use an empty string.
2211 if (*p == NUL || vim9_comment_start(p))
2212 {
2213 vim_free(line);
2214 line = vim_strsave((char_u *)"");
2215 }
2216
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002217 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2218 ++gap->ga_len;
2219 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002220 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002221 {
2222 vim_free(evalarg->eval_tofree);
2223 evalarg->eval_tofree = line;
2224 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002225
2226 // Advanced to the next line, "arg" no longer points into the previous
2227 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002228 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002229 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002230}
2231
Bram Moolenaare6b53242020-07-01 17:28:33 +02002232/*
2233 * Call eval_next_non_blank() and get the next line if needed.
2234 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002235 char_u *
2236skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2237{
2238 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002239 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002240
Bram Moolenaar962d7212020-07-04 14:15:00 +02002241 if (evalarg == NULL)
2242 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002243 eval_next_non_blank(p, evalarg, &getnext);
2244 if (getnext)
2245 return eval_next_line(evalarg);
2246 return p;
2247}
2248
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002249/*
Bram Moolenaar844fb642021-10-23 13:32:30 +01002250 * Initialize "evalarg" for use.
2251 */
2252 void
2253init_evalarg(evalarg_T *evalarg)
2254{
2255 CLEAR_POINTER(evalarg);
2256 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
2257}
2258
2259/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002260 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002261 */
2262 void
2263clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2264{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002265 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002266 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002267 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002268 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002269 if (eap != NULL)
2270 {
2271 // We may need to keep the original command line, e.g. for
2272 // ":let" it has the variable names. But we may also need the
2273 // new one, "nextcmd" points into it. Keep both.
2274 vim_free(eap->cmdline_tofree);
2275 eap->cmdline_tofree = *eap->cmdlinep;
2276 *eap->cmdlinep = evalarg->eval_tofree;
2277 }
2278 else
2279 vim_free(evalarg->eval_tofree);
2280 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002281 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002282
Bram Moolenaar844fb642021-10-23 13:32:30 +01002283 ga_clear_strings(&evalarg->eval_tofree_ga);
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002284 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002285 }
2286}
2287
2288/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002290 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2292 */
2293
2294/*
2295 * Handle zero level expression.
2296 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002298 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002299 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 * Return OK or FAIL.
2301 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002302 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002303eval0(
2304 char_u *arg,
2305 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002306 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002307 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002309 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2310}
2311
2312/*
2313 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2314 * expression and don't check what comes after the expression.
2315 */
2316 int
2317eval0_retarg(
2318 char_u *arg,
2319 typval_T *rettv,
2320 exarg_T *eap,
2321 evalarg_T *evalarg,
2322 char_u **retarg)
2323{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 int ret;
2325 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002326 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002327 int did_emsg_before = did_emsg;
2328 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002329 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002330 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002331 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332
2333 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002334 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002335 expr_end = p;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002336 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002337
Bram Moolenaar02929a32021-12-17 14:46:12 +00002338 // In Vim9 script a command block is not split at NL characters for
2339 // commands using an expression argument. Skip over a '#' comment to check
2340 // for a following NL. Require white space before the '#'.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002341 if (in_vim9script() && p > expr_end && retarg == NULL)
Bram Moolenaar02929a32021-12-17 14:46:12 +00002342 while (*p == '#')
2343 {
2344 char_u *nl = vim_strchr(p, NL);
2345
2346 if (nl == NULL)
2347 break;
2348 p = skipwhite(nl + 1);
2349 if (eap != NULL && *p != NUL)
2350 eap->nextcmd = p;
2351 check_for_end = FALSE;
2352 }
2353
2354 if (ret != FAIL && check_for_end)
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002355 end_error = !ends_excmd2(arg, p);
2356 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 {
2358 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 /*
2361 * Report the invalid expression unless the expression evaluation has
2362 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002363 * exception, or we already gave a more specific error.
2364 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002366 if (!aborting()
2367 && did_emsg == did_emsg_before
2368 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002369 && (flags & EVAL_CONSTANT) == 0
2370 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002371 {
2372 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002373 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002374 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002375 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002376 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002377
2378 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002379 // a next command to avoid more errors, unless "|" is following, which
2380 // could only be a command separator.
2381 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2382 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002383 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002385
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002386 if (retarg != NULL)
2387 *retarg = p;
2388 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002389 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002390
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 return ret;
2392}
2393
2394/*
2395 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002396 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002397 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 *
2399 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002400 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002402 * Note: "rettv.v_lock" is not set.
2403 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 * Return OK or FAIL.
2405 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002406 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002407eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002409 char_u *p;
2410 int getnext;
2411
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002412 CLEAR_POINTER(rettv);
2413
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 /*
2415 * Get the first variable.
2416 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002417 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 return FAIL;
2419
Bram Moolenaar793648f2020-06-26 21:28:25 +02002420 p = eval_next_non_blank(*arg, evalarg, &getnext);
2421 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002423 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002424 int result;
2425 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002426 evalarg_T *evalarg_used = evalarg;
2427 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002428 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002429 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002430 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002431
2432 if (evalarg == NULL)
2433 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002434 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002435 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002436 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002437 orig_flags = evalarg_used->eval_flags;
2438 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002439
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002440 if (getnext)
2441 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002442 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002443 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002444 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002445 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002446 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002447 clear_tv(rettv);
2448 return FAIL;
2449 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002450 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002451 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002452
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002454 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002456 int error = FALSE;
2457
Bram Moolenaar13106602020-10-04 16:06:05 +02002458 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002459 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002460 else if (vim9script)
2461 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002462 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002464 if (error || !op_falsy || !result)
2465 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002466 if (error)
2467 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 }
2469
2470 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002471 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002473 if (op_falsy)
2474 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002475 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002476 {
mityu4ac198c2021-05-28 17:52:40 +02002477 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002478 clear_tv(rettv);
2479 return FAIL;
2480 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002481 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002482 evalarg_used->eval_flags = (op_falsy ? !result : result)
2483 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2484 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002485 {
2486 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002488 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002489 if (!op_falsy || !result)
2490 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002492 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002494 /*
2495 * Check for the ":".
2496 */
2497 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2498 if (*p != ':')
2499 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002500 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002501 if (evaluate && result)
2502 clear_tv(rettv);
2503 evalarg_used->eval_flags = orig_flags;
2504 return FAIL;
2505 }
2506 if (getnext)
2507 *arg = eval_next_line(evalarg_used);
2508 else
2509 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002510 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002511 {
2512 error_white_both(p, 1);
2513 clear_tv(rettv);
2514 evalarg_used->eval_flags = orig_flags;
2515 return FAIL;
2516 }
2517 *arg = p;
2518 }
2519
2520 /*
2521 * Get the third variable. Recursive!
2522 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002523 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002524 {
mityu4ac198c2021-05-28 17:52:40 +02002525 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002526 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002527 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002528 return FAIL;
2529 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002530 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2531 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002532 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002533 if (eval1(arg, &var2, evalarg_used) == FAIL)
2534 {
2535 if (evaluate && result)
2536 clear_tv(rettv);
2537 evalarg_used->eval_flags = orig_flags;
2538 return FAIL;
2539 }
2540 if (evaluate && !result)
2541 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002543
2544 if (evalarg == NULL)
2545 clear_evalarg(&local_evalarg, NULL);
2546 else
2547 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 }
2549
2550 return OK;
2551}
2552
2553/*
2554 * Handle first level expression:
2555 * expr2 || expr2 || expr2 logical OR
2556 *
2557 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002558 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 *
2560 * Return OK or FAIL.
2561 */
2562 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002563eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002565 char_u *p;
2566 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567
2568 /*
2569 * Get the first variable.
2570 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002571 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 return FAIL;
2573
2574 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002575 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002577 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002578 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002580 evalarg_T *evalarg_used = evalarg;
2581 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002582 int evaluate;
2583 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002584 long result = FALSE;
2585 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002586 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002587 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002588
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002589 if (evalarg == NULL)
2590 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002591 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002592 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002593 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002594 orig_flags = evalarg_used->eval_flags;
2595 evaluate = orig_flags & EVAL_EVALUATE;
2596 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002597 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002598 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002599 result = tv_get_bool_chk(rettv, &error);
2600 else if (tv_get_number_chk(rettv, &error) != 0)
2601 result = TRUE;
2602 clear_tv(rettv);
2603 if (error)
2604 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 }
2606
2607 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002608 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002610 while (p[0] == '|' && p[1] == '|')
2611 {
2612 if (getnext)
2613 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002614 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002615 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002616 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002617 {
2618 error_white_both(p, 2);
2619 clear_tv(rettv);
2620 return FAIL;
2621 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002622 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002623 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002624
2625 /*
2626 * Get the second variable.
2627 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002628 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002629 {
mityu4ac198c2021-05-28 17:52:40 +02002630 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002631 clear_tv(rettv);
2632 return FAIL;
2633 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002634 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2635 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002636 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002637 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002638 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002639
2640 /*
2641 * Compute the result.
2642 */
2643 if (evaluate && !result)
2644 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002645 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002646 result = tv_get_bool_chk(&var2, &error);
2647 else if (tv_get_number_chk(&var2, &error) != 0)
2648 result = TRUE;
2649 clear_tv(&var2);
2650 if (error)
2651 return FAIL;
2652 }
2653 if (evaluate)
2654 {
2655 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002656 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002657 rettv->v_type = VAR_BOOL;
2658 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002659 }
2660 else
2661 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002662 rettv->v_type = VAR_NUMBER;
2663 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002664 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002665 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002666
2667 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002669
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002670 if (evalarg == NULL)
2671 clear_evalarg(&local_evalarg, NULL);
2672 else
2673 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 }
2675
2676 return OK;
2677}
2678
2679/*
2680 * Handle second level expression:
2681 * expr3 && expr3 && expr3 logical AND
2682 *
2683 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002684 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 *
2686 * Return OK or FAIL.
2687 */
2688 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002689eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002691 char_u *p;
2692 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693
2694 /*
2695 * Get the first variable.
2696 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002697 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 return FAIL;
2699
2700 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002701 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002703 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002704 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002706 evalarg_T *evalarg_used = evalarg;
2707 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002708 int orig_flags;
2709 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002710 long result = TRUE;
2711 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002712 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002713 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002714
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002715 if (evalarg == NULL)
2716 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002717 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002718 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002719 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002720 orig_flags = evalarg_used->eval_flags;
2721 evaluate = orig_flags & EVAL_EVALUATE;
2722 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002723 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002724 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002725 result = tv_get_bool_chk(rettv, &error);
2726 else if (tv_get_number_chk(rettv, &error) == 0)
2727 result = FALSE;
2728 clear_tv(rettv);
2729 if (error)
2730 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 }
2732
2733 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002734 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002736 while (p[0] == '&' && p[1] == '&')
2737 {
2738 if (getnext)
2739 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002740 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002741 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002742 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002743 {
2744 error_white_both(p, 2);
2745 clear_tv(rettv);
2746 return FAIL;
2747 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002748 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002749 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002750
2751 /*
2752 * Get the second variable.
2753 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002754 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002755 {
mityu4ac198c2021-05-28 17:52:40 +02002756 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002757 clear_tv(rettv);
2758 return FAIL;
2759 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002760 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2761 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002762 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002763 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002764 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002765 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002766
2767 /*
2768 * Compute the result.
2769 */
2770 if (evaluate && result)
2771 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002772 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002773 result = tv_get_bool_chk(&var2, &error);
2774 else if (tv_get_number_chk(&var2, &error) == 0)
2775 result = FALSE;
2776 clear_tv(&var2);
2777 if (error)
2778 return FAIL;
2779 }
2780 if (evaluate)
2781 {
2782 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002783 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002784 rettv->v_type = VAR_BOOL;
2785 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002786 }
2787 else
2788 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002789 rettv->v_type = VAR_NUMBER;
2790 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002791 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002792 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002793
2794 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002796
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002797 if (evalarg == NULL)
2798 clear_evalarg(&local_evalarg, NULL);
2799 else
2800 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 }
2802
2803 return OK;
2804}
2805
2806/*
2807 * Handle third level expression:
2808 * var1 == var2
2809 * var1 =~ var2
2810 * var1 != var2
2811 * var1 !~ var2
2812 * var1 > var2
2813 * var1 >= var2
2814 * var1 < var2
2815 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002816 * var1 is var2
2817 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 *
2819 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002820 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 *
2822 * Return OK or FAIL.
2823 */
2824 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002825eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002828 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002829 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002831 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832
2833 /*
2834 * Get the first variable.
2835 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002836 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 return FAIL;
2838
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002839 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002840 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841
2842 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002843 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002845 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002847 typval_T var2;
2848 int ic;
2849 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002850 int evaluate = evalarg == NULL
2851 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002852 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002853
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002854 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002855 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002856 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002857 p = *arg;
2858 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002859 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2860 {
mityu4ac198c2021-05-28 17:52:40 +02002861 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002862 clear_tv(rettv);
2863 return FAIL;
2864 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002865
Bram Moolenaar696ba232020-07-29 21:20:41 +02002866 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2867 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002868 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002869 clear_tv(rettv);
2870 return FAIL;
2871 }
2872
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002873 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 if (p[len] == '?')
2875 {
2876 ic = TRUE;
2877 ++len;
2878 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002879 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 else if (p[len] == '#')
2881 {
2882 ic = FALSE;
2883 ++len;
2884 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002885 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002887 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888
2889 /*
2890 * Get the second variable.
2891 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002892 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2893 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002894 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002895 clear_tv(rettv);
2896 return FAIL;
2897 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002898 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002899 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002901 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 return FAIL;
2903 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002904 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002905 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002906 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002907
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002908 // use the line of the comparison for messages
2909 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002910 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002911 {
2912 ret = FAIL;
2913 clear_tv(rettv);
2914 }
2915 else
2916 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002917 clear_tv(&var2);
2918 return ret;
2919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 }
2921
2922 return OK;
2923}
2924
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002925/*
2926 * Make a copy of blob "tv1" and append blob "tv2".
2927 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002928 void
2929eval_addblob(typval_T *tv1, typval_T *tv2)
2930{
2931 blob_T *b1 = tv1->vval.v_blob;
2932 blob_T *b2 = tv2->vval.v_blob;
2933 blob_T *b = blob_alloc();
2934 int i;
2935
2936 if (b != NULL)
2937 {
2938 for (i = 0; i < blob_len(b1); i++)
2939 ga_append(&b->bv_ga, blob_get(b1, i));
2940 for (i = 0; i < blob_len(b2); i++)
2941 ga_append(&b->bv_ga, blob_get(b2, i));
2942
2943 clear_tv(tv1);
2944 rettv_blob_set(tv1, b);
2945 }
2946}
2947
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002948/*
2949 * Make a copy of list "tv1" and append list "tv2".
2950 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951 int
2952eval_addlist(typval_T *tv1, typval_T *tv2)
2953{
2954 typval_T var3;
2955
2956 // concatenate Lists
2957 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2958 {
2959 clear_tv(tv1);
2960 clear_tv(tv2);
2961 return FAIL;
2962 }
2963 clear_tv(tv1);
2964 *tv1 = var3;
2965 return OK;
2966}
2967
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968/*
2969 * Handle fourth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00002970 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002972 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002973 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 *
2975 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002976 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 *
2978 * Return OK or FAIL.
2979 */
2980 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002981eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 /*
2984 * Get the first variable.
2985 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002986 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 return FAIL;
2988
2989 /*
2990 * Repeat computing, until no '+', '-' or '.' is following.
2991 */
2992 for (;;)
2993 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002994 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002995 int getnext;
2996 char_u *p;
2997 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002998 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002999 int concat;
3000 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003001 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003002
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003003 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003004 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003005 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003006 p = eval_next_non_blank(*arg, evalarg, &getnext);
3007 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003008 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003009 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3010 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003012 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3013 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003014
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003015 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003016 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003017 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003018 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003019 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003020 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003021 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003022 {
mityu4ac198c2021-05-28 17:52:40 +02003023 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003024 clear_tv(rettv);
3025 return FAIL;
3026 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003027 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003028 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003029 if ((op != '+' || (rettv->v_type != VAR_LIST
3030 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003031#ifdef FEAT_FLOAT
3032 && (op == '.' || rettv->v_type != VAR_FLOAT)
3033#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003034 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003035 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003036 int error = FALSE;
3037
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003038 // For "list + ...", an illegal use of the first operand as
3039 // a number cannot be determined before evaluating the 2nd
3040 // operand: if this is also a list, all is ok.
3041 // For "something . ...", "something - ..." or "non-list + ...",
3042 // we know that the first operand needs to be a string or number
3043 // without evaluating the 2nd operand. So check before to avoid
3044 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003045 if (op != '.')
3046 tv_get_number_chk(rettv, &error);
3047 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003048 {
3049 clear_tv(rettv);
3050 return FAIL;
3051 }
3052 }
3053
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 /*
3055 * Get the second variable.
3056 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003057 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003058 {
mityu89dcb4d2021-05-28 13:50:17 +02003059 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003060 clear_tv(rettv);
3061 return FAIL;
3062 }
3063 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02003064 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003066 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 return FAIL;
3068 }
3069
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003070 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 {
3072 /*
3073 * Compute the result.
3074 */
3075 if (op == '.')
3076 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003077 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3078 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003079 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003080
Bram Moolenaar2e086612020-08-25 22:37:48 +02003081 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003082 || var2.v_type == VAR_CHANNEL
3083 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003084 semsg(_(e_using_invalid_value_as_string_str),
3085 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003086#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003087 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003088 {
3089 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3090 var2.vval.v_float);
3091 s2 = buf2;
3092 }
3093#endif
3094 else
3095 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003096 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003097 {
3098 clear_tv(rettv);
3099 clear_tv(&var2);
3100 return FAIL;
3101 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003102 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003103 clear_tv(rettv);
3104 rettv->v_type = VAR_STRING;
3105 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003107 else if (op == '+' && rettv->v_type == VAR_BLOB
3108 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003110 else if (op == '+' && rettv->v_type == VAR_LIST
3111 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003112 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003114 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 else
3117 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003118 int error = FALSE;
3119 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003120#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003121 float_T f1 = 0, f2 = 0;
3122
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003123 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003124 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003125 f1 = rettv->vval.v_float;
3126 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003127 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003128 else
3129#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003130 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003131 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003132 if (error)
3133 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003134 // This can only happen for "list + non-list" or
3135 // "blob + non-blob". For "non-list + ..." or
3136 // "something - ...", we returned before evaluating the
3137 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003138 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003139 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003140 return FAIL;
3141 }
3142#ifdef FEAT_FLOAT
3143 if (var2.v_type == VAR_FLOAT)
3144 f1 = n1;
3145#endif
3146 }
3147#ifdef FEAT_FLOAT
3148 if (var2.v_type == VAR_FLOAT)
3149 {
3150 f2 = var2.vval.v_float;
3151 n2 = 0;
3152 }
3153 else
3154#endif
3155 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003156 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003157 if (error)
3158 {
3159 clear_tv(rettv);
3160 clear_tv(&var2);
3161 return FAIL;
3162 }
3163#ifdef FEAT_FLOAT
3164 if (rettv->v_type == VAR_FLOAT)
3165 f2 = n2;
3166#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003167 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003168 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003169
3170#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003171 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003172 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3173 {
3174 if (op == '+')
3175 f1 = f1 + f2;
3176 else
3177 f1 = f1 - f2;
3178 rettv->v_type = VAR_FLOAT;
3179 rettv->vval.v_float = f1;
3180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003182#endif
3183 {
3184 if (op == '+')
3185 n1 = n1 + n2;
3186 else
3187 n1 = n1 - n2;
3188 rettv->v_type = VAR_NUMBER;
3189 rettv->vval.v_number = n1;
3190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003192 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 }
3194 }
3195 return OK;
3196}
3197
3198/*
3199 * Handle fifth level expression:
3200 * * number multiplication
3201 * / number division
3202 * % number modulo
3203 *
3204 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003205 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 *
3207 * Return OK or FAIL.
3208 */
3209 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003210eval6(
3211 char_u **arg,
3212 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003213 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003214 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003216#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003217 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219
3220 /*
3221 * Get the first variable.
3222 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003223 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 return FAIL;
3225
3226 /*
3227 * Repeat computing, until no '*', '/' or '%' is following.
3228 */
3229 for (;;)
3230 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003231 int evaluate;
3232 int getnext;
3233 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003234 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003235 int op;
3236 varnumber_T n1, n2;
3237#ifdef FEAT_FLOAT
3238 float_T f1, f2;
3239#endif
3240 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003241
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003242 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003243 p = eval_next_non_blank(*arg, evalarg, &getnext);
3244 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003245 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003247
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003248 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003249 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003250 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003251 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003252 {
3253 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3254 {
mityu4ac198c2021-05-28 17:52:40 +02003255 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003256 clear_tv(rettv);
3257 return FAIL;
3258 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003259 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003262#ifdef FEAT_FLOAT
3263 f1 = 0;
3264 f2 = 0;
3265#endif
3266 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003267 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003269#ifdef FEAT_FLOAT
3270 if (rettv->v_type == VAR_FLOAT)
3271 {
3272 f1 = rettv->vval.v_float;
3273 use_float = TRUE;
3274 n1 = 0;
3275 }
3276 else
3277#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003278 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003279 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003280 if (error)
3281 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 }
3283 else
3284 n1 = 0;
3285
3286 /*
3287 * Get the second variable.
3288 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003289 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3290 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003291 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003292 clear_tv(rettv);
3293 return FAIL;
3294 }
3295 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003296 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 return FAIL;
3298
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003299 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003301#ifdef FEAT_FLOAT
3302 if (var2.v_type == VAR_FLOAT)
3303 {
3304 if (!use_float)
3305 {
3306 f1 = n1;
3307 use_float = TRUE;
3308 }
3309 f2 = var2.vval.v_float;
3310 n2 = 0;
3311 }
3312 else
3313#endif
3314 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003315 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003316 clear_tv(&var2);
3317 if (error)
3318 return FAIL;
3319#ifdef FEAT_FLOAT
3320 if (use_float)
3321 f2 = n2;
3322#endif
3323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324
3325 /*
3326 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003327 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003329#ifdef FEAT_FLOAT
3330 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003332 if (op == '*')
3333 f1 = f1 * f2;
3334 else if (op == '/')
3335 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003336# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003337 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003338 if (f2 == 0.0)
3339 {
3340 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003341 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003342 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003343 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003344 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003345 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003346 }
3347 else
3348 f1 = f1 / f2;
3349# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003350 // We rely on the floating point library to handle divide
3351 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003352 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003353# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003356 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003357 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003358 return FAIL;
3359 }
3360 rettv->v_type = VAR_FLOAT;
3361 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 }
3363 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003366 int failed = FALSE;
3367
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003368 if (op == '*')
3369 n1 = n1 * n2;
3370 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003371 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003373 n1 = num_modulus(n1, n2, &failed);
3374 if (failed)
3375 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003376
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003377 rettv->v_type = VAR_NUMBER;
3378 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 }
3381 }
3382
3383 return OK;
3384}
3385
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003386/*
3387 * Handle a type cast before a base level expression.
3388 * "arg" must point to the first non-white of the expression.
3389 * "arg" is advanced to just after the recognized expression.
3390 * Return OK or FAIL.
3391 */
3392 static int
3393eval7t(
3394 char_u **arg,
3395 typval_T *rettv,
3396 evalarg_T *evalarg,
3397 int want_string) // after "." operator
3398{
3399 type_T *want_type = NULL;
3400 garray_T type_list; // list of pointers to allocated types
3401 int res;
3402 int evaluate = evalarg == NULL ? 0
3403 : (evalarg->eval_flags & EVAL_EVALUATE);
3404
3405 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003406 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3407 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003408 {
3409 ++*arg;
3410 ga_init2(&type_list, sizeof(type_T *), 10);
3411 want_type = parse_type(arg, &type_list, TRUE);
3412 if (want_type == NULL && (evaluate || **arg != '>'))
3413 {
3414 clear_type_list(&type_list);
3415 return FAIL;
3416 }
3417
3418 if (**arg != '>')
3419 {
3420 if (*skipwhite(*arg) == '>')
3421 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3422 else
3423 emsg(_(e_missing_gt));
3424 clear_type_list(&type_list);
3425 return FAIL;
3426 }
3427 ++*arg;
3428 *arg = skipwhite_and_linebreak(*arg, evalarg);
3429 }
3430
3431 res = eval7(arg, rettv, evalarg, want_string);
3432
3433 if (want_type != NULL && evaluate)
3434 {
3435 if (res == OK)
3436 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003437 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3438 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003439
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003440 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003441 {
3442 if (want_type == &t_bool && actual != &t_bool
3443 && (actual->tt_flags & TTFLAG_BOOL_OK))
3444 {
3445 int n = tv2bool(rettv);
3446
3447 // can use "0" and "1" for boolean in some places
3448 clear_tv(rettv);
3449 rettv->v_type = VAR_BOOL;
3450 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3451 }
3452 else
3453 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003454 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003455
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003456 where.wt_variable = TRUE;
3457 res = check_type(want_type, actual, TRUE, where);
3458 }
3459 }
3460 }
3461 clear_type_list(&type_list);
3462 }
3463
3464 return res;
3465}
3466
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003467 int
3468eval_leader(char_u **arg, int vim9)
3469{
3470 char_u *s = *arg;
3471 char_u *p = *arg;
3472
3473 while (*p == '!' || *p == '-' || *p == '+')
3474 {
3475 char_u *n = skipwhite(p + 1);
3476
3477 // ++, --, -+ and +- are not accepted in Vim9 script
3478 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3479 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003480 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003481 return FAIL;
3482 }
3483 p = n;
3484 }
3485 *arg = p;
3486 return OK;
3487}
3488
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003490 * Check for a predefined value "true", "false" and "null.*".
3491 * Return OK when recognized.
3492 */
3493 int
3494handle_predefined(char_u *s, int len, typval_T *rettv)
3495{
3496 switch (len)
3497 {
3498 case 4: if (STRNCMP(s, "true", 4) == 0)
3499 {
3500 rettv->v_type = VAR_BOOL;
3501 rettv->vval.v_number = VVAL_TRUE;
3502 return OK;
3503 }
3504 if (STRNCMP(s, "null", 4) == 0)
3505 {
3506 rettv->v_type = VAR_SPECIAL;
3507 rettv->vval.v_number = VVAL_NULL;
3508 return OK;
3509 }
3510 break;
3511 case 5: if (STRNCMP(s, "false", 5) == 0)
3512 {
3513 rettv->v_type = VAR_BOOL;
3514 rettv->vval.v_number = VVAL_FALSE;
3515 return OK;
3516 }
3517 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003518 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3519 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003520#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003521 rettv->v_type = VAR_JOB;
3522 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003523#else
3524 rettv->v_type = VAR_SPECIAL;
3525 rettv->vval.v_number = VVAL_NULL;
3526#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003527 return OK;
3528 }
3529 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003530 case 9:
3531 if (STRNCMP(s, "null_", 5) != 0)
3532 break;
3533 if (STRNCMP(s + 5, "list", 4) == 0)
3534 {
3535 rettv->v_type = VAR_LIST;
3536 rettv->vval.v_list = NULL;
3537 return OK;
3538 }
3539 if (STRNCMP(s + 5, "dict", 4) == 0)
3540 {
3541 rettv->v_type = VAR_DICT;
3542 rettv->vval.v_dict = NULL;
3543 return OK;
3544 }
3545 if (STRNCMP(s + 5, "blob", 4) == 0)
3546 {
3547 rettv->v_type = VAR_BLOB;
3548 rettv->vval.v_blob = NULL;
3549 return OK;
3550 }
3551 break;
3552 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3553 {
3554 rettv->v_type = VAR_STRING;
3555 rettv->vval.v_string = NULL;
3556 return OK;
3557 }
3558 break;
3559 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003560 if (STRNCMP(s, "null_channel", 12) == 0)
3561 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003562#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003563 rettv->v_type = VAR_CHANNEL;
3564 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003565#else
3566 rettv->v_type = VAR_SPECIAL;
3567 rettv->vval.v_number = VVAL_NULL;
3568#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003569 return OK;
3570 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003571 if (STRNCMP(s, "null_partial", 12) == 0)
3572 {
3573 rettv->v_type = VAR_PARTIAL;
3574 rettv->vval.v_partial = NULL;
3575 return OK;
3576 }
3577 break;
3578 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3579 {
3580 rettv->v_type = VAR_FUNC;
3581 rettv->vval.v_string = NULL;
3582 return OK;
3583 }
3584 break;
3585 }
3586 return FAIL;
3587}
3588
3589/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 * Handle sixth level expression:
3591 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003592 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003593 * "string" string constant
3594 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 * &option-name option value
3596 * @r register contents
3597 * identifier variable value
3598 * function() function call
3599 * $VAR environment variable
3600 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003601 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003602 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003603 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003604 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 *
3606 * Also handle:
3607 * ! in front logical NOT
3608 * - in front unary minus
3609 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003610 * trailing [] subscript in String or List
3611 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003612 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 *
3614 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003615 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 *
3617 * Return OK or FAIL.
3618 */
3619 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003620eval7(
3621 char_u **arg,
3622 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003623 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003624 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003626 int evaluate = evalarg != NULL
3627 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 int len;
3629 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003630 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 char_u *start_leader, *end_leader;
3632 int ret = OK;
3633 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003634 static int recurse = 0;
3635 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636
3637 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003638 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003639 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003641 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642
3643 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003644 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 */
3646 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003647 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003648 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 end_leader = *arg;
3650
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003651 if (**arg == '.' && (!isdigit(*(*arg + 1))
3652#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003653 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003654#endif
3655 ))
3656 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003657 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003658 ++*arg;
3659 return FAIL;
3660 }
3661
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003662 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003663 // and crash. With MSVC the stack is smaller.
3664 if (recurse ==
3665#ifdef _MSC_VER
3666 300
3667#else
3668 1000
3669#endif
3670 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003671 {
3672 semsg(_(e_expression_too_recursive_str), *arg);
3673 return FAIL;
3674 }
3675 ++recurse;
3676
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 switch (**arg)
3678 {
3679 /*
3680 * Number constant.
3681 */
3682 case '0':
3683 case '1':
3684 case '2':
3685 case '3':
3686 case '4':
3687 case '5':
3688 case '6':
3689 case '7':
3690 case '8':
3691 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003692 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003693
3694 // Apply prefixed "-" and "+" now. Matters especially when
3695 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003696 if (ret == OK && evaluate && end_leader > start_leader
3697 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003698 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 break;
3700
3701 /*
3702 * String constant: "string".
3703 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003704 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 break;
3706
3707 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003708 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003710 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003711 break;
3712
3713 /*
3714 * List: [expr, expr]
3715 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003716 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 break;
3718
3719 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003720 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003721 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003722 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003723 {
3724 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3725 }
3726 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003727 {
3728 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003729 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003730 }
3731 else
3732 ret = NOTDONE;
3733 break;
3734
3735 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003736 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003737 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003738 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003739 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003740 ret = NOTDONE;
3741 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00003742 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003743 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003744 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003745 break;
3746
3747 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003748 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003750 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 break;
3752
3753 /*
3754 * Environment variable: $VAR.
3755 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003756 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 break;
3758
3759 /*
3760 * Register contents: @r.
3761 */
3762 case '@': ++*arg;
3763 if (evaluate)
3764 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003765 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003766 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003767 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003768 emsg_invreg(**arg);
3769 else
3770 {
3771 rettv->v_type = VAR_STRING;
3772 rettv->vval.v_string = get_reg_contents(**arg,
3773 GREG_EXPR_SRC);
3774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 }
3776 if (**arg != NUL)
3777 ++*arg;
3778 break;
3779
3780 /*
3781 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003782 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003784 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003785 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01003786 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003787 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003788 if (ret == OK && evaluate)
3789 {
3790 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3791
Bram Moolenaara9931532021-06-12 15:58:16 +02003792 // Compile it here to get the return type. The return
3793 // type is optional, when it's missing use t_unknown.
3794 // This is recognized in compile_return().
3795 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3796 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003797 if (compile_def_function(ufunc,
Bram Moolenaare99d4222021-06-13 14:01:26 +02003798 FALSE, COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003799 {
3800 clear_tv(rettv);
3801 ret = FAIL;
3802 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003803 }
3804 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003805 if (ret == NOTDONE)
3806 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003807 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003808 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003809
Bram Moolenaar9215f012020-06-27 21:18:00 +02003810 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003811 if (**arg == ')')
3812 ++*arg;
3813 else if (ret == OK)
3814 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003815 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003816 clear_tv(rettv);
3817 ret = FAIL;
3818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 }
3820 break;
3821
Bram Moolenaar8c711452005-01-14 21:53:12 +00003822 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 break;
3824 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003825
3826 if (ret == NOTDONE)
3827 {
3828 /*
3829 * Must be a variable or function name.
3830 * Can also be a curly-braces kind of name: {expr}.
3831 */
3832 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003833 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003834 if (alias != NULL)
3835 s = alias;
3836
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003837 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003838 ret = FAIL;
3839 else
3840 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003841 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3842
Bram Moolenaar4525a572022-02-13 11:57:33 +00003843 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003844 {
3845 emsg(_(e_cannot_use_underscore_here));
3846 ret = FAIL;
3847 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00003848 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00003849 && s[0] == 's' && s[1] == ':')
3850 {
3851 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
3852 ret = FAIL;
3853 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00003854 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003855 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003856 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003857 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003858 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003859 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003860 else if (flags & EVAL_CONSTANT)
3861 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003862 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003863 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003864 // get the value of "true", "false", etc. or a variable
3865 ret = FAIL;
3866 if (vim9script)
3867 ret = handle_predefined(s, len, rettv);
3868 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003869 {
3870 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003871 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003872 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003873 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003874 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003875 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003876 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003877 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003878 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003879 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003880 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003881 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003882 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003883 }
3884
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003885 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3886 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003887 if (ret == OK)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003888 ret = handle_subscript(arg, name_start, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889
3890 /*
3891 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3892 */
3893 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003894 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003895
3896 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003897 return ret;
3898}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003899
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003900/*
3901 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003902 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003903 * Adjusts "end_leaderp" until it is at "start_leader".
3904 */
3905 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003906eval7_leader(
3907 typval_T *rettv,
3908 int numeric_only,
3909 char_u *start_leader,
3910 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003911{
3912 char_u *end_leader = *end_leaderp;
3913 int ret = OK;
3914 int error = FALSE;
3915 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003916 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003917 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003918#ifdef FEAT_FLOAT
3919 float_T f = 0.0;
3920
3921 if (rettv->v_type == VAR_FLOAT)
3922 f = rettv->vval.v_float;
3923 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003924#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003925 {
3926 while (VIM_ISWHITE(end_leader[-1]))
3927 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003928 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003929 val = tv2bool(rettv);
3930 else
3931 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003932 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003933 if (error)
3934 {
3935 clear_tv(rettv);
3936 ret = FAIL;
3937 }
3938 else
3939 {
3940 while (end_leader > start_leader)
3941 {
3942 --end_leader;
3943 if (*end_leader == '!')
3944 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003945 if (numeric_only)
3946 {
3947 ++end_leader;
3948 break;
3949 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003950#ifdef FEAT_FLOAT
3951 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003952 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003953 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003954 {
3955 rettv->v_type = VAR_BOOL;
3956 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3957 }
3958 else
3959 f = !f;
3960 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003961 else
3962#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003963 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003964 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003965 type = VAR_BOOL;
3966 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003967 }
3968 else if (*end_leader == '-')
3969 {
3970#ifdef FEAT_FLOAT
3971 if (rettv->v_type == VAR_FLOAT)
3972 f = -f;
3973 else
3974#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003975 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003976 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003977 type = VAR_NUMBER;
3978 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003979 }
3980 }
3981#ifdef FEAT_FLOAT
3982 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003984 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003985 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003987 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003988#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003989 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003990 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003991 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003992 rettv->v_type = type;
3993 else
3994 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003995 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003998 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 return ret;
4000}
4001
4002/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004003 * Call the function referred to in "rettv".
4004 */
4005 static int
4006call_func_rettv(
4007 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004008 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004009 typval_T *rettv,
4010 int evaluate,
4011 dict_T *selfdict,
4012 typval_T *basetv)
4013{
4014 partial_T *pt = NULL;
4015 funcexe_T funcexe;
4016 typval_T functv;
4017 char_u *s;
4018 int ret;
4019
4020 // need to copy the funcref so that we can clear rettv
4021 if (evaluate)
4022 {
4023 functv = *rettv;
4024 rettv->v_type = VAR_UNKNOWN;
4025
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004026 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004027 if (functv.v_type == VAR_PARTIAL)
4028 {
4029 pt = functv.vval.v_partial;
4030 s = partial_name(pt);
4031 }
4032 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004033 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004034 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004035 if (s == NULL || *s == NUL)
4036 {
4037 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004038 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004039 goto theend;
4040 }
4041 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004042 }
4043 else
4044 s = (char_u *)"";
4045
Bram Moolenaara80faa82020-04-12 19:37:17 +02004046 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004047 funcexe.fe_firstline = curwin->w_cursor.lnum;
4048 funcexe.fe_lastline = curwin->w_cursor.lnum;
4049 funcexe.fe_evaluate = evaluate;
4050 funcexe.fe_partial = pt;
4051 funcexe.fe_selfdict = selfdict;
4052 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004053 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004054
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004055theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004056 // Clear the funcref afterwards, so that deleting it while
4057 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004058 if (evaluate)
4059 clear_tv(&functv);
4060
4061 return ret;
4062}
4063
4064/*
4065 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004066 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004067 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4068 */
4069 static int
4070eval_lambda(
4071 char_u **arg,
4072 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004073 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004074 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004075{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004076 int evaluate = evalarg != NULL
4077 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004078 typval_T base = *rettv;
4079 int ret;
4080
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004081 rettv->v_type = VAR_UNKNOWN;
4082
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004083 if (**arg == '{')
4084 {
4085 // ->{lambda}()
4086 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4087 }
4088 else
4089 {
4090 // ->(lambda)()
4091 ++*arg;
4092 ret = eval1(arg, rettv, evalarg);
4093 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar605ec912021-12-18 16:54:31 +00004094 if (**arg == ')')
4095 {
4096 ++*arg;
4097 }
4098 else
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004099 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004100 emsg(_(e_missing_closing_paren));
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004101 ret = FAIL;
4102 }
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004103 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004104 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004105 return FAIL;
4106 else if (**arg != '(')
4107 {
4108 if (verbose)
4109 {
4110 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004111 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004112 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004113 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004114 }
4115 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004116 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004117 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004118 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004119 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004120
4121 // Clear the funcref afterwards, so that deleting it while
4122 // evaluating the arguments is possible (see test55).
4123 if (evaluate)
4124 clear_tv(&base);
4125
4126 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004127}
4128
4129/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004130 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004131 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004132 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4133 */
4134 static int
4135eval_method(
4136 char_u **arg,
4137 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004138 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004139 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004140{
4141 char_u *name;
4142 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004143 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004144 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004145 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004146 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004147 int evaluate = evalarg != NULL
4148 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004149
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004150 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004151
Bram Moolenaarac92e252019-08-03 21:58:38 +02004152 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004153 len = get_name_len(arg, &alias, evaluate, TRUE);
4154 if (alias != NULL)
4155 name = alias;
4156
4157 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004158 {
4159 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004160 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004161 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004162 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004163 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004164 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004165 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004166
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004167 // If there is no "(" immediately following, but there is further on,
4168 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4169 // Does not handle anything where "(" is part of the expression.
4170 *arg = skipwhite(*arg);
4171
4172 if (**arg != '(' && alias == NULL
4173 && (paren = vim_strchr(*arg, '(')) != NULL)
4174 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004175 char_u *deref;
4176
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004177 *arg = name;
4178 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004179 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4180 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004181 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004182 *arg = name + len;
4183 ret = FAIL;
4184 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004185 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004186 {
4187 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004188 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004189 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004190 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004191 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004192
4193 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004194 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004195 *arg = skipwhite(*arg);
4196
4197 if (**arg != '(')
4198 {
4199 if (verbose)
4200 semsg(_(e_missing_parenthesis_str), name);
4201 ret = FAIL;
4202 }
4203 else if (VIM_ISWHITE((*arg)[-1]))
4204 {
4205 if (verbose)
4206 emsg(_(e_no_white_space_allowed_before_parenthesis));
4207 ret = FAIL;
4208 }
4209 else
4210 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004211 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004212 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004213 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004214
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004215 // Clear the funcref afterwards, so that deleting it while
4216 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004217 if (evaluate)
4218 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004219 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004220
Bram Moolenaarac92e252019-08-03 21:58:38 +02004221 return ret;
4222}
4223
4224/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004225 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4226 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004227 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4228 */
4229 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004230eval_index(
4231 char_u **arg,
4232 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004233 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004234 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004235{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004236 int evaluate = evalarg != NULL
4237 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004238 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004239 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004240 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004241 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004242 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004243 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004244
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004245 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4246 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004247
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004248 init_tv(&var1);
4249 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004250 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004251 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004252 /*
4253 * dict.name
4254 */
4255 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004256 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004257 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004258 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004259 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004260 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004261 }
4262 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004263 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004264 /*
4265 * something[idx]
4266 *
4267 * Get the (first) variable from inside the [].
4268 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004269 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004270 if (**arg == ':')
4271 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004272 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004273 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004274 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004275 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004276 semsg(_(e_white_space_required_before_and_after_str_at_str),
4277 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004278 clear_tv(&var1);
4279 return FAIL;
4280 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004281 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004282 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004283 int error = FALSE;
4284
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004285#ifdef FEAT_FLOAT
4286 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004287 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004288 && var1.v_type == VAR_FLOAT)
4289 {
4290 var1.vval.v_string = typval_tostring(&var1, TRUE);
4291 var1.v_type = VAR_STRING;
4292 }
4293#endif
Bram Moolenaar4525a572022-02-13 11:57:33 +00004294 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004295 tv_get_number_chk(&var1, &error);
4296 else
4297 error = tv_get_string_chk(&var1) == NULL;
4298 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004299 {
4300 // not a number or string
4301 clear_tv(&var1);
4302 return FAIL;
4303 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004304 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004305
4306 /*
4307 * Get the second variable from inside the [:].
4308 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004309 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004310 if (**arg == ':')
4311 {
4312 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004313 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004314 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004315 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004316 semsg(_(e_white_space_required_before_and_after_str_at_str),
4317 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004318 if (!empty1)
4319 clear_tv(&var1);
4320 return FAIL;
4321 }
4322 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004323 if (**arg == ']')
4324 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004325 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004327 if (!empty1)
4328 clear_tv(&var1);
4329 return FAIL;
4330 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004331 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004332 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004333 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004334 if (!empty1)
4335 clear_tv(&var1);
4336 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004337 return FAIL;
4338 }
4339 }
4340
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004341 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004342 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004343 if (**arg != ']')
4344 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004345 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004346 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004347 clear_tv(&var1);
4348 if (range)
4349 clear_tv(&var2);
4350 return FAIL;
4351 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004352 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004353 }
4354
4355 if (evaluate)
4356 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004357 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004358 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004359 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004360
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004361 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004362 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004363 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004364 clear_tv(&var2);
4365 return res;
4366 }
4367 return OK;
4368}
4369
4370/*
4371 * Check if "rettv" can have an [index] or [sli:ce]
4372 */
4373 int
4374check_can_index(typval_T *rettv, int evaluate, int verbose)
4375{
4376 switch (rettv->v_type)
4377 {
4378 case VAR_FUNC:
4379 case VAR_PARTIAL:
4380 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004381 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004382 return FAIL;
4383 case VAR_FLOAT:
4384#ifdef FEAT_FLOAT
4385 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004386 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004387 return FAIL;
4388#endif
4389 case VAR_BOOL:
4390 case VAR_SPECIAL:
4391 case VAR_JOB:
4392 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004393 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004394 if (verbose)
4395 emsg(_(e_cannot_index_special_variable));
4396 return FAIL;
4397 case VAR_UNKNOWN:
4398 case VAR_ANY:
4399 case VAR_VOID:
4400 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004401 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004402 emsg(_(e_cannot_index_special_variable));
4403 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004404 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004405 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004406
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004407 case VAR_STRING:
4408 case VAR_LIST:
4409 case VAR_DICT:
4410 case VAR_BLOB:
4411 break;
4412 case VAR_NUMBER:
4413 if (in_vim9script())
4414 emsg(_(e_cannot_index_number));
4415 break;
4416 }
4417 return OK;
4418}
4419
4420/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004421 * slice() function
4422 */
4423 void
4424f_slice(typval_T *argvars, typval_T *rettv)
4425{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004426 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004427 && ((argvars[0].v_type != VAR_STRING
4428 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004429 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004430 && check_for_list_arg(argvars, 0) == FAIL)
4431 || check_for_number_arg(argvars, 1) == FAIL
4432 || check_for_opt_number_arg(argvars, 2) == FAIL))
4433 return;
4434
Bram Moolenaar6601b622021-01-13 21:47:15 +01004435 if (check_can_index(argvars, TRUE, FALSE) == OK)
4436 {
4437 copy_tv(argvars, rettv);
4438 eval_index_inner(rettv, TRUE, argvars + 1,
4439 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4440 TRUE, NULL, 0, FALSE);
4441 }
4442}
4443
4444/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004445 * Apply index or range to "rettv".
4446 * "var1" is the first index, NULL for [:expr].
4447 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004448 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4449 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004450 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4451 */
4452 int
4453eval_index_inner(
4454 typval_T *rettv,
4455 int is_range,
4456 typval_T *var1,
4457 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004458 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004459 char_u *key,
4460 int keylen,
4461 int verbose)
4462{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004463 varnumber_T n1, n2 = 0;
4464 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004465
4466 n1 = 0;
4467 if (var1 != NULL && rettv->v_type != VAR_DICT)
4468 n1 = tv_get_number(var1);
4469
4470 if (is_range)
4471 {
4472 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004473 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004474 if (verbose)
4475 emsg(_(e_cannot_slice_dictionary));
4476 return FAIL;
4477 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004478 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004479 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004480 else
4481 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004482 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004483
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004484 switch (rettv->v_type)
4485 {
4486 case VAR_UNKNOWN:
4487 case VAR_ANY:
4488 case VAR_VOID:
4489 case VAR_FUNC:
4490 case VAR_PARTIAL:
4491 case VAR_FLOAT:
4492 case VAR_BOOL:
4493 case VAR_SPECIAL:
4494 case VAR_JOB:
4495 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004496 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004497 break; // not evaluating, skipping over subscript
4498
4499 case VAR_NUMBER:
4500 case VAR_STRING:
4501 {
4502 char_u *s = tv_get_string(rettv);
4503
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004504 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004505 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004506 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004507 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004508 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004509 else
4510 s = char_from_string(s, n1);
4511 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004512 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004513 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004514 // The resulting variable is a substring. If the indexes
4515 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004516 if (n1 < 0)
4517 {
4518 n1 = len + n1;
4519 if (n1 < 0)
4520 n1 = 0;
4521 }
4522 if (n2 < 0)
4523 n2 = len + n2;
4524 else if (n2 >= len)
4525 n2 = len;
4526 if (n1 >= len || n2 < 0 || n1 > n2)
4527 s = NULL;
4528 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004529 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004530 }
4531 else
4532 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004533 // The resulting variable is a string of a single
4534 // character. If the index is too big or negative the
4535 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004536 if (n1 >= len || n1 < 0)
4537 s = NULL;
4538 else
4539 s = vim_strnsave(s + n1, 1);
4540 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004541 clear_tv(rettv);
4542 rettv->v_type = VAR_STRING;
4543 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004544 }
4545 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004546
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004547 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004548 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4549 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004550 break;
4551
4552 case VAR_LIST:
4553 if (var1 == NULL)
4554 n1 = 0;
4555 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004556 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004557 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004558 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004559 return FAIL;
4560 break;
4561
4562 case VAR_DICT:
4563 {
4564 dictitem_T *item;
4565 typval_T tmp;
4566
4567 if (key == NULL)
4568 {
4569 key = tv_get_string_chk(var1);
4570 if (key == NULL)
4571 return FAIL;
4572 }
4573
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004574 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004575
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004576 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004577 {
4578 if (verbose)
4579 {
4580 if (keylen > 0)
4581 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004582 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004583 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004584 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004585 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004586
4587 copy_tv(&item->di_tv, &tmp);
4588 clear_tv(rettv);
4589 *rettv = tmp;
4590 }
4591 break;
4592 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004593 return OK;
4594}
4595
4596/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004597 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004598 */
4599 char_u *
4600partial_name(partial_T *pt)
4601{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004602 if (pt != NULL)
4603 {
4604 if (pt->pt_name != NULL)
4605 return pt->pt_name;
4606 if (pt->pt_func != NULL)
4607 return pt->pt_func->uf_name;
4608 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004609 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004610}
4611
Bram Moolenaarddecc252016-04-06 22:59:37 +02004612 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004613partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004614{
4615 int i;
4616
4617 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004618 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004619 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004620 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004621 if (pt->pt_name != NULL)
4622 {
4623 func_unref(pt->pt_name);
4624 vim_free(pt->pt_name);
4625 }
4626 else
4627 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004628
Bram Moolenaar54656012021-06-09 20:50:46 +02004629 // "out_up" is no longer used, decrement refcount on partial that owns it.
4630 partial_unref(pt->pt_outer.out_up_partial);
4631
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004632 // Using pt_outer from another partial.
4633 partial_unref(pt->pt_outer_partial);
4634
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004635 // Decrease the reference count for the context of a closure. If down
4636 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004637 if (pt->pt_funcstack != NULL)
4638 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004639 --pt->pt_funcstack->fs_refcount;
4640 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004641 }
4642
Bram Moolenaarddecc252016-04-06 22:59:37 +02004643 vim_free(pt);
4644}
4645
4646/*
4647 * Unreference a closure: decrement the reference count and free it when it
4648 * becomes zero.
4649 */
4650 void
4651partial_unref(partial_T *pt)
4652{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004653 if (pt != NULL)
4654 {
4655 if (--pt->pt_refcount <= 0)
4656 partial_free(pt);
4657
4658 // If the reference count goes down to one, the funcstack may be the
4659 // only reference and can be freed if no other partials reference it.
4660 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4661 funcstack_check_refcount(pt->pt_funcstack);
4662 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004663}
4664
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004665/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004666 * Return the next (unique) copy ID.
4667 * Used for serializing nested structures.
4668 */
4669 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004670get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004671{
4672 current_copyID += COPYID_INC;
4673 return current_copyID;
4674}
4675
4676/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004677 * Garbage collection for lists and dictionaries.
4678 *
4679 * We use reference counts to be able to free most items right away when they
4680 * are no longer used. But for composite items it's possible that it becomes
4681 * unused while the reference count is > 0: When there is a recursive
4682 * reference. Example:
4683 * :let l = [1, 2, 3]
4684 * :let d = {9: l}
4685 * :let l[1] = d
4686 *
4687 * Since this is quite unusual we handle this with garbage collection: every
4688 * once in a while find out which lists and dicts are not referenced from any
4689 * variable.
4690 *
4691 * Here is a good reference text about garbage collection (refers to Python
4692 * but it applies to all reference-counting mechanisms):
4693 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004694 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004695
4696/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004697 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004698 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004699 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004700 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004701 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004702garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004703{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004704 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004705 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004706 buf_T *buf;
4707 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004708 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004709 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004710
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004711 if (!testing)
4712 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004713 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004714 want_garbage_collect = FALSE;
4715 may_garbage_collect = FALSE;
4716 garbage_collect_at_exit = FALSE;
4717 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004718
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004719 // The execution stack can grow big, limit the size.
4720 if (exestack.ga_maxlen - exestack.ga_len > 500)
4721 {
4722 size_t new_len;
4723 char_u *pp;
4724 int n;
4725
4726 // Keep 150% of the current size, with a minimum of the growth size.
4727 n = exestack.ga_len / 2;
4728 if (n < exestack.ga_growsize)
4729 n = exestack.ga_growsize;
4730
4731 // Don't make it bigger though.
4732 if (exestack.ga_len + n < exestack.ga_maxlen)
4733 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004734 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004735 pp = vim_realloc(exestack.ga_data, new_len);
4736 if (pp == NULL)
4737 return FAIL;
4738 exestack.ga_maxlen = exestack.ga_len + n;
4739 exestack.ga_data = pp;
4740 }
4741 }
4742
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004743 // We advance by two because we add one for items referenced through
4744 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004745 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004746
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004747 /*
4748 * 1. Go through all accessible variables and mark all lists and dicts
4749 * with copyID.
4750 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004751
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004752 // Don't free variables in the previous_funccal list unless they are only
4753 // referenced through previous_funccal. This must be first, because if
4754 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004755 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004756
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004757 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004758 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004759
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004760 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004761 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004762 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4763 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004764
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004765 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004766 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004767 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4768 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004769 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004770 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4771 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004772#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004773 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004774 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4775 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004776 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004777 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004778 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4779 NULL, NULL);
4780#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004781
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004782 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004783 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004784 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4785 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004786 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004787 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004788
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004789 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004790 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004791
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004792 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004793 abort = abort || set_ref_in_functions(copyID);
4794
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004795 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004796 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004797
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004798 // funcstacks keep variables for closures
4799 abort = abort || set_ref_in_funcstacks(copyID);
4800
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004801 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004802 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004803
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004804 // callbacks in buffers
4805 abort = abort || set_ref_in_buffers(copyID);
4806
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004807 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
4808 abort = abort || set_ref_in_insexpand_funcs(copyID);
4809
4810 // 'operatorfunc' callback
4811 abort = abort || set_ref_in_opfunc(copyID);
4812
4813 // 'tagfunc' callback
4814 abort = abort || set_ref_in_tagfunc(copyID);
4815
4816 // 'imactivatefunc' and 'imstatusfunc' callbacks
4817 abort = abort || set_ref_in_im_funcs(copyID);
4818
Bram Moolenaar1dced572012-04-05 16:54:08 +02004819#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004820 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004821#endif
4822
Bram Moolenaardb913952012-06-29 12:54:53 +02004823#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004824 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004825#endif
4826
4827#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004828 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004829#endif
4830
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004831#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004832 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004833 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004834#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004835#ifdef FEAT_NETBEANS_INTG
4836 abort = abort || set_ref_in_nb_channel(copyID);
4837#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004838
Bram Moolenaare3188e22016-05-31 21:13:04 +02004839#ifdef FEAT_TIMERS
4840 abort = abort || set_ref_in_timer(copyID);
4841#endif
4842
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004843#ifdef FEAT_QUICKFIX
4844 abort = abort || set_ref_in_quickfix(copyID);
4845#endif
4846
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004847#ifdef FEAT_TERMINAL
4848 abort = abort || set_ref_in_term(copyID);
4849#endif
4850
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004851#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004852 abort = abort || set_ref_in_popups(copyID);
4853#endif
4854
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004855 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004856 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004857 /*
4858 * 2. Free lists and dictionaries that are not referenced.
4859 */
4860 did_free = free_unref_items(copyID);
4861
4862 /*
4863 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004864 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004865 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004866 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004867 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004868 else if (p_verbose > 0)
4869 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004870 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004871 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004872
4873 return did_free;
4874}
4875
4876/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004877 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004878 */
4879 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004880free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004881{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004882 int did_free = FALSE;
4883
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004884 // Let all "free" functions know that we are here. This means no
4885 // dictionaries, lists, channels or jobs are to be freed, because we will
4886 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004887 in_free_unref_items = TRUE;
4888
4889 /*
4890 * PASS 1: free the contents of the items. We don't free the items
4891 * themselves yet, so that it is possible to decrement refcount counters
4892 */
4893
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004894 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004895 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004896
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004897 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004898 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004899
4900#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004901 // Go through the list of jobs and free items without the copyID. This
4902 // must happen before doing channels, because jobs refer to channels, but
4903 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004904 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4905
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004906 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004907 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4908#endif
4909
4910 /*
4911 * PASS 2: free the items themselves.
4912 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004913 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004914 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004915
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004916#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004917 // Go through the list of jobs and free items without the copyID. This
4918 // must happen before doing channels, because jobs refer to channels, but
4919 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004920 free_unused_jobs(copyID, COPYID_MASK);
4921
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004922 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004923 free_unused_channels(copyID, COPYID_MASK);
4924#endif
4925
4926 in_free_unref_items = FALSE;
4927
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004928 return did_free;
4929}
4930
4931/*
4932 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004933 * "list_stack" is used to add lists to be marked. Can be NULL.
4934 *
4935 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004936 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004937 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004938set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004939{
4940 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004941 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004942 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004943 hashtab_T *cur_ht;
4944 ht_stack_T *ht_stack = NULL;
4945 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004946
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004947 cur_ht = ht;
4948 for (;;)
4949 {
4950 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004951 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004952 // Mark each item in the hashtab. If the item contains a hashtab
4953 // it is added to ht_stack, if it contains a list it is added to
4954 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004955 todo = (int)cur_ht->ht_used;
4956 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4957 if (!HASHITEM_EMPTY(hi))
4958 {
4959 --todo;
4960 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4961 &ht_stack, list_stack);
4962 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004963 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004964
4965 if (ht_stack == NULL)
4966 break;
4967
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004968 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004969 cur_ht = ht_stack->ht;
4970 tempitem = ht_stack;
4971 ht_stack = ht_stack->prev;
4972 free(tempitem);
4973 }
4974
4975 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004976}
4977
Dominique Pelle748b3082022-01-08 12:41:16 +00004978#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
4979 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004980/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004981 * Mark a dict and its items with "copyID".
4982 * Returns TRUE if setting references failed somehow.
4983 */
4984 int
4985set_ref_in_dict(dict_T *d, int copyID)
4986{
4987 if (d != NULL && d->dv_copyID != copyID)
4988 {
4989 d->dv_copyID = copyID;
4990 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4991 }
4992 return FALSE;
4993}
Dominique Pelle748b3082022-01-08 12:41:16 +00004994#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004995
4996/*
4997 * Mark a list and its items with "copyID".
4998 * Returns TRUE if setting references failed somehow.
4999 */
5000 int
5001set_ref_in_list(list_T *ll, int copyID)
5002{
5003 if (ll != NULL && ll->lv_copyID != copyID)
5004 {
5005 ll->lv_copyID = copyID;
5006 return set_ref_in_list_items(ll, copyID, NULL);
5007 }
5008 return FALSE;
5009}
5010
5011/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005012 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005013 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5014 *
5015 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005016 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005017 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005018set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005019{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005020 listitem_T *li;
5021 int abort = FALSE;
5022 list_T *cur_l;
5023 list_stack_T *list_stack = NULL;
5024 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005025
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005026 cur_l = l;
5027 for (;;)
5028 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005029 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005030 // Mark each item in the list. If the item contains a hashtab
5031 // it is added to ht_stack, if it contains a list it is added to
5032 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005033 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5034 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5035 ht_stack, &list_stack);
5036 if (list_stack == NULL)
5037 break;
5038
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005039 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005040 cur_l = list_stack->list;
5041 tempitem = list_stack;
5042 list_stack = list_stack->prev;
5043 free(tempitem);
5044 }
5045
5046 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005047}
5048
5049/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005050 * Mark the partial in callback 'cb' with "copyID".
5051 */
5052 int
5053set_ref_in_callback(callback_T *cb, int copyID)
5054{
5055 typval_T tv;
5056
5057 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5058 return FALSE;
5059
5060 tv.v_type = VAR_PARTIAL;
5061 tv.vval.v_partial = cb->cb_partial;
5062 return set_ref_in_item(&tv, copyID, NULL, NULL);
5063}
5064
5065/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005066 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005067 * "list_stack" is used to add lists to be marked. Can be NULL.
5068 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5069 *
5070 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005071 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005072 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005073set_ref_in_item(
5074 typval_T *tv,
5075 int copyID,
5076 ht_stack_T **ht_stack,
5077 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005078{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005079 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005080
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005081 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005082 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005083 dict_T *dd = tv->vval.v_dict;
5084
Bram Moolenaara03f2332016-02-06 18:09:59 +01005085 if (dd != NULL && dd->dv_copyID != copyID)
5086 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005087 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005088 dd->dv_copyID = copyID;
5089 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005090 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005091 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5092 }
5093 else
5094 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005095 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5096
Bram Moolenaara03f2332016-02-06 18:09:59 +01005097 if (newitem == NULL)
5098 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005099 else
5100 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005101 newitem->ht = &dd->dv_hashtab;
5102 newitem->prev = *ht_stack;
5103 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005104 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005105 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005106 }
5107 }
5108 else if (tv->v_type == VAR_LIST)
5109 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005110 list_T *ll = tv->vval.v_list;
5111
Bram Moolenaara03f2332016-02-06 18:09:59 +01005112 if (ll != NULL && ll->lv_copyID != copyID)
5113 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005114 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005115 ll->lv_copyID = copyID;
5116 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005117 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005118 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005119 }
5120 else
5121 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005122 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5123
Bram Moolenaara03f2332016-02-06 18:09:59 +01005124 if (newitem == NULL)
5125 abort = TRUE;
5126 else
5127 {
5128 newitem->list = ll;
5129 newitem->prev = *list_stack;
5130 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005131 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005132 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005133 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005134 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005135 else if (tv->v_type == VAR_FUNC)
5136 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005137 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005138 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005139 else if (tv->v_type == VAR_PARTIAL)
5140 {
5141 partial_T *pt = tv->vval.v_partial;
5142 int i;
5143
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005144 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005145 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005146 // Didn't see this partial yet.
5147 pt->pt_copyID = copyID;
5148
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005149 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005150
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005151 if (pt->pt_dict != NULL)
5152 {
5153 typval_T dtv;
5154
5155 dtv.v_type = VAR_DICT;
5156 dtv.vval.v_dict = pt->pt_dict;
5157 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5158 }
5159
5160 for (i = 0; i < pt->pt_argc; ++i)
5161 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5162 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005163 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005164 }
5165 }
5166#ifdef FEAT_JOB_CHANNEL
5167 else if (tv->v_type == VAR_JOB)
5168 {
5169 job_T *job = tv->vval.v_job;
5170 typval_T dtv;
5171
5172 if (job != NULL && job->jv_copyID != copyID)
5173 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005174 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005175 if (job->jv_channel != NULL)
5176 {
5177 dtv.v_type = VAR_CHANNEL;
5178 dtv.vval.v_channel = job->jv_channel;
5179 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5180 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005181 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005182 {
5183 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005184 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005185 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5186 }
5187 }
5188 }
5189 else if (tv->v_type == VAR_CHANNEL)
5190 {
5191 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005192 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005193 typval_T dtv;
5194 jsonq_T *jq;
5195 cbq_T *cq;
5196
5197 if (ch != NULL && ch->ch_copyID != copyID)
5198 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005199 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005200 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005201 {
5202 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5203 jq = jq->jq_next)
5204 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5205 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5206 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005207 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005208 {
5209 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005210 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005211 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5212 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005213 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005214 {
5215 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005216 dtv.vval.v_partial =
5217 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005218 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5219 }
5220 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005221 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005222 {
5223 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005224 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005225 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5226 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005227 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005228 {
5229 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005230 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005231 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5232 }
5233 }
5234 }
5235#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005236 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005237}
5238
Bram Moolenaar8c711452005-01-14 21:53:12 +00005239/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005240 * Return a string with the string representation of a variable.
5241 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005242 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005243 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005244 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005245 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005246 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005247 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5248 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005249 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005250 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005251 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005252echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005253 typval_T *tv,
5254 char_u **tofree,
5255 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005256 int copyID,
5257 int echo_style,
5258 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005259 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005260{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005261 static int recurse = 0;
5262 char_u *r = NULL;
5263
Bram Moolenaar33570922005-01-25 22:26:29 +00005264 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005265 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005266 if (!did_echo_string_emsg)
5267 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005268 // Only give this message once for a recursive call to avoid
5269 // flooding the user with errors. And stop iterating over lists
5270 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005271 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005272 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005273 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005274 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005275 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005276 }
5277 ++recurse;
5278
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 switch (tv->v_type)
5280 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005281 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005282 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005283 {
5284 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005285 r = tv->vval.v_string;
5286 if (r == NULL)
5287 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005288 }
5289 else
5290 {
5291 *tofree = string_quote(tv->vval.v_string, FALSE);
5292 r = *tofree;
5293 }
5294 break;
5295
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005296 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005297 if (echo_style)
5298 {
5299 *tofree = NULL;
5300 r = tv->vval.v_string;
5301 }
5302 else
5303 {
5304 *tofree = string_quote(tv->vval.v_string, TRUE);
5305 r = *tofree;
5306 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005307 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005308
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005309 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005310 {
5311 partial_T *pt = tv->vval.v_partial;
5312 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005313 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005314 garray_T ga;
5315 int i;
5316 char_u *tf;
5317
5318 ga_init2(&ga, 1, 100);
5319 ga_concat(&ga, (char_u *)"function(");
5320 if (fname != NULL)
5321 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005322 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005323 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005324 && vim_isupper(fname[1]))
5325 {
5326 ga_concat(&ga, (char_u *)"'g:");
5327 ga_concat(&ga, fname + 1);
5328 }
5329 else
5330 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005331 vim_free(fname);
5332 }
5333 if (pt != NULL && pt->pt_argc > 0)
5334 {
5335 ga_concat(&ga, (char_u *)", [");
5336 for (i = 0; i < pt->pt_argc; ++i)
5337 {
5338 if (i > 0)
5339 ga_concat(&ga, (char_u *)", ");
5340 ga_concat(&ga,
5341 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5342 vim_free(tf);
5343 }
5344 ga_concat(&ga, (char_u *)"]");
5345 }
5346 if (pt != NULL && pt->pt_dict != NULL)
5347 {
5348 typval_T dtv;
5349
5350 ga_concat(&ga, (char_u *)", ");
5351 dtv.v_type = VAR_DICT;
5352 dtv.vval.v_dict = pt->pt_dict;
5353 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5354 vim_free(tf);
5355 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005356 // terminate with ')' and a NUL
5357 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005358
5359 *tofree = ga.ga_data;
5360 r = *tofree;
5361 break;
5362 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005363
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005364 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005365 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005366 break;
5367
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005368 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005369 if (tv->vval.v_list == NULL)
5370 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005371 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005372 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005373 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005374 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005375 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5376 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005377 {
5378 *tofree = NULL;
5379 r = (char_u *)"[...]";
5380 }
5381 else
5382 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005383 int old_copyID = tv->vval.v_list->lv_copyID;
5384
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005385 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005386 *tofree = list2string(tv, copyID, restore_copyID);
5387 if (restore_copyID)
5388 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005389 r = *tofree;
5390 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005391 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005392
Bram Moolenaar8c711452005-01-14 21:53:12 +00005393 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005394 if (tv->vval.v_dict == NULL)
5395 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005396 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005397 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005398 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005399 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005400 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5401 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005402 {
5403 *tofree = NULL;
5404 r = (char_u *)"{...}";
5405 }
5406 else
5407 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005408 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005409
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005410 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005411 *tofree = dict2string(tv, copyID, restore_copyID);
5412 if (restore_copyID)
5413 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005414 r = *tofree;
5415 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005416 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005417
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005418 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005419 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005420 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005421 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005422 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005423 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005424 break;
5425
Bram Moolenaar835dc632016-02-07 14:27:38 +01005426 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005427 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005428#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005429 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005430 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5431 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005432 if (composite_val)
5433 {
5434 *tofree = string_quote(r, FALSE);
5435 r = *tofree;
5436 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005437#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005438 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005439
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005440 case VAR_INSTR:
5441 *tofree = NULL;
5442 r = (char_u *)"instructions";
5443 break;
5444
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005445 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005446#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005447 *tofree = NULL;
5448 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5449 r = numbuf;
5450 break;
5451#endif
5452
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005453 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005454 case VAR_SPECIAL:
5455 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005456 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005457 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005458 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005459
Bram Moolenaar8502c702014-06-17 12:51:16 +02005460 if (--recurse == 0)
5461 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005462 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005463}
5464
5465/*
5466 * Return a string with the string representation of a variable.
5467 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5468 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005469 * Does not put quotes around strings, as ":echo" displays values.
5470 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5471 * May return NULL.
5472 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005473 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005474echo_string(
5475 typval_T *tv,
5476 char_u **tofree,
5477 char_u *numbuf,
5478 int copyID)
5479{
5480 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5481}
5482
5483/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005484 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5485 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005486 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005487 */
5488 int
5489buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5490{
5491 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005492 char_u *t;
5493 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005494
5495 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5496 return -1;
5497
5498 if (lnum > buf->b_ml.ml_line_count)
5499 lnum = buf->b_ml.ml_line_count;
5500
5501 str = ml_get_buf(buf, lnum, FALSE);
5502 if (str == NULL)
5503 return -1;
5504
5505 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005506 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005507
Bram Moolenaar91458462021-01-13 20:08:38 +01005508 // count the number of characters
5509 t = str;
5510 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5511 t += mb_ptr2len(t);
5512
5513 // In insert mode, when the cursor is at the end of a non-empty line,
5514 // byteidx points to the NUL character immediately past the end of the
5515 // string. In this case, add one to the character count.
5516 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5517 count++;
5518
5519 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005520}
5521
5522/*
5523 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005524 * byte index. Works only for loaded buffers. Returns -1 on failure.
5525 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005526 */
5527 int
5528buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5529{
5530 char_u *str;
5531 char_u *t;
5532
5533 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5534 return -1;
5535
5536 if (lnum > buf->b_ml.ml_line_count)
5537 lnum = buf->b_ml.ml_line_count;
5538
5539 str = ml_get_buf(buf, lnum, FALSE);
5540 if (str == NULL)
5541 return -1;
5542
5543 // Convert the character offset to a byte offset
5544 t = str;
5545 while (*t != NUL && --charidx > 0)
5546 t += mb_ptr2len(t);
5547
Bram Moolenaar91458462021-01-13 20:08:38 +01005548 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005549}
5550
5551/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005553 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005555 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005556var2fpos(
5557 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005558 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005559 int *fnum, // set to fnum for '0, 'A, etc.
5560 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005562 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005564 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005566 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005567 if (varp->v_type == VAR_LIST)
5568 {
5569 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005570 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005571 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005572 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005573
5574 l = varp->vval.v_list;
5575 if (l == NULL)
5576 return NULL;
5577
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005578 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005579 pos.lnum = list_find_nr(l, 0L, &error);
5580 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005581 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005582 if (charcol)
5583 len = (long)mb_charlen(ml_get(pos.lnum));
5584 else
5585 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005586
Bram Moolenaarec65d772020-08-20 22:29:12 +02005587 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005588 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005589 li = list_find(l, 1L);
5590 if (li != NULL && li->li_tv.v_type == VAR_STRING
5591 && li->li_tv.vval.v_string != NULL
5592 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005593 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005594 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005595 }
5596 else
5597 {
5598 pos.col = list_find_nr(l, 1L, &error);
5599 if (error)
5600 return NULL;
5601 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005602
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005603 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005604 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005605 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005606 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005607
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005608 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005609 pos.coladd = list_find_nr(l, 2L, &error);
5610 if (error)
5611 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005612
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005613 return &pos;
5614 }
5615
Bram Moolenaarc5809432021-03-27 21:23:30 +01005616 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5617 return NULL;
5618
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005619 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005620 if (name == NULL)
5621 return NULL;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005622 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005623 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005624 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005625 pos = curwin->w_cursor;
5626 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005627 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005628 return &pos;
5629 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005630 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005631 {
5632 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005633 pos = VIsual;
5634 else
5635 pos = curwin->w_cursor;
5636 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005637 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005638 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005639 }
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005640 if (name[0] == '\'' && (!in_vim9script()
5641 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005643 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005644 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5646 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005647 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005648 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 return pp;
5650 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005651
Bram Moolenaara5525202006-03-02 22:52:09 +00005652 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005653
Bram Moolenaar477933c2007-07-17 14:32:23 +00005654 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005655 {
5656 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005657 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005658 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005659 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005660 // In silent Ex mode topline is zero, but that's not a valid line
5661 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005662 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005663 return &pos;
5664 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005665 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005666 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005667 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005668 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005669 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005670 return &pos;
5671 }
5672 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005673 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005675 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 {
5677 pos.lnum = curbuf->b_ml.ml_line_count;
5678 pos.col = 0;
5679 }
5680 else
5681 {
5682 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005683 if (charcol)
5684 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5685 else
5686 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 }
5688 return &pos;
5689 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005690 if (in_vim9script())
5691 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 return NULL;
5693}
5694
5695/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005696 * Convert list in "arg" into a position and optional file number.
5697 * When "fnump" is NULL there is no file number, only 3 items.
5698 * Note that the column is passed on as-is, the caller may want to decrement
5699 * it to use 1 for the first column.
5700 * Return FAIL when conversion is not possible, doesn't check the position for
5701 * validity.
5702 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005703 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005704list2fpos(
5705 typval_T *arg,
5706 pos_T *posp,
5707 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005708 colnr_T *curswantp,
5709 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005710{
5711 list_T *l = arg->vval.v_list;
5712 long i = 0;
5713 long n;
5714
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005715 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5716 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005717 if (arg->v_type != VAR_LIST
5718 || l == NULL
5719 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005720 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005721 return FAIL;
5722
5723 if (fnump != NULL)
5724 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005725 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005726 if (n < 0)
5727 return FAIL;
5728 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005729 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005730 *fnump = n;
5731 }
5732
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005733 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005734 if (n < 0)
5735 return FAIL;
5736 posp->lnum = n;
5737
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005738 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005739 if (n < 0)
5740 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005741 // If character position is specified, then convert to byte position
5742 if (charcol)
5743 {
5744 buf_T *buf;
5745
5746 // Get the text for the specified line in a loaded buffer
5747 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5748 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5749 return FAIL;
5750
Bram Moolenaar91458462021-01-13 20:08:38 +01005751 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005752 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005753 posp->col = n;
5754
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005755 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005756 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005757 posp->coladd = 0;
5758 else
5759 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005760
Bram Moolenaar493c1782014-05-28 14:34:46 +02005761 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005762 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005763
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005764 return OK;
5765}
5766
5767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 * Get the length of an environment variable name.
5769 * Advance "arg" to the first character after the name.
5770 * Return 0 for error.
5771 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005772 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005773get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774{
5775 char_u *p;
5776 int len;
5777
5778 for (p = *arg; vim_isIDc(*p); ++p)
5779 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005780 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 return 0;
5782
5783 len = (int)(p - *arg);
5784 *arg = p;
5785 return len;
5786}
5787
5788/*
5789 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005790 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 * Return 0 if something is wrong.
5792 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005793 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005794get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795{
5796 char_u *p;
5797 int len;
5798
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005799 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005801 {
5802 if (*p == ':')
5803 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005804 // "s:" is start of "s:var", but "n:" is not and can be used in
5805 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005806 len = (int)(p - *arg);
5807 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5808 || len > 1)
5809 break;
5810 }
5811 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005812 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 return 0;
5814
5815 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005816 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817
5818 return len;
5819}
5820
5821/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005822 * Get the length of the name of a variable or function.
5823 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005825 * Return -1 if curly braces expansion failed.
5826 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 * If the name contains 'magic' {}'s, expand them and return the
5828 * expanded name in an allocated string via 'alias' - caller must free.
5829 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005830 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005831get_name_len(
5832 char_u **arg,
5833 char_u **alias,
5834 int evaluate,
5835 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836{
5837 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 char_u *p;
5839 char_u *expr_start;
5840 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005842 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843
5844 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5845 && (*arg)[2] == (int)KE_SNR)
5846 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005847 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 *arg += 3;
5849 return get_id_len(arg) + 3;
5850 }
5851 len = eval_fname_script(*arg);
5852 if (len > 0)
5853 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005854 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 *arg += len;
5856 }
5857
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005859 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005861 p = find_name_end(*arg, &expr_start, &expr_end,
5862 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 if (expr_start != NULL)
5864 {
5865 char_u *temp_string;
5866
5867 if (!evaluate)
5868 {
5869 len += (int)(p - *arg);
5870 *arg = skipwhite(p);
5871 return len;
5872 }
5873
5874 /*
5875 * Include any <SID> etc in the expanded string:
5876 * Thus the -len here.
5877 */
5878 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5879 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005880 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 *alias = temp_string;
5882 *arg = skipwhite(p);
5883 return (int)STRLEN(temp_string);
5884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885
5886 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005887 // Only give an error when there is something, otherwise it will be
5888 // reported at a higher level.
5889 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005890 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891
5892 return len;
5893}
5894
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005895/*
5896 * Find the end of a variable or function name, taking care of magic braces.
5897 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5898 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005899 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005900 * Return a pointer to just after the name. Equal to "arg" if there is no
5901 * valid name.
5902 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005903 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005904find_name_end(
5905 char_u *arg,
5906 char_u **expr_start,
5907 char_u **expr_end,
5908 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005910 int mb_nest = 0;
5911 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005913 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005914 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005916 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005918 *expr_start = NULL;
5919 *expr_end = NULL;
5920 }
5921
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005922 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005923 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5924 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005925 return arg;
5926
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005927 for (p = arg; *p != NUL
5928 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005929 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005930 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005931 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005932 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005933 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005934 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005935 if (*p == '\'')
5936 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005937 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005938 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005939 ;
5940 if (*p == NUL)
5941 break;
5942 }
5943 else if (*p == '"')
5944 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005945 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005946 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005947 if (*p == '\\' && p[1] != NUL)
5948 ++p;
5949 if (*p == NUL)
5950 break;
5951 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005952 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5953 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005954 // "s:" is start of "s:var", but "n:" is not and can be used in
5955 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005956 len = (int)(p - arg);
5957 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005958 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005959 break;
5960 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005961
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005962 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005964 if (*p == '[')
5965 ++br_nest;
5966 else if (*p == ']')
5967 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005969
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005970 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005972 if (*p == '{')
5973 {
5974 mb_nest++;
5975 if (expr_start != NULL && *expr_start == NULL)
5976 *expr_start = p;
5977 }
5978 else if (*p == '}')
5979 {
5980 mb_nest--;
5981 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5982 *expr_end = p;
5983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 }
5986
5987 return p;
5988}
5989
5990/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005991 * Expands out the 'magic' {}'s in a variable/function name.
5992 * Note that this can call itself recursively, to deal with
5993 * constructs like foo{bar}{baz}{bam}
5994 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5995 * "in_start" ^
5996 * "expr_start" ^
5997 * "expr_end" ^
5998 * "in_end" ^
5999 *
6000 * Returns a new allocated string, which the caller must free.
6001 * Returns NULL for failure.
6002 */
6003 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006004make_expanded_name(
6005 char_u *in_start,
6006 char_u *expr_start,
6007 char_u *expr_end,
6008 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006009{
6010 char_u c1;
6011 char_u *retval = NULL;
6012 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006013
6014 if (expr_end == NULL || in_end == NULL)
6015 return NULL;
6016 *expr_start = NUL;
6017 *expr_end = NUL;
6018 c1 = *in_end;
6019 *in_end = NUL;
6020
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006021 temp_result = eval_to_string(expr_start + 1, FALSE);
6022 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006023 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006024 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6025 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006026 if (retval != NULL)
6027 {
6028 STRCPY(retval, in_start);
6029 STRCAT(retval, temp_result);
6030 STRCAT(retval, expr_end + 1);
6031 }
6032 }
6033 vim_free(temp_result);
6034
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006035 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006036 *expr_start = '{';
6037 *expr_end = '}';
6038
6039 if (retval != NULL)
6040 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006041 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006042 if (expr_start != NULL)
6043 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006044 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006045 temp_result = make_expanded_name(retval, expr_start,
6046 expr_end, temp_result);
6047 vim_free(retval);
6048 retval = temp_result;
6049 }
6050 }
6051
6052 return retval;
6053}
6054
6055/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006057 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006059 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006060eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006062 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006063}
6064
6065/*
6066 * Return TRUE if character "c" can be used as the first character in a
6067 * variable or function name (excluding '{' and '}').
6068 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006069 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006070eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006071{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006072 return ASCII_ISALPHA(c) || c == '_';
6073}
6074
6075/*
6076 * Return TRUE if character "c" can be used as the first character of a
6077 * dictionary key.
6078 */
6079 int
6080eval_isdictc(int c)
6081{
6082 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083}
6084
6085/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006086 * Handle:
6087 * - expr[expr], expr[expr:expr] subscript
6088 * - ".name" lookup
6089 * - function call with Funcref variable: func(expr)
6090 * - method call: var->method()
6091 *
6092 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006093 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006094 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006095 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006096handle_subscript(
6097 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006098 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006099 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006100 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006101 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006102{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006103 int evaluate = evalarg != NULL
6104 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006105 int ret = OK;
6106 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006107 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006108 int getnext;
6109 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006110
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006111 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006112 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006113 // When at the end of the line and ".name" or "->{" or "->X" follows in
6114 // the next line then consume the line break.
6115 p = eval_next_non_blank(*arg, evalarg, &getnext);
6116 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006117 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006118 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6119 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6120 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006121 {
6122 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006123 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006124 check_white = FALSE;
6125 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006126
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006127 if (rettv->v_type == VAR_ANY)
6128 {
6129 char_u *exp_name;
6130 int cc;
6131 int idx;
6132 ufunc_T *ufunc;
6133 type_T *type;
6134
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006135 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006136 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006137 if (**arg != '.')
6138 {
6139 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006140 semsg(_(e_expected_dot_after_name_str),
6141 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006142 ret = FAIL;
6143 break;
6144 }
6145 ++*arg;
6146 if (IS_WHITE_OR_NUL(**arg))
6147 {
6148 if (verbose)
6149 emsg(_(e_no_white_space_allowed_after_dot));
6150 ret = FAIL;
6151 break;
6152 }
6153
6154 // isolate the name
6155 exp_name = *arg;
6156 while (eval_isnamec(**arg))
6157 ++*arg;
6158 cc = **arg;
6159 **arg = NUL;
6160
6161 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00006162 evalarg->eval_cctx, evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006163 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006164
6165 if (idx < 0 && ufunc == NULL)
6166 {
6167 ret = FAIL;
6168 break;
6169 }
6170 if (idx >= 0)
6171 {
6172 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6173 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6174
6175 copy_tv(sv->sv_tv, rettv);
6176 }
6177 else
6178 {
6179 rettv->v_type = VAR_FUNC;
6180 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6181 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006182 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006183 }
6184
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006185 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6186 || rettv->v_type == VAR_PARTIAL))
6187 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006188 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006189 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6190 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006191
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006192 // Stop the expression evaluation when immediately aborting on
6193 // error, or when an interrupt occurred or an exception was thrown
6194 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006195 if (aborting())
6196 {
6197 if (ret == OK)
6198 clear_tv(rettv);
6199 ret = FAIL;
6200 }
6201 dict_unref(selfdict);
6202 selfdict = NULL;
6203 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006204 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006205 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006206 if (in_vim9script())
6207 *arg = skipwhite(p + 2);
6208 else
6209 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006210 if (ret == OK)
6211 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006212 if (VIM_ISWHITE(**arg))
6213 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006214 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006215 ret = FAIL;
6216 }
6217 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006218 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006219 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006220 else
6221 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006222 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006223 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006224 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006225 // "." is ".name" lookup when we found a dict or when evaluating and
6226 // scriptversion is at least 2, where string concatenation is "..".
6227 else if (**arg == '['
6228 || (**arg == '.' && (rettv->v_type == VAR_DICT
6229 || (!evaluate
6230 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006231 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006232 {
6233 dict_unref(selfdict);
6234 if (rettv->v_type == VAR_DICT)
6235 {
6236 selfdict = rettv->vval.v_dict;
6237 if (selfdict != NULL)
6238 ++selfdict->dv_refcount;
6239 }
6240 else
6241 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006242 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006243 {
6244 clear_tv(rettv);
6245 ret = FAIL;
6246 }
6247 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006248 else
6249 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006250 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006251
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006252 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6253 // Don't do this when "Func" is already a partial that was bound
6254 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006255 if (selfdict != NULL
6256 && (rettv->v_type == VAR_FUNC
6257 || (rettv->v_type == VAR_PARTIAL
6258 && (rettv->vval.v_partial->pt_auto
6259 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006260 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006261
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006262 dict_unref(selfdict);
6263 return ret;
6264}
6265
6266/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006267 * Make a copy of an item.
6268 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006269 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006270 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6271 * reference to an already copied list/dict can be used.
6272 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006273 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006274 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006275item_copy(
6276 typval_T *from,
6277 typval_T *to,
6278 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006279 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006280 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006281{
6282 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006283 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006284
Bram Moolenaar33570922005-01-25 22:26:29 +00006285 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006286 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006287 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006288 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006289 }
6290 ++recurse;
6291
6292 switch (from->v_type)
6293 {
6294 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006295 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006296 case VAR_STRING:
6297 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006298 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006299 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006300 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006301 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006302 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006303 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006304 copy_tv(from, to);
6305 break;
6306 case VAR_LIST:
6307 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006308 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006309 if (from->vval.v_list == NULL)
6310 to->vval.v_list = NULL;
6311 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6312 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006313 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006314 to->vval.v_list = from->vval.v_list->lv_copylist;
6315 ++to->vval.v_list->lv_refcount;
6316 }
6317 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006318 to->vval.v_list = list_copy(from->vval.v_list,
6319 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006320 if (to->vval.v_list == NULL)
6321 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006322 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006323 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006324 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006325 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006326 case VAR_DICT:
6327 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006328 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006329 if (from->vval.v_dict == NULL)
6330 to->vval.v_dict = NULL;
6331 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6332 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006333 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006334 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6335 ++to->vval.v_dict->dv_refcount;
6336 }
6337 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006338 to->vval.v_dict = dict_copy(from->vval.v_dict,
6339 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006340 if (to->vval.v_dict == NULL)
6341 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006342 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006343 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006344 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006345 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006346 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006347 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006348 }
6349 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006350 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006351}
6352
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006353 void
6354echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6355{
6356 char_u *tofree;
6357 char_u numbuf[NUMBUFLEN];
6358 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6359
6360 if (*atstart)
6361 {
6362 *atstart = FALSE;
6363 // Call msg_start() after eval1(), evaluating the expression
6364 // may cause a message to appear.
6365 if (with_space)
6366 {
6367 // Mark the saved text as finishing the line, so that what
6368 // follows is displayed on a new line when scrolling back
6369 // at the more prompt.
6370 msg_sb_eol();
6371 msg_start();
6372 }
6373 }
6374 else if (with_space)
6375 msg_puts_attr(" ", echo_attr);
6376
6377 if (p != NULL)
6378 for ( ; *p != NUL && !got_int; ++p)
6379 {
6380 if (*p == '\n' || *p == '\r' || *p == TAB)
6381 {
6382 if (*p != TAB && *needclr)
6383 {
6384 // remove any text still there from the command
6385 msg_clr_eos();
6386 *needclr = FALSE;
6387 }
6388 msg_putchar_attr(*p, echo_attr);
6389 }
6390 else
6391 {
6392 if (has_mbyte)
6393 {
6394 int i = (*mb_ptr2len)(p);
6395
6396 (void)msg_outtrans_len_attr(p, i, echo_attr);
6397 p += i - 1;
6398 }
6399 else
6400 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6401 }
6402 }
6403 vim_free(tofree);
6404}
6405
Bram Moolenaare9a41262005-01-15 22:18:47 +00006406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 * ":echo expr1 ..." print each argument separated with a space, add a
6408 * newline at the end.
6409 * ":echon expr1 ..." print each argument plain.
6410 */
6411 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006412ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413{
6414 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006415 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006416 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 int needclr = TRUE;
6418 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006419 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006420 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006421 evalarg_T evalarg;
6422
Bram Moolenaare707c882020-07-01 13:07:37 +02006423 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424
6425 if (eap->skip)
6426 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006427 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006429 // If eval1() causes an error message the text from the command may
6430 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006431 need_clr_eos = needclr;
6432
Bram Moolenaar68db9962021-05-09 23:19:22 +02006433 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006434 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 {
6436 /*
6437 * Report the invalid expression unless the expression evaluation
6438 * has been cancelled due to an aborting error, an interrupt, or an
6439 * exception.
6440 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006441 if (!aborting() && did_emsg == did_emsg_before
6442 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006443 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006444 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 break;
6446 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006447 need_clr_eos = FALSE;
6448
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006450 {
6451 if (rettv.v_type == VAR_VOID)
6452 {
6453 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6454 break;
6455 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006456 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006457 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006458
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006459 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 arg = skipwhite(arg);
6461 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006462 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006463 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464
6465 if (eap->skip)
6466 --emsg_skip;
6467 else
6468 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006469 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 if (needclr)
6471 msg_clr_eos();
6472 if (eap->cmdidx == CMD_echo)
6473 msg_end();
6474 }
6475}
6476
6477/*
6478 * ":echohl {name}".
6479 */
6480 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006481ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006483 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484}
6485
6486/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006487 * Returns the :echo attribute
6488 */
6489 int
6490get_echo_attr(void)
6491{
6492 return echo_attr;
6493}
6494
6495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 * ":execute expr1 ..." execute the result of an expression.
6497 * ":echomsg expr1 ..." Print a message
6498 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006499 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 * Each gets spaces around each argument and a newline at the end for
6501 * echo commands
6502 */
6503 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006504ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505{
6506 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006507 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 int ret = OK;
6509 char_u *p;
6510 garray_T ga;
6511 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006512 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513
6514 ga_init2(&ga, 1, 80);
6515
6516 if (eap->skip)
6517 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006518 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006520 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006521 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523
6524 if (!eap->skip)
6525 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006526 char_u buf[NUMBUFLEN];
6527
6528 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006529 {
6530 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6531 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006532 semsg(_(e_using_invalid_value_as_string_str),
6533 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006534 p = NULL;
6535 }
6536 else
6537 p = tv_get_string_buf(&rettv, buf);
6538 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006539 else
6540 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006541 if (p == NULL)
6542 {
6543 clear_tv(&rettv);
6544 ret = FAIL;
6545 break;
6546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 len = (int)STRLEN(p);
6548 if (ga_grow(&ga, len + 2) == FAIL)
6549 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006550 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 ret = FAIL;
6552 break;
6553 }
6554 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 ga.ga_len += len;
6558 }
6559
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006560 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 arg = skipwhite(arg);
6562 }
6563
6564 if (ret != FAIL && ga.ga_data != NULL)
6565 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006566 // use the first line of continuation lines for messages
6567 SOURCING_LNUM = start_lnum;
6568
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006569 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6570 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006571 // Mark the already saved text as finishing the line, so that what
6572 // follows is displayed on a new line when scrolling back at the
6573 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006574 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006575 }
6576
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006578 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006579 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006580 out_flush();
6581 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006582 else if (eap->cmdidx == CMD_echoconsole)
6583 {
6584 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6585 ui_write((char_u *)"\r\n", 2, TRUE);
6586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 else if (eap->cmdidx == CMD_echoerr)
6588 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006589 int save_did_emsg = did_emsg;
6590
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006591 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006592 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 if (!force_abort)
6594 did_emsg = save_did_emsg;
6595 }
6596 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006597 {
6598 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6599
6600 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6601 sticky_cmdmod_flags = cmdmod.cmod_flags
6602 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 do_cmdline((char_u *)ga.ga_data,
6604 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006605 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 }
6608
6609 ga_clear(&ga);
6610
6611 if (eap->skip)
6612 --emsg_skip;
6613
Bram Moolenaar63b91732021-08-05 20:40:03 +02006614 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615}
6616
6617/*
6618 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6619 * "arg" points to the "&" or '+' when called, to "option" when returning.
6620 * Returns NULL when no option name found. Otherwise pointer to the char
6621 * after the option name.
6622 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006623 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006624find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625{
6626 char_u *p = *arg;
6627
6628 ++p;
6629 if (*p == 'g' && p[1] == ':')
6630 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006631 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 p += 2;
6633 }
6634 else if (*p == 'l' && p[1] == ':')
6635 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006636 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 p += 2;
6638 }
6639 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006640 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641
6642 if (!ASCII_ISALPHA(*p))
6643 return NULL;
6644 *arg = p;
6645
6646 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006647 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 else
6649 while (ASCII_ISALPHA(*p))
6650 ++p;
6651 return p;
6652}
6653
6654/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006655 * Display script name where an item was last set.
6656 * Should only be invoked when 'verbose' is non-zero.
6657 */
6658 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006659last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006660{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006661 char_u *p;
6662
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006663 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006664 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006665 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006666 if (p != NULL)
6667 {
6668 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006669 msg_puts(_("\n\tLast set from "));
6670 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006671 if (script_ctx.sc_lnum > 0)
6672 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006673 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006674 msg_outnum((long)script_ctx.sc_lnum);
6675 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006676 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006677 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006678 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006679 }
6680}
6681
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006682#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683
6684/*
6685 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006686 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 * "flags" can be "g" to do a global substitute.
6688 * Returns an allocated string, NULL for error.
6689 */
6690 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006691do_string_sub(
6692 char_u *str,
6693 char_u *pat,
6694 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006695 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006696 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697{
6698 int sublen;
6699 regmatch_T regmatch;
6700 int i;
6701 int do_all;
6702 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006703 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 garray_T ga;
6705 char_u *ret;
6706 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006707 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006709 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006711 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712
6713 ga_init2(&ga, 1, 200);
6714
6715 do_all = (flags[0] == 'g');
6716
6717 regmatch.rm_ic = p_ic;
6718 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6719 if (regmatch.regprog != NULL)
6720 {
6721 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006722 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6724 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006725 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006726 if (regmatch.startp[0] == regmatch.endp[0])
6727 {
6728 if (zero_width == regmatch.startp[0])
6729 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006730 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006731 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006732 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6733 (size_t)i);
6734 ga.ga_len += i;
6735 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006736 continue;
6737 }
6738 zero_width = regmatch.startp[0];
6739 }
6740
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 /*
6742 * Get some space for a temporary buffer to do the substitution
6743 * into. It will contain:
6744 * - The text up to where the match is.
6745 * - The substituted text.
6746 * - The text after the match.
6747 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006748 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006749 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6751 {
6752 ga_clear(&ga);
6753 break;
6754 }
6755
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006756 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 i = (int)(regmatch.startp[0] - tail);
6758 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006759 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006760 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 + ga.ga_len + i, TRUE, TRUE, FALSE);
6762 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006763 tail = regmatch.endp[0];
6764 if (*tail == NUL)
6765 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 if (!do_all)
6767 break;
6768 }
6769
6770 if (ga.ga_data != NULL)
6771 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6772
Bram Moolenaar473de612013-06-08 18:19:48 +02006773 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 }
6775
6776 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6777 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006778 if (p_cpo == empty_option)
6779 p_cpo = save_cpo;
6780 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006781 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006782 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006783 // If it's still empty it was changed and restored, need to restore in
6784 // the complicated way.
6785 if (*p_cpo == NUL)
6786 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006787 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789
6790 return ret;
6791}