blob: c4c403c19dcc4b58bbfc6cf24e789d9905054211 [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 {
946 type_list = &tmp_type_list;
947 ga_init2(type_list, sizeof(type_T), 10);
948 }
949
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200950 // parse the type after the name
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000951 lp->ll_type = parse_type(&tp, type_list, !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100952 if (lp->ll_type == NULL && !quiet)
953 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200954 lp->ll_name_end = tp;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000955
956 // drop the type when not in a script
957 if (type_list == &tmp_type_list)
958 {
959 lp->ll_type = NULL;
960 clear_type_list(type_list);
961 }
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200962 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100963 }
964 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000965 if (lp->ll_name == NULL)
966 return p;
967
Bram Moolenaar62aec932022-01-29 21:45:34 +0000968 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000969 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000970 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000971
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000972 if (import != NULL)
973 {
974 ufunc_T *ufunc;
975 type_T *type;
976
977 lp->ll_sid = import->imp_sid;
978 lp->ll_name = skipwhite(p + 1);
979 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
980 lp->ll_name_end = p;
981
982 // check the item is exported
983 cc = *p;
984 *p = NUL;
985 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000986 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000987 {
988 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +0000989 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000990 }
991 *p = cc;
992 }
993 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100994
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100995 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000996 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000997 return p;
998
Bram Moolenaar4525a572022-02-13 11:57:33 +0000999 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001000 {
1001 // using local variable
1002 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001003 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001004 }
1005 else
1006 {
1007 cc = *p;
1008 *p = NUL;
1009 // When we would write to the variable pass &ht and prevent autoload.
1010 writing = !(flags & GLV_READ_ONLY);
1011 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001012 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001013 if (v == NULL && !quiet)
1014 semsg(_(e_undefined_variable_str), lp->ll_name);
1015 *p = cc;
1016 if (v == NULL)
1017 return NULL;
1018 lp->ll_tv = &v->di_tv;
1019 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001020
Bram Moolenaar4525a572022-02-13 11:57:33 +00001021 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001022 {
1023 if (!quiet)
1024 semsg(_(e_variable_already_declared), lp->ll_name);
1025 return NULL;
1026 }
1027
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001028 /*
1029 * Loop until no more [idx] or .key is following.
1030 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001031 var1.v_type = VAR_UNKNOWN;
1032 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001033 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001034 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001035 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1036 {
1037 if (!quiet)
1038 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1039 return NULL;
1040 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001041 if (lp->ll_tv->v_type != VAR_LIST
1042 && lp->ll_tv->v_type != VAR_DICT
1043 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001044 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001045 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001046 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001047 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001048 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001049
1050 // a NULL list/blob works like an empty list/blob, allocate one now.
1051 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1052 rettv_list_alloc(lp->ll_tv);
1053 else if (lp->ll_tv->v_type == VAR_BLOB
1054 && lp->ll_tv->vval.v_blob == NULL)
1055 rettv_blob_alloc(lp->ll_tv);
1056
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001057 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001058 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001059 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001060 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001061 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001062 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001063
Bram Moolenaar4525a572022-02-13 11:57:33 +00001064 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001065 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001066 && lp->ll_tv == &v->di_tv
1067 && ht != NULL && ht == get_script_local_ht())
1068 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001069 svar_T *sv = find_typval_in_script(lp->ll_tv, 0);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001070
1071 // Vim9 script local variable: get the type
1072 if (sv != NULL)
1073 lp->ll_valtype = sv->sv_type;
1074 }
1075
Bram Moolenaar8c711452005-01-14 21:53:12 +00001076 len = -1;
1077 if (*p == '.')
1078 {
1079 key = p + 1;
1080 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1081 ;
1082 if (len == 0)
1083 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001084 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001085 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001086 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001087 }
1088 p = key + len;
1089 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001090 else
1091 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001092 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001093 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001094 if (*p == ':')
1095 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001096 else
1097 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001098 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001099 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001100 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001101 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001102 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001103 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001104 clear_tv(&var1);
1105 return NULL;
1106 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001107 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001108 }
1109
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001110 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001111 if (*p == ':')
1112 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001113 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001114 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001115 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001116 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001117 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001118 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001119 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001120 if (rettv != NULL
1121 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001122 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001123 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001124 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001125 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001126 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001127 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001128 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001129 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001130 }
1131 p = skipwhite(p + 1);
1132 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001133 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001134 else
1135 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001136 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001137 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001138 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001139 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001140 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001141 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001142 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001143 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001144 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001145 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001146 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001147 clear_tv(&var2);
1148 return NULL;
1149 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001150 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001151 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001152 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001153 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001154 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001155
Bram Moolenaar8c711452005-01-14 21:53:12 +00001156 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001157 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001158 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001159 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001160 clear_tv(&var1);
1161 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001162 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001163 }
1164
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001165 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001166 ++p;
1167 }
1168
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001169 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001170 {
1171 if (len == -1)
1172 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001173 // "[key]": get key from "var1"
1174 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001175 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001176 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001177 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001178 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001179 }
1180 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001181 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001182
1183 // a NULL dict is equivalent with an empty dict
1184 if (lp->ll_tv->vval.v_dict == NULL)
1185 {
1186 lp->ll_tv->vval.v_dict = dict_alloc();
1187 if (lp->ll_tv->vval.v_dict == NULL)
1188 {
1189 clear_tv(&var1);
1190 return NULL;
1191 }
1192 ++lp->ll_tv->vval.v_dict->dv_refcount;
1193 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001194 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001195
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001196 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001197
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001198 // When assigning to a scope dictionary check that a function and
1199 // variable name is valid (only variable name unless it is l: or
1200 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001201 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001202 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001203 int prevval;
1204 int wrong;
1205
1206 if (len != -1)
1207 {
1208 prevval = key[len];
1209 key[len] = NUL;
1210 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001211 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001212 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001213 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1214 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001215 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001216 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001217 if (len != -1)
1218 key[len] = prevval;
1219 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001220 {
1221 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001222 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001223 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001224 }
1225
Bram Moolenaar10c65862020-10-08 21:16:42 +02001226 if (lp->ll_valtype != NULL)
1227 // use the type of the member
1228 lp->ll_valtype = lp->ll_valtype->tt_member;
1229
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001230 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001231 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001232 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001233 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001234 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001235 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001236 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001237 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001238 return NULL;
1239 }
1240
Bram Moolenaar31b81602019-02-10 22:14:27 +01001241 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001242 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001243 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001244 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001245 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001246 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001247 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001248 }
1249 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001250 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001251 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001252 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001253 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001254 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001255 p = NULL;
1256 break;
1257 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001258 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001259 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001260 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1261 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001262 {
1263 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001264 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001265 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001266
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001267 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001268 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001269 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001270 else if (lp->ll_tv->v_type == VAR_BLOB)
1271 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001272 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1273
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001274 /*
1275 * Get the number and item for the only or first index of the List.
1276 */
1277 if (empty1)
1278 lp->ll_n1 = 0;
1279 else
1280 // is number or string
1281 lp->ll_n1 = (long)tv_get_number(&var1);
1282 clear_tv(&var1);
1283
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001284 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001285 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001286 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001287 return NULL;
1288 }
1289 if (lp->ll_range && !lp->ll_empty2)
1290 {
1291 lp->ll_n2 = (long)tv_get_number(&var2);
1292 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001293 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1294 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001295 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001296 }
1297 lp->ll_blob = lp->ll_tv->vval.v_blob;
1298 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001299 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001300 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001301 else
1302 {
1303 /*
1304 * Get the number and item for the only or first index of the List.
1305 */
1306 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001307 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001308 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001309 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001310 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001311 clear_tv(&var1);
1312
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001313 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001314 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001315 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001316 if (lp->ll_li == NULL)
1317 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001318 clear_tv(&var2);
1319 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001320 }
1321
Bram Moolenaar10c65862020-10-08 21:16:42 +02001322 if (lp->ll_valtype != NULL)
1323 // use the type of the member
1324 lp->ll_valtype = lp->ll_valtype->tt_member;
1325
Bram Moolenaar8c711452005-01-14 21:53:12 +00001326 /*
1327 * May need to find the item or absolute index for the second
1328 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001329 * When no index given: "lp->ll_empty2" is TRUE.
1330 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001331 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001332 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001333 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001334 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001335 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001336 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001337 if (check_range_index_two(lp->ll_list,
1338 &lp->ll_n1, lp->ll_li,
1339 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001340 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001341 }
1342
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001343 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001344 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001345 }
1346
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001347 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001348 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001349 return p;
1350}
1351
1352/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001353 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001354 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001355 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001356clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001357{
1358 vim_free(lp->ll_exp_name);
1359 vim_free(lp->ll_newkey);
1360}
1361
1362/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001363 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001364 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001365 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1366 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001367 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001368 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001369set_var_lval(
1370 lval_T *lp,
1371 char_u *endp,
1372 typval_T *rettv,
1373 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001374 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1375 char_u *op,
1376 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001377{
1378 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001379 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001380
1381 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001382 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001383 cc = *endp;
1384 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001385 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1386 return;
1387
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001388 if (lp->ll_blob != NULL)
1389 {
1390 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001391
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001392 if (op != NULL && *op != '=')
1393 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001394 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001395 return;
1396 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001397 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001398 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001399
1400 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1401 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001402 if (lp->ll_empty2)
1403 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1404
Bram Moolenaar68452172021-04-12 21:21:02 +02001405 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1406 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001407 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001408 }
1409 else
1410 {
1411 val = (int)tv_get_number_chk(rettv, &error);
1412 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001413 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001414 }
1415 }
1416 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001417 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001418 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001419
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001420 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1421 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001422 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001423 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001424 *endp = cc;
1425 return;
1426 }
1427
Bram Moolenaarff697e62019-02-12 22:28:33 +01001428 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001429 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001430 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001431 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001432 {
1433 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001434 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1435 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001436 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001437 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001438 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001439 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001440 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001441 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001442 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001443 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001444 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1445 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001446 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001447 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001448 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001449 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001450 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001451 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001452 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001453 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001454 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001455 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001456 else if (lp->ll_range)
1457 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001458 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1459 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001460 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001461 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001462 return;
1463 }
1464
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001465 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1466 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001467 }
1468 else
1469 {
1470 /*
1471 * Assign to a List or Dictionary item.
1472 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001473 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1474 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001475 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001476 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001477 return;
1478 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001479
1480 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001481 && check_typval_arg_type(lp->ll_valtype, rettv,
1482 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001483 return;
1484
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001485 if (lp->ll_newkey != NULL)
1486 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001487 if (op != NULL && *op != '=')
1488 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001489 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001490 return;
1491 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001492 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1493 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001494 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001495
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001496 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001497 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001498 if (di == NULL)
1499 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001500 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1501 {
1502 vim_free(di);
1503 return;
1504 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001505 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001506 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001507 else if (op != NULL && *op != '=')
1508 {
1509 tv_op(lp->ll_tv, rettv, op);
1510 return;
1511 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001512 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001513 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001514
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001515 /*
1516 * Assign the value to the variable or list item.
1517 */
1518 if (copy)
1519 copy_tv(rettv, lp->ll_tv);
1520 else
1521 {
1522 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001523 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001524 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001525 }
1526 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001527}
1528
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001529/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001530 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1531 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001532 * Returns OK or FAIL.
1533 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001534 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001535tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001536{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001537 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001538 char_u numbuf[NUMBUFLEN];
1539 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001540 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001541
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001542 // Can't do anything with a Funcref or Dict on the right.
1543 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001544 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001545 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1546 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001547 {
1548 switch (tv1->v_type)
1549 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001550 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001551 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001553 case VAR_DICT:
1554 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001555 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001556 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001557 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001558 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001559 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001560 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001561 break;
1562
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001563 case VAR_BLOB:
1564 if (*op != '+' || tv2->v_type != VAR_BLOB)
1565 break;
1566 // BLOB += BLOB
1567 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1568 {
1569 blob_T *b1 = tv1->vval.v_blob;
1570 blob_T *b2 = tv2->vval.v_blob;
1571 int i, len = blob_len(b2);
1572 for (i = 0; i < len; i++)
1573 ga_append(&b1->bv_ga, blob_get(b2, i));
1574 }
1575 return OK;
1576
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001577 case VAR_LIST:
1578 if (*op != '+' || tv2->v_type != VAR_LIST)
1579 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001580 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001581 if (tv2->vval.v_list != NULL)
1582 {
1583 if (tv1->vval.v_list == NULL)
1584 {
1585 tv1->vval.v_list = tv2->vval.v_list;
1586 ++tv1->vval.v_list->lv_refcount;
1587 }
1588 else
1589 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1590 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001591 return OK;
1592
1593 case VAR_NUMBER:
1594 case VAR_STRING:
1595 if (tv2->v_type == VAR_LIST)
1596 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001597 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001598 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001599 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001600 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001601#ifdef FEAT_FLOAT
1602 if (tv2->v_type == VAR_FLOAT)
1603 {
1604 float_T f = n;
1605
Bram Moolenaarff697e62019-02-12 22:28:33 +01001606 if (*op == '%')
1607 break;
1608 switch (*op)
1609 {
1610 case '+': f += tv2->vval.v_float; break;
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 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001615 clear_tv(tv1);
1616 tv1->v_type = VAR_FLOAT;
1617 tv1->vval.v_float = f;
1618 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001619 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001620#endif
1621 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001622 switch (*op)
1623 {
1624 case '+': n += tv_get_number(tv2); break;
1625 case '-': n -= tv_get_number(tv2); break;
1626 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001627 case '/': n = num_divide(n, tv_get_number(tv2),
1628 &failed); break;
1629 case '%': n = num_modulus(n, tv_get_number(tv2),
1630 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001631 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001632 clear_tv(tv1);
1633 tv1->v_type = VAR_NUMBER;
1634 tv1->vval.v_number = n;
1635 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001636 }
1637 else
1638 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001639 if (tv2->v_type == VAR_FLOAT)
1640 break;
1641
Bram Moolenaarff697e62019-02-12 22:28:33 +01001642 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001643 s = tv_get_string(tv1);
1644 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001645 clear_tv(tv1);
1646 tv1->v_type = VAR_STRING;
1647 tv1->vval.v_string = s;
1648 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001649 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001650
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001651 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001652#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001653 {
1654 float_T f;
1655
Bram Moolenaarff697e62019-02-12 22:28:33 +01001656 if (*op == '%' || *op == '.'
1657 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001658 && tv2->v_type != VAR_NUMBER
1659 && tv2->v_type != VAR_STRING))
1660 break;
1661 if (tv2->v_type == VAR_FLOAT)
1662 f = tv2->vval.v_float;
1663 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001664 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001665 switch (*op)
1666 {
1667 case '+': tv1->vval.v_float += f; break;
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 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001672 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001673#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001674 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001675 }
1676 }
1677
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001678 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001679 return FAIL;
1680}
1681
1682/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001683 * Evaluate the expression used in a ":for var in expr" command.
1684 * "arg" points to "var".
1685 * Set "*errp" to TRUE for an error, FALSE otherwise;
1686 * Return a pointer that holds the info. Null when there is an error.
1687 */
1688 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001689eval_for_line(
1690 char_u *arg,
1691 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001692 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001693 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001694{
Bram Moolenaar33570922005-01-25 22:26:29 +00001695 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001696 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001697 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001698 typval_T tv;
1699 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001700 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001701
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001702 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001703
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001704 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001705 if (fi == NULL)
1706 return NULL;
1707
Bram Moolenaar404557e2021-07-05 21:41:48 +02001708 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1709 &fi->fi_semicolon, FALSE);
1710 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001711 return fi;
1712
Bram Moolenaar404557e2021-07-05 21:41:48 +02001713 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001714 if (expr[0] != 'i' || expr[1] != 'n'
1715 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001716 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001717 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1718 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1719 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001720 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001721 return fi;
1722 }
1723
1724 if (skip)
1725 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001726 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1727 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001728 {
1729 *errp = FALSE;
1730 if (!skip)
1731 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001732 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001733 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001734 l = tv.vval.v_list;
1735 if (l == NULL)
1736 {
1737 // a null list is like an empty list: do nothing
1738 clear_tv(&tv);
1739 }
1740 else
1741 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001743 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001744
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001745 // No need to increment the refcount, it's already set for
1746 // the list being used in "tv".
1747 fi->fi_list = l;
1748 list_add_watch(l, &fi->fi_lw);
1749 fi->fi_lw.lw_item = l->lv_first;
1750 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001751 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001752 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001753 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001754 fi->fi_bi = 0;
1755 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001756 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001757 typval_T btv;
1758
1759 // Make a copy, so that the iteration still works when the
1760 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001761 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001762 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001763 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001764 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001765 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001766 else if (tv.v_type == VAR_STRING)
1767 {
1768 fi->fi_byte_idx = 0;
1769 fi->fi_string = tv.vval.v_string;
1770 tv.vval.v_string = NULL;
1771 if (fi->fi_string == NULL)
1772 fi->fi_string = vim_strsave((char_u *)"");
1773 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001774 else
1775 {
Sean Dewar80d73952021-08-04 19:25:54 +02001776 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001777 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001778 }
1779 }
1780 }
1781 if (skip)
1782 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001783 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001784
1785 return fi;
1786}
1787
1788/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001789 * Used when looping over a :for line, skip the "in expr" part.
1790 */
1791 void
1792skip_for_lines(void *fi_void, evalarg_T *evalarg)
1793{
1794 forinfo_T *fi = (forinfo_T *)fi_void;
1795 int i;
1796
1797 for (i = 0; i < fi->fi_break_count; ++i)
1798 eval_next_line(evalarg);
1799}
1800
1801/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001802 * Use the first item in a ":for" list. Advance to the next.
1803 * Assign the values to the variable (list). "arg" points to the first one.
1804 * Return TRUE when a valid item was found, FALSE when at end of list or
1805 * something wrong.
1806 */
1807 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001808next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001809{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001810 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001811 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001812 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001813 ? (ASSIGN_FINAL
1814 // first round: error if variable exists
1815 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1816 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001817 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001818 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001819 int skip_assign = in_vim9script() && arg[0] == '_'
1820 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001821
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001822 if (fi->fi_blob != NULL)
1823 {
1824 typval_T tv;
1825
1826 if (fi->fi_bi >= blob_len(fi->fi_blob))
1827 return FALSE;
1828 tv.v_type = VAR_NUMBER;
1829 tv.v_lock = VAR_FIXED;
1830 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1831 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001832 if (skip_assign)
1833 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001834 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001835 fi->fi_varcount, flag, NULL) == OK;
1836 }
1837
1838 if (fi->fi_string != NULL)
1839 {
1840 typval_T tv;
1841 int len;
1842
1843 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1844 if (len == 0)
1845 return FALSE;
1846 tv.v_type = VAR_STRING;
1847 tv.v_lock = VAR_FIXED;
1848 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1849 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001850 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001851 if (skip_assign)
1852 result = TRUE;
1853 else
1854 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001855 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001856 vim_free(tv.vval.v_string);
1857 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001858 }
1859
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001860 item = fi->fi_lw.lw_item;
1861 if (item == NULL)
1862 result = FALSE;
1863 else
1864 {
1865 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001866 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001867 if (skip_assign)
1868 result = TRUE;
1869 else
1870 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001871 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001872 }
1873 return result;
1874}
1875
1876/*
1877 * Free the structure used to store info used by ":for".
1878 */
1879 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001880free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001881{
Bram Moolenaar33570922005-01-25 22:26:29 +00001882 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001883
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001884 if (fi == NULL)
1885 return;
1886 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001887 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001888 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001889 list_unref(fi->fi_list);
1890 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001891 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001892 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001893 else
1894 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001895 vim_free(fi);
1896}
1897
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001899set_context_for_expression(
1900 expand_T *xp,
1901 char_u *arg,
1902 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001904 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001908 if (cmdidx == CMD_let || cmdidx == CMD_var
1909 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001910 {
1911 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001912 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001914 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001915 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001916 {
1917 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001918 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001919 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001920 break;
1921 }
1922 return;
1923 }
1924 }
1925 else
1926 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1927 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 while ((xp->xp_pattern = vim_strpbrk(arg,
1929 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1930 {
1931 c = *xp->xp_pattern;
1932 if (c == '&')
1933 {
1934 c = xp->xp_pattern[1];
1935 if (c == '&')
1936 {
1937 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001938 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 }
1940 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001941 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001943 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1944 xp->xp_pattern += 2;
1945
1946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 }
1948 else if (c == '$')
1949 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001950 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 xp->xp_context = EXPAND_ENV_VARS;
1952 }
1953 else if (c == '=')
1954 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001955 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 xp->xp_context = EXPAND_EXPRESSION;
1957 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001958 else if (c == '#'
1959 && xp->xp_context == EXPAND_EXPRESSION)
1960 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001961 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001962 break;
1963 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001964 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 && xp->xp_context == EXPAND_FUNCTIONS
1966 && vim_strchr(xp->xp_pattern, '(') == NULL)
1967 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001968 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 break;
1970 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001971 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001973 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 {
1975 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1976 if (c == '\\' && xp->xp_pattern[1] != NUL)
1977 ++xp->xp_pattern;
1978 xp->xp_context = EXPAND_NOTHING;
1979 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001980 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001982 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1984 /* skip */ ;
1985 xp->xp_context = EXPAND_NOTHING;
1986 }
1987 else if (c == '|')
1988 {
1989 if (xp->xp_pattern[1] == '|')
1990 {
1991 ++xp->xp_pattern;
1992 xp->xp_context = EXPAND_EXPRESSION;
1993 }
1994 else
1995 xp->xp_context = EXPAND_COMMANDS;
1996 }
1997 else
1998 xp->xp_context = EXPAND_EXPRESSION;
1999 }
2000 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002001 // Doesn't look like something valid, expand as an expression
2002 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002003 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 arg = xp->xp_pattern;
2005 if (*arg != NUL)
2006 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2007 /* skip */ ;
2008 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002009
2010 // ":exe one two" completes "two"
2011 if ((cmdidx == CMD_execute
2012 || cmdidx == CMD_echo
2013 || cmdidx == CMD_echon
2014 || cmdidx == CMD_echomsg)
2015 && xp->xp_context == EXPAND_EXPRESSION)
2016 {
2017 for (;;)
2018 {
2019 char_u *n = skiptowhite(arg);
2020
2021 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2022 break;
2023 arg = skipwhite(n);
2024 }
2025 }
2026
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 xp->xp_pattern = arg;
2028}
2029
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002031 * Return TRUE if "pat" matches "text".
2032 * Does not use 'cpo' and always uses 'magic'.
2033 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002034 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002035pattern_match(char_u *pat, char_u *text, int ic)
2036{
2037 int matches = FALSE;
2038 char_u *save_cpo;
2039 regmatch_T regmatch;
2040
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002041 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002042 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002043 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002044 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2045 if (regmatch.regprog != NULL)
2046 {
2047 regmatch.rm_ic = ic;
2048 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2049 vim_regfree(regmatch.regprog);
2050 }
2051 p_cpo = save_cpo;
2052 return matches;
2053}
2054
2055/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002056 * Handle a name followed by "(". Both for just "name(arg)" and for
2057 * "expr->name(arg)".
2058 * Returns OK or FAIL.
2059 */
2060 static int
2061eval_func(
2062 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002063 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002064 char_u *name,
2065 int name_len,
2066 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002067 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002068 typval_T *basetv) // "expr" for "expr->name(arg)"
2069{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002070 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002071 char_u *s = name;
2072 int len = name_len;
2073 partial_T *partial;
2074 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002075 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002076 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002077
2078 if (!evaluate)
2079 check_vars(s, len);
2080
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002081 // If "s" is the name of a variable of type VAR_FUNC
2082 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002083 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002084 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002085
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002086 // Need to make a copy, in case evaluating the arguments makes
2087 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002088 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002089 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002090 ret = FAIL;
2091 else
2092 {
2093 funcexe_T funcexe;
2094
2095 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002096 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002097 funcexe.fe_firstline = curwin->w_cursor.lnum;
2098 funcexe.fe_lastline = curwin->w_cursor.lnum;
2099 funcexe.fe_evaluate = evaluate;
2100 funcexe.fe_partial = partial;
2101 funcexe.fe_basetv = basetv;
2102 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002103 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002104 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002105 }
2106 vim_free(s);
2107
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002108 // If evaluate is FALSE rettv->v_type was not set in
2109 // get_func_tv, but it's needed in handle_subscript() to parse
2110 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002111 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2112 {
2113 rettv->vval.v_string = NULL;
2114 rettv->v_type = VAR_FUNC;
2115 }
2116
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002117 // Stop the expression evaluation when immediately
2118 // aborting on error, or when an interrupt occurred or
2119 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002120 if (evaluate && aborting())
2121 {
2122 if (ret == OK)
2123 clear_tv(rettv);
2124 ret = FAIL;
2125 }
2126 return ret;
2127}
2128
2129/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002130 * Get the next line source line without advancing. But do skip over comment
2131 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002132 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002133 */
2134 static char_u *
2135getline_peek_skip_comments(evalarg_T *evalarg)
2136{
2137 for (;;)
2138 {
2139 char_u *next = getline_peek(evalarg->eval_getline,
2140 evalarg->eval_cookie);
2141 char_u *p;
2142
2143 if (next == NULL)
2144 break;
2145 p = skipwhite(next);
2146 if (*p != NUL && !vim9_comment_start(p))
2147 return next;
2148 (void)eval_next_line(evalarg);
2149 }
2150 return NULL;
2151}
2152
2153/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002154 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2155 * comment) and there is a next line, return the next line (skipping blanks)
2156 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002157 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2158 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002159 * "arg" must point somewhere inside a line, not at the start.
2160 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002161 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002162eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2163{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002164 char_u *p = skipwhite(arg);
2165
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002166 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002167 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002168 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002169 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002170 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002171 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002172 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002173
2174 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002175 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002176 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002177 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002178
Bram Moolenaar9d489562020-07-30 20:08:50 +02002179 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002180 {
2181 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002182 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002183 }
2184 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002185 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002186}
2187
2188/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002189 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002190 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002191 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002192 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002193eval_next_line(evalarg_T *evalarg)
2194{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002195 garray_T *gap = &evalarg->eval_ga;
2196 char_u *line;
2197
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002198 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002199 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2200 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002201 else
2202 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002203 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002204 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2205 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002206 char_u *p = skipwhite(line);
2207
2208 // Going to concatenate the lines after parsing. For an empty or
2209 // comment line use an empty string.
2210 if (*p == NUL || vim9_comment_start(p))
2211 {
2212 vim_free(line);
2213 line = vim_strsave((char_u *)"");
2214 }
2215
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002216 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2217 ++gap->ga_len;
2218 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002219 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002220 {
2221 vim_free(evalarg->eval_tofree);
2222 evalarg->eval_tofree = line;
2223 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002224
2225 // Advanced to the next line, "arg" no longer points into the previous
2226 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002227 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002228 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002229}
2230
Bram Moolenaare6b53242020-07-01 17:28:33 +02002231/*
2232 * Call eval_next_non_blank() and get the next line if needed.
2233 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002234 char_u *
2235skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2236{
2237 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002238 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002239
Bram Moolenaar962d7212020-07-04 14:15:00 +02002240 if (evalarg == NULL)
2241 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002242 eval_next_non_blank(p, evalarg, &getnext);
2243 if (getnext)
2244 return eval_next_line(evalarg);
2245 return p;
2246}
2247
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002248/*
Bram Moolenaar844fb642021-10-23 13:32:30 +01002249 * Initialize "evalarg" for use.
2250 */
2251 void
2252init_evalarg(evalarg_T *evalarg)
2253{
2254 CLEAR_POINTER(evalarg);
2255 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
2256}
2257
2258/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002259 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002260 */
2261 void
2262clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2263{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002264 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002265 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002266 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002267 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002268 if (eap != NULL)
2269 {
2270 // We may need to keep the original command line, e.g. for
2271 // ":let" it has the variable names. But we may also need the
2272 // new one, "nextcmd" points into it. Keep both.
2273 vim_free(eap->cmdline_tofree);
2274 eap->cmdline_tofree = *eap->cmdlinep;
2275 *eap->cmdlinep = evalarg->eval_tofree;
2276 }
2277 else
2278 vim_free(evalarg->eval_tofree);
2279 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002280 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002281
Bram Moolenaar844fb642021-10-23 13:32:30 +01002282 ga_clear_strings(&evalarg->eval_tofree_ga);
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002283 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002284 }
2285}
2286
2287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2291 */
2292
2293/*
2294 * Handle zero level expression.
2295 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002297 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002298 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 * Return OK or FAIL.
2300 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002301 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002302eval0(
2303 char_u *arg,
2304 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002305 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002306 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002308 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2309}
2310
2311/*
2312 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2313 * expression and don't check what comes after the expression.
2314 */
2315 int
2316eval0_retarg(
2317 char_u *arg,
2318 typval_T *rettv,
2319 exarg_T *eap,
2320 evalarg_T *evalarg,
2321 char_u **retarg)
2322{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 int ret;
2324 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002325 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002326 int did_emsg_before = did_emsg;
2327 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002328 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002329 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002330 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331
2332 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002333 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002334 expr_end = p;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002335 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002336
Bram Moolenaar02929a32021-12-17 14:46:12 +00002337 // In Vim9 script a command block is not split at NL characters for
2338 // commands using an expression argument. Skip over a '#' comment to check
2339 // for a following NL. Require white space before the '#'.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002340 if (in_vim9script() && p > expr_end && retarg == NULL)
Bram Moolenaar02929a32021-12-17 14:46:12 +00002341 while (*p == '#')
2342 {
2343 char_u *nl = vim_strchr(p, NL);
2344
2345 if (nl == NULL)
2346 break;
2347 p = skipwhite(nl + 1);
2348 if (eap != NULL && *p != NUL)
2349 eap->nextcmd = p;
2350 check_for_end = FALSE;
2351 }
2352
2353 if (ret != FAIL && check_for_end)
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002354 end_error = !ends_excmd2(arg, p);
2355 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 {
2357 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002358 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 /*
2360 * Report the invalid expression unless the expression evaluation has
2361 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002362 * exception, or we already gave a more specific error.
2363 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002365 if (!aborting()
2366 && did_emsg == did_emsg_before
2367 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002368 && (flags & EVAL_CONSTANT) == 0
2369 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002370 {
2371 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002372 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002373 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002374 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002375 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002376
2377 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002378 // a next command to avoid more errors, unless "|" is following, which
2379 // could only be a command separator.
2380 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2381 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002382 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002384
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002385 if (retarg != NULL)
2386 *retarg = p;
2387 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002388 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002389
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 return ret;
2391}
2392
2393/*
2394 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002395 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002396 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 *
2398 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002399 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002401 * Note: "rettv.v_lock" is not set.
2402 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 * Return OK or FAIL.
2404 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002405 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002406eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002408 char_u *p;
2409 int getnext;
2410
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002411 CLEAR_POINTER(rettv);
2412
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 /*
2414 * Get the first variable.
2415 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002416 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 return FAIL;
2418
Bram Moolenaar793648f2020-06-26 21:28:25 +02002419 p = eval_next_non_blank(*arg, evalarg, &getnext);
2420 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002422 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002423 int result;
2424 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002425 evalarg_T *evalarg_used = evalarg;
2426 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002427 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002428 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002429 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002430
2431 if (evalarg == NULL)
2432 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002433 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002434 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002435 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002436 orig_flags = evalarg_used->eval_flags;
2437 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002438
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002439 if (getnext)
2440 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002441 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002442 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002443 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002444 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002445 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002446 clear_tv(rettv);
2447 return FAIL;
2448 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002449 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002450 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002451
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002453 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002455 int error = FALSE;
2456
Bram Moolenaar13106602020-10-04 16:06:05 +02002457 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002458 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002459 else if (vim9script)
2460 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002461 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002463 if (error || !op_falsy || !result)
2464 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002465 if (error)
2466 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 }
2468
2469 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002470 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002472 if (op_falsy)
2473 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002474 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002475 {
mityu4ac198c2021-05-28 17:52:40 +02002476 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002477 clear_tv(rettv);
2478 return FAIL;
2479 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002480 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002481 evalarg_used->eval_flags = (op_falsy ? !result : result)
2482 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2483 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002484 {
2485 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002487 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002488 if (!op_falsy || !result)
2489 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002491 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002493 /*
2494 * Check for the ":".
2495 */
2496 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2497 if (*p != ':')
2498 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002499 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002500 if (evaluate && result)
2501 clear_tv(rettv);
2502 evalarg_used->eval_flags = orig_flags;
2503 return FAIL;
2504 }
2505 if (getnext)
2506 *arg = eval_next_line(evalarg_used);
2507 else
2508 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002509 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002510 {
2511 error_white_both(p, 1);
2512 clear_tv(rettv);
2513 evalarg_used->eval_flags = orig_flags;
2514 return FAIL;
2515 }
2516 *arg = p;
2517 }
2518
2519 /*
2520 * Get the third variable. Recursive!
2521 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002522 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002523 {
mityu4ac198c2021-05-28 17:52:40 +02002524 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002525 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002526 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002527 return FAIL;
2528 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002529 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2530 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002531 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002532 if (eval1(arg, &var2, evalarg_used) == FAIL)
2533 {
2534 if (evaluate && result)
2535 clear_tv(rettv);
2536 evalarg_used->eval_flags = orig_flags;
2537 return FAIL;
2538 }
2539 if (evaluate && !result)
2540 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002542
2543 if (evalarg == NULL)
2544 clear_evalarg(&local_evalarg, NULL);
2545 else
2546 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 }
2548
2549 return OK;
2550}
2551
2552/*
2553 * Handle first level expression:
2554 * expr2 || expr2 || expr2 logical OR
2555 *
2556 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002557 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 *
2559 * Return OK or FAIL.
2560 */
2561 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002562eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002564 char_u *p;
2565 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566
2567 /*
2568 * Get the first variable.
2569 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002570 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 return FAIL;
2572
2573 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002574 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002576 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002577 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002579 evalarg_T *evalarg_used = evalarg;
2580 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002581 int evaluate;
2582 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002583 long result = FALSE;
2584 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002585 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002586 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002587
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002588 if (evalarg == NULL)
2589 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002590 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002591 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002592 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002593 orig_flags = evalarg_used->eval_flags;
2594 evaluate = orig_flags & EVAL_EVALUATE;
2595 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002596 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002597 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002598 result = tv_get_bool_chk(rettv, &error);
2599 else if (tv_get_number_chk(rettv, &error) != 0)
2600 result = TRUE;
2601 clear_tv(rettv);
2602 if (error)
2603 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 }
2605
2606 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002607 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002609 while (p[0] == '|' && p[1] == '|')
2610 {
2611 if (getnext)
2612 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002613 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002614 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002615 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002616 {
2617 error_white_both(p, 2);
2618 clear_tv(rettv);
2619 return FAIL;
2620 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002621 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002622 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002623
2624 /*
2625 * Get the second variable.
2626 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002627 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002628 {
mityu4ac198c2021-05-28 17:52:40 +02002629 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002630 clear_tv(rettv);
2631 return FAIL;
2632 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002633 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2634 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002635 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002636 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002637 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002638
2639 /*
2640 * Compute the result.
2641 */
2642 if (evaluate && !result)
2643 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002644 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002645 result = tv_get_bool_chk(&var2, &error);
2646 else if (tv_get_number_chk(&var2, &error) != 0)
2647 result = TRUE;
2648 clear_tv(&var2);
2649 if (error)
2650 return FAIL;
2651 }
2652 if (evaluate)
2653 {
2654 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002655 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002656 rettv->v_type = VAR_BOOL;
2657 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002658 }
2659 else
2660 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002661 rettv->v_type = VAR_NUMBER;
2662 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002663 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002664 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002665
2666 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002668
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002669 if (evalarg == NULL)
2670 clear_evalarg(&local_evalarg, NULL);
2671 else
2672 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 }
2674
2675 return OK;
2676}
2677
2678/*
2679 * Handle second level expression:
2680 * expr3 && expr3 && expr3 logical AND
2681 *
2682 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002683 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 *
2685 * Return OK or FAIL.
2686 */
2687 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002688eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002690 char_u *p;
2691 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692
2693 /*
2694 * Get the first variable.
2695 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002696 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 return FAIL;
2698
2699 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002700 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002702 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002703 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002705 evalarg_T *evalarg_used = evalarg;
2706 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002707 int orig_flags;
2708 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002709 long result = TRUE;
2710 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002711 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002712 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002713
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002714 if (evalarg == NULL)
2715 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002716 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002717 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002718 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002719 orig_flags = evalarg_used->eval_flags;
2720 evaluate = orig_flags & EVAL_EVALUATE;
2721 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002722 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002723 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002724 result = tv_get_bool_chk(rettv, &error);
2725 else if (tv_get_number_chk(rettv, &error) == 0)
2726 result = FALSE;
2727 clear_tv(rettv);
2728 if (error)
2729 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 }
2731
2732 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002733 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002735 while (p[0] == '&' && p[1] == '&')
2736 {
2737 if (getnext)
2738 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002739 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002740 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002741 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002742 {
2743 error_white_both(p, 2);
2744 clear_tv(rettv);
2745 return FAIL;
2746 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002747 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002748 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002749
2750 /*
2751 * Get the second variable.
2752 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002753 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002754 {
mityu4ac198c2021-05-28 17:52:40 +02002755 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002756 clear_tv(rettv);
2757 return FAIL;
2758 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002759 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2760 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002761 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002762 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002763 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002764 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002765
2766 /*
2767 * Compute the result.
2768 */
2769 if (evaluate && result)
2770 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002771 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002772 result = tv_get_bool_chk(&var2, &error);
2773 else if (tv_get_number_chk(&var2, &error) == 0)
2774 result = FALSE;
2775 clear_tv(&var2);
2776 if (error)
2777 return FAIL;
2778 }
2779 if (evaluate)
2780 {
2781 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002782 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002783 rettv->v_type = VAR_BOOL;
2784 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002785 }
2786 else
2787 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002788 rettv->v_type = VAR_NUMBER;
2789 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002790 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002791 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002792
2793 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002795
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002796 if (evalarg == NULL)
2797 clear_evalarg(&local_evalarg, NULL);
2798 else
2799 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 }
2801
2802 return OK;
2803}
2804
2805/*
2806 * Handle third level expression:
2807 * var1 == var2
2808 * var1 =~ var2
2809 * var1 != var2
2810 * var1 !~ var2
2811 * var1 > var2
2812 * var1 >= var2
2813 * var1 < var2
2814 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002815 * var1 is var2
2816 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 *
2818 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002819 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 *
2821 * Return OK or FAIL.
2822 */
2823 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002824eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002827 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002828 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002830 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831
2832 /*
2833 * Get the first variable.
2834 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002835 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 return FAIL;
2837
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002838 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002839 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840
2841 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002842 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002844 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002846 typval_T var2;
2847 int ic;
2848 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002849 int evaluate = evalarg == NULL
2850 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002851
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002852 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002853 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002854 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002855 p = *arg;
2856 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002857 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2858 {
mityu4ac198c2021-05-28 17:52:40 +02002859 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002860 clear_tv(rettv);
2861 return FAIL;
2862 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002863
Bram Moolenaar696ba232020-07-29 21:20:41 +02002864 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2865 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002866 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002867 clear_tv(rettv);
2868 return FAIL;
2869 }
2870
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002871 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 if (p[len] == '?')
2873 {
2874 ic = TRUE;
2875 ++len;
2876 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002877 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 else if (p[len] == '#')
2879 {
2880 ic = FALSE;
2881 ++len;
2882 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002883 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002885 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886
2887 /*
2888 * Get the second variable.
2889 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002890 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2891 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002892 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002893 clear_tv(rettv);
2894 return FAIL;
2895 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002896 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002897 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002899 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 return FAIL;
2901 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002902 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002903 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002904 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002905
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002906 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002907 {
2908 ret = FAIL;
2909 clear_tv(rettv);
2910 }
2911 else
2912 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002913 clear_tv(&var2);
2914 return ret;
2915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 }
2917
2918 return OK;
2919}
2920
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002921/*
2922 * Make a copy of blob "tv1" and append blob "tv2".
2923 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924 void
2925eval_addblob(typval_T *tv1, typval_T *tv2)
2926{
2927 blob_T *b1 = tv1->vval.v_blob;
2928 blob_T *b2 = tv2->vval.v_blob;
2929 blob_T *b = blob_alloc();
2930 int i;
2931
2932 if (b != NULL)
2933 {
2934 for (i = 0; i < blob_len(b1); i++)
2935 ga_append(&b->bv_ga, blob_get(b1, i));
2936 for (i = 0; i < blob_len(b2); i++)
2937 ga_append(&b->bv_ga, blob_get(b2, i));
2938
2939 clear_tv(tv1);
2940 rettv_blob_set(tv1, b);
2941 }
2942}
2943
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002944/*
2945 * Make a copy of list "tv1" and append list "tv2".
2946 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947 int
2948eval_addlist(typval_T *tv1, typval_T *tv2)
2949{
2950 typval_T var3;
2951
2952 // concatenate Lists
2953 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2954 {
2955 clear_tv(tv1);
2956 clear_tv(tv2);
2957 return FAIL;
2958 }
2959 clear_tv(tv1);
2960 *tv1 = var3;
2961 return OK;
2962}
2963
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964/*
2965 * Handle fourth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00002966 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002968 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002969 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 *
2971 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002972 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 *
2974 * Return OK or FAIL.
2975 */
2976 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002977eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 /*
2980 * Get the first variable.
2981 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002982 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 return FAIL;
2984
2985 /*
2986 * Repeat computing, until no '+', '-' or '.' is following.
2987 */
2988 for (;;)
2989 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002990 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002991 int getnext;
2992 char_u *p;
2993 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002994 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002995 int concat;
2996 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002997 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002998
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002999 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003000 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003001 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003002 p = eval_next_non_blank(*arg, evalarg, &getnext);
3003 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003004 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003005 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3006 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003008 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3009 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003010
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003011 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003012 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003013 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003014 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003015 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003016 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003017 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003018 {
mityu4ac198c2021-05-28 17:52:40 +02003019 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003020 clear_tv(rettv);
3021 return FAIL;
3022 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003023 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003024 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003025 if ((op != '+' || (rettv->v_type != VAR_LIST
3026 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003027#ifdef FEAT_FLOAT
3028 && (op == '.' || rettv->v_type != VAR_FLOAT)
3029#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003030 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003031 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003032 int error = FALSE;
3033
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003034 // For "list + ...", an illegal use of the first operand as
3035 // a number cannot be determined before evaluating the 2nd
3036 // operand: if this is also a list, all is ok.
3037 // For "something . ...", "something - ..." or "non-list + ...",
3038 // we know that the first operand needs to be a string or number
3039 // without evaluating the 2nd operand. So check before to avoid
3040 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003041 if (op != '.')
3042 tv_get_number_chk(rettv, &error);
3043 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003044 {
3045 clear_tv(rettv);
3046 return FAIL;
3047 }
3048 }
3049
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 /*
3051 * Get the second variable.
3052 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003053 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003054 {
mityu89dcb4d2021-05-28 13:50:17 +02003055 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003056 clear_tv(rettv);
3057 return FAIL;
3058 }
3059 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02003060 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003062 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 return FAIL;
3064 }
3065
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003066 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 {
3068 /*
3069 * Compute the result.
3070 */
3071 if (op == '.')
3072 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003073 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3074 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003075 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003076
Bram Moolenaar2e086612020-08-25 22:37:48 +02003077 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003078 || var2.v_type == VAR_CHANNEL
3079 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003080 semsg(_(e_using_invalid_value_as_string_str),
3081 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003082#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003083 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003084 {
3085 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3086 var2.vval.v_float);
3087 s2 = buf2;
3088 }
3089#endif
3090 else
3091 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003092 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003093 {
3094 clear_tv(rettv);
3095 clear_tv(&var2);
3096 return FAIL;
3097 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003098 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003099 clear_tv(rettv);
3100 rettv->v_type = VAR_STRING;
3101 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003103 else if (op == '+' && rettv->v_type == VAR_BLOB
3104 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003106 else if (op == '+' && rettv->v_type == VAR_LIST
3107 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003108 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003110 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 else
3113 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003114 int error = FALSE;
3115 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003116#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003117 float_T f1 = 0, f2 = 0;
3118
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003119 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003120 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003121 f1 = rettv->vval.v_float;
3122 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003123 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003124 else
3125#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003126 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003127 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003128 if (error)
3129 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003130 // This can only happen for "list + non-list" or
3131 // "blob + non-blob". For "non-list + ..." or
3132 // "something - ...", we returned before evaluating the
3133 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003134 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003135 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003136 return FAIL;
3137 }
3138#ifdef FEAT_FLOAT
3139 if (var2.v_type == VAR_FLOAT)
3140 f1 = n1;
3141#endif
3142 }
3143#ifdef FEAT_FLOAT
3144 if (var2.v_type == VAR_FLOAT)
3145 {
3146 f2 = var2.vval.v_float;
3147 n2 = 0;
3148 }
3149 else
3150#endif
3151 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003152 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003153 if (error)
3154 {
3155 clear_tv(rettv);
3156 clear_tv(&var2);
3157 return FAIL;
3158 }
3159#ifdef FEAT_FLOAT
3160 if (rettv->v_type == VAR_FLOAT)
3161 f2 = n2;
3162#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003163 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003164 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003165
3166#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003167 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003168 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3169 {
3170 if (op == '+')
3171 f1 = f1 + f2;
3172 else
3173 f1 = f1 - f2;
3174 rettv->v_type = VAR_FLOAT;
3175 rettv->vval.v_float = f1;
3176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003178#endif
3179 {
3180 if (op == '+')
3181 n1 = n1 + n2;
3182 else
3183 n1 = n1 - n2;
3184 rettv->v_type = VAR_NUMBER;
3185 rettv->vval.v_number = n1;
3186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003188 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 }
3190 }
3191 return OK;
3192}
3193
3194/*
3195 * Handle fifth level expression:
3196 * * number multiplication
3197 * / number division
3198 * % number modulo
3199 *
3200 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003201 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 *
3203 * Return OK or FAIL.
3204 */
3205 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003206eval6(
3207 char_u **arg,
3208 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003209 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003210 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003212#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003213 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215
3216 /*
3217 * Get the first variable.
3218 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003219 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 return FAIL;
3221
3222 /*
3223 * Repeat computing, until no '*', '/' or '%' is following.
3224 */
3225 for (;;)
3226 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003227 int evaluate;
3228 int getnext;
3229 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003230 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003231 int op;
3232 varnumber_T n1, n2;
3233#ifdef FEAT_FLOAT
3234 float_T f1, f2;
3235#endif
3236 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003237
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003238 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003239 p = eval_next_non_blank(*arg, evalarg, &getnext);
3240 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003241 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003243
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003244 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003245 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003246 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003247 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003248 {
3249 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3250 {
mityu4ac198c2021-05-28 17:52:40 +02003251 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003252 clear_tv(rettv);
3253 return FAIL;
3254 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003255 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003258#ifdef FEAT_FLOAT
3259 f1 = 0;
3260 f2 = 0;
3261#endif
3262 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003263 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003265#ifdef FEAT_FLOAT
3266 if (rettv->v_type == VAR_FLOAT)
3267 {
3268 f1 = rettv->vval.v_float;
3269 use_float = TRUE;
3270 n1 = 0;
3271 }
3272 else
3273#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003274 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003275 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003276 if (error)
3277 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 }
3279 else
3280 n1 = 0;
3281
3282 /*
3283 * Get the second variable.
3284 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003285 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3286 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003287 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003288 clear_tv(rettv);
3289 return FAIL;
3290 }
3291 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003292 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 return FAIL;
3294
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003295 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003297#ifdef FEAT_FLOAT
3298 if (var2.v_type == VAR_FLOAT)
3299 {
3300 if (!use_float)
3301 {
3302 f1 = n1;
3303 use_float = TRUE;
3304 }
3305 f2 = var2.vval.v_float;
3306 n2 = 0;
3307 }
3308 else
3309#endif
3310 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003311 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003312 clear_tv(&var2);
3313 if (error)
3314 return FAIL;
3315#ifdef FEAT_FLOAT
3316 if (use_float)
3317 f2 = n2;
3318#endif
3319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320
3321 /*
3322 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003323 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003325#ifdef FEAT_FLOAT
3326 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003328 if (op == '*')
3329 f1 = f1 * f2;
3330 else if (op == '/')
3331 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003332# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003333 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003334 if (f2 == 0.0)
3335 {
3336 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003337 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003338 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003339 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003340 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003341 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003342 }
3343 else
3344 f1 = f1 / f2;
3345# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003346 // We rely on the floating point library to handle divide
3347 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003348 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003349# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003352 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003353 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003354 return FAIL;
3355 }
3356 rettv->v_type = VAR_FLOAT;
3357 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 }
3359 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003362 int failed = FALSE;
3363
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003364 if (op == '*')
3365 n1 = n1 * n2;
3366 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003367 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003369 n1 = num_modulus(n1, n2, &failed);
3370 if (failed)
3371 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003372
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003373 rettv->v_type = VAR_NUMBER;
3374 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 }
3377 }
3378
3379 return OK;
3380}
3381
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003382/*
3383 * Handle a type cast before a base level expression.
3384 * "arg" must point to the first non-white of the expression.
3385 * "arg" is advanced to just after the recognized expression.
3386 * Return OK or FAIL.
3387 */
3388 static int
3389eval7t(
3390 char_u **arg,
3391 typval_T *rettv,
3392 evalarg_T *evalarg,
3393 int want_string) // after "." operator
3394{
3395 type_T *want_type = NULL;
3396 garray_T type_list; // list of pointers to allocated types
3397 int res;
3398 int evaluate = evalarg == NULL ? 0
3399 : (evalarg->eval_flags & EVAL_EVALUATE);
3400
3401 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003402 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3403 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003404 {
3405 ++*arg;
3406 ga_init2(&type_list, sizeof(type_T *), 10);
3407 want_type = parse_type(arg, &type_list, TRUE);
3408 if (want_type == NULL && (evaluate || **arg != '>'))
3409 {
3410 clear_type_list(&type_list);
3411 return FAIL;
3412 }
3413
3414 if (**arg != '>')
3415 {
3416 if (*skipwhite(*arg) == '>')
3417 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3418 else
3419 emsg(_(e_missing_gt));
3420 clear_type_list(&type_list);
3421 return FAIL;
3422 }
3423 ++*arg;
3424 *arg = skipwhite_and_linebreak(*arg, evalarg);
3425 }
3426
3427 res = eval7(arg, rettv, evalarg, want_string);
3428
3429 if (want_type != NULL && evaluate)
3430 {
3431 if (res == OK)
3432 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003433 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3434 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003435
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003436 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003437 {
3438 if (want_type == &t_bool && actual != &t_bool
3439 && (actual->tt_flags & TTFLAG_BOOL_OK))
3440 {
3441 int n = tv2bool(rettv);
3442
3443 // can use "0" and "1" for boolean in some places
3444 clear_tv(rettv);
3445 rettv->v_type = VAR_BOOL;
3446 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3447 }
3448 else
3449 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003450 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003451
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003452 where.wt_variable = TRUE;
3453 res = check_type(want_type, actual, TRUE, where);
3454 }
3455 }
3456 }
3457 clear_type_list(&type_list);
3458 }
3459
3460 return res;
3461}
3462
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003463 int
3464eval_leader(char_u **arg, int vim9)
3465{
3466 char_u *s = *arg;
3467 char_u *p = *arg;
3468
3469 while (*p == '!' || *p == '-' || *p == '+')
3470 {
3471 char_u *n = skipwhite(p + 1);
3472
3473 // ++, --, -+ and +- are not accepted in Vim9 script
3474 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3475 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003476 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003477 return FAIL;
3478 }
3479 p = n;
3480 }
3481 *arg = p;
3482 return OK;
3483}
3484
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485/*
3486 * Handle sixth level expression:
3487 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003488 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003489 * "string" string constant
3490 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 * &option-name option value
3492 * @r register contents
3493 * identifier variable value
3494 * function() function call
3495 * $VAR environment variable
3496 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003497 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003499 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003500 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 *
3502 * Also handle:
3503 * ! in front logical NOT
3504 * - in front unary minus
3505 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003506 * trailing [] subscript in String or List
3507 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003508 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 *
3510 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003511 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 *
3513 * Return OK or FAIL.
3514 */
3515 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003516eval7(
3517 char_u **arg,
3518 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003519 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003522 int evaluate = evalarg != NULL
3523 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 int len;
3525 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003526 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 char_u *start_leader, *end_leader;
3528 int ret = OK;
3529 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003530 static int recurse = 0;
3531 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532
3533 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003534 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003535 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003537 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538
3539 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003540 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 */
3542 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003543 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003544 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 end_leader = *arg;
3546
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003547 if (**arg == '.' && (!isdigit(*(*arg + 1))
3548#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003549 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003550#endif
3551 ))
3552 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003553 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003554 ++*arg;
3555 return FAIL;
3556 }
3557
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003558 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003559 // and crash. With MSVC the stack is smaller.
3560 if (recurse ==
3561#ifdef _MSC_VER
3562 300
3563#else
3564 1000
3565#endif
3566 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003567 {
3568 semsg(_(e_expression_too_recursive_str), *arg);
3569 return FAIL;
3570 }
3571 ++recurse;
3572
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 switch (**arg)
3574 {
3575 /*
3576 * Number constant.
3577 */
3578 case '0':
3579 case '1':
3580 case '2':
3581 case '3':
3582 case '4':
3583 case '5':
3584 case '6':
3585 case '7':
3586 case '8':
3587 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003588 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003589
3590 // Apply prefixed "-" and "+" now. Matters especially when
3591 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003592 if (ret == OK && evaluate && end_leader > start_leader
3593 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003594 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 break;
3596
3597 /*
3598 * String constant: "string".
3599 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003600 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 break;
3602
3603 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003604 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003606 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003607 break;
3608
3609 /*
3610 * List: [expr, expr]
3611 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003612 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 break;
3614
3615 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003616 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003617 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003618 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003619 {
3620 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3621 }
3622 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003623 {
3624 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003625 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003626 }
3627 else
3628 ret = NOTDONE;
3629 break;
3630
3631 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003632 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003633 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003634 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003635 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003636 ret = NOTDONE;
3637 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00003638 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003639 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003640 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003641 break;
3642
3643 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003644 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003646 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 break;
3648
3649 /*
3650 * Environment variable: $VAR.
3651 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003652 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 break;
3654
3655 /*
3656 * Register contents: @r.
3657 */
3658 case '@': ++*arg;
3659 if (evaluate)
3660 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003661 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003662 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003663 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003664 emsg_invreg(**arg);
3665 else
3666 {
3667 rettv->v_type = VAR_STRING;
3668 rettv->vval.v_string = get_reg_contents(**arg,
3669 GREG_EXPR_SRC);
3670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 }
3672 if (**arg != NUL)
3673 ++*arg;
3674 break;
3675
3676 /*
3677 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003678 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003680 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003681 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01003682 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003683 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003684 if (ret == OK && evaluate)
3685 {
3686 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3687
Bram Moolenaara9931532021-06-12 15:58:16 +02003688 // Compile it here to get the return type. The return
3689 // type is optional, when it's missing use t_unknown.
3690 // This is recognized in compile_return().
3691 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3692 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003693 if (compile_def_function(ufunc,
Bram Moolenaare99d4222021-06-13 14:01:26 +02003694 FALSE, COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003695 {
3696 clear_tv(rettv);
3697 ret = FAIL;
3698 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003699 }
3700 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003701 if (ret == NOTDONE)
3702 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003703 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003704 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003705
Bram Moolenaar9215f012020-06-27 21:18:00 +02003706 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003707 if (**arg == ')')
3708 ++*arg;
3709 else if (ret == OK)
3710 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003711 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003712 clear_tv(rettv);
3713 ret = FAIL;
3714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 }
3716 break;
3717
Bram Moolenaar8c711452005-01-14 21:53:12 +00003718 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 break;
3720 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003721
3722 if (ret == NOTDONE)
3723 {
3724 /*
3725 * Must be a variable or function name.
3726 * Can also be a curly-braces kind of name: {expr}.
3727 */
3728 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003729 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003730 if (alias != NULL)
3731 s = alias;
3732
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003733 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003734 ret = FAIL;
3735 else
3736 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003737 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3738
Bram Moolenaar4525a572022-02-13 11:57:33 +00003739 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003740 {
3741 emsg(_(e_cannot_use_underscore_here));
3742 ret = FAIL;
3743 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00003744 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00003745 && s[0] == 's' && s[1] == ':')
3746 {
3747 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
3748 ret = FAIL;
3749 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00003750 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003751 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003752 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003753 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003754 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003755 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003756 else if (flags & EVAL_CONSTANT)
3757 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003758 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003759 {
3760 // get the value of "true", "false" or a variable
Bram Moolenaar4525a572022-02-13 11:57:33 +00003761 if (len == 4 && vim9script && STRNCMP(s, "true", 4) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003762 {
3763 rettv->v_type = VAR_BOOL;
3764 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003765 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003766 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00003767 else if (len == 5 && vim9script && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003768 {
3769 rettv->v_type = VAR_BOOL;
3770 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003771 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003772 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00003773 else if (len == 4 && vim9script && STRNCMP(s, "null", 4) == 0)
Bram Moolenaar5f639382021-01-03 22:05:19 +01003774 {
3775 rettv->v_type = VAR_SPECIAL;
3776 rettv->vval.v_number = VVAL_NULL;
3777 ret = OK;
3778 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003779 else
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003780 {
3781 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003782 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003783 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003784 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003785 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003786 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003787 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003788 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003789 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003790 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003791 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003792 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003793 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003794 }
3795
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003796 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3797 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003798 if (ret == OK)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003799 ret = handle_subscript(arg, name_start, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800
3801 /*
3802 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3803 */
3804 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003805 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003806
3807 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003808 return ret;
3809}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003810
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003811/*
3812 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003813 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003814 * Adjusts "end_leaderp" until it is at "start_leader".
3815 */
3816 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003817eval7_leader(
3818 typval_T *rettv,
3819 int numeric_only,
3820 char_u *start_leader,
3821 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003822{
3823 char_u *end_leader = *end_leaderp;
3824 int ret = OK;
3825 int error = FALSE;
3826 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003827 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003828 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003829#ifdef FEAT_FLOAT
3830 float_T f = 0.0;
3831
3832 if (rettv->v_type == VAR_FLOAT)
3833 f = rettv->vval.v_float;
3834 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003835#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003836 {
3837 while (VIM_ISWHITE(end_leader[-1]))
3838 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003839 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003840 val = tv2bool(rettv);
3841 else
3842 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003843 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003844 if (error)
3845 {
3846 clear_tv(rettv);
3847 ret = FAIL;
3848 }
3849 else
3850 {
3851 while (end_leader > start_leader)
3852 {
3853 --end_leader;
3854 if (*end_leader == '!')
3855 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003856 if (numeric_only)
3857 {
3858 ++end_leader;
3859 break;
3860 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003861#ifdef FEAT_FLOAT
3862 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003863 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003864 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003865 {
3866 rettv->v_type = VAR_BOOL;
3867 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3868 }
3869 else
3870 f = !f;
3871 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003872 else
3873#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003874 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003875 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003876 type = VAR_BOOL;
3877 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003878 }
3879 else if (*end_leader == '-')
3880 {
3881#ifdef FEAT_FLOAT
3882 if (rettv->v_type == VAR_FLOAT)
3883 f = -f;
3884 else
3885#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003886 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003887 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003888 type = VAR_NUMBER;
3889 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003890 }
3891 }
3892#ifdef FEAT_FLOAT
3893 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003895 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003896 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003898 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003899#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003900 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003901 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003902 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003903 rettv->v_type = type;
3904 else
3905 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003906 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003909 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 return ret;
3911}
3912
3913/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003914 * Call the function referred to in "rettv".
3915 */
3916 static int
3917call_func_rettv(
3918 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003919 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003920 typval_T *rettv,
3921 int evaluate,
3922 dict_T *selfdict,
3923 typval_T *basetv)
3924{
3925 partial_T *pt = NULL;
3926 funcexe_T funcexe;
3927 typval_T functv;
3928 char_u *s;
3929 int ret;
3930
3931 // need to copy the funcref so that we can clear rettv
3932 if (evaluate)
3933 {
3934 functv = *rettv;
3935 rettv->v_type = VAR_UNKNOWN;
3936
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003937 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003938 if (functv.v_type == VAR_PARTIAL)
3939 {
3940 pt = functv.vval.v_partial;
3941 s = partial_name(pt);
3942 }
3943 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003944 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003945 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003946 if (s == NULL || *s == NUL)
3947 {
3948 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02003949 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003950 goto theend;
3951 }
3952 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003953 }
3954 else
3955 s = (char_u *)"";
3956
Bram Moolenaara80faa82020-04-12 19:37:17 +02003957 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003958 funcexe.fe_firstline = curwin->w_cursor.lnum;
3959 funcexe.fe_lastline = curwin->w_cursor.lnum;
3960 funcexe.fe_evaluate = evaluate;
3961 funcexe.fe_partial = pt;
3962 funcexe.fe_selfdict = selfdict;
3963 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003964 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003965
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003966theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003967 // Clear the funcref afterwards, so that deleting it while
3968 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003969 if (evaluate)
3970 clear_tv(&functv);
3971
3972 return ret;
3973}
3974
3975/*
3976 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003977 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003978 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3979 */
3980 static int
3981eval_lambda(
3982 char_u **arg,
3983 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003984 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003985 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003986{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003987 int evaluate = evalarg != NULL
3988 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003989 typval_T base = *rettv;
3990 int ret;
3991
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003992 rettv->v_type = VAR_UNKNOWN;
3993
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003994 if (**arg == '{')
3995 {
3996 // ->{lambda}()
3997 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3998 }
3999 else
4000 {
4001 // ->(lambda)()
4002 ++*arg;
4003 ret = eval1(arg, rettv, evalarg);
4004 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar605ec912021-12-18 16:54:31 +00004005 if (**arg == ')')
4006 {
4007 ++*arg;
4008 }
4009 else
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004010 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004011 emsg(_(e_missing_closing_paren));
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004012 ret = FAIL;
4013 }
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004014 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004015 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004016 return FAIL;
4017 else if (**arg != '(')
4018 {
4019 if (verbose)
4020 {
4021 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004022 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004023 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004024 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004025 }
4026 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004027 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004028 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004029 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004030 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004031
4032 // Clear the funcref afterwards, so that deleting it while
4033 // evaluating the arguments is possible (see test55).
4034 if (evaluate)
4035 clear_tv(&base);
4036
4037 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004038}
4039
4040/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004041 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004042 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004043 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4044 */
4045 static int
4046eval_method(
4047 char_u **arg,
4048 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004049 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004050 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004051{
4052 char_u *name;
4053 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004054 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004055 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004056 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004057 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004058 int evaluate = evalarg != NULL
4059 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004060
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004061 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004062
Bram Moolenaarac92e252019-08-03 21:58:38 +02004063 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004064 len = get_name_len(arg, &alias, evaluate, TRUE);
4065 if (alias != NULL)
4066 name = alias;
4067
4068 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004069 {
4070 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004071 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004072 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004073 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004074 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004075 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004076 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004077
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004078 // If there is no "(" immediately following, but there is further on,
4079 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4080 // Does not handle anything where "(" is part of the expression.
4081 *arg = skipwhite(*arg);
4082
4083 if (**arg != '(' && alias == NULL
4084 && (paren = vim_strchr(*arg, '(')) != NULL)
4085 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004086 char_u *deref;
4087
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004088 *arg = name;
4089 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004090 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4091 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004092 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004093 *arg = name + len;
4094 ret = FAIL;
4095 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004096 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004097 {
4098 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004099 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004100 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004101 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004102 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004103
4104 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004105 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004106 *arg = skipwhite(*arg);
4107
4108 if (**arg != '(')
4109 {
4110 if (verbose)
4111 semsg(_(e_missing_parenthesis_str), name);
4112 ret = FAIL;
4113 }
4114 else if (VIM_ISWHITE((*arg)[-1]))
4115 {
4116 if (verbose)
4117 emsg(_(e_no_white_space_allowed_before_parenthesis));
4118 ret = FAIL;
4119 }
4120 else
4121 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004122 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004123 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004124 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004125
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004126 // Clear the funcref afterwards, so that deleting it while
4127 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004128 if (evaluate)
4129 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004130 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004131
Bram Moolenaarac92e252019-08-03 21:58:38 +02004132 return ret;
4133}
4134
4135/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004136 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4137 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004138 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4139 */
4140 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004141eval_index(
4142 char_u **arg,
4143 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004144 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004145 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004146{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004147 int evaluate = evalarg != NULL
4148 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004149 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004150 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004151 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004152 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004153 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004154 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004155
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004156 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4157 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004158
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004159 init_tv(&var1);
4160 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004161 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004162 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004163 /*
4164 * dict.name
4165 */
4166 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004167 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004168 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004169 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004170 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004171 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004172 }
4173 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004174 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004175 /*
4176 * something[idx]
4177 *
4178 * Get the (first) variable from inside the [].
4179 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004180 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004181 if (**arg == ':')
4182 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004183 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004184 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004185 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004186 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004187 semsg(_(e_white_space_required_before_and_after_str_at_str),
4188 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004189 clear_tv(&var1);
4190 return FAIL;
4191 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004192 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004193 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004194 int error = FALSE;
4195
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004196#ifdef FEAT_FLOAT
4197 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004198 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004199 && var1.v_type == VAR_FLOAT)
4200 {
4201 var1.vval.v_string = typval_tostring(&var1, TRUE);
4202 var1.v_type = VAR_STRING;
4203 }
4204#endif
Bram Moolenaar4525a572022-02-13 11:57:33 +00004205 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004206 tv_get_number_chk(&var1, &error);
4207 else
4208 error = tv_get_string_chk(&var1) == NULL;
4209 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004210 {
4211 // not a number or string
4212 clear_tv(&var1);
4213 return FAIL;
4214 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004215 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004216
4217 /*
4218 * Get the second variable from inside the [:].
4219 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004220 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004221 if (**arg == ':')
4222 {
4223 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004224 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004225 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004226 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004227 semsg(_(e_white_space_required_before_and_after_str_at_str),
4228 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004229 if (!empty1)
4230 clear_tv(&var1);
4231 return FAIL;
4232 }
4233 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004234 if (**arg == ']')
4235 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004236 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004237 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004238 if (!empty1)
4239 clear_tv(&var1);
4240 return FAIL;
4241 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004242 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004243 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004244 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004245 if (!empty1)
4246 clear_tv(&var1);
4247 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004248 return FAIL;
4249 }
4250 }
4251
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004252 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004253 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004254 if (**arg != ']')
4255 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004256 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004257 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004258 clear_tv(&var1);
4259 if (range)
4260 clear_tv(&var2);
4261 return FAIL;
4262 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004263 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004264 }
4265
4266 if (evaluate)
4267 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004268 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004269 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004270 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004271
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004272 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004273 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004274 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004275 clear_tv(&var2);
4276 return res;
4277 }
4278 return OK;
4279}
4280
4281/*
4282 * Check if "rettv" can have an [index] or [sli:ce]
4283 */
4284 int
4285check_can_index(typval_T *rettv, int evaluate, int verbose)
4286{
4287 switch (rettv->v_type)
4288 {
4289 case VAR_FUNC:
4290 case VAR_PARTIAL:
4291 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004292 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004293 return FAIL;
4294 case VAR_FLOAT:
4295#ifdef FEAT_FLOAT
4296 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004297 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004298 return FAIL;
4299#endif
4300 case VAR_BOOL:
4301 case VAR_SPECIAL:
4302 case VAR_JOB:
4303 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004304 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004305 if (verbose)
4306 emsg(_(e_cannot_index_special_variable));
4307 return FAIL;
4308 case VAR_UNKNOWN:
4309 case VAR_ANY:
4310 case VAR_VOID:
4311 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004312 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004313 emsg(_(e_cannot_index_special_variable));
4314 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004315 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004316 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004317
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004318 case VAR_STRING:
4319 case VAR_LIST:
4320 case VAR_DICT:
4321 case VAR_BLOB:
4322 break;
4323 case VAR_NUMBER:
4324 if (in_vim9script())
4325 emsg(_(e_cannot_index_number));
4326 break;
4327 }
4328 return OK;
4329}
4330
4331/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004332 * slice() function
4333 */
4334 void
4335f_slice(typval_T *argvars, typval_T *rettv)
4336{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004337 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004338 && ((argvars[0].v_type != VAR_STRING
4339 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004340 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004341 && check_for_list_arg(argvars, 0) == FAIL)
4342 || check_for_number_arg(argvars, 1) == FAIL
4343 || check_for_opt_number_arg(argvars, 2) == FAIL))
4344 return;
4345
Bram Moolenaar6601b622021-01-13 21:47:15 +01004346 if (check_can_index(argvars, TRUE, FALSE) == OK)
4347 {
4348 copy_tv(argvars, rettv);
4349 eval_index_inner(rettv, TRUE, argvars + 1,
4350 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4351 TRUE, NULL, 0, FALSE);
4352 }
4353}
4354
4355/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004356 * Apply index or range to "rettv".
4357 * "var1" is the first index, NULL for [:expr].
4358 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004359 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4360 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004361 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4362 */
4363 int
4364eval_index_inner(
4365 typval_T *rettv,
4366 int is_range,
4367 typval_T *var1,
4368 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004369 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004370 char_u *key,
4371 int keylen,
4372 int verbose)
4373{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004374 varnumber_T n1, n2 = 0;
4375 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004376
4377 n1 = 0;
4378 if (var1 != NULL && rettv->v_type != VAR_DICT)
4379 n1 = tv_get_number(var1);
4380
4381 if (is_range)
4382 {
4383 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004384 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004385 if (verbose)
4386 emsg(_(e_cannot_slice_dictionary));
4387 return FAIL;
4388 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004389 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004390 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004391 else
4392 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004393 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004394
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004395 switch (rettv->v_type)
4396 {
4397 case VAR_UNKNOWN:
4398 case VAR_ANY:
4399 case VAR_VOID:
4400 case VAR_FUNC:
4401 case VAR_PARTIAL:
4402 case VAR_FLOAT:
4403 case VAR_BOOL:
4404 case VAR_SPECIAL:
4405 case VAR_JOB:
4406 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004407 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004408 break; // not evaluating, skipping over subscript
4409
4410 case VAR_NUMBER:
4411 case VAR_STRING:
4412 {
4413 char_u *s = tv_get_string(rettv);
4414
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004415 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004416 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004417 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004418 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004419 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004420 else
4421 s = char_from_string(s, n1);
4422 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004423 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004424 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004425 // The resulting variable is a substring. If the indexes
4426 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004427 if (n1 < 0)
4428 {
4429 n1 = len + n1;
4430 if (n1 < 0)
4431 n1 = 0;
4432 }
4433 if (n2 < 0)
4434 n2 = len + n2;
4435 else if (n2 >= len)
4436 n2 = len;
4437 if (n1 >= len || n2 < 0 || n1 > n2)
4438 s = NULL;
4439 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004440 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004441 }
4442 else
4443 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004444 // The resulting variable is a string of a single
4445 // character. If the index is too big or negative the
4446 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004447 if (n1 >= len || n1 < 0)
4448 s = NULL;
4449 else
4450 s = vim_strnsave(s + n1, 1);
4451 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004452 clear_tv(rettv);
4453 rettv->v_type = VAR_STRING;
4454 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004455 }
4456 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004457
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004458 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004459 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4460 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004461 break;
4462
4463 case VAR_LIST:
4464 if (var1 == NULL)
4465 n1 = 0;
4466 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004467 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004468 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004469 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004470 return FAIL;
4471 break;
4472
4473 case VAR_DICT:
4474 {
4475 dictitem_T *item;
4476 typval_T tmp;
4477
4478 if (key == NULL)
4479 {
4480 key = tv_get_string_chk(var1);
4481 if (key == NULL)
4482 return FAIL;
4483 }
4484
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004485 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004486
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004487 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004488 {
4489 if (verbose)
4490 {
4491 if (keylen > 0)
4492 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004493 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004494 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004495 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004496 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004497
4498 copy_tv(&item->di_tv, &tmp);
4499 clear_tv(rettv);
4500 *rettv = tmp;
4501 }
4502 break;
4503 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004504 return OK;
4505}
4506
4507/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004508 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004509 */
4510 char_u *
4511partial_name(partial_T *pt)
4512{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004513 if (pt != NULL)
4514 {
4515 if (pt->pt_name != NULL)
4516 return pt->pt_name;
4517 if (pt->pt_func != NULL)
4518 return pt->pt_func->uf_name;
4519 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004520 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004521}
4522
Bram Moolenaarddecc252016-04-06 22:59:37 +02004523 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004524partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004525{
4526 int i;
4527
4528 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004529 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004530 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004531 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004532 if (pt->pt_name != NULL)
4533 {
4534 func_unref(pt->pt_name);
4535 vim_free(pt->pt_name);
4536 }
4537 else
4538 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004539
Bram Moolenaar54656012021-06-09 20:50:46 +02004540 // "out_up" is no longer used, decrement refcount on partial that owns it.
4541 partial_unref(pt->pt_outer.out_up_partial);
4542
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004543 // Using pt_outer from another partial.
4544 partial_unref(pt->pt_outer_partial);
4545
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004546 // Decrease the reference count for the context of a closure. If down
4547 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004548 if (pt->pt_funcstack != NULL)
4549 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004550 --pt->pt_funcstack->fs_refcount;
4551 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004552 }
4553
Bram Moolenaarddecc252016-04-06 22:59:37 +02004554 vim_free(pt);
4555}
4556
4557/*
4558 * Unreference a closure: decrement the reference count and free it when it
4559 * becomes zero.
4560 */
4561 void
4562partial_unref(partial_T *pt)
4563{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004564 if (pt != NULL)
4565 {
4566 if (--pt->pt_refcount <= 0)
4567 partial_free(pt);
4568
4569 // If the reference count goes down to one, the funcstack may be the
4570 // only reference and can be freed if no other partials reference it.
4571 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4572 funcstack_check_refcount(pt->pt_funcstack);
4573 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004574}
4575
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004576/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004577 * Return the next (unique) copy ID.
4578 * Used for serializing nested structures.
4579 */
4580 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004581get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004582{
4583 current_copyID += COPYID_INC;
4584 return current_copyID;
4585}
4586
4587/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004588 * Garbage collection for lists and dictionaries.
4589 *
4590 * We use reference counts to be able to free most items right away when they
4591 * are no longer used. But for composite items it's possible that it becomes
4592 * unused while the reference count is > 0: When there is a recursive
4593 * reference. Example:
4594 * :let l = [1, 2, 3]
4595 * :let d = {9: l}
4596 * :let l[1] = d
4597 *
4598 * Since this is quite unusual we handle this with garbage collection: every
4599 * once in a while find out which lists and dicts are not referenced from any
4600 * variable.
4601 *
4602 * Here is a good reference text about garbage collection (refers to Python
4603 * but it applies to all reference-counting mechanisms):
4604 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004605 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004606
4607/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004608 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004609 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004610 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004611 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004612 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004613garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004614{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004615 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004616 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004617 buf_T *buf;
4618 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004619 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004620 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004621
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004622 if (!testing)
4623 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004624 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004625 want_garbage_collect = FALSE;
4626 may_garbage_collect = FALSE;
4627 garbage_collect_at_exit = FALSE;
4628 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004629
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004630 // The execution stack can grow big, limit the size.
4631 if (exestack.ga_maxlen - exestack.ga_len > 500)
4632 {
4633 size_t new_len;
4634 char_u *pp;
4635 int n;
4636
4637 // Keep 150% of the current size, with a minimum of the growth size.
4638 n = exestack.ga_len / 2;
4639 if (n < exestack.ga_growsize)
4640 n = exestack.ga_growsize;
4641
4642 // Don't make it bigger though.
4643 if (exestack.ga_len + n < exestack.ga_maxlen)
4644 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004645 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004646 pp = vim_realloc(exestack.ga_data, new_len);
4647 if (pp == NULL)
4648 return FAIL;
4649 exestack.ga_maxlen = exestack.ga_len + n;
4650 exestack.ga_data = pp;
4651 }
4652 }
4653
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004654 // We advance by two because we add one for items referenced through
4655 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004656 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004657
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004658 /*
4659 * 1. Go through all accessible variables and mark all lists and dicts
4660 * with copyID.
4661 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004662
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004663 // Don't free variables in the previous_funccal list unless they are only
4664 // referenced through previous_funccal. This must be first, because if
4665 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004666 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004667
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004668 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004669 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004670
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004671 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004672 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004673 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4674 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004675
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004676 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004677 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004678 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4679 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004680 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004681 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4682 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004683#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004684 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004685 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4686 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004687 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004688 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004689 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4690 NULL, NULL);
4691#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004692
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004693 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004694 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004695 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4696 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004697 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004698 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004699
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004700 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004701 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004702
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004703 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004704 abort = abort || set_ref_in_functions(copyID);
4705
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004706 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004707 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004708
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004709 // funcstacks keep variables for closures
4710 abort = abort || set_ref_in_funcstacks(copyID);
4711
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004712 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004713 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004714
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004715 // callbacks in buffers
4716 abort = abort || set_ref_in_buffers(copyID);
4717
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004718 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
4719 abort = abort || set_ref_in_insexpand_funcs(copyID);
4720
4721 // 'operatorfunc' callback
4722 abort = abort || set_ref_in_opfunc(copyID);
4723
4724 // 'tagfunc' callback
4725 abort = abort || set_ref_in_tagfunc(copyID);
4726
4727 // 'imactivatefunc' and 'imstatusfunc' callbacks
4728 abort = abort || set_ref_in_im_funcs(copyID);
4729
Bram Moolenaar1dced572012-04-05 16:54:08 +02004730#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004731 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004732#endif
4733
Bram Moolenaardb913952012-06-29 12:54:53 +02004734#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004735 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004736#endif
4737
4738#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004739 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004740#endif
4741
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004742#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004743 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004744 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004745#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004746#ifdef FEAT_NETBEANS_INTG
4747 abort = abort || set_ref_in_nb_channel(copyID);
4748#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004749
Bram Moolenaare3188e22016-05-31 21:13:04 +02004750#ifdef FEAT_TIMERS
4751 abort = abort || set_ref_in_timer(copyID);
4752#endif
4753
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004754#ifdef FEAT_QUICKFIX
4755 abort = abort || set_ref_in_quickfix(copyID);
4756#endif
4757
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004758#ifdef FEAT_TERMINAL
4759 abort = abort || set_ref_in_term(copyID);
4760#endif
4761
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004762#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004763 abort = abort || set_ref_in_popups(copyID);
4764#endif
4765
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004766 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004767 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004768 /*
4769 * 2. Free lists and dictionaries that are not referenced.
4770 */
4771 did_free = free_unref_items(copyID);
4772
4773 /*
4774 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004775 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004776 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004777 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004778 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004779 else if (p_verbose > 0)
4780 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004781 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004782 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004783
4784 return did_free;
4785}
4786
4787/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004788 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004789 */
4790 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004791free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004792{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004793 int did_free = FALSE;
4794
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004795 // Let all "free" functions know that we are here. This means no
4796 // dictionaries, lists, channels or jobs are to be freed, because we will
4797 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004798 in_free_unref_items = TRUE;
4799
4800 /*
4801 * PASS 1: free the contents of the items. We don't free the items
4802 * themselves yet, so that it is possible to decrement refcount counters
4803 */
4804
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004805 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004806 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004807
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004808 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004809 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004810
4811#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004812 // Go through the list of jobs and free items without the copyID. This
4813 // must happen before doing channels, because jobs refer to channels, but
4814 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004815 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4816
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004817 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004818 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4819#endif
4820
4821 /*
4822 * PASS 2: free the items themselves.
4823 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004824 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004825 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004826
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004827#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004828 // Go through the list of jobs and free items without the copyID. This
4829 // must happen before doing channels, because jobs refer to channels, but
4830 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004831 free_unused_jobs(copyID, COPYID_MASK);
4832
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004833 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004834 free_unused_channels(copyID, COPYID_MASK);
4835#endif
4836
4837 in_free_unref_items = FALSE;
4838
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004839 return did_free;
4840}
4841
4842/*
4843 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004844 * "list_stack" is used to add lists to be marked. Can be NULL.
4845 *
4846 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004847 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004848 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004849set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004850{
4851 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004852 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004853 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004854 hashtab_T *cur_ht;
4855 ht_stack_T *ht_stack = NULL;
4856 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004857
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004858 cur_ht = ht;
4859 for (;;)
4860 {
4861 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004862 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004863 // Mark each item in the hashtab. If the item contains a hashtab
4864 // it is added to ht_stack, if it contains a list it is added to
4865 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004866 todo = (int)cur_ht->ht_used;
4867 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4868 if (!HASHITEM_EMPTY(hi))
4869 {
4870 --todo;
4871 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4872 &ht_stack, list_stack);
4873 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004874 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004875
4876 if (ht_stack == NULL)
4877 break;
4878
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004879 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004880 cur_ht = ht_stack->ht;
4881 tempitem = ht_stack;
4882 ht_stack = ht_stack->prev;
4883 free(tempitem);
4884 }
4885
4886 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004887}
4888
Dominique Pelle748b3082022-01-08 12:41:16 +00004889#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
4890 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004891/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004892 * Mark a dict and its items with "copyID".
4893 * Returns TRUE if setting references failed somehow.
4894 */
4895 int
4896set_ref_in_dict(dict_T *d, int copyID)
4897{
4898 if (d != NULL && d->dv_copyID != copyID)
4899 {
4900 d->dv_copyID = copyID;
4901 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4902 }
4903 return FALSE;
4904}
Dominique Pelle748b3082022-01-08 12:41:16 +00004905#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004906
4907/*
4908 * Mark a list and its items with "copyID".
4909 * Returns TRUE if setting references failed somehow.
4910 */
4911 int
4912set_ref_in_list(list_T *ll, int copyID)
4913{
4914 if (ll != NULL && ll->lv_copyID != copyID)
4915 {
4916 ll->lv_copyID = copyID;
4917 return set_ref_in_list_items(ll, copyID, NULL);
4918 }
4919 return FALSE;
4920}
4921
4922/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004923 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004924 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4925 *
4926 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004927 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004928 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004929set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004930{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004931 listitem_T *li;
4932 int abort = FALSE;
4933 list_T *cur_l;
4934 list_stack_T *list_stack = NULL;
4935 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004936
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004937 cur_l = l;
4938 for (;;)
4939 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004940 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004941 // Mark each item in the list. If the item contains a hashtab
4942 // it is added to ht_stack, if it contains a list it is added to
4943 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004944 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4945 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4946 ht_stack, &list_stack);
4947 if (list_stack == NULL)
4948 break;
4949
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004950 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004951 cur_l = list_stack->list;
4952 tempitem = list_stack;
4953 list_stack = list_stack->prev;
4954 free(tempitem);
4955 }
4956
4957 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004958}
4959
4960/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004961 * Mark the partial in callback 'cb' with "copyID".
4962 */
4963 int
4964set_ref_in_callback(callback_T *cb, int copyID)
4965{
4966 typval_T tv;
4967
4968 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
4969 return FALSE;
4970
4971 tv.v_type = VAR_PARTIAL;
4972 tv.vval.v_partial = cb->cb_partial;
4973 return set_ref_in_item(&tv, copyID, NULL, NULL);
4974}
4975
4976/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004977 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004978 * "list_stack" is used to add lists to be marked. Can be NULL.
4979 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4980 *
4981 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004982 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004983 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004984set_ref_in_item(
4985 typval_T *tv,
4986 int copyID,
4987 ht_stack_T **ht_stack,
4988 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004989{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004990 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004991
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004992 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004993 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004994 dict_T *dd = tv->vval.v_dict;
4995
Bram Moolenaara03f2332016-02-06 18:09:59 +01004996 if (dd != NULL && dd->dv_copyID != copyID)
4997 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004998 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004999 dd->dv_copyID = copyID;
5000 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005001 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005002 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5003 }
5004 else
5005 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005006 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5007
Bram Moolenaara03f2332016-02-06 18:09:59 +01005008 if (newitem == NULL)
5009 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005010 else
5011 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005012 newitem->ht = &dd->dv_hashtab;
5013 newitem->prev = *ht_stack;
5014 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005015 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005016 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005017 }
5018 }
5019 else if (tv->v_type == VAR_LIST)
5020 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005021 list_T *ll = tv->vval.v_list;
5022
Bram Moolenaara03f2332016-02-06 18:09:59 +01005023 if (ll != NULL && ll->lv_copyID != copyID)
5024 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005025 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005026 ll->lv_copyID = copyID;
5027 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005028 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005029 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005030 }
5031 else
5032 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005033 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5034
Bram Moolenaara03f2332016-02-06 18:09:59 +01005035 if (newitem == NULL)
5036 abort = TRUE;
5037 else
5038 {
5039 newitem->list = ll;
5040 newitem->prev = *list_stack;
5041 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005042 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005043 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005044 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005045 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005046 else if (tv->v_type == VAR_FUNC)
5047 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005048 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005049 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005050 else if (tv->v_type == VAR_PARTIAL)
5051 {
5052 partial_T *pt = tv->vval.v_partial;
5053 int i;
5054
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005055 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005056 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005057 // Didn't see this partial yet.
5058 pt->pt_copyID = copyID;
5059
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005060 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005061
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005062 if (pt->pt_dict != NULL)
5063 {
5064 typval_T dtv;
5065
5066 dtv.v_type = VAR_DICT;
5067 dtv.vval.v_dict = pt->pt_dict;
5068 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5069 }
5070
5071 for (i = 0; i < pt->pt_argc; ++i)
5072 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5073 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005074 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005075 }
5076 }
5077#ifdef FEAT_JOB_CHANNEL
5078 else if (tv->v_type == VAR_JOB)
5079 {
5080 job_T *job = tv->vval.v_job;
5081 typval_T dtv;
5082
5083 if (job != NULL && job->jv_copyID != copyID)
5084 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005085 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005086 if (job->jv_channel != NULL)
5087 {
5088 dtv.v_type = VAR_CHANNEL;
5089 dtv.vval.v_channel = job->jv_channel;
5090 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5091 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005092 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005093 {
5094 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005095 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005096 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5097 }
5098 }
5099 }
5100 else if (tv->v_type == VAR_CHANNEL)
5101 {
5102 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005103 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005104 typval_T dtv;
5105 jsonq_T *jq;
5106 cbq_T *cq;
5107
5108 if (ch != NULL && ch->ch_copyID != copyID)
5109 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005110 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005111 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005112 {
5113 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5114 jq = jq->jq_next)
5115 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5116 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5117 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005118 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005119 {
5120 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005121 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005122 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5123 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005124 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005125 {
5126 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005127 dtv.vval.v_partial =
5128 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005129 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5130 }
5131 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005132 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005133 {
5134 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005135 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005136 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5137 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005138 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005139 {
5140 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005141 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005142 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5143 }
5144 }
5145 }
5146#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005147 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005148}
5149
Bram Moolenaar8c711452005-01-14 21:53:12 +00005150/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005151 * Return a string with the string representation of a variable.
5152 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005153 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005154 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005155 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005156 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005157 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005158 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5159 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005160 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005161 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005162 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005163echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005164 typval_T *tv,
5165 char_u **tofree,
5166 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005167 int copyID,
5168 int echo_style,
5169 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005170 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005171{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005172 static int recurse = 0;
5173 char_u *r = NULL;
5174
Bram Moolenaar33570922005-01-25 22:26:29 +00005175 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005176 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005177 if (!did_echo_string_emsg)
5178 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005179 // Only give this message once for a recursive call to avoid
5180 // flooding the user with errors. And stop iterating over lists
5181 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005182 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005183 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005184 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005185 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005186 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005187 }
5188 ++recurse;
5189
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005190 switch (tv->v_type)
5191 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005192 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005193 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005194 {
5195 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005196 r = tv->vval.v_string;
5197 if (r == NULL)
5198 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005199 }
5200 else
5201 {
5202 *tofree = string_quote(tv->vval.v_string, FALSE);
5203 r = *tofree;
5204 }
5205 break;
5206
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005207 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005208 if (echo_style)
5209 {
5210 *tofree = NULL;
5211 r = tv->vval.v_string;
5212 }
5213 else
5214 {
5215 *tofree = string_quote(tv->vval.v_string, TRUE);
5216 r = *tofree;
5217 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005218 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005219
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005220 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005221 {
5222 partial_T *pt = tv->vval.v_partial;
5223 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005224 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005225 garray_T ga;
5226 int i;
5227 char_u *tf;
5228
5229 ga_init2(&ga, 1, 100);
5230 ga_concat(&ga, (char_u *)"function(");
5231 if (fname != NULL)
5232 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005233 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005234 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005235 && vim_isupper(fname[1]))
5236 {
5237 ga_concat(&ga, (char_u *)"'g:");
5238 ga_concat(&ga, fname + 1);
5239 }
5240 else
5241 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005242 vim_free(fname);
5243 }
5244 if (pt != NULL && pt->pt_argc > 0)
5245 {
5246 ga_concat(&ga, (char_u *)", [");
5247 for (i = 0; i < pt->pt_argc; ++i)
5248 {
5249 if (i > 0)
5250 ga_concat(&ga, (char_u *)", ");
5251 ga_concat(&ga,
5252 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5253 vim_free(tf);
5254 }
5255 ga_concat(&ga, (char_u *)"]");
5256 }
5257 if (pt != NULL && pt->pt_dict != NULL)
5258 {
5259 typval_T dtv;
5260
5261 ga_concat(&ga, (char_u *)", ");
5262 dtv.v_type = VAR_DICT;
5263 dtv.vval.v_dict = pt->pt_dict;
5264 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5265 vim_free(tf);
5266 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005267 // terminate with ')' and a NUL
5268 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005269
5270 *tofree = ga.ga_data;
5271 r = *tofree;
5272 break;
5273 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005274
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005275 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005276 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005277 break;
5278
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005280 if (tv->vval.v_list == NULL)
5281 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005282 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005283 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005284 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005285 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005286 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5287 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005288 {
5289 *tofree = NULL;
5290 r = (char_u *)"[...]";
5291 }
5292 else
5293 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005294 int old_copyID = tv->vval.v_list->lv_copyID;
5295
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005296 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005297 *tofree = list2string(tv, copyID, restore_copyID);
5298 if (restore_copyID)
5299 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005300 r = *tofree;
5301 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005302 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005303
Bram Moolenaar8c711452005-01-14 21:53:12 +00005304 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005305 if (tv->vval.v_dict == NULL)
5306 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005307 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005308 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005309 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005310 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005311 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5312 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005313 {
5314 *tofree = NULL;
5315 r = (char_u *)"{...}";
5316 }
5317 else
5318 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005319 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005320
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005321 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005322 *tofree = dict2string(tv, copyID, restore_copyID);
5323 if (restore_copyID)
5324 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005325 r = *tofree;
5326 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005327 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005328
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005329 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005330 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005331 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005332 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005333 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005334 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005335 break;
5336
Bram Moolenaar835dc632016-02-07 14:27:38 +01005337 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005338 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005339#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005340 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005341 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5342 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005343 if (composite_val)
5344 {
5345 *tofree = string_quote(r, FALSE);
5346 r = *tofree;
5347 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005348#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005350
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005351 case VAR_INSTR:
5352 *tofree = NULL;
5353 r = (char_u *)"instructions";
5354 break;
5355
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005356 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005357#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005358 *tofree = NULL;
5359 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5360 r = numbuf;
5361 break;
5362#endif
5363
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005364 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005365 case VAR_SPECIAL:
5366 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005367 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005368 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005369 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005370
Bram Moolenaar8502c702014-06-17 12:51:16 +02005371 if (--recurse == 0)
5372 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005373 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005374}
5375
5376/*
5377 * Return a string with the string representation of a variable.
5378 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5379 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005380 * Does not put quotes around strings, as ":echo" displays values.
5381 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5382 * May return NULL.
5383 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005384 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005385echo_string(
5386 typval_T *tv,
5387 char_u **tofree,
5388 char_u *numbuf,
5389 int copyID)
5390{
5391 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5392}
5393
5394/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005395 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5396 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005397 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005398 */
5399 int
5400buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5401{
5402 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005403 char_u *t;
5404 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005405
5406 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5407 return -1;
5408
5409 if (lnum > buf->b_ml.ml_line_count)
5410 lnum = buf->b_ml.ml_line_count;
5411
5412 str = ml_get_buf(buf, lnum, FALSE);
5413 if (str == NULL)
5414 return -1;
5415
5416 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005417 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005418
Bram Moolenaar91458462021-01-13 20:08:38 +01005419 // count the number of characters
5420 t = str;
5421 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5422 t += mb_ptr2len(t);
5423
5424 // In insert mode, when the cursor is at the end of a non-empty line,
5425 // byteidx points to the NUL character immediately past the end of the
5426 // string. In this case, add one to the character count.
5427 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5428 count++;
5429
5430 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005431}
5432
5433/*
5434 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005435 * byte index. Works only for loaded buffers. Returns -1 on failure.
5436 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005437 */
5438 int
5439buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5440{
5441 char_u *str;
5442 char_u *t;
5443
5444 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5445 return -1;
5446
5447 if (lnum > buf->b_ml.ml_line_count)
5448 lnum = buf->b_ml.ml_line_count;
5449
5450 str = ml_get_buf(buf, lnum, FALSE);
5451 if (str == NULL)
5452 return -1;
5453
5454 // Convert the character offset to a byte offset
5455 t = str;
5456 while (*t != NUL && --charidx > 0)
5457 t += mb_ptr2len(t);
5458
Bram Moolenaar91458462021-01-13 20:08:38 +01005459 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005460}
5461
5462/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005464 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005466 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005467var2fpos(
5468 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005469 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005470 int *fnum, // set to fnum for '0, 'A, etc.
5471 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005473 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005475 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005477 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005478 if (varp->v_type == VAR_LIST)
5479 {
5480 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005481 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005482 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005483 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005484
5485 l = varp->vval.v_list;
5486 if (l == NULL)
5487 return NULL;
5488
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005489 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005490 pos.lnum = list_find_nr(l, 0L, &error);
5491 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005492 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005493 if (charcol)
5494 len = (long)mb_charlen(ml_get(pos.lnum));
5495 else
5496 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005497
Bram Moolenaarec65d772020-08-20 22:29:12 +02005498 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005499 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005500 li = list_find(l, 1L);
5501 if (li != NULL && li->li_tv.v_type == VAR_STRING
5502 && li->li_tv.vval.v_string != NULL
5503 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005504 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005505 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005506 }
5507 else
5508 {
5509 pos.col = list_find_nr(l, 1L, &error);
5510 if (error)
5511 return NULL;
5512 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005513
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005514 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005515 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005516 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005517 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005518
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005519 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005520 pos.coladd = list_find_nr(l, 2L, &error);
5521 if (error)
5522 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005523
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005524 return &pos;
5525 }
5526
Bram Moolenaarc5809432021-03-27 21:23:30 +01005527 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5528 return NULL;
5529
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005530 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005531 if (name == NULL)
5532 return NULL;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005533 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005534 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005535 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005536 pos = curwin->w_cursor;
5537 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005538 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005539 return &pos;
5540 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005541 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005542 {
5543 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005544 pos = VIsual;
5545 else
5546 pos = curwin->w_cursor;
5547 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005548 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005549 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005550 }
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005551 if (name[0] == '\'' && (!in_vim9script()
5552 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005554 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005555 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5557 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005558 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005559 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 return pp;
5561 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005562
Bram Moolenaara5525202006-03-02 22:52:09 +00005563 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005564
Bram Moolenaar477933c2007-07-17 14:32:23 +00005565 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005566 {
5567 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005568 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005569 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005570 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005571 // In silent Ex mode topline is zero, but that's not a valid line
5572 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005573 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005574 return &pos;
5575 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005576 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005577 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005578 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005579 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005580 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005581 return &pos;
5582 }
5583 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005584 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005586 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 {
5588 pos.lnum = curbuf->b_ml.ml_line_count;
5589 pos.col = 0;
5590 }
5591 else
5592 {
5593 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005594 if (charcol)
5595 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5596 else
5597 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 }
5599 return &pos;
5600 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005601 if (in_vim9script())
5602 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 return NULL;
5604}
5605
5606/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005607 * Convert list in "arg" into a position and optional file number.
5608 * When "fnump" is NULL there is no file number, only 3 items.
5609 * Note that the column is passed on as-is, the caller may want to decrement
5610 * it to use 1 for the first column.
5611 * Return FAIL when conversion is not possible, doesn't check the position for
5612 * validity.
5613 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005614 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005615list2fpos(
5616 typval_T *arg,
5617 pos_T *posp,
5618 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005619 colnr_T *curswantp,
5620 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005621{
5622 list_T *l = arg->vval.v_list;
5623 long i = 0;
5624 long n;
5625
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005626 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5627 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005628 if (arg->v_type != VAR_LIST
5629 || l == NULL
5630 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005631 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005632 return FAIL;
5633
5634 if (fnump != NULL)
5635 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005636 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005637 if (n < 0)
5638 return FAIL;
5639 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005640 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005641 *fnump = n;
5642 }
5643
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005644 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005645 if (n < 0)
5646 return FAIL;
5647 posp->lnum = n;
5648
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005649 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005650 if (n < 0)
5651 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005652 // If character position is specified, then convert to byte position
5653 if (charcol)
5654 {
5655 buf_T *buf;
5656
5657 // Get the text for the specified line in a loaded buffer
5658 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5659 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5660 return FAIL;
5661
Bram Moolenaar91458462021-01-13 20:08:38 +01005662 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005663 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005664 posp->col = n;
5665
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005666 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005667 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005668 posp->coladd = 0;
5669 else
5670 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005671
Bram Moolenaar493c1782014-05-28 14:34:46 +02005672 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005673 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005674
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005675 return OK;
5676}
5677
5678/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 * Get the length of an environment variable name.
5680 * Advance "arg" to the first character after the name.
5681 * Return 0 for error.
5682 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005683 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005684get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685{
5686 char_u *p;
5687 int len;
5688
5689 for (p = *arg; vim_isIDc(*p); ++p)
5690 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005691 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 return 0;
5693
5694 len = (int)(p - *arg);
5695 *arg = p;
5696 return len;
5697}
5698
5699/*
5700 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005701 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 * Return 0 if something is wrong.
5703 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005704 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005705get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706{
5707 char_u *p;
5708 int len;
5709
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005710 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005712 {
5713 if (*p == ':')
5714 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005715 // "s:" is start of "s:var", but "n:" is not and can be used in
5716 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005717 len = (int)(p - *arg);
5718 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5719 || len > 1)
5720 break;
5721 }
5722 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005723 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 return 0;
5725
5726 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005727 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728
5729 return len;
5730}
5731
5732/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005733 * Get the length of the name of a variable or function.
5734 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005736 * Return -1 if curly braces expansion failed.
5737 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 * If the name contains 'magic' {}'s, expand them and return the
5739 * expanded name in an allocated string via 'alias' - caller must free.
5740 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005741 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005742get_name_len(
5743 char_u **arg,
5744 char_u **alias,
5745 int evaluate,
5746 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747{
5748 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 char_u *p;
5750 char_u *expr_start;
5751 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005753 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754
5755 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5756 && (*arg)[2] == (int)KE_SNR)
5757 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005758 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 *arg += 3;
5760 return get_id_len(arg) + 3;
5761 }
5762 len = eval_fname_script(*arg);
5763 if (len > 0)
5764 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005765 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 *arg += len;
5767 }
5768
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005770 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005772 p = find_name_end(*arg, &expr_start, &expr_end,
5773 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 if (expr_start != NULL)
5775 {
5776 char_u *temp_string;
5777
5778 if (!evaluate)
5779 {
5780 len += (int)(p - *arg);
5781 *arg = skipwhite(p);
5782 return len;
5783 }
5784
5785 /*
5786 * Include any <SID> etc in the expanded string:
5787 * Thus the -len here.
5788 */
5789 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5790 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005791 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 *alias = temp_string;
5793 *arg = skipwhite(p);
5794 return (int)STRLEN(temp_string);
5795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796
5797 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005798 // Only give an error when there is something, otherwise it will be
5799 // reported at a higher level.
5800 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005801 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802
5803 return len;
5804}
5805
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005806/*
5807 * Find the end of a variable or function name, taking care of magic braces.
5808 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5809 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005810 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005811 * Return a pointer to just after the name. Equal to "arg" if there is no
5812 * valid name.
5813 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005814 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005815find_name_end(
5816 char_u *arg,
5817 char_u **expr_start,
5818 char_u **expr_end,
5819 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005821 int mb_nest = 0;
5822 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005824 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005825 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005827 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005829 *expr_start = NULL;
5830 *expr_end = NULL;
5831 }
5832
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005833 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005834 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5835 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005836 return arg;
5837
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005838 for (p = arg; *p != NUL
5839 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005840 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005841 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005842 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005843 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005844 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005845 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005846 if (*p == '\'')
5847 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005848 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005849 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005850 ;
5851 if (*p == NUL)
5852 break;
5853 }
5854 else if (*p == '"')
5855 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005856 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005857 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005858 if (*p == '\\' && p[1] != NUL)
5859 ++p;
5860 if (*p == NUL)
5861 break;
5862 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005863 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5864 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005865 // "s:" is start of "s:var", but "n:" is not and can be used in
5866 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005867 len = (int)(p - arg);
5868 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005869 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005870 break;
5871 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005872
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005873 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005875 if (*p == '[')
5876 ++br_nest;
5877 else if (*p == ']')
5878 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005880
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005881 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005883 if (*p == '{')
5884 {
5885 mb_nest++;
5886 if (expr_start != NULL && *expr_start == NULL)
5887 *expr_start = p;
5888 }
5889 else if (*p == '}')
5890 {
5891 mb_nest--;
5892 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5893 *expr_end = p;
5894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 }
5897
5898 return p;
5899}
5900
5901/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005902 * Expands out the 'magic' {}'s in a variable/function name.
5903 * Note that this can call itself recursively, to deal with
5904 * constructs like foo{bar}{baz}{bam}
5905 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5906 * "in_start" ^
5907 * "expr_start" ^
5908 * "expr_end" ^
5909 * "in_end" ^
5910 *
5911 * Returns a new allocated string, which the caller must free.
5912 * Returns NULL for failure.
5913 */
5914 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005915make_expanded_name(
5916 char_u *in_start,
5917 char_u *expr_start,
5918 char_u *expr_end,
5919 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005920{
5921 char_u c1;
5922 char_u *retval = NULL;
5923 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005924
5925 if (expr_end == NULL || in_end == NULL)
5926 return NULL;
5927 *expr_start = NUL;
5928 *expr_end = NUL;
5929 c1 = *in_end;
5930 *in_end = NUL;
5931
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005932 temp_result = eval_to_string(expr_start + 1, FALSE);
5933 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005934 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005935 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5936 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005937 if (retval != NULL)
5938 {
5939 STRCPY(retval, in_start);
5940 STRCAT(retval, temp_result);
5941 STRCAT(retval, expr_end + 1);
5942 }
5943 }
5944 vim_free(temp_result);
5945
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005946 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005947 *expr_start = '{';
5948 *expr_end = '}';
5949
5950 if (retval != NULL)
5951 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005952 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005953 if (expr_start != NULL)
5954 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005955 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005956 temp_result = make_expanded_name(retval, expr_start,
5957 expr_end, temp_result);
5958 vim_free(retval);
5959 retval = temp_result;
5960 }
5961 }
5962
5963 return retval;
5964}
5965
5966/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005968 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005970 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005971eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005973 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005974}
5975
5976/*
5977 * Return TRUE if character "c" can be used as the first character in a
5978 * variable or function name (excluding '{' and '}').
5979 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005980 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005981eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005982{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005983 return ASCII_ISALPHA(c) || c == '_';
5984}
5985
5986/*
5987 * Return TRUE if character "c" can be used as the first character of a
5988 * dictionary key.
5989 */
5990 int
5991eval_isdictc(int c)
5992{
5993 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994}
5995
5996/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005997 * Handle:
5998 * - expr[expr], expr[expr:expr] subscript
5999 * - ".name" lookup
6000 * - function call with Funcref variable: func(expr)
6001 * - method call: var->method()
6002 *
6003 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006004 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006005 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006006 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006007handle_subscript(
6008 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006009 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006010 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006011 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006012 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006013{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006014 int evaluate = evalarg != NULL
6015 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006016 int ret = OK;
6017 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006018 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006019 int getnext;
6020 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006021
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006022 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006023 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006024 // When at the end of the line and ".name" or "->{" or "->X" follows in
6025 // the next line then consume the line break.
6026 p = eval_next_non_blank(*arg, evalarg, &getnext);
6027 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006028 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006029 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6030 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6031 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006032 {
6033 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006034 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006035 check_white = FALSE;
6036 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006037
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006038 if (rettv->v_type == VAR_ANY)
6039 {
6040 char_u *exp_name;
6041 int cc;
6042 int idx;
6043 ufunc_T *ufunc;
6044 type_T *type;
6045
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006046 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006047 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006048 if (**arg != '.')
6049 {
6050 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006051 semsg(_(e_expected_dot_after_name_str),
6052 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006053 ret = FAIL;
6054 break;
6055 }
6056 ++*arg;
6057 if (IS_WHITE_OR_NUL(**arg))
6058 {
6059 if (verbose)
6060 emsg(_(e_no_white_space_allowed_after_dot));
6061 ret = FAIL;
6062 break;
6063 }
6064
6065 // isolate the name
6066 exp_name = *arg;
6067 while (eval_isnamec(**arg))
6068 ++*arg;
6069 cc = **arg;
6070 **arg = NUL;
6071
6072 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00006073 evalarg->eval_cctx, evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006074 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006075
6076 if (idx < 0 && ufunc == NULL)
6077 {
6078 ret = FAIL;
6079 break;
6080 }
6081 if (idx >= 0)
6082 {
6083 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6084 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6085
6086 copy_tv(sv->sv_tv, rettv);
6087 }
6088 else
6089 {
6090 rettv->v_type = VAR_FUNC;
6091 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6092 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006093 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006094 }
6095
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006096 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6097 || rettv->v_type == VAR_PARTIAL))
6098 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006099 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006100 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6101 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006102
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006103 // Stop the expression evaluation when immediately aborting on
6104 // error, or when an interrupt occurred or an exception was thrown
6105 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006106 if (aborting())
6107 {
6108 if (ret == OK)
6109 clear_tv(rettv);
6110 ret = FAIL;
6111 }
6112 dict_unref(selfdict);
6113 selfdict = NULL;
6114 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006115 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006116 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006117 if (in_vim9script())
6118 *arg = skipwhite(p + 2);
6119 else
6120 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006121 if (ret == OK)
6122 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006123 if (VIM_ISWHITE(**arg))
6124 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006125 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006126 ret = FAIL;
6127 }
6128 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006129 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006130 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006131 else
6132 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006133 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006134 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006135 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006136 // "." is ".name" lookup when we found a dict or when evaluating and
6137 // scriptversion is at least 2, where string concatenation is "..".
6138 else if (**arg == '['
6139 || (**arg == '.' && (rettv->v_type == VAR_DICT
6140 || (!evaluate
6141 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006142 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006143 {
6144 dict_unref(selfdict);
6145 if (rettv->v_type == VAR_DICT)
6146 {
6147 selfdict = rettv->vval.v_dict;
6148 if (selfdict != NULL)
6149 ++selfdict->dv_refcount;
6150 }
6151 else
6152 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006153 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006154 {
6155 clear_tv(rettv);
6156 ret = FAIL;
6157 }
6158 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006159 else
6160 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006161 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006162
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006163 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6164 // Don't do this when "Func" is already a partial that was bound
6165 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006166 if (selfdict != NULL
6167 && (rettv->v_type == VAR_FUNC
6168 || (rettv->v_type == VAR_PARTIAL
6169 && (rettv->vval.v_partial->pt_auto
6170 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006171 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006172
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006173 dict_unref(selfdict);
6174 return ret;
6175}
6176
6177/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006178 * Make a copy of an item.
6179 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006180 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006181 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6182 * reference to an already copied list/dict can be used.
6183 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006184 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006185 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006186item_copy(
6187 typval_T *from,
6188 typval_T *to,
6189 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006190 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006191 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006192{
6193 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006194 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006195
Bram Moolenaar33570922005-01-25 22:26:29 +00006196 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006197 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006198 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006199 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006200 }
6201 ++recurse;
6202
6203 switch (from->v_type)
6204 {
6205 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006206 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006207 case VAR_STRING:
6208 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006209 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006210 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006211 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006212 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006213 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006214 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006215 copy_tv(from, to);
6216 break;
6217 case VAR_LIST:
6218 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006219 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006220 if (from->vval.v_list == NULL)
6221 to->vval.v_list = NULL;
6222 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6223 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006224 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006225 to->vval.v_list = from->vval.v_list->lv_copylist;
6226 ++to->vval.v_list->lv_refcount;
6227 }
6228 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006229 to->vval.v_list = list_copy(from->vval.v_list,
6230 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006231 if (to->vval.v_list == NULL)
6232 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006233 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006234 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006235 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006236 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006237 case VAR_DICT:
6238 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006239 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006240 if (from->vval.v_dict == NULL)
6241 to->vval.v_dict = NULL;
6242 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6243 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006244 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006245 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6246 ++to->vval.v_dict->dv_refcount;
6247 }
6248 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006249 to->vval.v_dict = dict_copy(from->vval.v_dict,
6250 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006251 if (to->vval.v_dict == NULL)
6252 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006253 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006254 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006255 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006256 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006257 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006258 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006259 }
6260 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006261 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006262}
6263
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006264 void
6265echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6266{
6267 char_u *tofree;
6268 char_u numbuf[NUMBUFLEN];
6269 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6270
6271 if (*atstart)
6272 {
6273 *atstart = FALSE;
6274 // Call msg_start() after eval1(), evaluating the expression
6275 // may cause a message to appear.
6276 if (with_space)
6277 {
6278 // Mark the saved text as finishing the line, so that what
6279 // follows is displayed on a new line when scrolling back
6280 // at the more prompt.
6281 msg_sb_eol();
6282 msg_start();
6283 }
6284 }
6285 else if (with_space)
6286 msg_puts_attr(" ", echo_attr);
6287
6288 if (p != NULL)
6289 for ( ; *p != NUL && !got_int; ++p)
6290 {
6291 if (*p == '\n' || *p == '\r' || *p == TAB)
6292 {
6293 if (*p != TAB && *needclr)
6294 {
6295 // remove any text still there from the command
6296 msg_clr_eos();
6297 *needclr = FALSE;
6298 }
6299 msg_putchar_attr(*p, echo_attr);
6300 }
6301 else
6302 {
6303 if (has_mbyte)
6304 {
6305 int i = (*mb_ptr2len)(p);
6306
6307 (void)msg_outtrans_len_attr(p, i, echo_attr);
6308 p += i - 1;
6309 }
6310 else
6311 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6312 }
6313 }
6314 vim_free(tofree);
6315}
6316
Bram Moolenaare9a41262005-01-15 22:18:47 +00006317/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 * ":echo expr1 ..." print each argument separated with a space, add a
6319 * newline at the end.
6320 * ":echon expr1 ..." print each argument plain.
6321 */
6322 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006323ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324{
6325 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006326 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006327 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 int needclr = TRUE;
6329 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006330 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006331 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006332 evalarg_T evalarg;
6333
Bram Moolenaare707c882020-07-01 13:07:37 +02006334 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335
6336 if (eap->skip)
6337 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006338 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006340 // If eval1() causes an error message the text from the command may
6341 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006342 need_clr_eos = needclr;
6343
Bram Moolenaar68db9962021-05-09 23:19:22 +02006344 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006345 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 {
6347 /*
6348 * Report the invalid expression unless the expression evaluation
6349 * has been cancelled due to an aborting error, an interrupt, or an
6350 * exception.
6351 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006352 if (!aborting() && did_emsg == did_emsg_before
6353 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006354 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006355 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 break;
6357 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006358 need_clr_eos = FALSE;
6359
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006361 {
6362 if (rettv.v_type == VAR_VOID)
6363 {
6364 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6365 break;
6366 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006367 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006368 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006369
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006370 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 arg = skipwhite(arg);
6372 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006373 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006374 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375
6376 if (eap->skip)
6377 --emsg_skip;
6378 else
6379 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006380 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 if (needclr)
6382 msg_clr_eos();
6383 if (eap->cmdidx == CMD_echo)
6384 msg_end();
6385 }
6386}
6387
6388/*
6389 * ":echohl {name}".
6390 */
6391 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006392ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006394 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395}
6396
6397/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006398 * Returns the :echo attribute
6399 */
6400 int
6401get_echo_attr(void)
6402{
6403 return echo_attr;
6404}
6405
6406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 * ":execute expr1 ..." execute the result of an expression.
6408 * ":echomsg expr1 ..." Print a message
6409 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006410 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 * Each gets spaces around each argument and a newline at the end for
6412 * echo commands
6413 */
6414 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006415ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416{
6417 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006418 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 int ret = OK;
6420 char_u *p;
6421 garray_T ga;
6422 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006423 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424
6425 ga_init2(&ga, 1, 80);
6426
6427 if (eap->skip)
6428 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006429 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006431 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006432 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434
6435 if (!eap->skip)
6436 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006437 char_u buf[NUMBUFLEN];
6438
6439 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006440 {
6441 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6442 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006443 semsg(_(e_using_invalid_value_as_string_str),
6444 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006445 p = NULL;
6446 }
6447 else
6448 p = tv_get_string_buf(&rettv, buf);
6449 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006450 else
6451 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006452 if (p == NULL)
6453 {
6454 clear_tv(&rettv);
6455 ret = FAIL;
6456 break;
6457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 len = (int)STRLEN(p);
6459 if (ga_grow(&ga, len + 2) == FAIL)
6460 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006461 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 ret = FAIL;
6463 break;
6464 }
6465 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 ga.ga_len += len;
6469 }
6470
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006471 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 arg = skipwhite(arg);
6473 }
6474
6475 if (ret != FAIL && ga.ga_data != NULL)
6476 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006477 // use the first line of continuation lines for messages
6478 SOURCING_LNUM = start_lnum;
6479
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006480 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6481 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006482 // Mark the already saved text as finishing the line, so that what
6483 // follows is displayed on a new line when scrolling back at the
6484 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006485 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006486 }
6487
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006489 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006490 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006491 out_flush();
6492 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006493 else if (eap->cmdidx == CMD_echoconsole)
6494 {
6495 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6496 ui_write((char_u *)"\r\n", 2, TRUE);
6497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 else if (eap->cmdidx == CMD_echoerr)
6499 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006500 int save_did_emsg = did_emsg;
6501
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006502 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006503 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 if (!force_abort)
6505 did_emsg = save_did_emsg;
6506 }
6507 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006508 {
6509 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6510
6511 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6512 sticky_cmdmod_flags = cmdmod.cmod_flags
6513 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 do_cmdline((char_u *)ga.ga_data,
6515 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006516 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 }
6519
6520 ga_clear(&ga);
6521
6522 if (eap->skip)
6523 --emsg_skip;
6524
Bram Moolenaar63b91732021-08-05 20:40:03 +02006525 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526}
6527
6528/*
6529 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6530 * "arg" points to the "&" or '+' when called, to "option" when returning.
6531 * Returns NULL when no option name found. Otherwise pointer to the char
6532 * after the option name.
6533 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006534 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006535find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536{
6537 char_u *p = *arg;
6538
6539 ++p;
6540 if (*p == 'g' && p[1] == ':')
6541 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006542 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 p += 2;
6544 }
6545 else if (*p == 'l' && p[1] == ':')
6546 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006547 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 p += 2;
6549 }
6550 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006551 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552
6553 if (!ASCII_ISALPHA(*p))
6554 return NULL;
6555 *arg = p;
6556
6557 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006558 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 else
6560 while (ASCII_ISALPHA(*p))
6561 ++p;
6562 return p;
6563}
6564
6565/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006566 * Display script name where an item was last set.
6567 * Should only be invoked when 'verbose' is non-zero.
6568 */
6569 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006570last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006571{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006572 char_u *p;
6573
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006574 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006575 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006576 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006577 if (p != NULL)
6578 {
6579 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006580 msg_puts(_("\n\tLast set from "));
6581 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006582 if (script_ctx.sc_lnum > 0)
6583 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006584 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006585 msg_outnum((long)script_ctx.sc_lnum);
6586 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006587 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006588 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006589 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006590 }
6591}
6592
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006593#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594
6595/*
6596 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006597 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 * "flags" can be "g" to do a global substitute.
6599 * Returns an allocated string, NULL for error.
6600 */
6601 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006602do_string_sub(
6603 char_u *str,
6604 char_u *pat,
6605 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006606 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006607 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608{
6609 int sublen;
6610 regmatch_T regmatch;
6611 int i;
6612 int do_all;
6613 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006614 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 garray_T ga;
6616 char_u *ret;
6617 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006618 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006620 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006622 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623
6624 ga_init2(&ga, 1, 200);
6625
6626 do_all = (flags[0] == 'g');
6627
6628 regmatch.rm_ic = p_ic;
6629 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6630 if (regmatch.regprog != NULL)
6631 {
6632 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006633 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6635 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006636 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006637 if (regmatch.startp[0] == regmatch.endp[0])
6638 {
6639 if (zero_width == regmatch.startp[0])
6640 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006641 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006642 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006643 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6644 (size_t)i);
6645 ga.ga_len += i;
6646 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006647 continue;
6648 }
6649 zero_width = regmatch.startp[0];
6650 }
6651
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 /*
6653 * Get some space for a temporary buffer to do the substitution
6654 * into. It will contain:
6655 * - The text up to where the match is.
6656 * - The substituted text.
6657 * - The text after the match.
6658 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006659 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006660 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6662 {
6663 ga_clear(&ga);
6664 break;
6665 }
6666
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006667 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 i = (int)(regmatch.startp[0] - tail);
6669 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006670 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006671 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 + ga.ga_len + i, TRUE, TRUE, FALSE);
6673 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006674 tail = regmatch.endp[0];
6675 if (*tail == NUL)
6676 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 if (!do_all)
6678 break;
6679 }
6680
6681 if (ga.ga_data != NULL)
6682 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6683
Bram Moolenaar473de612013-06-08 18:19:48 +02006684 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 }
6686
6687 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6688 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006689 if (p_cpo == empty_option)
6690 p_cpo = save_cpo;
6691 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006692 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006693 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006694 // If it's still empty it was changed and restored, need to restore in
6695 // the complicated way.
6696 if (*p_cpo == NUL)
6697 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006698 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700
6701 return ret;
6702}