blob: 3f6be184797418920fba046efad8ea8f94fb2841 [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 Moolenaarfe8e9f62022-03-16 13:09:15 +0000698 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100700 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200701 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000702 funcexe.fe_firstline = curwin->w_cursor.lnum;
703 funcexe.fe_lastline = curwin->w_cursor.lnum;
704 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000705
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000706 // The name might be "import.Func" or "Funcref". We don't know, we need to
707 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000708 // autoload script has errors. Guess that when there is a dot in the name
709 // showing errors is the right choice.
710 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000711 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000712 if (ignore_errors)
713 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000714 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000715 if (ignore_errors)
716 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000717 if (name == NULL)
718 name = func;
719
720 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
721
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000722 if (ret == FAIL)
723 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000724 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000725
726 return ret;
727}
728
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100729/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100730 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000731 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
732 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000733 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000734 */
735 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100736call_func_retstr(
737 char_u *func,
738 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200739 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000740{
741 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000742 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000743
Bram Moolenaarded27a12018-08-01 19:06:03 +0200744 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000745 return NULL;
746
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100747 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000748 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 return retval;
750}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000751
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000752/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100753 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000754 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000755 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000756 */
757 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100758call_func_retlist(
759 char_u *func,
760 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200761 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000762{
763 typval_T rettv;
764
Bram Moolenaarded27a12018-08-01 19:06:03 +0200765 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000766 return NULL;
767
768 if (rettv.v_type != VAR_LIST)
769 {
770 clear_tv(&rettv);
771 return NULL;
772 }
773
774 return rettv.vval.v_list;
775}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776
Bram Moolenaare70dd112022-01-21 16:31:11 +0000777#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100779 * Evaluate "arg", which is 'foldexpr'.
780 * Note: caller must set "curwin" to match "arg".
781 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
782 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 */
784 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000785eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000787 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000788 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200789 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000791 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000792 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
793 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794
Bram Moolenaare70dd112022-01-21 16:31:11 +0000795 arg = wp->w_p_fde;
796 current_sctx = wp->w_p_script_ctx[WV_FDE];
797
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000799 if (use_sandbox)
800 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200801 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200803 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
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 number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000808 if (tv.v_type == VAR_NUMBER)
809 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000810 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 retval = 0;
812 else
813 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100814 // If the result is a string, check if there is a non-digit before
815 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000816 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 if (!VIM_ISDIGIT(*s) && *s != '-')
818 *cp = *s++;
819 retval = atol((char *)s);
820 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000821 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 }
823 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000824 if (use_sandbox)
825 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200826 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200827 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000828 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200830 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831}
832#endif
833
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000835 * Get an lval: variable, Dict item or List item that can be assigned a value
836 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
837 * "name.key", "name.key[expr]" etc.
838 * Indexing only works if "name" is an existing List or Dictionary.
839 * "name" points to the start of the name.
840 * If "rettv" is not NULL it points to the value to be assigned.
841 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
842 * wrong; must end in space or cmd separator.
843 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100844 * flags:
845 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100846 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100847 * GLV_NO_AUTOLOAD: do not use script autoloading
848 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000849 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000850 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000851 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000852 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200853 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100854get_lval(
855 char_u *name,
856 typval_T *rettv,
857 lval_T *lp,
858 int unlet,
859 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100860 int flags, // GLV_ values
861 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000862{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000863 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000864 char_u *expr_start, *expr_end;
865 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000866 dictitem_T *v;
867 typval_T var1;
868 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000869 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000870 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000871 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100872 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100873 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100874 int writing;
Bram Moolenaar4525a572022-02-13 11:57:33 +0000875 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000876
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100877 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200878 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000879
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100880 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000881 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100882 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000883 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200884 lp->ll_name_end = find_name_end(name, NULL, NULL,
885 FNE_INCL_BR | fne_flags);
886 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000887 }
888
Bram Moolenaara749a422022-02-12 19:52:25 +0000889 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +0000890 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +0000891 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
892 {
893 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
894 return NULL;
895 }
896
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100897 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000898 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100899 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000900 if (expr_start != NULL)
901 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100902 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100903 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000904 && *p != '[' && *p != '.')
905 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000906 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000907 return NULL;
908 }
909
910 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
911 if (lp->ll_exp_name == NULL)
912 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100913 // Report an invalid expression in braces, unless the
914 // expression evaluation has been cancelled due to an
915 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000916 if (!aborting() && !quiet)
917 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000918 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000919 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000920 return NULL;
921 }
922 }
923 lp->ll_name = lp->ll_exp_name;
924 }
925 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100926 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000927 lp->ll_name = name;
928
Bram Moolenaar4525a572022-02-13 11:57:33 +0000929 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100930 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200931 // "a: type" is declaring variable "a" with a type, not "a:".
932 if (p == name + 2 && p[-1] == ':')
933 {
934 --p;
935 lp->ll_name_end = p;
936 }
937 if (*p == ':')
938 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000939 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +0200940
941 if (tp == p + 1 && !quiet)
942 {
943 semsg(_(e_white_space_required_after_str_str), ":", p);
944 return NULL;
945 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100946
Bram Moolenaar4c8b5462022-03-16 20:01:39 +0000947 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000948 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +0000949 semsg(_(e_using_type_not_in_script_context_str), p);
950 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000951 }
952
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200953 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +0000954 lp->ll_type = parse_type(&tp,
955 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
956 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100957 if (lp->ll_type == NULL && !quiet)
958 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200959 lp->ll_name_end = tp;
960 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100961 }
962 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000963 if (lp->ll_name == NULL)
964 return p;
965
Bram Moolenaar62aec932022-01-29 21:45:34 +0000966 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000967 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000968 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000969
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000970 if (import != NULL)
971 {
972 ufunc_T *ufunc;
973 type_T *type;
974
975 lp->ll_sid = import->imp_sid;
976 lp->ll_name = skipwhite(p + 1);
977 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
978 lp->ll_name_end = p;
979
980 // check the item is exported
981 cc = *p;
982 *p = NUL;
983 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000984 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000985 {
986 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +0000987 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000988 }
989 *p = cc;
990 }
991 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100992
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100993 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000994 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000995 return p;
996
Bram Moolenaar4525a572022-02-13 11:57:33 +0000997 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200998 {
999 // using local variable
1000 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001001 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001002 }
1003 else
1004 {
1005 cc = *p;
1006 *p = NUL;
1007 // When we would write to the variable pass &ht and prevent autoload.
1008 writing = !(flags & GLV_READ_ONLY);
1009 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001010 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001011 if (v == NULL && !quiet)
1012 semsg(_(e_undefined_variable_str), lp->ll_name);
1013 *p = cc;
1014 if (v == NULL)
1015 return NULL;
1016 lp->ll_tv = &v->di_tv;
1017 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001018
Bram Moolenaar4525a572022-02-13 11:57:33 +00001019 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001020 {
1021 if (!quiet)
1022 semsg(_(e_variable_already_declared), lp->ll_name);
1023 return NULL;
1024 }
1025
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001026 /*
1027 * Loop until no more [idx] or .key is following.
1028 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001029 var1.v_type = VAR_UNKNOWN;
1030 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001031 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001032 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001033 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1034 {
1035 if (!quiet)
1036 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1037 return NULL;
1038 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001039 if (lp->ll_tv->v_type != VAR_LIST
1040 && lp->ll_tv->v_type != VAR_DICT
1041 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001042 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001043 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001044 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001045 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001046 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001047
1048 // a NULL list/blob works like an empty list/blob, allocate one now.
1049 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1050 rettv_list_alloc(lp->ll_tv);
1051 else if (lp->ll_tv->v_type == VAR_BLOB
1052 && lp->ll_tv->vval.v_blob == NULL)
1053 rettv_blob_alloc(lp->ll_tv);
1054
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001055 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001056 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001057 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001058 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001059 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001060 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001061
Bram Moolenaar4525a572022-02-13 11:57:33 +00001062 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001063 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001064 && lp->ll_tv == &v->di_tv
1065 && ht != NULL && ht == get_script_local_ht())
1066 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001067 svar_T *sv = find_typval_in_script(lp->ll_tv, 0);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001068
1069 // Vim9 script local variable: get the type
1070 if (sv != NULL)
1071 lp->ll_valtype = sv->sv_type;
1072 }
1073
Bram Moolenaar8c711452005-01-14 21:53:12 +00001074 len = -1;
1075 if (*p == '.')
1076 {
1077 key = p + 1;
1078 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1079 ;
1080 if (len == 0)
1081 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001082 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001083 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001084 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001085 }
1086 p = key + len;
1087 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001088 else
1089 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001090 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001091 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001092 if (*p == ':')
1093 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001094 else
1095 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001096 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001097 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001098 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001099 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001100 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001101 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001102 clear_tv(&var1);
1103 return NULL;
1104 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001105 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001106 }
1107
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001108 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001109 if (*p == ':')
1110 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001111 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001112 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001113 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001114 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001115 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001116 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001117 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001118 if (rettv != NULL
1119 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001120 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001121 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001122 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001123 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001124 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001125 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001126 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001127 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001128 }
1129 p = skipwhite(p + 1);
1130 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001131 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001132 else
1133 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001134 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001135 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001136 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001137 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001138 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001139 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001140 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001141 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001142 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001143 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001144 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001145 clear_tv(&var2);
1146 return NULL;
1147 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001148 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001149 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001150 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001151 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001152 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001153
Bram Moolenaar8c711452005-01-14 21:53:12 +00001154 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001155 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001156 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001157 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001158 clear_tv(&var1);
1159 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001160 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001161 }
1162
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001163 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001164 ++p;
1165 }
1166
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001167 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001168 {
1169 if (len == -1)
1170 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001171 // "[key]": get key from "var1"
1172 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001173 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001174 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001175 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001176 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001177 }
1178 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001179 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001180
1181 // a NULL dict is equivalent with an empty dict
1182 if (lp->ll_tv->vval.v_dict == NULL)
1183 {
1184 lp->ll_tv->vval.v_dict = dict_alloc();
1185 if (lp->ll_tv->vval.v_dict == NULL)
1186 {
1187 clear_tv(&var1);
1188 return NULL;
1189 }
1190 ++lp->ll_tv->vval.v_dict->dv_refcount;
1191 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001192 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001193
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001194 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001195
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001196 // When assigning to a scope dictionary check that a function and
1197 // variable name is valid (only variable name unless it is l: or
1198 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001199 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001200 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001201 int prevval;
1202 int wrong;
1203
1204 if (len != -1)
1205 {
1206 prevval = key[len];
1207 key[len] = NUL;
1208 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001209 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001210 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001211 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1212 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001213 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001214 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001215 if (len != -1)
1216 key[len] = prevval;
1217 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001218 {
1219 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001220 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001221 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001222 }
1223
Bram Moolenaar10c65862020-10-08 21:16:42 +02001224 if (lp->ll_valtype != NULL)
1225 // use the type of the member
1226 lp->ll_valtype = lp->ll_valtype->tt_member;
1227
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001228 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001229 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001230 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001231 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001232 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001233 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001234 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001235 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001236 return NULL;
1237 }
1238
Bram Moolenaar31b81602019-02-10 22:14:27 +01001239 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001240 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001241 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001242 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001243 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001244 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001245 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001246 }
1247 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001248 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001249 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001250 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001251 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001252 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001253 p = NULL;
1254 break;
1255 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001256 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001257 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001258 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1259 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001260 {
1261 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001262 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001263 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001264
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001265 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001266 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001267 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001268 else if (lp->ll_tv->v_type == VAR_BLOB)
1269 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001270 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1271
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001272 /*
1273 * Get the number and item for the only or first index of the List.
1274 */
1275 if (empty1)
1276 lp->ll_n1 = 0;
1277 else
1278 // is number or string
1279 lp->ll_n1 = (long)tv_get_number(&var1);
1280 clear_tv(&var1);
1281
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001282 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001283 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001284 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001285 return NULL;
1286 }
1287 if (lp->ll_range && !lp->ll_empty2)
1288 {
1289 lp->ll_n2 = (long)tv_get_number(&var2);
1290 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001291 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1292 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001293 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001294 }
1295 lp->ll_blob = lp->ll_tv->vval.v_blob;
1296 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001297 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001298 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001299 else
1300 {
1301 /*
1302 * Get the number and item for the only or first index of the List.
1303 */
1304 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001305 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001306 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001307 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001308 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001309 clear_tv(&var1);
1310
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001311 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001312 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001313 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001314 if (lp->ll_li == NULL)
1315 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001316 clear_tv(&var2);
1317 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001318 }
1319
Bram Moolenaar10c65862020-10-08 21:16:42 +02001320 if (lp->ll_valtype != NULL)
1321 // use the type of the member
1322 lp->ll_valtype = lp->ll_valtype->tt_member;
1323
Bram Moolenaar8c711452005-01-14 21:53:12 +00001324 /*
1325 * May need to find the item or absolute index for the second
1326 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001327 * When no index given: "lp->ll_empty2" is TRUE.
1328 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001329 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001330 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001331 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001332 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001333 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001334 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001335 if (check_range_index_two(lp->ll_list,
1336 &lp->ll_n1, lp->ll_li,
1337 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001338 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001339 }
1340
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001341 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001342 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001343 }
1344
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001345 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001346 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001347 return p;
1348}
1349
1350/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001351 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001352 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001353 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001354clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001355{
1356 vim_free(lp->ll_exp_name);
1357 vim_free(lp->ll_newkey);
1358}
1359
1360/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001361 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001362 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001363 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1364 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001365 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001366 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001367set_var_lval(
1368 lval_T *lp,
1369 char_u *endp,
1370 typval_T *rettv,
1371 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001372 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1373 char_u *op,
1374 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001375{
1376 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001377 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001378
1379 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001380 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001381 cc = *endp;
1382 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001383 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1384 return;
1385
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001386 if (lp->ll_blob != NULL)
1387 {
1388 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001389
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001390 if (op != NULL && *op != '=')
1391 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001392 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001393 return;
1394 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001395 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001396 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001397
1398 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1399 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001400 if (lp->ll_empty2)
1401 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1402
Bram Moolenaar68452172021-04-12 21:21:02 +02001403 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1404 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001405 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001406 }
1407 else
1408 {
1409 val = (int)tv_get_number_chk(rettv, &error);
1410 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001411 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001412 }
1413 }
1414 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001415 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001416 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001417
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001418 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1419 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001420 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001421 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001422 *endp = cc;
1423 return;
1424 }
1425
Bram Moolenaarff697e62019-02-12 22:28:33 +01001426 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001427 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001428 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001429 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001430 {
1431 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001432 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1433 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001434 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001435 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001436 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001437 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001438 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001439 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001440 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001441 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001442 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1443 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001444 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001445 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001446 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001447 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001448 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001449 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001450 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001451 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001452 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001453 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001454 else if (lp->ll_range)
1455 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001456 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1457 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001458 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001459 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001460 return;
1461 }
1462
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001463 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1464 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001465 }
1466 else
1467 {
1468 /*
1469 * Assign to a List or Dictionary item.
1470 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001471 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1472 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001473 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001474 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001475 return;
1476 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001477
1478 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001479 && check_typval_arg_type(lp->ll_valtype, rettv,
1480 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001481 return;
1482
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001483 if (lp->ll_newkey != NULL)
1484 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001485 if (op != NULL && *op != '=')
1486 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001487 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001488 return;
1489 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001490 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1491 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001492 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001493
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001494 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001495 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001496 if (di == NULL)
1497 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001498 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1499 {
1500 vim_free(di);
1501 return;
1502 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001503 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001504 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001505 else if (op != NULL && *op != '=')
1506 {
1507 tv_op(lp->ll_tv, rettv, op);
1508 return;
1509 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001510 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001511 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001512
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001513 /*
1514 * Assign the value to the variable or list item.
1515 */
1516 if (copy)
1517 copy_tv(rettv, lp->ll_tv);
1518 else
1519 {
1520 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001521 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001522 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001523 }
1524 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001525}
1526
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001527/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001528 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1529 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001530 * Returns OK or FAIL.
1531 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001532 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001533tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001534{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001535 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001536 char_u numbuf[NUMBUFLEN];
1537 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001538 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001539
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001540 // Can't do anything with a Funcref or Dict on the right.
1541 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001542 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001543 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1544 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001545 {
1546 switch (tv1->v_type)
1547 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001548 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001549 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001550 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001551 case VAR_DICT:
1552 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001553 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001554 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001555 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001556 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001557 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001558 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001559 break;
1560
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001561 case VAR_BLOB:
1562 if (*op != '+' || tv2->v_type != VAR_BLOB)
1563 break;
1564 // BLOB += BLOB
1565 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1566 {
1567 blob_T *b1 = tv1->vval.v_blob;
1568 blob_T *b2 = tv2->vval.v_blob;
1569 int i, len = blob_len(b2);
1570 for (i = 0; i < len; i++)
1571 ga_append(&b1->bv_ga, blob_get(b2, i));
1572 }
1573 return OK;
1574
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001575 case VAR_LIST:
1576 if (*op != '+' || tv2->v_type != VAR_LIST)
1577 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001578 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001579 if (tv2->vval.v_list != NULL)
1580 {
1581 if (tv1->vval.v_list == NULL)
1582 {
1583 tv1->vval.v_list = tv2->vval.v_list;
1584 ++tv1->vval.v_list->lv_refcount;
1585 }
1586 else
1587 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1588 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001589 return OK;
1590
1591 case VAR_NUMBER:
1592 case VAR_STRING:
1593 if (tv2->v_type == VAR_LIST)
1594 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001595 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001596 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001597 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001598 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001599#ifdef FEAT_FLOAT
1600 if (tv2->v_type == VAR_FLOAT)
1601 {
1602 float_T f = n;
1603
Bram Moolenaarff697e62019-02-12 22:28:33 +01001604 if (*op == '%')
1605 break;
1606 switch (*op)
1607 {
1608 case '+': f += tv2->vval.v_float; break;
1609 case '-': f -= tv2->vval.v_float; break;
1610 case '*': f *= tv2->vval.v_float; break;
1611 case '/': f /= tv2->vval.v_float; break;
1612 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001613 clear_tv(tv1);
1614 tv1->v_type = VAR_FLOAT;
1615 tv1->vval.v_float = f;
1616 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001617 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001618#endif
1619 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001620 switch (*op)
1621 {
1622 case '+': n += tv_get_number(tv2); break;
1623 case '-': n -= tv_get_number(tv2); break;
1624 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001625 case '/': n = num_divide(n, tv_get_number(tv2),
1626 &failed); break;
1627 case '%': n = num_modulus(n, tv_get_number(tv2),
1628 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001629 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001630 clear_tv(tv1);
1631 tv1->v_type = VAR_NUMBER;
1632 tv1->vval.v_number = n;
1633 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001634 }
1635 else
1636 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001637 if (tv2->v_type == VAR_FLOAT)
1638 break;
1639
Bram Moolenaarff697e62019-02-12 22:28:33 +01001640 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001641 s = tv_get_string(tv1);
1642 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001643 clear_tv(tv1);
1644 tv1->v_type = VAR_STRING;
1645 tv1->vval.v_string = s;
1646 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001647 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001648
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001649 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001650#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001651 {
1652 float_T f;
1653
Bram Moolenaarff697e62019-02-12 22:28:33 +01001654 if (*op == '%' || *op == '.'
1655 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001656 && tv2->v_type != VAR_NUMBER
1657 && tv2->v_type != VAR_STRING))
1658 break;
1659 if (tv2->v_type == VAR_FLOAT)
1660 f = tv2->vval.v_float;
1661 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001662 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001663 switch (*op)
1664 {
1665 case '+': tv1->vval.v_float += f; break;
1666 case '-': tv1->vval.v_float -= f; break;
1667 case '*': tv1->vval.v_float *= f; break;
1668 case '/': tv1->vval.v_float /= f; break;
1669 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001670 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001671#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001672 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001673 }
1674 }
1675
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001676 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001677 return FAIL;
1678}
1679
1680/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001681 * Evaluate the expression used in a ":for var in expr" command.
1682 * "arg" points to "var".
1683 * Set "*errp" to TRUE for an error, FALSE otherwise;
1684 * Return a pointer that holds the info. Null when there is an error.
1685 */
1686 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001687eval_for_line(
1688 char_u *arg,
1689 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001690 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001691 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001692{
Bram Moolenaar33570922005-01-25 22:26:29 +00001693 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001694 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001695 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001696 typval_T tv;
1697 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001698 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001699
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001700 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001701
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001702 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001703 if (fi == NULL)
1704 return NULL;
1705
Bram Moolenaar404557e2021-07-05 21:41:48 +02001706 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1707 &fi->fi_semicolon, FALSE);
1708 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001709 return fi;
1710
Bram Moolenaar404557e2021-07-05 21:41:48 +02001711 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001712 if (expr[0] != 'i' || expr[1] != 'n'
1713 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001714 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001715 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1716 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1717 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001718 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001719 return fi;
1720 }
1721
1722 if (skip)
1723 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001724 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1725 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001726 {
1727 *errp = FALSE;
1728 if (!skip)
1729 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001730 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001731 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001732 l = tv.vval.v_list;
1733 if (l == NULL)
1734 {
1735 // a null list is like an empty list: do nothing
1736 clear_tv(&tv);
1737 }
1738 else
1739 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001740 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001741 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001743 // No need to increment the refcount, it's already set for
1744 // the list being used in "tv".
1745 fi->fi_list = l;
1746 list_add_watch(l, &fi->fi_lw);
1747 fi->fi_lw.lw_item = l->lv_first;
1748 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001749 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001750 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001751 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001752 fi->fi_bi = 0;
1753 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001754 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001755 typval_T btv;
1756
1757 // Make a copy, so that the iteration still works when the
1758 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001759 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001760 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001761 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001762 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001763 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001764 else if (tv.v_type == VAR_STRING)
1765 {
1766 fi->fi_byte_idx = 0;
1767 fi->fi_string = tv.vval.v_string;
1768 tv.vval.v_string = NULL;
1769 if (fi->fi_string == NULL)
1770 fi->fi_string = vim_strsave((char_u *)"");
1771 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001772 else
1773 {
Sean Dewar80d73952021-08-04 19:25:54 +02001774 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001775 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001776 }
1777 }
1778 }
1779 if (skip)
1780 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001781 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001782
1783 return fi;
1784}
1785
1786/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001787 * Used when looping over a :for line, skip the "in expr" part.
1788 */
1789 void
1790skip_for_lines(void *fi_void, evalarg_T *evalarg)
1791{
1792 forinfo_T *fi = (forinfo_T *)fi_void;
1793 int i;
1794
1795 for (i = 0; i < fi->fi_break_count; ++i)
1796 eval_next_line(evalarg);
1797}
1798
1799/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001800 * Use the first item in a ":for" list. Advance to the next.
1801 * Assign the values to the variable (list). "arg" points to the first one.
1802 * Return TRUE when a valid item was found, FALSE when at end of list or
1803 * something wrong.
1804 */
1805 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001806next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001807{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001808 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001809 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001810 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001811 ? (ASSIGN_FINAL
1812 // first round: error if variable exists
1813 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1814 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001815 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001816 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001817 int skip_assign = in_vim9script() && arg[0] == '_'
1818 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001819
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001820 if (fi->fi_blob != NULL)
1821 {
1822 typval_T tv;
1823
1824 if (fi->fi_bi >= blob_len(fi->fi_blob))
1825 return FALSE;
1826 tv.v_type = VAR_NUMBER;
1827 tv.v_lock = VAR_FIXED;
1828 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1829 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001830 if (skip_assign)
1831 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001832 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001833 fi->fi_varcount, flag, NULL) == OK;
1834 }
1835
1836 if (fi->fi_string != NULL)
1837 {
1838 typval_T tv;
1839 int len;
1840
1841 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1842 if (len == 0)
1843 return FALSE;
1844 tv.v_type = VAR_STRING;
1845 tv.v_lock = VAR_FIXED;
1846 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1847 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001848 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001849 if (skip_assign)
1850 result = TRUE;
1851 else
1852 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001853 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001854 vim_free(tv.vval.v_string);
1855 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001856 }
1857
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001858 item = fi->fi_lw.lw_item;
1859 if (item == NULL)
1860 result = FALSE;
1861 else
1862 {
1863 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001864 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001865 if (skip_assign)
1866 result = TRUE;
1867 else
1868 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001869 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001870 }
1871 return result;
1872}
1873
1874/*
1875 * Free the structure used to store info used by ":for".
1876 */
1877 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001878free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001879{
Bram Moolenaar33570922005-01-25 22:26:29 +00001880 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001881
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001882 if (fi == NULL)
1883 return;
1884 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001885 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001886 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001887 list_unref(fi->fi_list);
1888 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001889 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001890 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001891 else
1892 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001893 vim_free(fi);
1894}
1895
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001897set_context_for_expression(
1898 expand_T *xp,
1899 char_u *arg,
1900 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001902 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001904 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001906 if (cmdidx == CMD_let || cmdidx == CMD_var
1907 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001908 {
1909 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001910 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001911 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001912 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001913 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001914 {
1915 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001916 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001917 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001918 break;
1919 }
1920 return;
1921 }
1922 }
1923 else
1924 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1925 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 while ((xp->xp_pattern = vim_strpbrk(arg,
1927 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1928 {
1929 c = *xp->xp_pattern;
1930 if (c == '&')
1931 {
1932 c = xp->xp_pattern[1];
1933 if (c == '&')
1934 {
1935 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001936 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 }
1938 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001939 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001941 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1942 xp->xp_pattern += 2;
1943
1944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 }
1946 else if (c == '$')
1947 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001948 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 xp->xp_context = EXPAND_ENV_VARS;
1950 }
1951 else if (c == '=')
1952 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001953 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 xp->xp_context = EXPAND_EXPRESSION;
1955 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001956 else if (c == '#'
1957 && xp->xp_context == EXPAND_EXPRESSION)
1958 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001959 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001960 break;
1961 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001962 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 && xp->xp_context == EXPAND_FUNCTIONS
1964 && vim_strchr(xp->xp_pattern, '(') == NULL)
1965 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001966 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 break;
1968 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001969 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001971 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 {
1973 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1974 if (c == '\\' && xp->xp_pattern[1] != NUL)
1975 ++xp->xp_pattern;
1976 xp->xp_context = EXPAND_NOTHING;
1977 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001978 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001980 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1982 /* skip */ ;
1983 xp->xp_context = EXPAND_NOTHING;
1984 }
1985 else if (c == '|')
1986 {
1987 if (xp->xp_pattern[1] == '|')
1988 {
1989 ++xp->xp_pattern;
1990 xp->xp_context = EXPAND_EXPRESSION;
1991 }
1992 else
1993 xp->xp_context = EXPAND_COMMANDS;
1994 }
1995 else
1996 xp->xp_context = EXPAND_EXPRESSION;
1997 }
1998 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001999 // Doesn't look like something valid, expand as an expression
2000 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002001 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 arg = xp->xp_pattern;
2003 if (*arg != NUL)
2004 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2005 /* skip */ ;
2006 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002007
2008 // ":exe one two" completes "two"
2009 if ((cmdidx == CMD_execute
2010 || cmdidx == CMD_echo
2011 || cmdidx == CMD_echon
2012 || cmdidx == CMD_echomsg)
2013 && xp->xp_context == EXPAND_EXPRESSION)
2014 {
2015 for (;;)
2016 {
2017 char_u *n = skiptowhite(arg);
2018
2019 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2020 break;
2021 arg = skipwhite(n);
2022 }
2023 }
2024
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 xp->xp_pattern = arg;
2026}
2027
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002029 * Return TRUE if "pat" matches "text".
2030 * Does not use 'cpo' and always uses 'magic'.
2031 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002032 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002033pattern_match(char_u *pat, char_u *text, int ic)
2034{
2035 int matches = FALSE;
2036 char_u *save_cpo;
2037 regmatch_T regmatch;
2038
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002039 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002040 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002041 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002042 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2043 if (regmatch.regprog != NULL)
2044 {
2045 regmatch.rm_ic = ic;
2046 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2047 vim_regfree(regmatch.regprog);
2048 }
2049 p_cpo = save_cpo;
2050 return matches;
2051}
2052
2053/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002054 * Handle a name followed by "(". Both for just "name(arg)" and for
2055 * "expr->name(arg)".
2056 * Returns OK or FAIL.
2057 */
2058 static int
2059eval_func(
2060 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002061 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002062 char_u *name,
2063 int name_len,
2064 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002065 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002066 typval_T *basetv) // "expr" for "expr->name(arg)"
2067{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002068 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002069 char_u *s = name;
2070 int len = name_len;
2071 partial_T *partial;
2072 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002073 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002074 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002075
2076 if (!evaluate)
2077 check_vars(s, len);
2078
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002079 // If "s" is the name of a variable of type VAR_FUNC
2080 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002081 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002082 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002083
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002084 // Need to make a copy, in case evaluating the arguments makes
2085 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002086 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002087 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002088 ret = FAIL;
2089 else
2090 {
2091 funcexe_T funcexe;
2092
2093 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002094 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002095 funcexe.fe_firstline = curwin->w_cursor.lnum;
2096 funcexe.fe_lastline = curwin->w_cursor.lnum;
2097 funcexe.fe_evaluate = evaluate;
2098 funcexe.fe_partial = partial;
2099 funcexe.fe_basetv = basetv;
2100 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002101 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002102 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002103 }
2104 vim_free(s);
2105
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002106 // If evaluate is FALSE rettv->v_type was not set in
2107 // get_func_tv, but it's needed in handle_subscript() to parse
2108 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002109 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2110 {
2111 rettv->vval.v_string = NULL;
2112 rettv->v_type = VAR_FUNC;
2113 }
2114
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002115 // Stop the expression evaluation when immediately
2116 // aborting on error, or when an interrupt occurred or
2117 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002118 if (evaluate && aborting())
2119 {
2120 if (ret == OK)
2121 clear_tv(rettv);
2122 ret = FAIL;
2123 }
2124 return ret;
2125}
2126
2127/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002128 * Get the next line source line without advancing. But do skip over comment
2129 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002130 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002131 */
2132 static char_u *
2133getline_peek_skip_comments(evalarg_T *evalarg)
2134{
2135 for (;;)
2136 {
2137 char_u *next = getline_peek(evalarg->eval_getline,
2138 evalarg->eval_cookie);
2139 char_u *p;
2140
2141 if (next == NULL)
2142 break;
2143 p = skipwhite(next);
2144 if (*p != NUL && !vim9_comment_start(p))
2145 return next;
2146 (void)eval_next_line(evalarg);
2147 }
2148 return NULL;
2149}
2150
2151/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002152 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2153 * comment) and there is a next line, return the next line (skipping blanks)
2154 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002155 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2156 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002157 * "arg" must point somewhere inside a line, not at the start.
2158 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002159 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002160eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2161{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002162 char_u *p = skipwhite(arg);
2163
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002164 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002165 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002166 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002167 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002168 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002169 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002170 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002171
2172 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002173 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002174 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002175 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002176
Bram Moolenaar9d489562020-07-30 20:08:50 +02002177 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002178 {
2179 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002180 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002181 }
2182 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002183 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002184}
2185
2186/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002187 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002188 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002189 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002190 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002191eval_next_line(evalarg_T *evalarg)
2192{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002193 garray_T *gap = &evalarg->eval_ga;
2194 char_u *line;
2195
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002196 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002197 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2198 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002199 else
2200 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002201 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002202 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2203 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002204 char_u *p = skipwhite(line);
2205
2206 // Going to concatenate the lines after parsing. For an empty or
2207 // comment line use an empty string.
2208 if (*p == NUL || vim9_comment_start(p))
2209 {
2210 vim_free(line);
2211 line = vim_strsave((char_u *)"");
2212 }
2213
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002214 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2215 ++gap->ga_len;
2216 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002217 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002218 {
2219 vim_free(evalarg->eval_tofree);
2220 evalarg->eval_tofree = line;
2221 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002222
2223 // Advanced to the next line, "arg" no longer points into the previous
2224 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002225 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002226 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002227}
2228
Bram Moolenaare6b53242020-07-01 17:28:33 +02002229/*
2230 * Call eval_next_non_blank() and get the next line if needed.
2231 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002232 char_u *
2233skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2234{
2235 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002236 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002237
Bram Moolenaar962d7212020-07-04 14:15:00 +02002238 if (evalarg == NULL)
2239 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002240 eval_next_non_blank(p, evalarg, &getnext);
2241 if (getnext)
2242 return eval_next_line(evalarg);
2243 return p;
2244}
2245
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002246/*
Bram Moolenaar844fb642021-10-23 13:32:30 +01002247 * Initialize "evalarg" for use.
2248 */
2249 void
2250init_evalarg(evalarg_T *evalarg)
2251{
2252 CLEAR_POINTER(evalarg);
2253 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
2254}
2255
2256/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002257 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002258 */
2259 void
2260clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2261{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002262 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002263 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002264 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002265 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002266 if (eap != NULL)
2267 {
2268 // We may need to keep the original command line, e.g. for
2269 // ":let" it has the variable names. But we may also need the
2270 // new one, "nextcmd" points into it. Keep both.
2271 vim_free(eap->cmdline_tofree);
2272 eap->cmdline_tofree = *eap->cmdlinep;
2273 *eap->cmdlinep = evalarg->eval_tofree;
2274 }
2275 else
2276 vim_free(evalarg->eval_tofree);
2277 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002278 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002279
Bram Moolenaar844fb642021-10-23 13:32:30 +01002280 ga_clear_strings(&evalarg->eval_tofree_ga);
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002281 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002282 }
2283}
2284
2285/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002287 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2289 */
2290
2291/*
2292 * Handle zero level expression.
2293 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002294 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002295 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002296 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 * Return OK or FAIL.
2298 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002299 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002300eval0(
2301 char_u *arg,
2302 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002303 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002304 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002306 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2307}
2308
2309/*
2310 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2311 * expression and don't check what comes after the expression.
2312 */
2313 int
2314eval0_retarg(
2315 char_u *arg,
2316 typval_T *rettv,
2317 exarg_T *eap,
2318 evalarg_T *evalarg,
2319 char_u **retarg)
2320{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 int ret;
2322 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002323 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002324 int did_emsg_before = did_emsg;
2325 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002326 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002327 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002328 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329
2330 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002331 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002332 expr_end = p;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002333 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002334
Bram Moolenaar02929a32021-12-17 14:46:12 +00002335 // In Vim9 script a command block is not split at NL characters for
2336 // commands using an expression argument. Skip over a '#' comment to check
2337 // for a following NL. Require white space before the '#'.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002338 if (in_vim9script() && p > expr_end && retarg == NULL)
Bram Moolenaar02929a32021-12-17 14:46:12 +00002339 while (*p == '#')
2340 {
2341 char_u *nl = vim_strchr(p, NL);
2342
2343 if (nl == NULL)
2344 break;
2345 p = skipwhite(nl + 1);
2346 if (eap != NULL && *p != NUL)
2347 eap->nextcmd = p;
2348 check_for_end = FALSE;
2349 }
2350
2351 if (ret != FAIL && check_for_end)
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002352 end_error = !ends_excmd2(arg, p);
2353 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 {
2355 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002356 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 /*
2358 * Report the invalid expression unless the expression evaluation has
2359 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002360 * exception, or we already gave a more specific error.
2361 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002363 if (!aborting()
2364 && did_emsg == did_emsg_before
2365 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002366 && (flags & EVAL_CONSTANT) == 0
2367 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002368 {
2369 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002370 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002371 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002372 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002373 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002374
2375 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002376 // a next command to avoid more errors, unless "|" is following, which
2377 // could only be a command separator.
2378 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2379 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002380 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002382
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002383 if (retarg != NULL)
2384 *retarg = p;
2385 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002386 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002387
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 return ret;
2389}
2390
2391/*
2392 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002393 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002394 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 *
2396 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002397 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002399 * Note: "rettv.v_lock" is not set.
2400 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 * Return OK or FAIL.
2402 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002403 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002404eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002406 char_u *p;
2407 int getnext;
2408
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002409 CLEAR_POINTER(rettv);
2410
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 /*
2412 * Get the first variable.
2413 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002414 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 return FAIL;
2416
Bram Moolenaar793648f2020-06-26 21:28:25 +02002417 p = eval_next_non_blank(*arg, evalarg, &getnext);
2418 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002420 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002421 int result;
2422 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002423 evalarg_T *evalarg_used = evalarg;
2424 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002425 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002426 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002427 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002428
2429 if (evalarg == NULL)
2430 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002431 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002432 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002433 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002434 orig_flags = evalarg_used->eval_flags;
2435 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002436
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002437 if (getnext)
2438 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002439 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002440 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002441 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002442 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002443 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002444 clear_tv(rettv);
2445 return FAIL;
2446 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002447 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002448 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002449
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002451 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002453 int error = FALSE;
2454
Bram Moolenaar13106602020-10-04 16:06:05 +02002455 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002456 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002457 else if (vim9script)
2458 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002459 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002461 if (error || !op_falsy || !result)
2462 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002463 if (error)
2464 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 }
2466
2467 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002468 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002470 if (op_falsy)
2471 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002472 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002473 {
mityu4ac198c2021-05-28 17:52:40 +02002474 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002475 clear_tv(rettv);
2476 return FAIL;
2477 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002478 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002479 evalarg_used->eval_flags = (op_falsy ? !result : result)
2480 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2481 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002482 {
2483 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002485 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002486 if (!op_falsy || !result)
2487 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002489 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002491 /*
2492 * Check for the ":".
2493 */
2494 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2495 if (*p != ':')
2496 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002497 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002498 if (evaluate && result)
2499 clear_tv(rettv);
2500 evalarg_used->eval_flags = orig_flags;
2501 return FAIL;
2502 }
2503 if (getnext)
2504 *arg = eval_next_line(evalarg_used);
2505 else
2506 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002507 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002508 {
2509 error_white_both(p, 1);
2510 clear_tv(rettv);
2511 evalarg_used->eval_flags = orig_flags;
2512 return FAIL;
2513 }
2514 *arg = p;
2515 }
2516
2517 /*
2518 * Get the third variable. Recursive!
2519 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002520 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002521 {
mityu4ac198c2021-05-28 17:52:40 +02002522 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002523 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002524 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002525 return FAIL;
2526 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002527 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2528 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002529 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002530 if (eval1(arg, &var2, evalarg_used) == FAIL)
2531 {
2532 if (evaluate && result)
2533 clear_tv(rettv);
2534 evalarg_used->eval_flags = orig_flags;
2535 return FAIL;
2536 }
2537 if (evaluate && !result)
2538 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002540
2541 if (evalarg == NULL)
2542 clear_evalarg(&local_evalarg, NULL);
2543 else
2544 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 }
2546
2547 return OK;
2548}
2549
2550/*
2551 * Handle first level expression:
2552 * expr2 || expr2 || expr2 logical OR
2553 *
2554 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002555 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 *
2557 * Return OK or FAIL.
2558 */
2559 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002560eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002562 char_u *p;
2563 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564
2565 /*
2566 * Get the first variable.
2567 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002568 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 return FAIL;
2570
2571 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002572 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002574 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002575 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002577 evalarg_T *evalarg_used = evalarg;
2578 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002579 int evaluate;
2580 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002581 long result = FALSE;
2582 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002583 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002584 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002585
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002586 if (evalarg == NULL)
2587 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002588 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002589 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002590 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002591 orig_flags = evalarg_used->eval_flags;
2592 evaluate = orig_flags & EVAL_EVALUATE;
2593 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002594 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002595 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002596 result = tv_get_bool_chk(rettv, &error);
2597 else if (tv_get_number_chk(rettv, &error) != 0)
2598 result = TRUE;
2599 clear_tv(rettv);
2600 if (error)
2601 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 }
2603
2604 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002605 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002607 while (p[0] == '|' && p[1] == '|')
2608 {
2609 if (getnext)
2610 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002611 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002612 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002613 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002614 {
2615 error_white_both(p, 2);
2616 clear_tv(rettv);
2617 return FAIL;
2618 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002619 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002620 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002621
2622 /*
2623 * Get the second variable.
2624 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002625 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002626 {
mityu4ac198c2021-05-28 17:52:40 +02002627 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002628 clear_tv(rettv);
2629 return FAIL;
2630 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002631 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2632 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002633 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002634 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002635 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002636
2637 /*
2638 * Compute the result.
2639 */
2640 if (evaluate && !result)
2641 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002642 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002643 result = tv_get_bool_chk(&var2, &error);
2644 else if (tv_get_number_chk(&var2, &error) != 0)
2645 result = TRUE;
2646 clear_tv(&var2);
2647 if (error)
2648 return FAIL;
2649 }
2650 if (evaluate)
2651 {
2652 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002653 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002654 rettv->v_type = VAR_BOOL;
2655 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002656 }
2657 else
2658 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002659 rettv->v_type = VAR_NUMBER;
2660 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002661 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002662 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002663
2664 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002666
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002667 if (evalarg == NULL)
2668 clear_evalarg(&local_evalarg, NULL);
2669 else
2670 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 }
2672
2673 return OK;
2674}
2675
2676/*
2677 * Handle second level expression:
2678 * expr3 && expr3 && expr3 logical AND
2679 *
2680 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002681 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 *
2683 * Return OK or FAIL.
2684 */
2685 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002686eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002688 char_u *p;
2689 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690
2691 /*
2692 * Get the first variable.
2693 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002694 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 return FAIL;
2696
2697 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002698 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002700 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002701 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002703 evalarg_T *evalarg_used = evalarg;
2704 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002705 int orig_flags;
2706 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002707 long result = TRUE;
2708 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002709 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002710 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002711
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002712 if (evalarg == NULL)
2713 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002714 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002715 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002716 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002717 orig_flags = evalarg_used->eval_flags;
2718 evaluate = orig_flags & EVAL_EVALUATE;
2719 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002720 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002721 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002722 result = tv_get_bool_chk(rettv, &error);
2723 else if (tv_get_number_chk(rettv, &error) == 0)
2724 result = FALSE;
2725 clear_tv(rettv);
2726 if (error)
2727 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 }
2729
2730 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002731 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002733 while (p[0] == '&' && p[1] == '&')
2734 {
2735 if (getnext)
2736 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002737 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002738 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002739 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002740 {
2741 error_white_both(p, 2);
2742 clear_tv(rettv);
2743 return FAIL;
2744 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002745 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002746 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002747
2748 /*
2749 * Get the second variable.
2750 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002751 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002752 {
mityu4ac198c2021-05-28 17:52:40 +02002753 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002754 clear_tv(rettv);
2755 return FAIL;
2756 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002757 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2758 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002759 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002760 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002761 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002762 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002763
2764 /*
2765 * Compute the result.
2766 */
2767 if (evaluate && result)
2768 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002769 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002770 result = tv_get_bool_chk(&var2, &error);
2771 else if (tv_get_number_chk(&var2, &error) == 0)
2772 result = FALSE;
2773 clear_tv(&var2);
2774 if (error)
2775 return FAIL;
2776 }
2777 if (evaluate)
2778 {
2779 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002780 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002781 rettv->v_type = VAR_BOOL;
2782 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002783 }
2784 else
2785 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002786 rettv->v_type = VAR_NUMBER;
2787 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002788 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002789 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002790
2791 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002793
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002794 if (evalarg == NULL)
2795 clear_evalarg(&local_evalarg, NULL);
2796 else
2797 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 }
2799
2800 return OK;
2801}
2802
2803/*
2804 * Handle third level expression:
2805 * var1 == var2
2806 * var1 =~ var2
2807 * var1 != var2
2808 * var1 !~ var2
2809 * var1 > var2
2810 * var1 >= var2
2811 * var1 < var2
2812 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002813 * var1 is var2
2814 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 *
2816 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002817 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 *
2819 * Return OK or FAIL.
2820 */
2821 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002822eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002825 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002826 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002828 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829
2830 /*
2831 * Get the first variable.
2832 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002833 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 return FAIL;
2835
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002836 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002837 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838
2839 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002840 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002842 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002844 typval_T var2;
2845 int ic;
2846 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002847 int evaluate = evalarg == NULL
2848 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002849 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002850
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002851 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002852 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002853 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002854 p = *arg;
2855 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002856 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2857 {
mityu4ac198c2021-05-28 17:52:40 +02002858 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002859 clear_tv(rettv);
2860 return FAIL;
2861 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002862
Bram Moolenaar696ba232020-07-29 21:20:41 +02002863 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2864 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002865 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002866 clear_tv(rettv);
2867 return FAIL;
2868 }
2869
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002870 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 if (p[len] == '?')
2872 {
2873 ic = TRUE;
2874 ++len;
2875 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002876 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 else if (p[len] == '#')
2878 {
2879 ic = FALSE;
2880 ++len;
2881 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002882 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002884 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885
2886 /*
2887 * Get the second variable.
2888 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002889 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2890 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002891 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002892 clear_tv(rettv);
2893 return FAIL;
2894 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002895 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002896 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002898 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 return FAIL;
2900 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002901 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002902 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002903 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002904
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002905 // use the line of the comparison for messages
2906 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002907 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002908 {
2909 ret = FAIL;
2910 clear_tv(rettv);
2911 }
2912 else
2913 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002914 clear_tv(&var2);
2915 return ret;
2916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 }
2918
2919 return OK;
2920}
2921
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002922/*
2923 * Make a copy of blob "tv1" and append blob "tv2".
2924 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925 void
2926eval_addblob(typval_T *tv1, typval_T *tv2)
2927{
2928 blob_T *b1 = tv1->vval.v_blob;
2929 blob_T *b2 = tv2->vval.v_blob;
2930 blob_T *b = blob_alloc();
2931 int i;
2932
2933 if (b != NULL)
2934 {
2935 for (i = 0; i < blob_len(b1); i++)
2936 ga_append(&b->bv_ga, blob_get(b1, i));
2937 for (i = 0; i < blob_len(b2); i++)
2938 ga_append(&b->bv_ga, blob_get(b2, i));
2939
2940 clear_tv(tv1);
2941 rettv_blob_set(tv1, b);
2942 }
2943}
2944
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002945/*
2946 * Make a copy of list "tv1" and append list "tv2".
2947 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 int
2949eval_addlist(typval_T *tv1, typval_T *tv2)
2950{
2951 typval_T var3;
2952
2953 // concatenate Lists
2954 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2955 {
2956 clear_tv(tv1);
2957 clear_tv(tv2);
2958 return FAIL;
2959 }
2960 clear_tv(tv1);
2961 *tv1 = var3;
2962 return OK;
2963}
2964
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965/*
2966 * Handle fourth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00002967 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002969 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002970 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 *
2972 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002973 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 *
2975 * Return OK or FAIL.
2976 */
2977 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002978eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 /*
2981 * Get the first variable.
2982 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002983 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 return FAIL;
2985
2986 /*
2987 * Repeat computing, until no '+', '-' or '.' is following.
2988 */
2989 for (;;)
2990 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002991 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002992 int getnext;
2993 char_u *p;
2994 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002995 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002996 int concat;
2997 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002998 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002999
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003000 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003001 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003002 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003003 p = eval_next_non_blank(*arg, evalarg, &getnext);
3004 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003005 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003006 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3007 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003009 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3010 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003011
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003012 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003013 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003014 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003015 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003016 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003017 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003018 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003019 {
mityu4ac198c2021-05-28 17:52:40 +02003020 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003021 clear_tv(rettv);
3022 return FAIL;
3023 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003024 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003025 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003026 if ((op != '+' || (rettv->v_type != VAR_LIST
3027 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003028#ifdef FEAT_FLOAT
3029 && (op == '.' || rettv->v_type != VAR_FLOAT)
3030#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003031 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003032 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003033 int error = FALSE;
3034
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003035 // For "list + ...", an illegal use of the first operand as
3036 // a number cannot be determined before evaluating the 2nd
3037 // operand: if this is also a list, all is ok.
3038 // For "something . ...", "something - ..." or "non-list + ...",
3039 // we know that the first operand needs to be a string or number
3040 // without evaluating the 2nd operand. So check before to avoid
3041 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003042 if (op != '.')
3043 tv_get_number_chk(rettv, &error);
3044 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003045 {
3046 clear_tv(rettv);
3047 return FAIL;
3048 }
3049 }
3050
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 /*
3052 * Get the second variable.
3053 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003054 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003055 {
mityu89dcb4d2021-05-28 13:50:17 +02003056 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003057 clear_tv(rettv);
3058 return FAIL;
3059 }
3060 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02003061 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003063 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 return FAIL;
3065 }
3066
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003067 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 {
3069 /*
3070 * Compute the result.
3071 */
3072 if (op == '.')
3073 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003074 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3075 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003076 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003077
Bram Moolenaar2e086612020-08-25 22:37:48 +02003078 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003079 || var2.v_type == VAR_CHANNEL
3080 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003081 semsg(_(e_using_invalid_value_as_string_str),
3082 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003083#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003084 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003085 {
3086 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3087 var2.vval.v_float);
3088 s2 = buf2;
3089 }
3090#endif
3091 else
3092 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003093 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003094 {
3095 clear_tv(rettv);
3096 clear_tv(&var2);
3097 return FAIL;
3098 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003099 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003100 clear_tv(rettv);
3101 rettv->v_type = VAR_STRING;
3102 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003104 else if (op == '+' && rettv->v_type == VAR_BLOB
3105 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003107 else if (op == '+' && rettv->v_type == VAR_LIST
3108 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003109 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003111 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 else
3114 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003115 int error = FALSE;
3116 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003117#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003118 float_T f1 = 0, f2 = 0;
3119
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003120 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003121 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003122 f1 = rettv->vval.v_float;
3123 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003124 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003125 else
3126#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003127 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003128 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003129 if (error)
3130 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003131 // This can only happen for "list + non-list" or
3132 // "blob + non-blob". For "non-list + ..." or
3133 // "something - ...", we returned before evaluating the
3134 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003135 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003136 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003137 return FAIL;
3138 }
3139#ifdef FEAT_FLOAT
3140 if (var2.v_type == VAR_FLOAT)
3141 f1 = n1;
3142#endif
3143 }
3144#ifdef FEAT_FLOAT
3145 if (var2.v_type == VAR_FLOAT)
3146 {
3147 f2 = var2.vval.v_float;
3148 n2 = 0;
3149 }
3150 else
3151#endif
3152 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003153 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003154 if (error)
3155 {
3156 clear_tv(rettv);
3157 clear_tv(&var2);
3158 return FAIL;
3159 }
3160#ifdef FEAT_FLOAT
3161 if (rettv->v_type == VAR_FLOAT)
3162 f2 = n2;
3163#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003164 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003165 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003166
3167#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003168 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003169 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3170 {
3171 if (op == '+')
3172 f1 = f1 + f2;
3173 else
3174 f1 = f1 - f2;
3175 rettv->v_type = VAR_FLOAT;
3176 rettv->vval.v_float = f1;
3177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003179#endif
3180 {
3181 if (op == '+')
3182 n1 = n1 + n2;
3183 else
3184 n1 = n1 - n2;
3185 rettv->v_type = VAR_NUMBER;
3186 rettv->vval.v_number = n1;
3187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003189 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 }
3191 }
3192 return OK;
3193}
3194
3195/*
3196 * Handle fifth level expression:
3197 * * number multiplication
3198 * / number division
3199 * % number modulo
3200 *
3201 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003202 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 *
3204 * Return OK or FAIL.
3205 */
3206 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003207eval6(
3208 char_u **arg,
3209 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003210 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003211 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003213#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003214 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216
3217 /*
3218 * Get the first variable.
3219 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003220 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 return FAIL;
3222
3223 /*
3224 * Repeat computing, until no '*', '/' or '%' is following.
3225 */
3226 for (;;)
3227 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003228 int evaluate;
3229 int getnext;
3230 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003231 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003232 int op;
3233 varnumber_T n1, n2;
3234#ifdef FEAT_FLOAT
3235 float_T f1, f2;
3236#endif
3237 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003238
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003239 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003240 p = eval_next_non_blank(*arg, evalarg, &getnext);
3241 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003242 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003244
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003245 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003246 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003247 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003248 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003249 {
3250 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3251 {
mityu4ac198c2021-05-28 17:52:40 +02003252 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003253 clear_tv(rettv);
3254 return FAIL;
3255 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003256 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003259#ifdef FEAT_FLOAT
3260 f1 = 0;
3261 f2 = 0;
3262#endif
3263 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003264 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003266#ifdef FEAT_FLOAT
3267 if (rettv->v_type == VAR_FLOAT)
3268 {
3269 f1 = rettv->vval.v_float;
3270 use_float = TRUE;
3271 n1 = 0;
3272 }
3273 else
3274#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003275 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003276 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003277 if (error)
3278 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 }
3280 else
3281 n1 = 0;
3282
3283 /*
3284 * Get the second variable.
3285 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003286 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3287 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003288 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003289 clear_tv(rettv);
3290 return FAIL;
3291 }
3292 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003293 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 return FAIL;
3295
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003296 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003298#ifdef FEAT_FLOAT
3299 if (var2.v_type == VAR_FLOAT)
3300 {
3301 if (!use_float)
3302 {
3303 f1 = n1;
3304 use_float = TRUE;
3305 }
3306 f2 = var2.vval.v_float;
3307 n2 = 0;
3308 }
3309 else
3310#endif
3311 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003312 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003313 clear_tv(&var2);
3314 if (error)
3315 return FAIL;
3316#ifdef FEAT_FLOAT
3317 if (use_float)
3318 f2 = n2;
3319#endif
3320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321
3322 /*
3323 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003324 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003326#ifdef FEAT_FLOAT
3327 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003329 if (op == '*')
3330 f1 = f1 * f2;
3331 else if (op == '/')
3332 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003333# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003334 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003335 if (f2 == 0.0)
3336 {
3337 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003338 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003339 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003340 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003341 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003342 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003343 }
3344 else
3345 f1 = f1 / f2;
3346# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003347 // We rely on the floating point library to handle divide
3348 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003349 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003350# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003353 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003354 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003355 return FAIL;
3356 }
3357 rettv->v_type = VAR_FLOAT;
3358 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 }
3360 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003363 int failed = FALSE;
3364
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003365 if (op == '*')
3366 n1 = n1 * n2;
3367 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003368 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003370 n1 = num_modulus(n1, n2, &failed);
3371 if (failed)
3372 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003373
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003374 rettv->v_type = VAR_NUMBER;
3375 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 }
3378 }
3379
3380 return OK;
3381}
3382
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003383/*
3384 * Handle a type cast before a base level expression.
3385 * "arg" must point to the first non-white of the expression.
3386 * "arg" is advanced to just after the recognized expression.
3387 * Return OK or FAIL.
3388 */
3389 static int
3390eval7t(
3391 char_u **arg,
3392 typval_T *rettv,
3393 evalarg_T *evalarg,
3394 int want_string) // after "." operator
3395{
3396 type_T *want_type = NULL;
3397 garray_T type_list; // list of pointers to allocated types
3398 int res;
3399 int evaluate = evalarg == NULL ? 0
3400 : (evalarg->eval_flags & EVAL_EVALUATE);
3401
3402 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003403 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3404 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003405 {
3406 ++*arg;
3407 ga_init2(&type_list, sizeof(type_T *), 10);
3408 want_type = parse_type(arg, &type_list, TRUE);
3409 if (want_type == NULL && (evaluate || **arg != '>'))
3410 {
3411 clear_type_list(&type_list);
3412 return FAIL;
3413 }
3414
3415 if (**arg != '>')
3416 {
3417 if (*skipwhite(*arg) == '>')
3418 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3419 else
3420 emsg(_(e_missing_gt));
3421 clear_type_list(&type_list);
3422 return FAIL;
3423 }
3424 ++*arg;
3425 *arg = skipwhite_and_linebreak(*arg, evalarg);
3426 }
3427
3428 res = eval7(arg, rettv, evalarg, want_string);
3429
3430 if (want_type != NULL && evaluate)
3431 {
3432 if (res == OK)
3433 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003434 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3435 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003436
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003437 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003438 {
3439 if (want_type == &t_bool && actual != &t_bool
3440 && (actual->tt_flags & TTFLAG_BOOL_OK))
3441 {
3442 int n = tv2bool(rettv);
3443
3444 // can use "0" and "1" for boolean in some places
3445 clear_tv(rettv);
3446 rettv->v_type = VAR_BOOL;
3447 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3448 }
3449 else
3450 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003451 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003452
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003453 where.wt_variable = TRUE;
3454 res = check_type(want_type, actual, TRUE, where);
3455 }
3456 }
3457 }
3458 clear_type_list(&type_list);
3459 }
3460
3461 return res;
3462}
3463
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003464 int
3465eval_leader(char_u **arg, int vim9)
3466{
3467 char_u *s = *arg;
3468 char_u *p = *arg;
3469
3470 while (*p == '!' || *p == '-' || *p == '+')
3471 {
3472 char_u *n = skipwhite(p + 1);
3473
3474 // ++, --, -+ and +- are not accepted in Vim9 script
3475 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3476 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003477 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003478 return FAIL;
3479 }
3480 p = n;
3481 }
3482 *arg = p;
3483 return OK;
3484}
3485
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003487 * Check for a predefined value "true", "false" and "null.*".
3488 * Return OK when recognized.
3489 */
3490 int
3491handle_predefined(char_u *s, int len, typval_T *rettv)
3492{
3493 switch (len)
3494 {
3495 case 4: if (STRNCMP(s, "true", 4) == 0)
3496 {
3497 rettv->v_type = VAR_BOOL;
3498 rettv->vval.v_number = VVAL_TRUE;
3499 return OK;
3500 }
3501 if (STRNCMP(s, "null", 4) == 0)
3502 {
3503 rettv->v_type = VAR_SPECIAL;
3504 rettv->vval.v_number = VVAL_NULL;
3505 return OK;
3506 }
3507 break;
3508 case 5: if (STRNCMP(s, "false", 5) == 0)
3509 {
3510 rettv->v_type = VAR_BOOL;
3511 rettv->vval.v_number = VVAL_FALSE;
3512 return OK;
3513 }
3514 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003515 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3516 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003517#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003518 rettv->v_type = VAR_JOB;
3519 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003520#else
3521 rettv->v_type = VAR_SPECIAL;
3522 rettv->vval.v_number = VVAL_NULL;
3523#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003524 return OK;
3525 }
3526 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003527 case 9:
3528 if (STRNCMP(s, "null_", 5) != 0)
3529 break;
3530 if (STRNCMP(s + 5, "list", 4) == 0)
3531 {
3532 rettv->v_type = VAR_LIST;
3533 rettv->vval.v_list = NULL;
3534 return OK;
3535 }
3536 if (STRNCMP(s + 5, "dict", 4) == 0)
3537 {
3538 rettv->v_type = VAR_DICT;
3539 rettv->vval.v_dict = NULL;
3540 return OK;
3541 }
3542 if (STRNCMP(s + 5, "blob", 4) == 0)
3543 {
3544 rettv->v_type = VAR_BLOB;
3545 rettv->vval.v_blob = NULL;
3546 return OK;
3547 }
3548 break;
3549 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3550 {
3551 rettv->v_type = VAR_STRING;
3552 rettv->vval.v_string = NULL;
3553 return OK;
3554 }
3555 break;
3556 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003557 if (STRNCMP(s, "null_channel", 12) == 0)
3558 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003559#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003560 rettv->v_type = VAR_CHANNEL;
3561 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003562#else
3563 rettv->v_type = VAR_SPECIAL;
3564 rettv->vval.v_number = VVAL_NULL;
3565#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003566 return OK;
3567 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003568 if (STRNCMP(s, "null_partial", 12) == 0)
3569 {
3570 rettv->v_type = VAR_PARTIAL;
3571 rettv->vval.v_partial = NULL;
3572 return OK;
3573 }
3574 break;
3575 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3576 {
3577 rettv->v_type = VAR_FUNC;
3578 rettv->vval.v_string = NULL;
3579 return OK;
3580 }
3581 break;
3582 }
3583 return FAIL;
3584}
3585
3586/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 * Handle sixth level expression:
3588 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003589 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003590 * "string" string constant
3591 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 * &option-name option value
3593 * @r register contents
3594 * identifier variable value
3595 * function() function call
3596 * $VAR environment variable
3597 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003598 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003599 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003600 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003601 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 *
3603 * Also handle:
3604 * ! in front logical NOT
3605 * - in front unary minus
3606 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003607 * trailing [] subscript in String or List
3608 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003609 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 *
3611 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003612 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 *
3614 * Return OK or FAIL.
3615 */
3616 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003617eval7(
3618 char_u **arg,
3619 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003620 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003623 int evaluate = evalarg != NULL
3624 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 int len;
3626 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003627 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 char_u *start_leader, *end_leader;
3629 int ret = OK;
3630 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003631 static int recurse = 0;
3632 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633
3634 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003635 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003636 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003638 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639
3640 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003641 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 */
3643 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003644 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003645 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 end_leader = *arg;
3647
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003648 if (**arg == '.' && (!isdigit(*(*arg + 1))
3649#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003650 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003651#endif
3652 ))
3653 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003654 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003655 ++*arg;
3656 return FAIL;
3657 }
3658
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003659 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003660 // and crash. With MSVC the stack is smaller.
3661 if (recurse ==
3662#ifdef _MSC_VER
3663 300
3664#else
3665 1000
3666#endif
3667 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003668 {
3669 semsg(_(e_expression_too_recursive_str), *arg);
3670 return FAIL;
3671 }
3672 ++recurse;
3673
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 switch (**arg)
3675 {
3676 /*
3677 * Number constant.
3678 */
3679 case '0':
3680 case '1':
3681 case '2':
3682 case '3':
3683 case '4':
3684 case '5':
3685 case '6':
3686 case '7':
3687 case '8':
3688 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003689 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003690
3691 // Apply prefixed "-" and "+" now. Matters especially when
3692 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003693 if (ret == OK && evaluate && end_leader > start_leader
3694 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003695 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 break;
3697
3698 /*
3699 * String constant: "string".
3700 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003701 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 break;
3703
3704 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003705 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003707 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003708 break;
3709
3710 /*
3711 * List: [expr, expr]
3712 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003713 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 break;
3715
3716 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003717 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003718 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003719 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003720 {
3721 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3722 }
3723 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003724 {
3725 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003726 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003727 }
3728 else
3729 ret = NOTDONE;
3730 break;
3731
3732 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003733 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003734 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003735 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003736 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003737 ret = NOTDONE;
3738 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00003739 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003740 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003741 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003742 break;
3743
3744 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003745 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003747 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 break;
3749
3750 /*
3751 * Environment variable: $VAR.
3752 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003753 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 break;
3755
3756 /*
3757 * Register contents: @r.
3758 */
3759 case '@': ++*arg;
3760 if (evaluate)
3761 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003762 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003763 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003764 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003765 emsg_invreg(**arg);
3766 else
3767 {
3768 rettv->v_type = VAR_STRING;
3769 rettv->vval.v_string = get_reg_contents(**arg,
3770 GREG_EXPR_SRC);
3771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 }
3773 if (**arg != NUL)
3774 ++*arg;
3775 break;
3776
3777 /*
3778 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003779 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003781 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003782 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01003783 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003784 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003785 if (ret == OK && evaluate)
3786 {
3787 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3788
Bram Moolenaara9931532021-06-12 15:58:16 +02003789 // Compile it here to get the return type. The return
3790 // type is optional, when it's missing use t_unknown.
3791 // This is recognized in compile_return().
3792 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3793 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00003794 if (compile_def_function(ufunc, FALSE,
3795 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003796 {
3797 clear_tv(rettv);
3798 ret = FAIL;
3799 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003800 }
3801 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003802 if (ret == NOTDONE)
3803 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003804 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003805 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003806
Bram Moolenaar9215f012020-06-27 21:18:00 +02003807 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003808 if (**arg == ')')
3809 ++*arg;
3810 else if (ret == OK)
3811 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003812 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003813 clear_tv(rettv);
3814 ret = FAIL;
3815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 }
3817 break;
3818
Bram Moolenaar8c711452005-01-14 21:53:12 +00003819 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 break;
3821 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003822
3823 if (ret == NOTDONE)
3824 {
3825 /*
3826 * Must be a variable or function name.
3827 * Can also be a curly-braces kind of name: {expr}.
3828 */
3829 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003830 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003831 if (alias != NULL)
3832 s = alias;
3833
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003834 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003835 ret = FAIL;
3836 else
3837 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003838 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3839
Bram Moolenaar4525a572022-02-13 11:57:33 +00003840 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003841 {
3842 emsg(_(e_cannot_use_underscore_here));
3843 ret = FAIL;
3844 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00003845 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00003846 && s[0] == 's' && s[1] == ':')
3847 {
3848 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
3849 ret = FAIL;
3850 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00003851 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003852 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003853 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003854 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003855 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003856 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003857 else if (flags & EVAL_CONSTANT)
3858 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003859 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003860 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003861 // get the value of "true", "false", etc. or a variable
3862 ret = FAIL;
3863 if (vim9script)
3864 ret = handle_predefined(s, len, rettv);
3865 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003866 {
3867 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003868 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003869 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003870 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003871 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003872 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003873 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003874 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003875 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003876 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003877 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003878 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003879 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003880 }
3881
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003882 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3883 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003884 if (ret == OK)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003885 ret = handle_subscript(arg, name_start, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886
3887 /*
3888 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3889 */
3890 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003891 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003892
3893 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003894 return ret;
3895}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003896
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003897/*
3898 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003899 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003900 * Adjusts "end_leaderp" until it is at "start_leader".
3901 */
3902 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003903eval7_leader(
3904 typval_T *rettv,
3905 int numeric_only,
3906 char_u *start_leader,
3907 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003908{
3909 char_u *end_leader = *end_leaderp;
3910 int ret = OK;
3911 int error = FALSE;
3912 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003913 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003914 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003915#ifdef FEAT_FLOAT
3916 float_T f = 0.0;
3917
3918 if (rettv->v_type == VAR_FLOAT)
3919 f = rettv->vval.v_float;
3920 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003921#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003922 {
3923 while (VIM_ISWHITE(end_leader[-1]))
3924 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003925 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003926 val = tv2bool(rettv);
3927 else
3928 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003929 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003930 if (error)
3931 {
3932 clear_tv(rettv);
3933 ret = FAIL;
3934 }
3935 else
3936 {
3937 while (end_leader > start_leader)
3938 {
3939 --end_leader;
3940 if (*end_leader == '!')
3941 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003942 if (numeric_only)
3943 {
3944 ++end_leader;
3945 break;
3946 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003947#ifdef FEAT_FLOAT
3948 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003949 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003950 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003951 {
3952 rettv->v_type = VAR_BOOL;
3953 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3954 }
3955 else
3956 f = !f;
3957 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003958 else
3959#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003960 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003961 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003962 type = VAR_BOOL;
3963 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003964 }
3965 else if (*end_leader == '-')
3966 {
3967#ifdef FEAT_FLOAT
3968 if (rettv->v_type == VAR_FLOAT)
3969 f = -f;
3970 else
3971#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003972 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003973 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003974 type = VAR_NUMBER;
3975 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003976 }
3977 }
3978#ifdef FEAT_FLOAT
3979 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003981 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003982 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003984 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003985#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003986 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003987 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003988 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003989 rettv->v_type = type;
3990 else
3991 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003992 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003995 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 return ret;
3997}
3998
3999/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004000 * Call the function referred to in "rettv".
4001 */
4002 static int
4003call_func_rettv(
4004 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004005 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004006 typval_T *rettv,
4007 int evaluate,
4008 dict_T *selfdict,
4009 typval_T *basetv)
4010{
4011 partial_T *pt = NULL;
4012 funcexe_T funcexe;
4013 typval_T functv;
4014 char_u *s;
4015 int ret;
4016
4017 // need to copy the funcref so that we can clear rettv
4018 if (evaluate)
4019 {
4020 functv = *rettv;
4021 rettv->v_type = VAR_UNKNOWN;
4022
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004023 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004024 if (functv.v_type == VAR_PARTIAL)
4025 {
4026 pt = functv.vval.v_partial;
4027 s = partial_name(pt);
4028 }
4029 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004030 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004031 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004032 if (s == NULL || *s == NUL)
4033 {
4034 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004035 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004036 goto theend;
4037 }
4038 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004039 }
4040 else
4041 s = (char_u *)"";
4042
Bram Moolenaara80faa82020-04-12 19:37:17 +02004043 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004044 funcexe.fe_firstline = curwin->w_cursor.lnum;
4045 funcexe.fe_lastline = curwin->w_cursor.lnum;
4046 funcexe.fe_evaluate = evaluate;
4047 funcexe.fe_partial = pt;
4048 funcexe.fe_selfdict = selfdict;
4049 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004050 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004051
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004052theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004053 // Clear the funcref afterwards, so that deleting it while
4054 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004055 if (evaluate)
4056 clear_tv(&functv);
4057
4058 return ret;
4059}
4060
4061/*
4062 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004063 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004064 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4065 */
4066 static int
4067eval_lambda(
4068 char_u **arg,
4069 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004070 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004071 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004072{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004073 int evaluate = evalarg != NULL
4074 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004075 typval_T base = *rettv;
4076 int ret;
4077
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004078 rettv->v_type = VAR_UNKNOWN;
4079
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004080 if (**arg == '{')
4081 {
4082 // ->{lambda}()
4083 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4084 }
4085 else
4086 {
4087 // ->(lambda)()
4088 ++*arg;
4089 ret = eval1(arg, rettv, evalarg);
4090 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar605ec912021-12-18 16:54:31 +00004091 if (**arg == ')')
4092 {
4093 ++*arg;
4094 }
4095 else
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004096 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004097 emsg(_(e_missing_closing_paren));
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004098 ret = FAIL;
4099 }
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004100 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004101 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004102 return FAIL;
4103 else if (**arg != '(')
4104 {
4105 if (verbose)
4106 {
4107 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004108 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004109 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004110 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004111 }
4112 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004113 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004114 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004115 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004116 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004117
4118 // Clear the funcref afterwards, so that deleting it while
4119 // evaluating the arguments is possible (see test55).
4120 if (evaluate)
4121 clear_tv(&base);
4122
4123 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004124}
4125
4126/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004127 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004128 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004129 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4130 */
4131 static int
4132eval_method(
4133 char_u **arg,
4134 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004135 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004136 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004137{
4138 char_u *name;
4139 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004140 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004141 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004142 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004143 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004144 int evaluate = evalarg != NULL
4145 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004146
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004147 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004148
Bram Moolenaarac92e252019-08-03 21:58:38 +02004149 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004150 len = get_name_len(arg, &alias, evaluate, TRUE);
4151 if (alias != NULL)
4152 name = alias;
4153
4154 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004155 {
4156 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004157 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004158 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004159 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004160 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004161 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004162 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004163
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004164 // If there is no "(" immediately following, but there is further on,
4165 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4166 // Does not handle anything where "(" is part of the expression.
4167 *arg = skipwhite(*arg);
4168
4169 if (**arg != '(' && alias == NULL
4170 && (paren = vim_strchr(*arg, '(')) != NULL)
4171 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004172 char_u *deref;
4173
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004174 *arg = name;
4175 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004176 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4177 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004178 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004179 *arg = name + len;
4180 ret = FAIL;
4181 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004182 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004183 {
4184 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004185 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004186 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004187 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004188 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004189
4190 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004191 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004192 *arg = skipwhite(*arg);
4193
4194 if (**arg != '(')
4195 {
4196 if (verbose)
4197 semsg(_(e_missing_parenthesis_str), name);
4198 ret = FAIL;
4199 }
4200 else if (VIM_ISWHITE((*arg)[-1]))
4201 {
4202 if (verbose)
4203 emsg(_(e_no_white_space_allowed_before_parenthesis));
4204 ret = FAIL;
4205 }
4206 else
4207 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004208 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004209 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004210 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004211
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004212 // Clear the funcref afterwards, so that deleting it while
4213 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004214 if (evaluate)
4215 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004216 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004217
Bram Moolenaarac92e252019-08-03 21:58:38 +02004218 return ret;
4219}
4220
4221/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004222 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4223 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004224 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4225 */
4226 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004227eval_index(
4228 char_u **arg,
4229 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004230 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004231 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004232{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004233 int evaluate = evalarg != NULL
4234 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004235 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004236 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004237 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004238 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004239 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004240 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004241
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004242 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4243 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004244
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004245 init_tv(&var1);
4246 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004247 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004248 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004249 /*
4250 * dict.name
4251 */
4252 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004253 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004254 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004255 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004256 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004257 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004258 }
4259 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004260 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004261 /*
4262 * something[idx]
4263 *
4264 * Get the (first) variable from inside the [].
4265 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004266 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004267 if (**arg == ':')
4268 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004269 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004270 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004271 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004272 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004273 semsg(_(e_white_space_required_before_and_after_str_at_str),
4274 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004275 clear_tv(&var1);
4276 return FAIL;
4277 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004278 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004279 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004280 int error = FALSE;
4281
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004282#ifdef FEAT_FLOAT
4283 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004284 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004285 && var1.v_type == VAR_FLOAT)
4286 {
4287 var1.vval.v_string = typval_tostring(&var1, TRUE);
4288 var1.v_type = VAR_STRING;
4289 }
4290#endif
Bram Moolenaar4525a572022-02-13 11:57:33 +00004291 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004292 tv_get_number_chk(&var1, &error);
4293 else
4294 error = tv_get_string_chk(&var1) == NULL;
4295 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004296 {
4297 // not a number or string
4298 clear_tv(&var1);
4299 return FAIL;
4300 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004301 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004302
4303 /*
4304 * Get the second variable from inside the [:].
4305 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004306 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004307 if (**arg == ':')
4308 {
4309 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004310 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004311 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004312 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004313 semsg(_(e_white_space_required_before_and_after_str_at_str),
4314 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004315 if (!empty1)
4316 clear_tv(&var1);
4317 return FAIL;
4318 }
4319 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004320 if (**arg == ']')
4321 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004322 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004323 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004324 if (!empty1)
4325 clear_tv(&var1);
4326 return FAIL;
4327 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004328 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004329 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004330 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004331 if (!empty1)
4332 clear_tv(&var1);
4333 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004334 return FAIL;
4335 }
4336 }
4337
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004338 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004339 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004340 if (**arg != ']')
4341 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004342 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004343 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004344 clear_tv(&var1);
4345 if (range)
4346 clear_tv(&var2);
4347 return FAIL;
4348 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004349 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004350 }
4351
4352 if (evaluate)
4353 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004354 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004355 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004356 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004357
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004358 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004359 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004360 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004361 clear_tv(&var2);
4362 return res;
4363 }
4364 return OK;
4365}
4366
4367/*
4368 * Check if "rettv" can have an [index] or [sli:ce]
4369 */
4370 int
4371check_can_index(typval_T *rettv, int evaluate, int verbose)
4372{
4373 switch (rettv->v_type)
4374 {
4375 case VAR_FUNC:
4376 case VAR_PARTIAL:
4377 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004378 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004379 return FAIL;
4380 case VAR_FLOAT:
4381#ifdef FEAT_FLOAT
4382 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004383 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004384 return FAIL;
4385#endif
4386 case VAR_BOOL:
4387 case VAR_SPECIAL:
4388 case VAR_JOB:
4389 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004390 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004391 if (verbose)
4392 emsg(_(e_cannot_index_special_variable));
4393 return FAIL;
4394 case VAR_UNKNOWN:
4395 case VAR_ANY:
4396 case VAR_VOID:
4397 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004398 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004399 emsg(_(e_cannot_index_special_variable));
4400 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004401 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004402 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004403
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004404 case VAR_STRING:
4405 case VAR_LIST:
4406 case VAR_DICT:
4407 case VAR_BLOB:
4408 break;
4409 case VAR_NUMBER:
4410 if (in_vim9script())
4411 emsg(_(e_cannot_index_number));
4412 break;
4413 }
4414 return OK;
4415}
4416
4417/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004418 * slice() function
4419 */
4420 void
4421f_slice(typval_T *argvars, typval_T *rettv)
4422{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004423 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004424 && ((argvars[0].v_type != VAR_STRING
4425 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004426 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004427 && check_for_list_arg(argvars, 0) == FAIL)
4428 || check_for_number_arg(argvars, 1) == FAIL
4429 || check_for_opt_number_arg(argvars, 2) == FAIL))
4430 return;
4431
Bram Moolenaar6601b622021-01-13 21:47:15 +01004432 if (check_can_index(argvars, TRUE, FALSE) == OK)
4433 {
4434 copy_tv(argvars, rettv);
4435 eval_index_inner(rettv, TRUE, argvars + 1,
4436 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4437 TRUE, NULL, 0, FALSE);
4438 }
4439}
4440
4441/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004442 * Apply index or range to "rettv".
4443 * "var1" is the first index, NULL for [:expr].
4444 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004445 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4446 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004447 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4448 */
4449 int
4450eval_index_inner(
4451 typval_T *rettv,
4452 int is_range,
4453 typval_T *var1,
4454 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004455 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004456 char_u *key,
4457 int keylen,
4458 int verbose)
4459{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004460 varnumber_T n1, n2 = 0;
4461 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004462
4463 n1 = 0;
4464 if (var1 != NULL && rettv->v_type != VAR_DICT)
4465 n1 = tv_get_number(var1);
4466
4467 if (is_range)
4468 {
4469 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004470 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004471 if (verbose)
4472 emsg(_(e_cannot_slice_dictionary));
4473 return FAIL;
4474 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004475 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004476 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004477 else
4478 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004479 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004480
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004481 switch (rettv->v_type)
4482 {
4483 case VAR_UNKNOWN:
4484 case VAR_ANY:
4485 case VAR_VOID:
4486 case VAR_FUNC:
4487 case VAR_PARTIAL:
4488 case VAR_FLOAT:
4489 case VAR_BOOL:
4490 case VAR_SPECIAL:
4491 case VAR_JOB:
4492 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004493 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004494 break; // not evaluating, skipping over subscript
4495
4496 case VAR_NUMBER:
4497 case VAR_STRING:
4498 {
4499 char_u *s = tv_get_string(rettv);
4500
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004501 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004502 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004503 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004504 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004505 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004506 else
4507 s = char_from_string(s, n1);
4508 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004509 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004510 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004511 // The resulting variable is a substring. If the indexes
4512 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004513 if (n1 < 0)
4514 {
4515 n1 = len + n1;
4516 if (n1 < 0)
4517 n1 = 0;
4518 }
4519 if (n2 < 0)
4520 n2 = len + n2;
4521 else if (n2 >= len)
4522 n2 = len;
4523 if (n1 >= len || n2 < 0 || n1 > n2)
4524 s = NULL;
4525 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004526 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004527 }
4528 else
4529 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004530 // The resulting variable is a string of a single
4531 // character. If the index is too big or negative the
4532 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004533 if (n1 >= len || n1 < 0)
4534 s = NULL;
4535 else
4536 s = vim_strnsave(s + n1, 1);
4537 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004538 clear_tv(rettv);
4539 rettv->v_type = VAR_STRING;
4540 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004541 }
4542 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004543
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004544 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004545 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4546 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004547 break;
4548
4549 case VAR_LIST:
4550 if (var1 == NULL)
4551 n1 = 0;
4552 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004553 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004554 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004555 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004556 return FAIL;
4557 break;
4558
4559 case VAR_DICT:
4560 {
4561 dictitem_T *item;
4562 typval_T tmp;
4563
4564 if (key == NULL)
4565 {
4566 key = tv_get_string_chk(var1);
4567 if (key == NULL)
4568 return FAIL;
4569 }
4570
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004571 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004572
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004573 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004574 {
4575 if (verbose)
4576 {
4577 if (keylen > 0)
4578 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004579 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004580 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004581 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004582 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004583
4584 copy_tv(&item->di_tv, &tmp);
4585 clear_tv(rettv);
4586 *rettv = tmp;
4587 }
4588 break;
4589 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004590 return OK;
4591}
4592
4593/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004594 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004595 */
4596 char_u *
4597partial_name(partial_T *pt)
4598{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004599 if (pt != NULL)
4600 {
4601 if (pt->pt_name != NULL)
4602 return pt->pt_name;
4603 if (pt->pt_func != NULL)
4604 return pt->pt_func->uf_name;
4605 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004606 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004607}
4608
Bram Moolenaarddecc252016-04-06 22:59:37 +02004609 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004610partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004611{
4612 int i;
4613
4614 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004615 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004616 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004617 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004618 if (pt->pt_name != NULL)
4619 {
4620 func_unref(pt->pt_name);
4621 vim_free(pt->pt_name);
4622 }
4623 else
4624 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004625
Bram Moolenaar54656012021-06-09 20:50:46 +02004626 // "out_up" is no longer used, decrement refcount on partial that owns it.
4627 partial_unref(pt->pt_outer.out_up_partial);
4628
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004629 // Using pt_outer from another partial.
4630 partial_unref(pt->pt_outer_partial);
4631
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004632 // Decrease the reference count for the context of a closure. If down
4633 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004634 if (pt->pt_funcstack != NULL)
4635 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004636 --pt->pt_funcstack->fs_refcount;
4637 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004638 }
4639
Bram Moolenaarddecc252016-04-06 22:59:37 +02004640 vim_free(pt);
4641}
4642
4643/*
4644 * Unreference a closure: decrement the reference count and free it when it
4645 * becomes zero.
4646 */
4647 void
4648partial_unref(partial_T *pt)
4649{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004650 if (pt != NULL)
4651 {
4652 if (--pt->pt_refcount <= 0)
4653 partial_free(pt);
4654
4655 // If the reference count goes down to one, the funcstack may be the
4656 // only reference and can be freed if no other partials reference it.
4657 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4658 funcstack_check_refcount(pt->pt_funcstack);
4659 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004660}
4661
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004662/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004663 * Return the next (unique) copy ID.
4664 * Used for serializing nested structures.
4665 */
4666 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004667get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004668{
4669 current_copyID += COPYID_INC;
4670 return current_copyID;
4671}
4672
4673/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004674 * Garbage collection for lists and dictionaries.
4675 *
4676 * We use reference counts to be able to free most items right away when they
4677 * are no longer used. But for composite items it's possible that it becomes
4678 * unused while the reference count is > 0: When there is a recursive
4679 * reference. Example:
4680 * :let l = [1, 2, 3]
4681 * :let d = {9: l}
4682 * :let l[1] = d
4683 *
4684 * Since this is quite unusual we handle this with garbage collection: every
4685 * once in a while find out which lists and dicts are not referenced from any
4686 * variable.
4687 *
4688 * Here is a good reference text about garbage collection (refers to Python
4689 * but it applies to all reference-counting mechanisms):
4690 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004691 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004692
4693/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004694 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004695 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004696 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004697 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004698 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004699garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004700{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004701 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004702 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004703 buf_T *buf;
4704 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004705 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004706 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004707
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004708 if (!testing)
4709 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004710 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004711 want_garbage_collect = FALSE;
4712 may_garbage_collect = FALSE;
4713 garbage_collect_at_exit = FALSE;
4714 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004715
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004716 // The execution stack can grow big, limit the size.
4717 if (exestack.ga_maxlen - exestack.ga_len > 500)
4718 {
4719 size_t new_len;
4720 char_u *pp;
4721 int n;
4722
4723 // Keep 150% of the current size, with a minimum of the growth size.
4724 n = exestack.ga_len / 2;
4725 if (n < exestack.ga_growsize)
4726 n = exestack.ga_growsize;
4727
4728 // Don't make it bigger though.
4729 if (exestack.ga_len + n < exestack.ga_maxlen)
4730 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004731 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004732 pp = vim_realloc(exestack.ga_data, new_len);
4733 if (pp == NULL)
4734 return FAIL;
4735 exestack.ga_maxlen = exestack.ga_len + n;
4736 exestack.ga_data = pp;
4737 }
4738 }
4739
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004740 // We advance by two because we add one for items referenced through
4741 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004742 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004743
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004744 /*
4745 * 1. Go through all accessible variables and mark all lists and dicts
4746 * with copyID.
4747 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004748
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004749 // Don't free variables in the previous_funccal list unless they are only
4750 // referenced through previous_funccal. This must be first, because if
4751 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004752 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004753
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004754 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004755 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004756
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004757 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004758 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004759 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4760 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004761
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004762 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004763 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004764 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4765 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004766 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004767 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4768 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004769#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004770 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004771 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4772 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004773 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004774 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004775 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4776 NULL, NULL);
4777#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004778
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004779 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004780 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004781 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4782 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004783 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004784 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004785
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004786 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004787 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004788
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004789 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004790 abort = abort || set_ref_in_functions(copyID);
4791
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004792 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004793 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004794
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004795 // funcstacks keep variables for closures
4796 abort = abort || set_ref_in_funcstacks(copyID);
4797
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004798 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004799 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004800
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004801 // callbacks in buffers
4802 abort = abort || set_ref_in_buffers(copyID);
4803
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004804 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
4805 abort = abort || set_ref_in_insexpand_funcs(copyID);
4806
4807 // 'operatorfunc' callback
4808 abort = abort || set_ref_in_opfunc(copyID);
4809
4810 // 'tagfunc' callback
4811 abort = abort || set_ref_in_tagfunc(copyID);
4812
4813 // 'imactivatefunc' and 'imstatusfunc' callbacks
4814 abort = abort || set_ref_in_im_funcs(copyID);
4815
Bram Moolenaar1dced572012-04-05 16:54:08 +02004816#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004817 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004818#endif
4819
Bram Moolenaardb913952012-06-29 12:54:53 +02004820#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004821 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004822#endif
4823
4824#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004825 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004826#endif
4827
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004828#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004829 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004830 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004831#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004832#ifdef FEAT_NETBEANS_INTG
4833 abort = abort || set_ref_in_nb_channel(copyID);
4834#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004835
Bram Moolenaare3188e22016-05-31 21:13:04 +02004836#ifdef FEAT_TIMERS
4837 abort = abort || set_ref_in_timer(copyID);
4838#endif
4839
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004840#ifdef FEAT_QUICKFIX
4841 abort = abort || set_ref_in_quickfix(copyID);
4842#endif
4843
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004844#ifdef FEAT_TERMINAL
4845 abort = abort || set_ref_in_term(copyID);
4846#endif
4847
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004848#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004849 abort = abort || set_ref_in_popups(copyID);
4850#endif
4851
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004852 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004853 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004854 /*
4855 * 2. Free lists and dictionaries that are not referenced.
4856 */
4857 did_free = free_unref_items(copyID);
4858
4859 /*
4860 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004861 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004862 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004863 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004864 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004865 else if (p_verbose > 0)
4866 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004867 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004868 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004869
4870 return did_free;
4871}
4872
4873/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004874 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004875 */
4876 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004877free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004878{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004879 int did_free = FALSE;
4880
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004881 // Let all "free" functions know that we are here. This means no
4882 // dictionaries, lists, channels or jobs are to be freed, because we will
4883 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004884 in_free_unref_items = TRUE;
4885
4886 /*
4887 * PASS 1: free the contents of the items. We don't free the items
4888 * themselves yet, so that it is possible to decrement refcount counters
4889 */
4890
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004891 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004892 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004893
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004894 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004895 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004896
4897#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004898 // Go through the list of jobs and free items without the copyID. This
4899 // must happen before doing channels, because jobs refer to channels, but
4900 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004901 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4902
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004903 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004904 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4905#endif
4906
4907 /*
4908 * PASS 2: free the items themselves.
4909 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004910 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004911 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004912
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004913#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004914 // Go through the list of jobs and free items without the copyID. This
4915 // must happen before doing channels, because jobs refer to channels, but
4916 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004917 free_unused_jobs(copyID, COPYID_MASK);
4918
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004919 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004920 free_unused_channels(copyID, COPYID_MASK);
4921#endif
4922
4923 in_free_unref_items = FALSE;
4924
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004925 return did_free;
4926}
4927
4928/*
4929 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004930 * "list_stack" is used to add lists to be marked. Can be NULL.
4931 *
4932 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004933 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004934 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004935set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004936{
4937 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004938 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004939 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004940 hashtab_T *cur_ht;
4941 ht_stack_T *ht_stack = NULL;
4942 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004943
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004944 cur_ht = ht;
4945 for (;;)
4946 {
4947 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004948 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004949 // Mark each item in the hashtab. If the item contains a hashtab
4950 // it is added to ht_stack, if it contains a list it is added to
4951 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004952 todo = (int)cur_ht->ht_used;
4953 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4954 if (!HASHITEM_EMPTY(hi))
4955 {
4956 --todo;
4957 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4958 &ht_stack, list_stack);
4959 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004960 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004961
4962 if (ht_stack == NULL)
4963 break;
4964
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004965 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004966 cur_ht = ht_stack->ht;
4967 tempitem = ht_stack;
4968 ht_stack = ht_stack->prev;
4969 free(tempitem);
4970 }
4971
4972 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004973}
4974
Dominique Pelle748b3082022-01-08 12:41:16 +00004975#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
4976 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004977/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004978 * Mark a dict and its items with "copyID".
4979 * Returns TRUE if setting references failed somehow.
4980 */
4981 int
4982set_ref_in_dict(dict_T *d, int copyID)
4983{
4984 if (d != NULL && d->dv_copyID != copyID)
4985 {
4986 d->dv_copyID = copyID;
4987 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4988 }
4989 return FALSE;
4990}
Dominique Pelle748b3082022-01-08 12:41:16 +00004991#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004992
4993/*
4994 * Mark a list and its items with "copyID".
4995 * Returns TRUE if setting references failed somehow.
4996 */
4997 int
4998set_ref_in_list(list_T *ll, int copyID)
4999{
5000 if (ll != NULL && ll->lv_copyID != copyID)
5001 {
5002 ll->lv_copyID = copyID;
5003 return set_ref_in_list_items(ll, copyID, NULL);
5004 }
5005 return FALSE;
5006}
5007
5008/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005009 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005010 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5011 *
5012 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005013 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005014 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005015set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005016{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005017 listitem_T *li;
5018 int abort = FALSE;
5019 list_T *cur_l;
5020 list_stack_T *list_stack = NULL;
5021 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005022
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005023 cur_l = l;
5024 for (;;)
5025 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005026 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005027 // Mark each item in the list. If the item contains a hashtab
5028 // it is added to ht_stack, if it contains a list it is added to
5029 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005030 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5031 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5032 ht_stack, &list_stack);
5033 if (list_stack == NULL)
5034 break;
5035
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005036 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005037 cur_l = list_stack->list;
5038 tempitem = list_stack;
5039 list_stack = list_stack->prev;
5040 free(tempitem);
5041 }
5042
5043 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005044}
5045
5046/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005047 * Mark the partial in callback 'cb' with "copyID".
5048 */
5049 int
5050set_ref_in_callback(callback_T *cb, int copyID)
5051{
5052 typval_T tv;
5053
5054 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5055 return FALSE;
5056
5057 tv.v_type = VAR_PARTIAL;
5058 tv.vval.v_partial = cb->cb_partial;
5059 return set_ref_in_item(&tv, copyID, NULL, NULL);
5060}
5061
5062/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005063 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005064 * "list_stack" is used to add lists to be marked. Can be NULL.
5065 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5066 *
5067 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005068 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005069 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005070set_ref_in_item(
5071 typval_T *tv,
5072 int copyID,
5073 ht_stack_T **ht_stack,
5074 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005075{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005076 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005077
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005078 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005079 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005080 dict_T *dd = tv->vval.v_dict;
5081
Bram Moolenaara03f2332016-02-06 18:09:59 +01005082 if (dd != NULL && dd->dv_copyID != copyID)
5083 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005084 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005085 dd->dv_copyID = copyID;
5086 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005087 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005088 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5089 }
5090 else
5091 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005092 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5093
Bram Moolenaara03f2332016-02-06 18:09:59 +01005094 if (newitem == NULL)
5095 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005096 else
5097 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005098 newitem->ht = &dd->dv_hashtab;
5099 newitem->prev = *ht_stack;
5100 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005101 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005102 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005103 }
5104 }
5105 else if (tv->v_type == VAR_LIST)
5106 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005107 list_T *ll = tv->vval.v_list;
5108
Bram Moolenaara03f2332016-02-06 18:09:59 +01005109 if (ll != NULL && ll->lv_copyID != copyID)
5110 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005111 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005112 ll->lv_copyID = copyID;
5113 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005114 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005115 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005116 }
5117 else
5118 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005119 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5120
Bram Moolenaara03f2332016-02-06 18:09:59 +01005121 if (newitem == NULL)
5122 abort = TRUE;
5123 else
5124 {
5125 newitem->list = ll;
5126 newitem->prev = *list_stack;
5127 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005128 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005129 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005130 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005131 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005132 else if (tv->v_type == VAR_FUNC)
5133 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005134 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005135 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005136 else if (tv->v_type == VAR_PARTIAL)
5137 {
5138 partial_T *pt = tv->vval.v_partial;
5139 int i;
5140
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005141 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005142 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005143 // Didn't see this partial yet.
5144 pt->pt_copyID = copyID;
5145
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005146 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005147
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005148 if (pt->pt_dict != NULL)
5149 {
5150 typval_T dtv;
5151
5152 dtv.v_type = VAR_DICT;
5153 dtv.vval.v_dict = pt->pt_dict;
5154 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5155 }
5156
5157 for (i = 0; i < pt->pt_argc; ++i)
5158 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5159 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005160 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005161 }
5162 }
5163#ifdef FEAT_JOB_CHANNEL
5164 else if (tv->v_type == VAR_JOB)
5165 {
5166 job_T *job = tv->vval.v_job;
5167 typval_T dtv;
5168
5169 if (job != NULL && job->jv_copyID != copyID)
5170 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005171 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005172 if (job->jv_channel != NULL)
5173 {
5174 dtv.v_type = VAR_CHANNEL;
5175 dtv.vval.v_channel = job->jv_channel;
5176 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5177 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005178 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005179 {
5180 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005181 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005182 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5183 }
5184 }
5185 }
5186 else if (tv->v_type == VAR_CHANNEL)
5187 {
5188 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005189 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005190 typval_T dtv;
5191 jsonq_T *jq;
5192 cbq_T *cq;
5193
5194 if (ch != NULL && ch->ch_copyID != copyID)
5195 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005196 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005197 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005198 {
5199 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5200 jq = jq->jq_next)
5201 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5202 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5203 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005204 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005205 {
5206 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005207 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005208 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5209 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005210 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005211 {
5212 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005213 dtv.vval.v_partial =
5214 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005215 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5216 }
5217 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005218 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005219 {
5220 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005221 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005222 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5223 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005224 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005225 {
5226 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005227 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005228 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5229 }
5230 }
5231 }
5232#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005233 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005234}
5235
Bram Moolenaar8c711452005-01-14 21:53:12 +00005236/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005237 * Return a string with the string representation of a variable.
5238 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005239 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005240 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005241 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005242 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005243 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005244 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5245 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005246 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005247 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005248 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005249echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005250 typval_T *tv,
5251 char_u **tofree,
5252 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005253 int copyID,
5254 int echo_style,
5255 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005256 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005257{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005258 static int recurse = 0;
5259 char_u *r = NULL;
5260
Bram Moolenaar33570922005-01-25 22:26:29 +00005261 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005262 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005263 if (!did_echo_string_emsg)
5264 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005265 // Only give this message once for a recursive call to avoid
5266 // flooding the user with errors. And stop iterating over lists
5267 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005268 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005269 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005270 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005271 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005272 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005273 }
5274 ++recurse;
5275
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005276 switch (tv->v_type)
5277 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005278 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005279 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005280 {
5281 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005282 r = tv->vval.v_string;
5283 if (r == NULL)
5284 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005285 }
5286 else
5287 {
5288 *tofree = string_quote(tv->vval.v_string, FALSE);
5289 r = *tofree;
5290 }
5291 break;
5292
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005294 if (echo_style)
5295 {
5296 *tofree = NULL;
5297 r = tv->vval.v_string;
5298 }
5299 else
5300 {
5301 *tofree = string_quote(tv->vval.v_string, TRUE);
5302 r = *tofree;
5303 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005304 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005305
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005306 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005307 {
5308 partial_T *pt = tv->vval.v_partial;
5309 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005310 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005311 garray_T ga;
5312 int i;
5313 char_u *tf;
5314
5315 ga_init2(&ga, 1, 100);
5316 ga_concat(&ga, (char_u *)"function(");
5317 if (fname != NULL)
5318 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005319 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005320 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005321 && vim_isupper(fname[1]))
5322 {
5323 ga_concat(&ga, (char_u *)"'g:");
5324 ga_concat(&ga, fname + 1);
5325 }
5326 else
5327 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005328 vim_free(fname);
5329 }
5330 if (pt != NULL && pt->pt_argc > 0)
5331 {
5332 ga_concat(&ga, (char_u *)", [");
5333 for (i = 0; i < pt->pt_argc; ++i)
5334 {
5335 if (i > 0)
5336 ga_concat(&ga, (char_u *)", ");
5337 ga_concat(&ga,
5338 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5339 vim_free(tf);
5340 }
5341 ga_concat(&ga, (char_u *)"]");
5342 }
5343 if (pt != NULL && pt->pt_dict != NULL)
5344 {
5345 typval_T dtv;
5346
5347 ga_concat(&ga, (char_u *)", ");
5348 dtv.v_type = VAR_DICT;
5349 dtv.vval.v_dict = pt->pt_dict;
5350 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5351 vim_free(tf);
5352 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005353 // terminate with ')' and a NUL
5354 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005355
5356 *tofree = ga.ga_data;
5357 r = *tofree;
5358 break;
5359 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005360
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005361 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005362 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005363 break;
5364
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005366 if (tv->vval.v_list == NULL)
5367 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005368 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005369 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005370 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005371 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005372 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5373 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005374 {
5375 *tofree = NULL;
5376 r = (char_u *)"[...]";
5377 }
5378 else
5379 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005380 int old_copyID = tv->vval.v_list->lv_copyID;
5381
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005382 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005383 *tofree = list2string(tv, copyID, restore_copyID);
5384 if (restore_copyID)
5385 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005386 r = *tofree;
5387 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005388 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005389
Bram Moolenaar8c711452005-01-14 21:53:12 +00005390 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005391 if (tv->vval.v_dict == NULL)
5392 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005393 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005394 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005395 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005396 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005397 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5398 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005399 {
5400 *tofree = NULL;
5401 r = (char_u *)"{...}";
5402 }
5403 else
5404 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005405 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005406
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005407 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005408 *tofree = dict2string(tv, copyID, restore_copyID);
5409 if (restore_copyID)
5410 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005411 r = *tofree;
5412 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005413 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005414
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005415 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005416 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005417 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005418 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005419 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005420 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005421 break;
5422
Bram Moolenaar835dc632016-02-07 14:27:38 +01005423 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005424 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005425#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005426 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005427 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5428 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005429 if (composite_val)
5430 {
5431 *tofree = string_quote(r, FALSE);
5432 r = *tofree;
5433 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005434#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005435 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005436
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005437 case VAR_INSTR:
5438 *tofree = NULL;
5439 r = (char_u *)"instructions";
5440 break;
5441
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005442 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005443#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005444 *tofree = NULL;
5445 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5446 r = numbuf;
5447 break;
5448#endif
5449
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005450 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005451 case VAR_SPECIAL:
5452 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005453 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005454 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005455 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005456
Bram Moolenaar8502c702014-06-17 12:51:16 +02005457 if (--recurse == 0)
5458 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005459 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005460}
5461
5462/*
5463 * Return a string with the string representation of a variable.
5464 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5465 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005466 * Does not put quotes around strings, as ":echo" displays values.
5467 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5468 * May return NULL.
5469 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005470 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005471echo_string(
5472 typval_T *tv,
5473 char_u **tofree,
5474 char_u *numbuf,
5475 int copyID)
5476{
5477 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5478}
5479
5480/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005481 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5482 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005483 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005484 */
5485 int
5486buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5487{
5488 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005489 char_u *t;
5490 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005491
5492 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5493 return -1;
5494
5495 if (lnum > buf->b_ml.ml_line_count)
5496 lnum = buf->b_ml.ml_line_count;
5497
5498 str = ml_get_buf(buf, lnum, FALSE);
5499 if (str == NULL)
5500 return -1;
5501
5502 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005503 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005504
Bram Moolenaar91458462021-01-13 20:08:38 +01005505 // count the number of characters
5506 t = str;
5507 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5508 t += mb_ptr2len(t);
5509
5510 // In insert mode, when the cursor is at the end of a non-empty line,
5511 // byteidx points to the NUL character immediately past the end of the
5512 // string. In this case, add one to the character count.
5513 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5514 count++;
5515
5516 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005517}
5518
5519/*
5520 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005521 * byte index. Works only for loaded buffers. Returns -1 on failure.
5522 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005523 */
5524 int
5525buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5526{
5527 char_u *str;
5528 char_u *t;
5529
5530 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5531 return -1;
5532
5533 if (lnum > buf->b_ml.ml_line_count)
5534 lnum = buf->b_ml.ml_line_count;
5535
5536 str = ml_get_buf(buf, lnum, FALSE);
5537 if (str == NULL)
5538 return -1;
5539
5540 // Convert the character offset to a byte offset
5541 t = str;
5542 while (*t != NUL && --charidx > 0)
5543 t += mb_ptr2len(t);
5544
Bram Moolenaar91458462021-01-13 20:08:38 +01005545 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005546}
5547
5548/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005550 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005552 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005553var2fpos(
5554 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005555 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005556 int *fnum, // set to fnum for '0, 'A, etc.
5557 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005559 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005561 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005563 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005564 if (varp->v_type == VAR_LIST)
5565 {
5566 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005567 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005568 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005569 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005570
5571 l = varp->vval.v_list;
5572 if (l == NULL)
5573 return NULL;
5574
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005575 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005576 pos.lnum = list_find_nr(l, 0L, &error);
5577 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005578 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005579 if (charcol)
5580 len = (long)mb_charlen(ml_get(pos.lnum));
5581 else
5582 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005583
Bram Moolenaarec65d772020-08-20 22:29:12 +02005584 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005585 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005586 li = list_find(l, 1L);
5587 if (li != NULL && li->li_tv.v_type == VAR_STRING
5588 && li->li_tv.vval.v_string != NULL
5589 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005590 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005591 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005592 }
5593 else
5594 {
5595 pos.col = list_find_nr(l, 1L, &error);
5596 if (error)
5597 return NULL;
5598 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005599
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005600 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005601 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005602 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005603 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005604
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005605 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005606 pos.coladd = list_find_nr(l, 2L, &error);
5607 if (error)
5608 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005609
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005610 return &pos;
5611 }
5612
Bram Moolenaarc5809432021-03-27 21:23:30 +01005613 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5614 return NULL;
5615
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005616 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005617 if (name == NULL)
5618 return NULL;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005619 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005620 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005621 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005622 pos = curwin->w_cursor;
5623 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005624 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005625 return &pos;
5626 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005627 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005628 {
5629 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005630 pos = VIsual;
5631 else
5632 pos = curwin->w_cursor;
5633 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005634 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005635 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005636 }
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005637 if (name[0] == '\'' && (!in_vim9script()
5638 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005640 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005641 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5643 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005644 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005645 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 return pp;
5647 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005648
Bram Moolenaara5525202006-03-02 22:52:09 +00005649 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005650
Bram Moolenaar477933c2007-07-17 14:32:23 +00005651 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005652 {
5653 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005654 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005655 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005656 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005657 // In silent Ex mode topline is zero, but that's not a valid line
5658 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005659 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005660 return &pos;
5661 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005662 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005663 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005664 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005665 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005666 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005667 return &pos;
5668 }
5669 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005670 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005672 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 {
5674 pos.lnum = curbuf->b_ml.ml_line_count;
5675 pos.col = 0;
5676 }
5677 else
5678 {
5679 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005680 if (charcol)
5681 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5682 else
5683 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 }
5685 return &pos;
5686 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005687 if (in_vim9script())
5688 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 return NULL;
5690}
5691
5692/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005693 * Convert list in "arg" into a position and optional file number.
5694 * When "fnump" is NULL there is no file number, only 3 items.
5695 * Note that the column is passed on as-is, the caller may want to decrement
5696 * it to use 1 for the first column.
5697 * Return FAIL when conversion is not possible, doesn't check the position for
5698 * validity.
5699 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005700 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005701list2fpos(
5702 typval_T *arg,
5703 pos_T *posp,
5704 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005705 colnr_T *curswantp,
5706 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005707{
5708 list_T *l = arg->vval.v_list;
5709 long i = 0;
5710 long n;
5711
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005712 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5713 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005714 if (arg->v_type != VAR_LIST
5715 || l == NULL
5716 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005717 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005718 return FAIL;
5719
5720 if (fnump != NULL)
5721 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005722 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005723 if (n < 0)
5724 return FAIL;
5725 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005726 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005727 *fnump = n;
5728 }
5729
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005730 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005731 if (n < 0)
5732 return FAIL;
5733 posp->lnum = n;
5734
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005735 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005736 if (n < 0)
5737 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005738 // If character position is specified, then convert to byte position
5739 if (charcol)
5740 {
5741 buf_T *buf;
5742
5743 // Get the text for the specified line in a loaded buffer
5744 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5745 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5746 return FAIL;
5747
Bram Moolenaar91458462021-01-13 20:08:38 +01005748 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005749 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005750 posp->col = n;
5751
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005752 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005753 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005754 posp->coladd = 0;
5755 else
5756 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005757
Bram Moolenaar493c1782014-05-28 14:34:46 +02005758 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005759 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005760
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005761 return OK;
5762}
5763
5764/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 * Get the length of an environment variable name.
5766 * Advance "arg" to the first character after the name.
5767 * Return 0 for error.
5768 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005769 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005770get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771{
5772 char_u *p;
5773 int len;
5774
5775 for (p = *arg; vim_isIDc(*p); ++p)
5776 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005777 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 return 0;
5779
5780 len = (int)(p - *arg);
5781 *arg = p;
5782 return len;
5783}
5784
5785/*
5786 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005787 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 * Return 0 if something is wrong.
5789 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005790 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005791get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792{
5793 char_u *p;
5794 int len;
5795
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005796 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005798 {
5799 if (*p == ':')
5800 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005801 // "s:" is start of "s:var", but "n:" is not and can be used in
5802 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005803 len = (int)(p - *arg);
5804 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5805 || len > 1)
5806 break;
5807 }
5808 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005809 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 return 0;
5811
5812 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005813 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814
5815 return len;
5816}
5817
5818/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005819 * Get the length of the name of a variable or function.
5820 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005822 * Return -1 if curly braces expansion failed.
5823 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 * If the name contains 'magic' {}'s, expand them and return the
5825 * expanded name in an allocated string via 'alias' - caller must free.
5826 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005827 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005828get_name_len(
5829 char_u **arg,
5830 char_u **alias,
5831 int evaluate,
5832 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833{
5834 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 char_u *p;
5836 char_u *expr_start;
5837 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005839 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840
5841 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5842 && (*arg)[2] == (int)KE_SNR)
5843 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005844 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 *arg += 3;
5846 return get_id_len(arg) + 3;
5847 }
5848 len = eval_fname_script(*arg);
5849 if (len > 0)
5850 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005851 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 *arg += len;
5853 }
5854
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005856 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005858 p = find_name_end(*arg, &expr_start, &expr_end,
5859 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 if (expr_start != NULL)
5861 {
5862 char_u *temp_string;
5863
5864 if (!evaluate)
5865 {
5866 len += (int)(p - *arg);
5867 *arg = skipwhite(p);
5868 return len;
5869 }
5870
5871 /*
5872 * Include any <SID> etc in the expanded string:
5873 * Thus the -len here.
5874 */
5875 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5876 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005877 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 *alias = temp_string;
5879 *arg = skipwhite(p);
5880 return (int)STRLEN(temp_string);
5881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882
5883 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005884 // Only give an error when there is something, otherwise it will be
5885 // reported at a higher level.
5886 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005887 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888
5889 return len;
5890}
5891
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005892/*
5893 * Find the end of a variable or function name, taking care of magic braces.
5894 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5895 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005896 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005897 * Return a pointer to just after the name. Equal to "arg" if there is no
5898 * valid name.
5899 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005900 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005901find_name_end(
5902 char_u *arg,
5903 char_u **expr_start,
5904 char_u **expr_end,
5905 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005907 int mb_nest = 0;
5908 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005910 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005911 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005913 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005915 *expr_start = NULL;
5916 *expr_end = NULL;
5917 }
5918
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005919 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005920 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5921 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005922 return arg;
5923
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005924 for (p = arg; *p != NUL
5925 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005926 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005927 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005928 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005929 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005930 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005931 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005932 if (*p == '\'')
5933 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005934 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005935 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005936 ;
5937 if (*p == NUL)
5938 break;
5939 }
5940 else if (*p == '"')
5941 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005942 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005943 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005944 if (*p == '\\' && p[1] != NUL)
5945 ++p;
5946 if (*p == NUL)
5947 break;
5948 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005949 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5950 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005951 // "s:" is start of "s:var", but "n:" is not and can be used in
5952 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005953 len = (int)(p - arg);
5954 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005955 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005956 break;
5957 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005958
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005959 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005961 if (*p == '[')
5962 ++br_nest;
5963 else if (*p == ']')
5964 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005966
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005967 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005969 if (*p == '{')
5970 {
5971 mb_nest++;
5972 if (expr_start != NULL && *expr_start == NULL)
5973 *expr_start = p;
5974 }
5975 else if (*p == '}')
5976 {
5977 mb_nest--;
5978 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5979 *expr_end = p;
5980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 }
5983
5984 return p;
5985}
5986
5987/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005988 * Expands out the 'magic' {}'s in a variable/function name.
5989 * Note that this can call itself recursively, to deal with
5990 * constructs like foo{bar}{baz}{bam}
5991 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5992 * "in_start" ^
5993 * "expr_start" ^
5994 * "expr_end" ^
5995 * "in_end" ^
5996 *
5997 * Returns a new allocated string, which the caller must free.
5998 * Returns NULL for failure.
5999 */
6000 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006001make_expanded_name(
6002 char_u *in_start,
6003 char_u *expr_start,
6004 char_u *expr_end,
6005 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006006{
6007 char_u c1;
6008 char_u *retval = NULL;
6009 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006010
6011 if (expr_end == NULL || in_end == NULL)
6012 return NULL;
6013 *expr_start = NUL;
6014 *expr_end = NUL;
6015 c1 = *in_end;
6016 *in_end = NUL;
6017
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006018 temp_result = eval_to_string(expr_start + 1, FALSE);
6019 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006020 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006021 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6022 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006023 if (retval != NULL)
6024 {
6025 STRCPY(retval, in_start);
6026 STRCAT(retval, temp_result);
6027 STRCAT(retval, expr_end + 1);
6028 }
6029 }
6030 vim_free(temp_result);
6031
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006032 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006033 *expr_start = '{';
6034 *expr_end = '}';
6035
6036 if (retval != NULL)
6037 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006038 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006039 if (expr_start != NULL)
6040 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006041 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006042 temp_result = make_expanded_name(retval, expr_start,
6043 expr_end, temp_result);
6044 vim_free(retval);
6045 retval = temp_result;
6046 }
6047 }
6048
6049 return retval;
6050}
6051
6052/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006054 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006056 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006057eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006059 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006060}
6061
6062/*
6063 * Return TRUE if character "c" can be used as the first character in a
6064 * variable or function name (excluding '{' and '}').
6065 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006066 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006067eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006068{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006069 return ASCII_ISALPHA(c) || c == '_';
6070}
6071
6072/*
6073 * Return TRUE if character "c" can be used as the first character of a
6074 * dictionary key.
6075 */
6076 int
6077eval_isdictc(int c)
6078{
6079 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080}
6081
6082/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006083 * Handle:
6084 * - expr[expr], expr[expr:expr] subscript
6085 * - ".name" lookup
6086 * - function call with Funcref variable: func(expr)
6087 * - method call: var->method()
6088 *
6089 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006090 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006091 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006092 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006093handle_subscript(
6094 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006095 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006096 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006097 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006098 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006099{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006100 int evaluate = evalarg != NULL
6101 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006102 int ret = OK;
6103 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006104 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006105 int getnext;
6106 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006107
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006108 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006109 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006110 // When at the end of the line and ".name" or "->{" or "->X" follows in
6111 // the next line then consume the line break.
6112 p = eval_next_non_blank(*arg, evalarg, &getnext);
6113 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006114 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006115 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6116 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6117 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006118 {
6119 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006120 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006121 check_white = FALSE;
6122 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006123
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006124 if (rettv->v_type == VAR_ANY)
6125 {
6126 char_u *exp_name;
6127 int cc;
6128 int idx;
6129 ufunc_T *ufunc;
6130 type_T *type;
6131
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006132 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006133 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006134 if (**arg != '.')
6135 {
6136 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006137 semsg(_(e_expected_dot_after_name_str),
6138 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006139 ret = FAIL;
6140 break;
6141 }
6142 ++*arg;
6143 if (IS_WHITE_OR_NUL(**arg))
6144 {
6145 if (verbose)
6146 emsg(_(e_no_white_space_allowed_after_dot));
6147 ret = FAIL;
6148 break;
6149 }
6150
6151 // isolate the name
6152 exp_name = *arg;
6153 while (eval_isnamec(**arg))
6154 ++*arg;
6155 cc = **arg;
6156 **arg = NUL;
6157
6158 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00006159 evalarg->eval_cctx, evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006160 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006161
6162 if (idx < 0 && ufunc == NULL)
6163 {
6164 ret = FAIL;
6165 break;
6166 }
6167 if (idx >= 0)
6168 {
6169 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6170 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6171
6172 copy_tv(sv->sv_tv, rettv);
6173 }
6174 else
6175 {
6176 rettv->v_type = VAR_FUNC;
6177 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6178 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006179 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006180 }
6181
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006182 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6183 || rettv->v_type == VAR_PARTIAL))
6184 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006185 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006186 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6187 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006188
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006189 // Stop the expression evaluation when immediately aborting on
6190 // error, or when an interrupt occurred or an exception was thrown
6191 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006192 if (aborting())
6193 {
6194 if (ret == OK)
6195 clear_tv(rettv);
6196 ret = FAIL;
6197 }
6198 dict_unref(selfdict);
6199 selfdict = NULL;
6200 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006201 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006202 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006203 if (in_vim9script())
6204 *arg = skipwhite(p + 2);
6205 else
6206 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006207 if (ret == OK)
6208 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006209 if (VIM_ISWHITE(**arg))
6210 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006211 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006212 ret = FAIL;
6213 }
6214 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006215 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006216 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006217 else
6218 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006219 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006220 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006221 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006222 // "." is ".name" lookup when we found a dict or when evaluating and
6223 // scriptversion is at least 2, where string concatenation is "..".
6224 else if (**arg == '['
6225 || (**arg == '.' && (rettv->v_type == VAR_DICT
6226 || (!evaluate
6227 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006228 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006229 {
6230 dict_unref(selfdict);
6231 if (rettv->v_type == VAR_DICT)
6232 {
6233 selfdict = rettv->vval.v_dict;
6234 if (selfdict != NULL)
6235 ++selfdict->dv_refcount;
6236 }
6237 else
6238 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006239 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006240 {
6241 clear_tv(rettv);
6242 ret = FAIL;
6243 }
6244 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006245 else
6246 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006247 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006248
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006249 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6250 // Don't do this when "Func" is already a partial that was bound
6251 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006252 if (selfdict != NULL
6253 && (rettv->v_type == VAR_FUNC
6254 || (rettv->v_type == VAR_PARTIAL
6255 && (rettv->vval.v_partial->pt_auto
6256 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006257 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006258
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006259 dict_unref(selfdict);
6260 return ret;
6261}
6262
6263/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006264 * Make a copy of an item.
6265 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006266 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006267 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6268 * reference to an already copied list/dict can be used.
6269 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006270 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006271 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006272item_copy(
6273 typval_T *from,
6274 typval_T *to,
6275 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006276 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006277 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006278{
6279 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006280 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006281
Bram Moolenaar33570922005-01-25 22:26:29 +00006282 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006283 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006284 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006285 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006286 }
6287 ++recurse;
6288
6289 switch (from->v_type)
6290 {
6291 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006292 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006293 case VAR_STRING:
6294 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006295 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006296 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006297 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006298 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006299 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006300 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006301 copy_tv(from, to);
6302 break;
6303 case VAR_LIST:
6304 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006305 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006306 if (from->vval.v_list == NULL)
6307 to->vval.v_list = NULL;
6308 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6309 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006310 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006311 to->vval.v_list = from->vval.v_list->lv_copylist;
6312 ++to->vval.v_list->lv_refcount;
6313 }
6314 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006315 to->vval.v_list = list_copy(from->vval.v_list,
6316 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006317 if (to->vval.v_list == NULL)
6318 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006319 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006320 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006321 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006322 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006323 case VAR_DICT:
6324 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006325 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006326 if (from->vval.v_dict == NULL)
6327 to->vval.v_dict = NULL;
6328 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6329 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006330 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006331 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6332 ++to->vval.v_dict->dv_refcount;
6333 }
6334 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006335 to->vval.v_dict = dict_copy(from->vval.v_dict,
6336 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006337 if (to->vval.v_dict == NULL)
6338 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006339 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006340 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006341 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006342 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006343 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006344 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006345 }
6346 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006347 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006348}
6349
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006350 void
6351echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6352{
6353 char_u *tofree;
6354 char_u numbuf[NUMBUFLEN];
6355 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6356
6357 if (*atstart)
6358 {
6359 *atstart = FALSE;
6360 // Call msg_start() after eval1(), evaluating the expression
6361 // may cause a message to appear.
6362 if (with_space)
6363 {
6364 // Mark the saved text as finishing the line, so that what
6365 // follows is displayed on a new line when scrolling back
6366 // at the more prompt.
6367 msg_sb_eol();
6368 msg_start();
6369 }
6370 }
6371 else if (with_space)
6372 msg_puts_attr(" ", echo_attr);
6373
6374 if (p != NULL)
6375 for ( ; *p != NUL && !got_int; ++p)
6376 {
6377 if (*p == '\n' || *p == '\r' || *p == TAB)
6378 {
6379 if (*p != TAB && *needclr)
6380 {
6381 // remove any text still there from the command
6382 msg_clr_eos();
6383 *needclr = FALSE;
6384 }
6385 msg_putchar_attr(*p, echo_attr);
6386 }
6387 else
6388 {
6389 if (has_mbyte)
6390 {
6391 int i = (*mb_ptr2len)(p);
6392
6393 (void)msg_outtrans_len_attr(p, i, echo_attr);
6394 p += i - 1;
6395 }
6396 else
6397 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6398 }
6399 }
6400 vim_free(tofree);
6401}
6402
Bram Moolenaare9a41262005-01-15 22:18:47 +00006403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 * ":echo expr1 ..." print each argument separated with a space, add a
6405 * newline at the end.
6406 * ":echon expr1 ..." print each argument plain.
6407 */
6408 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006409ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410{
6411 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006412 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006413 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 int needclr = TRUE;
6415 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006416 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006417 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006418 evalarg_T evalarg;
6419
Bram Moolenaare707c882020-07-01 13:07:37 +02006420 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421
6422 if (eap->skip)
6423 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006424 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006426 // If eval1() causes an error message the text from the command may
6427 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006428 need_clr_eos = needclr;
6429
Bram Moolenaar68db9962021-05-09 23:19:22 +02006430 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006431 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 {
6433 /*
6434 * Report the invalid expression unless the expression evaluation
6435 * has been cancelled due to an aborting error, an interrupt, or an
6436 * exception.
6437 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006438 if (!aborting() && did_emsg == did_emsg_before
6439 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006440 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006441 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 break;
6443 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006444 need_clr_eos = FALSE;
6445
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006447 {
6448 if (rettv.v_type == VAR_VOID)
6449 {
6450 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6451 break;
6452 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006453 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006454 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006455
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006456 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 arg = skipwhite(arg);
6458 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006459 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006460 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461
6462 if (eap->skip)
6463 --emsg_skip;
6464 else
6465 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006466 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 if (needclr)
6468 msg_clr_eos();
6469 if (eap->cmdidx == CMD_echo)
6470 msg_end();
6471 }
6472}
6473
6474/*
6475 * ":echohl {name}".
6476 */
6477 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006478ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006480 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481}
6482
6483/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006484 * Returns the :echo attribute
6485 */
6486 int
6487get_echo_attr(void)
6488{
6489 return echo_attr;
6490}
6491
6492/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 * ":execute expr1 ..." execute the result of an expression.
6494 * ":echomsg expr1 ..." Print a message
6495 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006496 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 * Each gets spaces around each argument and a newline at the end for
6498 * echo commands
6499 */
6500 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006501ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502{
6503 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006504 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 int ret = OK;
6506 char_u *p;
6507 garray_T ga;
6508 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006509 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510
6511 ga_init2(&ga, 1, 80);
6512
6513 if (eap->skip)
6514 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006515 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006517 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006518 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520
6521 if (!eap->skip)
6522 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006523 char_u buf[NUMBUFLEN];
6524
6525 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006526 {
6527 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6528 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006529 semsg(_(e_using_invalid_value_as_string_str),
6530 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006531 p = NULL;
6532 }
6533 else
6534 p = tv_get_string_buf(&rettv, buf);
6535 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006536 else
6537 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006538 if (p == NULL)
6539 {
6540 clear_tv(&rettv);
6541 ret = FAIL;
6542 break;
6543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 len = (int)STRLEN(p);
6545 if (ga_grow(&ga, len + 2) == FAIL)
6546 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006547 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 ret = FAIL;
6549 break;
6550 }
6551 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 ga.ga_len += len;
6555 }
6556
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006557 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 arg = skipwhite(arg);
6559 }
6560
6561 if (ret != FAIL && ga.ga_data != NULL)
6562 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006563 // use the first line of continuation lines for messages
6564 SOURCING_LNUM = start_lnum;
6565
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006566 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6567 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006568 // Mark the already saved text as finishing the line, so that what
6569 // follows is displayed on a new line when scrolling back at the
6570 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006571 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006572 }
6573
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006575 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006576 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006577 out_flush();
6578 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006579 else if (eap->cmdidx == CMD_echoconsole)
6580 {
6581 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6582 ui_write((char_u *)"\r\n", 2, TRUE);
6583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 else if (eap->cmdidx == CMD_echoerr)
6585 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006586 int save_did_emsg = did_emsg;
6587
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006588 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006589 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 if (!force_abort)
6591 did_emsg = save_did_emsg;
6592 }
6593 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006594 {
6595 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6596
6597 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6598 sticky_cmdmod_flags = cmdmod.cmod_flags
6599 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 do_cmdline((char_u *)ga.ga_data,
6601 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006602 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 }
6605
6606 ga_clear(&ga);
6607
6608 if (eap->skip)
6609 --emsg_skip;
6610
Bram Moolenaar63b91732021-08-05 20:40:03 +02006611 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612}
6613
6614/*
6615 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6616 * "arg" points to the "&" or '+' when called, to "option" when returning.
6617 * Returns NULL when no option name found. Otherwise pointer to the char
6618 * after the option name.
6619 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006620 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006621find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622{
6623 char_u *p = *arg;
6624
6625 ++p;
6626 if (*p == 'g' && p[1] == ':')
6627 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006628 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 p += 2;
6630 }
6631 else if (*p == 'l' && p[1] == ':')
6632 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006633 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 p += 2;
6635 }
6636 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006637 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638
6639 if (!ASCII_ISALPHA(*p))
6640 return NULL;
6641 *arg = p;
6642
6643 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006644 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 else
6646 while (ASCII_ISALPHA(*p))
6647 ++p;
6648 return p;
6649}
6650
6651/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006652 * Display script name where an item was last set.
6653 * Should only be invoked when 'verbose' is non-zero.
6654 */
6655 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006656last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006657{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006658 char_u *p;
6659
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006660 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006661 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006662 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006663 if (p != NULL)
6664 {
6665 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006666 msg_puts(_("\n\tLast set from "));
6667 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006668 if (script_ctx.sc_lnum > 0)
6669 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006670 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006671 msg_outnum((long)script_ctx.sc_lnum);
6672 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006673 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006674 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006675 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006676 }
6677}
6678
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006679#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680
6681/*
6682 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006683 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 * "flags" can be "g" to do a global substitute.
6685 * Returns an allocated string, NULL for error.
6686 */
6687 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006688do_string_sub(
6689 char_u *str,
6690 char_u *pat,
6691 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006692 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006693 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694{
6695 int sublen;
6696 regmatch_T regmatch;
6697 int i;
6698 int do_all;
6699 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006700 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 garray_T ga;
6702 char_u *ret;
6703 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006704 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006706 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006708 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709
6710 ga_init2(&ga, 1, 200);
6711
6712 do_all = (flags[0] == 'g');
6713
6714 regmatch.rm_ic = p_ic;
6715 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6716 if (regmatch.regprog != NULL)
6717 {
6718 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006719 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6721 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006722 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006723 if (regmatch.startp[0] == regmatch.endp[0])
6724 {
6725 if (zero_width == regmatch.startp[0])
6726 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006727 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006728 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006729 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6730 (size_t)i);
6731 ga.ga_len += i;
6732 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006733 continue;
6734 }
6735 zero_width = regmatch.startp[0];
6736 }
6737
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 /*
6739 * Get some space for a temporary buffer to do the substitution
6740 * into. It will contain:
6741 * - The text up to where the match is.
6742 * - The substituted text.
6743 * - The text after the match.
6744 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006745 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006746 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6748 {
6749 ga_clear(&ga);
6750 break;
6751 }
6752
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006753 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 i = (int)(regmatch.startp[0] - tail);
6755 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006756 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006757 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 + ga.ga_len + i, TRUE, TRUE, FALSE);
6759 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006760 tail = regmatch.endp[0];
6761 if (*tail == NUL)
6762 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 if (!do_all)
6764 break;
6765 }
6766
6767 if (ga.ga_data != NULL)
6768 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6769
Bram Moolenaar473de612013-06-08 18:19:48 +02006770 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 }
6772
6773 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6774 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006775 if (p_cpo == empty_option)
6776 p_cpo = save_cpo;
6777 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006778 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006779 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006780 // If it's still empty it was changed and restored, need to restore in
6781 // the complicated way.
6782 if (*p_cpo == NUL)
6783 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006784 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786
6787 return ret;
6788}