blob: 317446c7623da7a904ea53b5bc1e1621091e9868 [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;
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +0000143 if (sourcing_a_script(eap))
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200144 {
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)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100642 {
643 dictitem_T *v;
644
645 // If <SID>VarName was used it would not be found, try another way.
646 v = find_var_also_in_script(name, NULL, FALSE);
647 if (v == NULL)
648 return NULL;
649 copy_tv(&v->di_tv, &ref);
650 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000651 if (*skipwhite(*arg) != NUL)
652 {
653 if (verbose)
654 semsg(_(e_trailing_characters_str), *arg);
655 name = NULL;
656 }
657 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
658 {
659 name = ref.vval.v_string;
660 ref.vval.v_string = NULL;
661 *tofree = name;
662 }
663 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
664 {
665 if (ref.vval.v_partial->pt_argc > 0
666 || ref.vval.v_partial->pt_dict != NULL)
667 {
668 if (verbose)
669 emsg(_(e_cannot_use_partial_here));
670 name = NULL;
671 }
672 else
673 {
674 name = vim_strsave(partial_name(ref.vval.v_partial));
675 *tofree = name;
676 }
677 }
678 else
679 {
680 if (verbose)
681 semsg(_(e_not_callable_type_str), name);
682 name = NULL;
683 }
684 clear_tv(&ref);
685 return name;
686}
687
688/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100689 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200690 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
691 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000692 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200694 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100695call_vim_function(
696 char_u *func,
697 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200698 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200699 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000701 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200702 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000703 char_u *arg;
704 char_u *name;
705 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000706 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100708 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200709 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000710 funcexe.fe_firstline = curwin->w_cursor.lnum;
711 funcexe.fe_lastline = curwin->w_cursor.lnum;
712 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000713
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000714 // The name might be "import.Func" or "Funcref". We don't know, we need to
715 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000716 // autoload script has errors. Guess that when there is a dot in the name
717 // showing errors is the right choice.
718 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000719 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000720 if (ignore_errors)
721 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000722 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000723 if (ignore_errors)
724 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000725 if (name == NULL)
726 name = func;
727
728 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
729
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000730 if (ret == FAIL)
731 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000732 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000733
734 return ret;
735}
736
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100737/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100738 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000739 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
740 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000741 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000742 */
743 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100744call_func_retstr(
745 char_u *func,
746 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200747 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000748{
749 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000750 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000751
Bram Moolenaarded27a12018-08-01 19:06:03 +0200752 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000753 return NULL;
754
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100755 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000756 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 return retval;
758}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000759
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000760/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100761 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000762 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000763 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000764 */
765 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100766call_func_retlist(
767 char_u *func,
768 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200769 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000770{
771 typval_T rettv;
772
Bram Moolenaarded27a12018-08-01 19:06:03 +0200773 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000774 return NULL;
775
776 if (rettv.v_type != VAR_LIST)
777 {
778 clear_tv(&rettv);
779 return NULL;
780 }
781
782 return rettv.vval.v_list;
783}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784
Bram Moolenaare70dd112022-01-21 16:31:11 +0000785#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100787 * Evaluate "arg", which is 'foldexpr'.
788 * Note: caller must set "curwin" to match "arg".
789 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
790 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 */
792 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000793eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000795 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000796 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200797 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000799 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000800 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
801 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802
Bram Moolenaare70dd112022-01-21 16:31:11 +0000803 arg = wp->w_p_fde;
804 current_sctx = wp->w_p_script_ctx[WV_FDE];
805
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000807 if (use_sandbox)
808 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200809 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200811 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 retval = 0;
813 else
814 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100815 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000816 if (tv.v_type == VAR_NUMBER)
817 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000818 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 retval = 0;
820 else
821 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100822 // If the result is a string, check if there is a non-digit before
823 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000824 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 if (!VIM_ISDIGIT(*s) && *s != '-')
826 *cp = *s++;
827 retval = atol((char *)s);
828 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000829 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 }
831 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000832 if (use_sandbox)
833 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200834 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200835 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000836 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200838 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839}
840#endif
841
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000843 * Get an lval: variable, Dict item or List item that can be assigned a value
844 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
845 * "name.key", "name.key[expr]" etc.
846 * Indexing only works if "name" is an existing List or Dictionary.
847 * "name" points to the start of the name.
848 * If "rettv" is not NULL it points to the value to be assigned.
849 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
850 * wrong; must end in space or cmd separator.
851 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100852 * flags:
853 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100854 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100855 * GLV_NO_AUTOLOAD: do not use script autoloading
856 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000857 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000858 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000859 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000860 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200861 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100862get_lval(
863 char_u *name,
864 typval_T *rettv,
865 lval_T *lp,
866 int unlet,
867 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100868 int flags, // GLV_ values
869 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000870{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000871 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000872 char_u *expr_start, *expr_end;
873 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000874 dictitem_T *v;
875 typval_T var1;
876 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000877 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000878 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000879 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100880 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100881 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100882 int writing;
Bram Moolenaar4525a572022-02-13 11:57:33 +0000883 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000884
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100885 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200886 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000887
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100888 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000889 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100890 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000891 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200892 lp->ll_name_end = find_name_end(name, NULL, NULL,
893 FNE_INCL_BR | fne_flags);
894 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000895 }
896
Bram Moolenaara749a422022-02-12 19:52:25 +0000897 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +0000898 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +0000899 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
900 {
901 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
902 return NULL;
903 }
904
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100905 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000906 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100907 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000908 if (expr_start != NULL)
909 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100910 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100911 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000912 && *p != '[' && *p != '.')
913 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000914 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000915 return NULL;
916 }
917
918 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
919 if (lp->ll_exp_name == NULL)
920 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100921 // Report an invalid expression in braces, unless the
922 // expression evaluation has been cancelled due to an
923 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000924 if (!aborting() && !quiet)
925 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000926 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000927 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000928 return NULL;
929 }
930 }
931 lp->ll_name = lp->ll_exp_name;
932 }
933 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000935 lp->ll_name = name;
936
Bram Moolenaar4525a572022-02-13 11:57:33 +0000937 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100938 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200939 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +0000940 // However, "g:[key]" is indexing a dictionary.
941 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200942 {
943 --p;
944 lp->ll_name_end = p;
945 }
946 if (*p == ':')
947 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000948 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +0200949
950 if (tp == p + 1 && !quiet)
951 {
952 semsg(_(e_white_space_required_after_str_str), ":", p);
953 return NULL;
954 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100955
Bram Moolenaar4c8b5462022-03-16 20:01:39 +0000956 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000957 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +0000958 semsg(_(e_using_type_not_in_script_context_str), p);
959 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000960 }
961
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200962 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +0000963 lp->ll_type = parse_type(&tp,
964 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
965 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100966 if (lp->ll_type == NULL && !quiet)
967 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200968 lp->ll_name_end = tp;
969 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100970 }
971 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000972 if (lp->ll_name == NULL)
973 return p;
974
Bram Moolenaar62aec932022-01-29 21:45:34 +0000975 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000976 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000977 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000978
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000979 if (import != NULL)
980 {
981 ufunc_T *ufunc;
982 type_T *type;
983
984 lp->ll_sid = import->imp_sid;
985 lp->ll_name = skipwhite(p + 1);
986 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
987 lp->ll_name_end = p;
988
989 // check the item is exported
990 cc = *p;
991 *p = NUL;
992 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000993 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000994 {
995 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +0000996 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000997 }
998 *p = cc;
999 }
1000 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001001
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001002 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001003 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001004 return p;
1005
Bram Moolenaar4525a572022-02-13 11:57:33 +00001006 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001007 {
1008 // using local variable
1009 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001010 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001011 }
1012 else
1013 {
1014 cc = *p;
1015 *p = NUL;
1016 // When we would write to the variable pass &ht and prevent autoload.
1017 writing = !(flags & GLV_READ_ONLY);
1018 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001019 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001020 if (v == NULL && !quiet)
1021 semsg(_(e_undefined_variable_str), lp->ll_name);
1022 *p = cc;
1023 if (v == NULL)
1024 return NULL;
1025 lp->ll_tv = &v->di_tv;
1026 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001027
Bram Moolenaar4525a572022-02-13 11:57:33 +00001028 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001029 {
1030 if (!quiet)
1031 semsg(_(e_variable_already_declared), lp->ll_name);
1032 return NULL;
1033 }
1034
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001035 /*
1036 * Loop until no more [idx] or .key is following.
1037 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001038 var1.v_type = VAR_UNKNOWN;
1039 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001040 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001041 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001042 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1043 {
1044 if (!quiet)
1045 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1046 return NULL;
1047 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001048 if (lp->ll_tv->v_type != VAR_LIST
1049 && lp->ll_tv->v_type != VAR_DICT
1050 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001051 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001052 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001053 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001054 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001055 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001056
1057 // a NULL list/blob works like an empty list/blob, allocate one now.
1058 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1059 rettv_list_alloc(lp->ll_tv);
1060 else if (lp->ll_tv->v_type == VAR_BLOB
1061 && lp->ll_tv->vval.v_blob == NULL)
1062 rettv_blob_alloc(lp->ll_tv);
1063
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001064 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001065 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001066 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001067 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001068 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001069 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001070
Bram Moolenaar4525a572022-02-13 11:57:33 +00001071 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001072 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001073 && lp->ll_tv == &v->di_tv
1074 && ht != NULL && ht == get_script_local_ht())
1075 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001076 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001077
1078 // Vim9 script local variable: get the type
1079 if (sv != NULL)
1080 lp->ll_valtype = sv->sv_type;
1081 }
1082
Bram Moolenaar8c711452005-01-14 21:53:12 +00001083 len = -1;
1084 if (*p == '.')
1085 {
1086 key = p + 1;
1087 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1088 ;
1089 if (len == 0)
1090 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001091 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001092 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001093 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001094 }
1095 p = key + len;
1096 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001097 else
1098 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001099 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001100 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001101 if (*p == ':')
1102 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001103 else
1104 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001105 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001106 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001107 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001108 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001109 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001110 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001111 clear_tv(&var1);
1112 return NULL;
1113 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001114 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001115 }
1116
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001117 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001118 if (*p == ':')
1119 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001120 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001121 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001122 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001123 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001124 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001125 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001126 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001127 if (rettv != NULL
1128 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001129 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001130 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001131 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001132 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001133 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001134 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001135 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001136 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001137 }
1138 p = skipwhite(p + 1);
1139 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001140 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001141 else
1142 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001143 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001144 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001145 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001146 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001147 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001148 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001149 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001150 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001151 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001152 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001153 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001154 clear_tv(&var2);
1155 return NULL;
1156 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001157 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001158 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001159 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001160 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001161 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001162
Bram Moolenaar8c711452005-01-14 21:53:12 +00001163 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001164 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001165 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001166 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001167 clear_tv(&var1);
1168 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001169 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001170 }
1171
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001172 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001173 ++p;
1174 }
1175
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001176 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001177 {
1178 if (len == -1)
1179 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001180 // "[key]": get key from "var1"
1181 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001182 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001183 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001184 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001185 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001186 }
1187 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001188 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001189
1190 // a NULL dict is equivalent with an empty dict
1191 if (lp->ll_tv->vval.v_dict == NULL)
1192 {
1193 lp->ll_tv->vval.v_dict = dict_alloc();
1194 if (lp->ll_tv->vval.v_dict == NULL)
1195 {
1196 clear_tv(&var1);
1197 return NULL;
1198 }
1199 ++lp->ll_tv->vval.v_dict->dv_refcount;
1200 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001201 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001202
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001203 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001204
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001205 // When assigning to a scope dictionary check that a function and
1206 // variable name is valid (only variable name unless it is l: or
1207 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001208 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001209 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001210 int prevval;
1211 int wrong;
1212
1213 if (len != -1)
1214 {
1215 prevval = key[len];
1216 key[len] = NUL;
1217 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001218 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001219 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001220 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1221 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001222 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001223 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001224 if (len != -1)
1225 key[len] = prevval;
1226 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001227 {
1228 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001229 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001230 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001231 }
1232
Bram Moolenaar10c65862020-10-08 21:16:42 +02001233 if (lp->ll_valtype != NULL)
1234 // use the type of the member
1235 lp->ll_valtype = lp->ll_valtype->tt_member;
1236
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001237 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001238 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001239 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001240 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001241 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001242 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001243 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001244 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001245 return NULL;
1246 }
1247
Bram Moolenaar31b81602019-02-10 22:14:27 +01001248 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001249 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001250 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001251 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001252 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001253 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001254 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001255 }
1256 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001257 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001258 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001259 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001260 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001261 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001262 p = NULL;
1263 break;
1264 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001265 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001266 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001267 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1268 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001269 {
1270 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001271 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001272 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001273
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001274 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001275 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001276 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001277 else if (lp->ll_tv->v_type == VAR_BLOB)
1278 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001279 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1280
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001281 /*
1282 * Get the number and item for the only or first index of the List.
1283 */
1284 if (empty1)
1285 lp->ll_n1 = 0;
1286 else
1287 // is number or string
1288 lp->ll_n1 = (long)tv_get_number(&var1);
1289 clear_tv(&var1);
1290
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001291 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001292 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001293 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001294 return NULL;
1295 }
1296 if (lp->ll_range && !lp->ll_empty2)
1297 {
1298 lp->ll_n2 = (long)tv_get_number(&var2);
1299 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001300 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1301 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001302 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001303 }
1304 lp->ll_blob = lp->ll_tv->vval.v_blob;
1305 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001306 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001307 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001308 else
1309 {
1310 /*
1311 * Get the number and item for the only or first index of the List.
1312 */
1313 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001314 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001315 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001316 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001317 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001318 clear_tv(&var1);
1319
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001320 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001321 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001322 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1323 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001324 if (lp->ll_li == NULL)
1325 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001326 clear_tv(&var2);
1327 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001328 }
1329
Bram Moolenaar10c65862020-10-08 21:16:42 +02001330 if (lp->ll_valtype != NULL)
1331 // use the type of the member
1332 lp->ll_valtype = lp->ll_valtype->tt_member;
1333
Bram Moolenaar8c711452005-01-14 21:53:12 +00001334 /*
1335 * May need to find the item or absolute index for the second
1336 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001337 * When no index given: "lp->ll_empty2" is TRUE.
1338 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001339 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001340 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001341 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001342 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001343 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001344 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001345 if (check_range_index_two(lp->ll_list,
1346 &lp->ll_n1, lp->ll_li,
1347 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001348 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001349 }
1350
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001351 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001352 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001353 }
1354
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001355 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001357 return p;
1358}
1359
1360/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001361 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001362 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001363 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001364clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001365{
1366 vim_free(lp->ll_exp_name);
1367 vim_free(lp->ll_newkey);
1368}
1369
1370/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001371 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001372 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001373 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1374 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001375 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001376 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001377set_var_lval(
1378 lval_T *lp,
1379 char_u *endp,
1380 typval_T *rettv,
1381 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001382 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1383 char_u *op,
1384 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001385{
1386 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001387 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001388
1389 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001390 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001391 cc = *endp;
1392 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001393 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1394 return;
1395
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001396 if (lp->ll_blob != NULL)
1397 {
1398 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001399
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001400 if (op != NULL && *op != '=')
1401 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001402 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001403 return;
1404 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001405 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001406 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001407
1408 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1409 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001410 if (lp->ll_empty2)
1411 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1412
Bram Moolenaar68452172021-04-12 21:21:02 +02001413 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1414 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001415 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001416 }
1417 else
1418 {
1419 val = (int)tv_get_number_chk(rettv, &error);
1420 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001421 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001422 }
1423 }
1424 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001425 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001426 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001427
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001428 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1429 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001430 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001431 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001432 *endp = cc;
1433 return;
1434 }
1435
Bram Moolenaarff697e62019-02-12 22:28:33 +01001436 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001437 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001438 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001439 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001440 {
1441 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001442 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1443 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001444 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001445 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001446 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001447 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001448 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001449 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001450 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001451 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001452 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1453 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001454 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001455 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001456 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001457 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001458 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001459 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001460 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001461 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001462 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001463 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001464 else if (lp->ll_range)
1465 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001466 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1467 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001468 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001469 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001470 return;
1471 }
1472
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001473 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1474 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001475 }
1476 else
1477 {
1478 /*
1479 * Assign to a List or Dictionary item.
1480 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001481 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1482 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001483 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001484 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001485 return;
1486 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001487
1488 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001489 && check_typval_arg_type(lp->ll_valtype, rettv,
1490 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001491 return;
1492
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001493 if (lp->ll_newkey != NULL)
1494 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001495 if (op != NULL && *op != '=')
1496 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001497 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001498 return;
1499 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001500 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1501 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001502 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001503
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001504 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001505 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001506 if (di == NULL)
1507 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001508 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1509 {
1510 vim_free(di);
1511 return;
1512 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001513 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001514 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001515 else if (op != NULL && *op != '=')
1516 {
1517 tv_op(lp->ll_tv, rettv, op);
1518 return;
1519 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001520 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001521 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001522
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001523 /*
1524 * Assign the value to the variable or list item.
1525 */
1526 if (copy)
1527 copy_tv(rettv, lp->ll_tv);
1528 else
1529 {
1530 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001531 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001532 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001533 }
1534 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001535}
1536
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001537/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001538 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1539 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001540 * Returns OK or FAIL.
1541 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001542 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001543tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001544{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001545 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001546 char_u numbuf[NUMBUFLEN];
1547 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001548 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001549
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001550 // Can't do anything with a Funcref or Dict on the right.
1551 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001552 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001553 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1554 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001555 {
1556 switch (tv1->v_type)
1557 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001558 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001559 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001561 case VAR_DICT:
1562 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001563 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001564 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001565 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001566 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001567 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001568 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001569 break;
1570
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001571 case VAR_BLOB:
1572 if (*op != '+' || tv2->v_type != VAR_BLOB)
1573 break;
1574 // BLOB += BLOB
1575 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1576 {
1577 blob_T *b1 = tv1->vval.v_blob;
1578 blob_T *b2 = tv2->vval.v_blob;
1579 int i, len = blob_len(b2);
1580 for (i = 0; i < len; i++)
1581 ga_append(&b1->bv_ga, blob_get(b2, i));
1582 }
1583 return OK;
1584
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001585 case VAR_LIST:
1586 if (*op != '+' || tv2->v_type != VAR_LIST)
1587 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001588 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001589 if (tv2->vval.v_list != NULL)
1590 {
1591 if (tv1->vval.v_list == NULL)
1592 {
1593 tv1->vval.v_list = tv2->vval.v_list;
1594 ++tv1->vval.v_list->lv_refcount;
1595 }
1596 else
1597 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1598 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001599 return OK;
1600
1601 case VAR_NUMBER:
1602 case VAR_STRING:
1603 if (tv2->v_type == VAR_LIST)
1604 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001605 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001606 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001607 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001608 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001609#ifdef FEAT_FLOAT
1610 if (tv2->v_type == VAR_FLOAT)
1611 {
1612 float_T f = n;
1613
Bram Moolenaarff697e62019-02-12 22:28:33 +01001614 if (*op == '%')
1615 break;
1616 switch (*op)
1617 {
1618 case '+': f += tv2->vval.v_float; break;
1619 case '-': f -= tv2->vval.v_float; break;
1620 case '*': f *= tv2->vval.v_float; break;
1621 case '/': f /= tv2->vval.v_float; break;
1622 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001623 clear_tv(tv1);
1624 tv1->v_type = VAR_FLOAT;
1625 tv1->vval.v_float = f;
1626 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001627 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001628#endif
1629 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001630 switch (*op)
1631 {
1632 case '+': n += tv_get_number(tv2); break;
1633 case '-': n -= tv_get_number(tv2); break;
1634 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001635 case '/': n = num_divide(n, tv_get_number(tv2),
1636 &failed); break;
1637 case '%': n = num_modulus(n, tv_get_number(tv2),
1638 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001639 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001640 clear_tv(tv1);
1641 tv1->v_type = VAR_NUMBER;
1642 tv1->vval.v_number = n;
1643 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001644 }
1645 else
1646 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001647 if (tv2->v_type == VAR_FLOAT)
1648 break;
1649
Bram Moolenaarff697e62019-02-12 22:28:33 +01001650 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001651 s = tv_get_string(tv1);
1652 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001653 clear_tv(tv1);
1654 tv1->v_type = VAR_STRING;
1655 tv1->vval.v_string = s;
1656 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001657 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001658
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001659 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001660#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001661 {
1662 float_T f;
1663
Bram Moolenaarff697e62019-02-12 22:28:33 +01001664 if (*op == '%' || *op == '.'
1665 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001666 && tv2->v_type != VAR_NUMBER
1667 && tv2->v_type != VAR_STRING))
1668 break;
1669 if (tv2->v_type == VAR_FLOAT)
1670 f = tv2->vval.v_float;
1671 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001672 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001673 switch (*op)
1674 {
1675 case '+': tv1->vval.v_float += f; break;
1676 case '-': tv1->vval.v_float -= f; break;
1677 case '*': tv1->vval.v_float *= f; break;
1678 case '/': tv1->vval.v_float /= f; break;
1679 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001680 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001681#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001682 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001683 }
1684 }
1685
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001686 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001687 return FAIL;
1688}
1689
1690/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001691 * Evaluate the expression used in a ":for var in expr" command.
1692 * "arg" points to "var".
1693 * Set "*errp" to TRUE for an error, FALSE otherwise;
1694 * Return a pointer that holds the info. Null when there is an error.
1695 */
1696 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001697eval_for_line(
1698 char_u *arg,
1699 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001700 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001701 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001702{
Bram Moolenaar33570922005-01-25 22:26:29 +00001703 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001704 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001705 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001706 typval_T tv;
1707 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001708 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001709
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001710 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001711
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001712 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001713 if (fi == NULL)
1714 return NULL;
1715
Bram Moolenaar404557e2021-07-05 21:41:48 +02001716 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1717 &fi->fi_semicolon, FALSE);
1718 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001719 return fi;
1720
Bram Moolenaar404557e2021-07-05 21:41:48 +02001721 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001722 if (expr[0] != 'i' || expr[1] != 'n'
1723 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001724 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001725 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1726 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1727 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001728 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001729 return fi;
1730 }
1731
1732 if (skip)
1733 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001734 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1735 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001736 {
1737 *errp = FALSE;
1738 if (!skip)
1739 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001740 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001741 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001742 l = tv.vval.v_list;
1743 if (l == NULL)
1744 {
1745 // a null list is like an empty list: do nothing
1746 clear_tv(&tv);
1747 }
1748 else
1749 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001751 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001753 // No need to increment the refcount, it's already set for
1754 // the list being used in "tv".
1755 fi->fi_list = l;
1756 list_add_watch(l, &fi->fi_lw);
1757 fi->fi_lw.lw_item = l->lv_first;
1758 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001759 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001760 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001761 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001762 fi->fi_bi = 0;
1763 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001764 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001765 typval_T btv;
1766
1767 // Make a copy, so that the iteration still works when the
1768 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001769 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001770 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001771 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001772 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001773 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001774 else if (tv.v_type == VAR_STRING)
1775 {
1776 fi->fi_byte_idx = 0;
1777 fi->fi_string = tv.vval.v_string;
1778 tv.vval.v_string = NULL;
1779 if (fi->fi_string == NULL)
1780 fi->fi_string = vim_strsave((char_u *)"");
1781 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001782 else
1783 {
Sean Dewar80d73952021-08-04 19:25:54 +02001784 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001785 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001786 }
1787 }
1788 }
1789 if (skip)
1790 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001791 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001792
1793 return fi;
1794}
1795
1796/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001797 * Used when looping over a :for line, skip the "in expr" part.
1798 */
1799 void
1800skip_for_lines(void *fi_void, evalarg_T *evalarg)
1801{
1802 forinfo_T *fi = (forinfo_T *)fi_void;
1803 int i;
1804
1805 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01001806 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001807}
1808
1809/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001810 * Use the first item in a ":for" list. Advance to the next.
1811 * Assign the values to the variable (list). "arg" points to the first one.
1812 * Return TRUE when a valid item was found, FALSE when at end of list or
1813 * something wrong.
1814 */
1815 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001816next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001817{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001818 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001819 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001820 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001821 ? (ASSIGN_FINAL
1822 // first round: error if variable exists
1823 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1824 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001825 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001826 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001827 int skip_assign = in_vim9script() && arg[0] == '_'
1828 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001829
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001830 if (fi->fi_blob != NULL)
1831 {
1832 typval_T tv;
1833
1834 if (fi->fi_bi >= blob_len(fi->fi_blob))
1835 return FALSE;
1836 tv.v_type = VAR_NUMBER;
1837 tv.v_lock = VAR_FIXED;
1838 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1839 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001840 if (skip_assign)
1841 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001842 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001843 fi->fi_varcount, flag, NULL) == OK;
1844 }
1845
1846 if (fi->fi_string != NULL)
1847 {
1848 typval_T tv;
1849 int len;
1850
1851 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1852 if (len == 0)
1853 return FALSE;
1854 tv.v_type = VAR_STRING;
1855 tv.v_lock = VAR_FIXED;
1856 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1857 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001858 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001859 if (skip_assign)
1860 result = TRUE;
1861 else
1862 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001863 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001864 vim_free(tv.vval.v_string);
1865 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001866 }
1867
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001868 item = fi->fi_lw.lw_item;
1869 if (item == NULL)
1870 result = FALSE;
1871 else
1872 {
1873 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001874 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001875 if (skip_assign)
1876 result = TRUE;
1877 else
1878 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001879 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001880 }
1881 return result;
1882}
1883
1884/*
1885 * Free the structure used to store info used by ":for".
1886 */
1887 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001888free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001889{
Bram Moolenaar33570922005-01-25 22:26:29 +00001890 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001891
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001892 if (fi == NULL)
1893 return;
1894 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001895 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001896 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001897 list_unref(fi->fi_list);
1898 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001899 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001900 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001901 else
1902 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001903 vim_free(fi);
1904}
1905
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001907set_context_for_expression(
1908 expand_T *xp,
1909 char_u *arg,
1910 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001912 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001914 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001916 if (cmdidx == CMD_let || cmdidx == CMD_var
1917 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001918 {
1919 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001920 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001922 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001923 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001924 {
1925 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001926 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001927 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928 break;
1929 }
1930 return;
1931 }
1932 }
1933 else
1934 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1935 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 while ((xp->xp_pattern = vim_strpbrk(arg,
1937 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1938 {
1939 c = *xp->xp_pattern;
1940 if (c == '&')
1941 {
1942 c = xp->xp_pattern[1];
1943 if (c == '&')
1944 {
1945 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001946 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 }
1948 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001949 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001951 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1952 xp->xp_pattern += 2;
1953
1954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 }
1956 else if (c == '$')
1957 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001958 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 xp->xp_context = EXPAND_ENV_VARS;
1960 }
1961 else if (c == '=')
1962 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001963 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 xp->xp_context = EXPAND_EXPRESSION;
1965 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001966 else if (c == '#'
1967 && xp->xp_context == EXPAND_EXPRESSION)
1968 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001969 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001970 break;
1971 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001972 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 && xp->xp_context == EXPAND_FUNCTIONS
1974 && vim_strchr(xp->xp_pattern, '(') == NULL)
1975 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001976 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 break;
1978 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001979 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001981 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 {
1983 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1984 if (c == '\\' && xp->xp_pattern[1] != NUL)
1985 ++xp->xp_pattern;
1986 xp->xp_context = EXPAND_NOTHING;
1987 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001988 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001990 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1992 /* skip */ ;
1993 xp->xp_context = EXPAND_NOTHING;
1994 }
1995 else if (c == '|')
1996 {
1997 if (xp->xp_pattern[1] == '|')
1998 {
1999 ++xp->xp_pattern;
2000 xp->xp_context = EXPAND_EXPRESSION;
2001 }
2002 else
2003 xp->xp_context = EXPAND_COMMANDS;
2004 }
2005 else
2006 xp->xp_context = EXPAND_EXPRESSION;
2007 }
2008 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002009 // Doesn't look like something valid, expand as an expression
2010 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002011 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 arg = xp->xp_pattern;
2013 if (*arg != NUL)
2014 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2015 /* skip */ ;
2016 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002017
2018 // ":exe one two" completes "two"
2019 if ((cmdidx == CMD_execute
2020 || cmdidx == CMD_echo
2021 || cmdidx == CMD_echon
2022 || cmdidx == CMD_echomsg)
2023 && xp->xp_context == EXPAND_EXPRESSION)
2024 {
2025 for (;;)
2026 {
2027 char_u *n = skiptowhite(arg);
2028
2029 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2030 break;
2031 arg = skipwhite(n);
2032 }
2033 }
2034
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 xp->xp_pattern = arg;
2036}
2037
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002039 * Return TRUE if "pat" matches "text".
2040 * Does not use 'cpo' and always uses 'magic'.
2041 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002042 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002043pattern_match(char_u *pat, char_u *text, int ic)
2044{
2045 int matches = FALSE;
2046 char_u *save_cpo;
2047 regmatch_T regmatch;
2048
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002049 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002050 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002051 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002052 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2053 if (regmatch.regprog != NULL)
2054 {
2055 regmatch.rm_ic = ic;
2056 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2057 vim_regfree(regmatch.regprog);
2058 }
2059 p_cpo = save_cpo;
2060 return matches;
2061}
2062
2063/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002064 * Handle a name followed by "(". Both for just "name(arg)" and for
2065 * "expr->name(arg)".
2066 * Returns OK or FAIL.
2067 */
2068 static int
2069eval_func(
2070 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002071 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002072 char_u *name,
2073 int name_len,
2074 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002075 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002076 typval_T *basetv) // "expr" for "expr->name(arg)"
2077{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002078 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002079 char_u *s = name;
2080 int len = name_len;
2081 partial_T *partial;
2082 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002083 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002084 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002085
2086 if (!evaluate)
2087 check_vars(s, len);
2088
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002089 // If "s" is the name of a variable of type VAR_FUNC
2090 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002091 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002092 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002093
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002094 // Need to make a copy, in case evaluating the arguments makes
2095 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002096 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002097 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002098 ret = FAIL;
2099 else
2100 {
2101 funcexe_T funcexe;
2102
2103 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002104 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002105 funcexe.fe_firstline = curwin->w_cursor.lnum;
2106 funcexe.fe_lastline = curwin->w_cursor.lnum;
2107 funcexe.fe_evaluate = evaluate;
2108 funcexe.fe_partial = partial;
2109 funcexe.fe_basetv = basetv;
2110 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002111 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002112 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002113 }
2114 vim_free(s);
2115
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002116 // If evaluate is FALSE rettv->v_type was not set in
2117 // get_func_tv, but it's needed in handle_subscript() to parse
2118 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002119 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2120 {
2121 rettv->vval.v_string = NULL;
2122 rettv->v_type = VAR_FUNC;
2123 }
2124
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002125 // Stop the expression evaluation when immediately
2126 // aborting on error, or when an interrupt occurred or
2127 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002128 if (evaluate && aborting())
2129 {
2130 if (ret == OK)
2131 clear_tv(rettv);
2132 ret = FAIL;
2133 }
2134 return ret;
2135}
2136
2137/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002138 * Get the next line source line without advancing. But do skip over comment
2139 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002140 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002141 */
2142 static char_u *
2143getline_peek_skip_comments(evalarg_T *evalarg)
2144{
2145 for (;;)
2146 {
2147 char_u *next = getline_peek(evalarg->eval_getline,
2148 evalarg->eval_cookie);
2149 char_u *p;
2150
2151 if (next == NULL)
2152 break;
2153 p = skipwhite(next);
2154 if (*p != NUL && !vim9_comment_start(p))
2155 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002156 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002157 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002158 }
2159 return NULL;
2160}
2161
2162/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002163 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2164 * comment) and there is a next line, return the next line (skipping blanks)
2165 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002166 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2167 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002168 * "arg" must point somewhere inside a line, not at the start.
2169 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002170 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002171eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2172{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002173 char_u *p = skipwhite(arg);
2174
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002175 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002176 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002177 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002178 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2179 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002180 && (*p == NUL || *p == NL
2181 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002182 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002183 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002184
Bram Moolenaare442d592022-05-05 12:20:28 +01002185 if (*p == NL)
2186 next = p + 1;
2187 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002188 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002189 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002190 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002191
Bram Moolenaar9d489562020-07-30 20:08:50 +02002192 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002193 {
2194 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002195 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002196 }
2197 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002198 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002199}
2200
2201/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002202 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002203 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002204 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002205 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002206eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002207{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002208 garray_T *gap = &evalarg->eval_ga;
2209 char_u *line;
2210
Bram Moolenaare442d592022-05-05 12:20:28 +01002211 if (arg != NULL && *arg == NL)
2212 return skipwhite(arg + 1);
2213
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002214 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002215 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2216 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002217 else
2218 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002219 if (line == NULL)
2220 return NULL;
2221
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002222 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002223 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2224 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002225 char_u *p = skipwhite(line);
2226
2227 // Going to concatenate the lines after parsing. For an empty or
2228 // comment line use an empty string.
2229 if (*p == NUL || vim9_comment_start(p))
2230 {
2231 vim_free(line);
2232 line = vim_strsave((char_u *)"");
2233 }
2234
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002235 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2236 ++gap->ga_len;
2237 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002238 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002239 {
2240 vim_free(evalarg->eval_tofree);
2241 evalarg->eval_tofree = line;
2242 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002243
2244 // Advanced to the next line, "arg" no longer points into the previous
2245 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002246 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002247 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002248}
2249
Bram Moolenaare6b53242020-07-01 17:28:33 +02002250/*
2251 * Call eval_next_non_blank() and get the next line if needed.
2252 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002253 char_u *
2254skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2255{
2256 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002257 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002258
Bram Moolenaar962d7212020-07-04 14:15:00 +02002259 if (evalarg == NULL)
2260 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002261 eval_next_non_blank(p, evalarg, &getnext);
2262 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002263 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002264 return p;
2265}
2266
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002267/*
Bram Moolenaar844fb642021-10-23 13:32:30 +01002268 * Initialize "evalarg" for use.
2269 */
2270 void
2271init_evalarg(evalarg_T *evalarg)
2272{
2273 CLEAR_POINTER(evalarg);
2274 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
2275}
2276
2277/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002278 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002279 */
2280 void
2281clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2282{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002283 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002284 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002285 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002286 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002287 if (eap != NULL)
2288 {
2289 // We may need to keep the original command line, e.g. for
2290 // ":let" it has the variable names. But we may also need the
2291 // new one, "nextcmd" points into it. Keep both.
2292 vim_free(eap->cmdline_tofree);
2293 eap->cmdline_tofree = *eap->cmdlinep;
2294 *eap->cmdlinep = evalarg->eval_tofree;
2295 }
2296 else
2297 vim_free(evalarg->eval_tofree);
2298 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002299 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002300
Bram Moolenaar844fb642021-10-23 13:32:30 +01002301 ga_clear_strings(&evalarg->eval_tofree_ga);
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002302 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002303 }
2304}
2305
2306/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002308 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2310 */
2311
2312/*
2313 * Handle zero level expression.
2314 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002316 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002317 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 * Return OK or FAIL.
2319 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002320 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002321eval0(
2322 char_u *arg,
2323 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002324 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002325 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002327 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2328}
2329
2330/*
2331 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2332 * expression and don't check what comes after the expression.
2333 */
2334 int
2335eval0_retarg(
2336 char_u *arg,
2337 typval_T *rettv,
2338 exarg_T *eap,
2339 evalarg_T *evalarg,
2340 char_u **retarg)
2341{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 int ret;
2343 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002344 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002345 int did_emsg_before = did_emsg;
2346 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002347 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002348 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002349 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350
2351 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002352 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002353 expr_end = p;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002354 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002355
Bram Moolenaar02929a32021-12-17 14:46:12 +00002356 // In Vim9 script a command block is not split at NL characters for
2357 // commands using an expression argument. Skip over a '#' comment to check
2358 // for a following NL. Require white space before the '#'.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002359 if (in_vim9script() && p > expr_end && retarg == NULL)
Bram Moolenaar02929a32021-12-17 14:46:12 +00002360 while (*p == '#')
2361 {
2362 char_u *nl = vim_strchr(p, NL);
2363
2364 if (nl == NULL)
2365 break;
2366 p = skipwhite(nl + 1);
2367 if (eap != NULL && *p != NUL)
2368 eap->nextcmd = p;
2369 check_for_end = FALSE;
2370 }
2371
2372 if (ret != FAIL && check_for_end)
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002373 end_error = !ends_excmd2(arg, p);
2374 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 {
2376 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002377 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 /*
2379 * Report the invalid expression unless the expression evaluation has
2380 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002381 * exception, or we already gave a more specific error.
2382 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002384 if (!aborting()
2385 && did_emsg == did_emsg_before
2386 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002387 && (flags & EVAL_CONSTANT) == 0
2388 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002389 {
2390 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002391 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002392 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002393 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002394 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002395
2396 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002397 // a next command to avoid more errors, unless "|" is following, which
2398 // could only be a command separator.
2399 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2400 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002401 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002403
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002404 if (retarg != NULL)
2405 *retarg = p;
2406 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002407 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002408
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 return ret;
2410}
2411
2412/*
2413 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002414 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002415 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 *
2417 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002418 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002420 * Note: "rettv.v_lock" is not set.
2421 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 * Return OK or FAIL.
2423 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002424 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002425eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002427 char_u *p;
2428 int getnext;
2429
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002430 CLEAR_POINTER(rettv);
2431
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 /*
2433 * Get the first variable.
2434 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002435 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 return FAIL;
2437
Bram Moolenaar793648f2020-06-26 21:28:25 +02002438 p = eval_next_non_blank(*arg, evalarg, &getnext);
2439 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002441 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002442 int result;
2443 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002444 evalarg_T *evalarg_used = evalarg;
2445 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002446 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002447 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002448 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002449
2450 if (evalarg == NULL)
2451 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002452 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002453 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002454 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002455 orig_flags = evalarg_used->eval_flags;
2456 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002457
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002458 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002459 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002460 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002461 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002462 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002463 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002464 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002465 clear_tv(rettv);
2466 return FAIL;
2467 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002468 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002469 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002470
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002472 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002474 int error = FALSE;
2475
Bram Moolenaar13106602020-10-04 16:06:05 +02002476 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002477 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002478 else if (vim9script)
2479 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002480 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002482 if (error || !op_falsy || !result)
2483 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002484 if (error)
2485 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 }
2487
2488 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002489 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002491 if (op_falsy)
2492 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002493 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002494 {
mityu4ac198c2021-05-28 17:52:40 +02002495 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002496 clear_tv(rettv);
2497 return FAIL;
2498 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002499 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002500 evalarg_used->eval_flags = (op_falsy ? !result : result)
2501 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2502 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002503 {
2504 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002506 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002507 if (!op_falsy || !result)
2508 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002510 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002512 /*
2513 * Check for the ":".
2514 */
2515 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2516 if (*p != ':')
2517 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002518 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002519 if (evaluate && result)
2520 clear_tv(rettv);
2521 evalarg_used->eval_flags = orig_flags;
2522 return FAIL;
2523 }
2524 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002525 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002526 else
2527 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002528 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002529 {
2530 error_white_both(p, 1);
2531 clear_tv(rettv);
2532 evalarg_used->eval_flags = orig_flags;
2533 return FAIL;
2534 }
2535 *arg = p;
2536 }
2537
2538 /*
2539 * Get the third variable. Recursive!
2540 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002541 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002542 {
mityu4ac198c2021-05-28 17:52:40 +02002543 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002544 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002545 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002546 return FAIL;
2547 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002548 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2549 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002550 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002551 if (eval1(arg, &var2, evalarg_used) == FAIL)
2552 {
2553 if (evaluate && result)
2554 clear_tv(rettv);
2555 evalarg_used->eval_flags = orig_flags;
2556 return FAIL;
2557 }
2558 if (evaluate && !result)
2559 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002561
2562 if (evalarg == NULL)
2563 clear_evalarg(&local_evalarg, NULL);
2564 else
2565 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 }
2567
2568 return OK;
2569}
2570
2571/*
2572 * Handle first level expression:
2573 * expr2 || expr2 || expr2 logical OR
2574 *
2575 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002576 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 *
2578 * Return OK or FAIL.
2579 */
2580 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002581eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002583 char_u *p;
2584 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585
2586 /*
2587 * Get the first variable.
2588 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002589 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 return FAIL;
2591
2592 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002593 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002595 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002596 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002598 evalarg_T *evalarg_used = evalarg;
2599 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002600 int evaluate;
2601 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002602 long result = FALSE;
2603 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002604 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002605 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002606
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002607 if (evalarg == NULL)
2608 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002609 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002610 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002611 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002612 orig_flags = evalarg_used->eval_flags;
2613 evaluate = orig_flags & EVAL_EVALUATE;
2614 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002615 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002616 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002617 result = tv_get_bool_chk(rettv, &error);
2618 else if (tv_get_number_chk(rettv, &error) != 0)
2619 result = TRUE;
2620 clear_tv(rettv);
2621 if (error)
2622 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 }
2624
2625 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002626 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002628 while (p[0] == '|' && p[1] == '|')
2629 {
2630 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002631 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002632 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002633 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002634 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002635 {
2636 error_white_both(p, 2);
2637 clear_tv(rettv);
2638 return FAIL;
2639 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002640 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002641 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002642
2643 /*
2644 * Get the second variable.
2645 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002646 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002647 {
mityu4ac198c2021-05-28 17:52:40 +02002648 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002649 clear_tv(rettv);
2650 return FAIL;
2651 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002652 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2653 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002654 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002655 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002656 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002657
2658 /*
2659 * Compute the result.
2660 */
2661 if (evaluate && !result)
2662 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002663 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002664 result = tv_get_bool_chk(&var2, &error);
2665 else if (tv_get_number_chk(&var2, &error) != 0)
2666 result = TRUE;
2667 clear_tv(&var2);
2668 if (error)
2669 return FAIL;
2670 }
2671 if (evaluate)
2672 {
2673 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002674 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002675 rettv->v_type = VAR_BOOL;
2676 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002677 }
2678 else
2679 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002680 rettv->v_type = VAR_NUMBER;
2681 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002682 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002683 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002684
2685 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002687
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002688 if (evalarg == NULL)
2689 clear_evalarg(&local_evalarg, NULL);
2690 else
2691 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 }
2693
2694 return OK;
2695}
2696
2697/*
2698 * Handle second level expression:
2699 * expr3 && expr3 && expr3 logical AND
2700 *
2701 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002702 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 *
2704 * Return OK or FAIL.
2705 */
2706 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002707eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002709 char_u *p;
2710 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711
2712 /*
2713 * Get the first variable.
2714 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002715 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 return FAIL;
2717
2718 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002719 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002721 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002722 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002724 evalarg_T *evalarg_used = evalarg;
2725 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002726 int orig_flags;
2727 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002728 long result = TRUE;
2729 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002730 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002731 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002732
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002733 if (evalarg == NULL)
2734 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002735 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002736 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002737 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002738 orig_flags = evalarg_used->eval_flags;
2739 evaluate = orig_flags & EVAL_EVALUATE;
2740 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002741 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002742 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002743 result = tv_get_bool_chk(rettv, &error);
2744 else if (tv_get_number_chk(rettv, &error) == 0)
2745 result = FALSE;
2746 clear_tv(rettv);
2747 if (error)
2748 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 }
2750
2751 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002752 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002754 while (p[0] == '&' && p[1] == '&')
2755 {
2756 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002757 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002758 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002759 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002760 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002761 {
2762 error_white_both(p, 2);
2763 clear_tv(rettv);
2764 return FAIL;
2765 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002766 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002767 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002768
2769 /*
2770 * Get the second variable.
2771 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002772 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002773 {
mityu4ac198c2021-05-28 17:52:40 +02002774 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002775 clear_tv(rettv);
2776 return FAIL;
2777 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002778 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2779 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002780 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002781 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002782 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002783 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002784
2785 /*
2786 * Compute the result.
2787 */
2788 if (evaluate && result)
2789 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002790 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002791 result = tv_get_bool_chk(&var2, &error);
2792 else if (tv_get_number_chk(&var2, &error) == 0)
2793 result = FALSE;
2794 clear_tv(&var2);
2795 if (error)
2796 return FAIL;
2797 }
2798 if (evaluate)
2799 {
2800 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002801 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002802 rettv->v_type = VAR_BOOL;
2803 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002804 }
2805 else
2806 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002807 rettv->v_type = VAR_NUMBER;
2808 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002809 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002810 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002811
2812 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002814
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002815 if (evalarg == NULL)
2816 clear_evalarg(&local_evalarg, NULL);
2817 else
2818 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 }
2820
2821 return OK;
2822}
2823
2824/*
2825 * Handle third level expression:
2826 * var1 == var2
2827 * var1 =~ var2
2828 * var1 != var2
2829 * var1 !~ var2
2830 * var1 > var2
2831 * var1 >= var2
2832 * var1 < var2
2833 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002834 * var1 is var2
2835 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 *
2837 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002838 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 *
2840 * Return OK or FAIL.
2841 */
2842 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002843eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002846 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002847 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002849 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850
2851 /*
2852 * Get the first variable.
2853 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002854 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 return FAIL;
2856
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002857 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002858 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859
2860 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002861 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002863 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002865 typval_T var2;
2866 int ic;
2867 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002868 int evaluate = evalarg == NULL
2869 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002870 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002871
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002872 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002873 {
Bram Moolenaare442d592022-05-05 12:20:28 +01002874 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002875 p = *arg;
2876 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002877 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2878 {
mityu4ac198c2021-05-28 17:52:40 +02002879 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002880 clear_tv(rettv);
2881 return FAIL;
2882 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002883
Bram Moolenaar696ba232020-07-29 21:20:41 +02002884 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2885 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002886 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002887 clear_tv(rettv);
2888 return FAIL;
2889 }
2890
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002891 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 if (p[len] == '?')
2893 {
2894 ic = TRUE;
2895 ++len;
2896 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002897 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 else if (p[len] == '#')
2899 {
2900 ic = FALSE;
2901 ++len;
2902 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002903 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002905 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906
2907 /*
2908 * Get the second variable.
2909 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002910 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2911 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002912 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002913 clear_tv(rettv);
2914 return FAIL;
2915 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002916 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002917 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002919 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 return FAIL;
2921 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002922 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002923 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002924 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002925
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002926 // use the line of the comparison for messages
2927 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002928 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002929 {
2930 ret = FAIL;
2931 clear_tv(rettv);
2932 }
2933 else
2934 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002935 clear_tv(&var2);
2936 return ret;
2937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 }
2939
2940 return OK;
2941}
2942
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002943/*
2944 * Make a copy of blob "tv1" and append blob "tv2".
2945 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 void
2947eval_addblob(typval_T *tv1, typval_T *tv2)
2948{
2949 blob_T *b1 = tv1->vval.v_blob;
2950 blob_T *b2 = tv2->vval.v_blob;
2951 blob_T *b = blob_alloc();
2952 int i;
2953
2954 if (b != NULL)
2955 {
2956 for (i = 0; i < blob_len(b1); i++)
2957 ga_append(&b->bv_ga, blob_get(b1, i));
2958 for (i = 0; i < blob_len(b2); i++)
2959 ga_append(&b->bv_ga, blob_get(b2, i));
2960
2961 clear_tv(tv1);
2962 rettv_blob_set(tv1, b);
2963 }
2964}
2965
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002966/*
2967 * Make a copy of list "tv1" and append list "tv2".
2968 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 int
2970eval_addlist(typval_T *tv1, typval_T *tv2)
2971{
2972 typval_T var3;
2973
2974 // concatenate Lists
2975 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2976 {
2977 clear_tv(tv1);
2978 clear_tv(tv2);
2979 return FAIL;
2980 }
2981 clear_tv(tv1);
2982 *tv1 = var3;
2983 return OK;
2984}
2985
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986/*
2987 * Handle fourth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00002988 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002990 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002991 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 *
2993 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002994 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 *
2996 * Return OK or FAIL.
2997 */
2998 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002999eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 /*
3002 * Get the first variable.
3003 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003004 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 return FAIL;
3006
3007 /*
3008 * Repeat computing, until no '+', '-' or '.' is following.
3009 */
3010 for (;;)
3011 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003012 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003013 int getnext;
3014 char_u *p;
3015 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003016 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003017 int concat;
3018 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003019 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003020
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003021 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003022 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003023 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003024 p = eval_next_non_blank(*arg, evalarg, &getnext);
3025 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003026 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003027 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3028 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003030 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3031 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003032
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003033 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003034 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003035 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003036 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003037 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003038 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003039 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003040 {
mityu4ac198c2021-05-28 17:52:40 +02003041 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003042 clear_tv(rettv);
3043 return FAIL;
3044 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003045 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003046 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003047 if ((op != '+' || (rettv->v_type != VAR_LIST
3048 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003049#ifdef FEAT_FLOAT
3050 && (op == '.' || rettv->v_type != VAR_FLOAT)
3051#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003052 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003053 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003054 int error = FALSE;
3055
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003056 // For "list + ...", an illegal use of the first operand as
3057 // a number cannot be determined before evaluating the 2nd
3058 // operand: if this is also a list, all is ok.
3059 // For "something . ...", "something - ..." or "non-list + ...",
3060 // we know that the first operand needs to be a string or number
3061 // without evaluating the 2nd operand. So check before to avoid
3062 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003063 if (op != '.')
3064 tv_get_number_chk(rettv, &error);
3065 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003066 {
3067 clear_tv(rettv);
3068 return FAIL;
3069 }
3070 }
3071
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 /*
3073 * Get the second variable.
3074 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003075 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003076 {
mityu89dcb4d2021-05-28 13:50:17 +02003077 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003078 clear_tv(rettv);
3079 return FAIL;
3080 }
3081 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02003082 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003084 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 return FAIL;
3086 }
3087
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003088 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 {
3090 /*
3091 * Compute the result.
3092 */
3093 if (op == '.')
3094 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003095 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3096 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003097 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003098
Bram Moolenaar2e086612020-08-25 22:37:48 +02003099 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003100 || var2.v_type == VAR_CHANNEL
3101 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003102 semsg(_(e_using_invalid_value_as_string_str),
3103 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003104#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003105 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003106 {
3107 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3108 var2.vval.v_float);
3109 s2 = buf2;
3110 }
3111#endif
3112 else
3113 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003114 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003115 {
3116 clear_tv(rettv);
3117 clear_tv(&var2);
3118 return FAIL;
3119 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003120 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003121 clear_tv(rettv);
3122 rettv->v_type = VAR_STRING;
3123 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003125 else if (op == '+' && rettv->v_type == VAR_BLOB
3126 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003128 else if (op == '+' && rettv->v_type == VAR_LIST
3129 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003130 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003132 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 else
3135 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003136 int error = FALSE;
3137 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003138#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003139 float_T f1 = 0, f2 = 0;
3140
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003141 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003142 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003143 f1 = rettv->vval.v_float;
3144 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003145 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003146 else
3147#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003148 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003149 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003150 if (error)
3151 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003152 // This can only happen for "list + non-list" or
3153 // "blob + non-blob". For "non-list + ..." or
3154 // "something - ...", we returned before evaluating the
3155 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003156 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003157 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003158 return FAIL;
3159 }
3160#ifdef FEAT_FLOAT
3161 if (var2.v_type == VAR_FLOAT)
3162 f1 = n1;
3163#endif
3164 }
3165#ifdef FEAT_FLOAT
3166 if (var2.v_type == VAR_FLOAT)
3167 {
3168 f2 = var2.vval.v_float;
3169 n2 = 0;
3170 }
3171 else
3172#endif
3173 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003174 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003175 if (error)
3176 {
3177 clear_tv(rettv);
3178 clear_tv(&var2);
3179 return FAIL;
3180 }
3181#ifdef FEAT_FLOAT
3182 if (rettv->v_type == VAR_FLOAT)
3183 f2 = n2;
3184#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003185 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003186 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003187
3188#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003189 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003190 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3191 {
3192 if (op == '+')
3193 f1 = f1 + f2;
3194 else
3195 f1 = f1 - f2;
3196 rettv->v_type = VAR_FLOAT;
3197 rettv->vval.v_float = f1;
3198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003200#endif
3201 {
3202 if (op == '+')
3203 n1 = n1 + n2;
3204 else
3205 n1 = n1 - n2;
3206 rettv->v_type = VAR_NUMBER;
3207 rettv->vval.v_number = n1;
3208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003210 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 }
3212 }
3213 return OK;
3214}
3215
3216/*
3217 * Handle fifth level expression:
3218 * * number multiplication
3219 * / number division
3220 * % number modulo
3221 *
3222 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003223 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 *
3225 * Return OK or FAIL.
3226 */
3227 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003228eval6(
3229 char_u **arg,
3230 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003231 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003232 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003234#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003235 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237
3238 /*
3239 * Get the first variable.
3240 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003241 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 return FAIL;
3243
3244 /*
3245 * Repeat computing, until no '*', '/' or '%' is following.
3246 */
3247 for (;;)
3248 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003249 int evaluate;
3250 int getnext;
3251 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003252 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003253 int op;
3254 varnumber_T n1, n2;
3255#ifdef FEAT_FLOAT
3256 float_T f1, f2;
3257#endif
3258 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003259
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003260 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003261 p = eval_next_non_blank(*arg, evalarg, &getnext);
3262 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003263 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003265
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003266 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003267 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003268 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003269 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003270 {
3271 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3272 {
mityu4ac198c2021-05-28 17:52:40 +02003273 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003274 clear_tv(rettv);
3275 return FAIL;
3276 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003277 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003280#ifdef FEAT_FLOAT
3281 f1 = 0;
3282 f2 = 0;
3283#endif
3284 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003285 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003287#ifdef FEAT_FLOAT
3288 if (rettv->v_type == VAR_FLOAT)
3289 {
3290 f1 = rettv->vval.v_float;
3291 use_float = TRUE;
3292 n1 = 0;
3293 }
3294 else
3295#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003296 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003297 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003298 if (error)
3299 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 }
3301 else
3302 n1 = 0;
3303
3304 /*
3305 * Get the second variable.
3306 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003307 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3308 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003309 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003310 clear_tv(rettv);
3311 return FAIL;
3312 }
3313 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003314 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 return FAIL;
3316
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003317 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003319#ifdef FEAT_FLOAT
3320 if (var2.v_type == VAR_FLOAT)
3321 {
3322 if (!use_float)
3323 {
3324 f1 = n1;
3325 use_float = TRUE;
3326 }
3327 f2 = var2.vval.v_float;
3328 n2 = 0;
3329 }
3330 else
3331#endif
3332 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003333 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003334 clear_tv(&var2);
3335 if (error)
3336 return FAIL;
3337#ifdef FEAT_FLOAT
3338 if (use_float)
3339 f2 = n2;
3340#endif
3341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342
3343 /*
3344 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003345 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003347#ifdef FEAT_FLOAT
3348 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003350 if (op == '*')
3351 f1 = f1 * f2;
3352 else if (op == '/')
3353 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003354# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003355 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003356 if (f2 == 0.0)
3357 {
3358 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003359 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003360 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003361 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003362 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003363 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003364 }
3365 else
3366 f1 = f1 / f2;
3367# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003368 // We rely on the floating point library to handle divide
3369 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003370 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003371# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003374 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003375 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003376 return FAIL;
3377 }
3378 rettv->v_type = VAR_FLOAT;
3379 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 }
3381 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003384 int failed = FALSE;
3385
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003386 if (op == '*')
3387 n1 = n1 * n2;
3388 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003389 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003391 n1 = num_modulus(n1, n2, &failed);
3392 if (failed)
3393 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003394
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003395 rettv->v_type = VAR_NUMBER;
3396 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 }
3399 }
3400
3401 return OK;
3402}
3403
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003404/*
3405 * Handle a type cast before a base level expression.
3406 * "arg" must point to the first non-white of the expression.
3407 * "arg" is advanced to just after the recognized expression.
3408 * Return OK or FAIL.
3409 */
3410 static int
3411eval7t(
3412 char_u **arg,
3413 typval_T *rettv,
3414 evalarg_T *evalarg,
3415 int want_string) // after "." operator
3416{
3417 type_T *want_type = NULL;
3418 garray_T type_list; // list of pointers to allocated types
3419 int res;
3420 int evaluate = evalarg == NULL ? 0
3421 : (evalarg->eval_flags & EVAL_EVALUATE);
3422
3423 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003424 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3425 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003426 {
3427 ++*arg;
3428 ga_init2(&type_list, sizeof(type_T *), 10);
3429 want_type = parse_type(arg, &type_list, TRUE);
3430 if (want_type == NULL && (evaluate || **arg != '>'))
3431 {
3432 clear_type_list(&type_list);
3433 return FAIL;
3434 }
3435
3436 if (**arg != '>')
3437 {
3438 if (*skipwhite(*arg) == '>')
3439 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3440 else
3441 emsg(_(e_missing_gt));
3442 clear_type_list(&type_list);
3443 return FAIL;
3444 }
3445 ++*arg;
3446 *arg = skipwhite_and_linebreak(*arg, evalarg);
3447 }
3448
3449 res = eval7(arg, rettv, evalarg, want_string);
3450
3451 if (want_type != NULL && evaluate)
3452 {
3453 if (res == OK)
3454 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003455 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3456 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003457
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003458 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003459 {
3460 if (want_type == &t_bool && actual != &t_bool
3461 && (actual->tt_flags & TTFLAG_BOOL_OK))
3462 {
3463 int n = tv2bool(rettv);
3464
3465 // can use "0" and "1" for boolean in some places
3466 clear_tv(rettv);
3467 rettv->v_type = VAR_BOOL;
3468 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3469 }
3470 else
3471 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003472 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003473
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003474 where.wt_variable = TRUE;
3475 res = check_type(want_type, actual, TRUE, where);
3476 }
3477 }
3478 }
3479 clear_type_list(&type_list);
3480 }
3481
3482 return res;
3483}
3484
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003485 int
3486eval_leader(char_u **arg, int vim9)
3487{
3488 char_u *s = *arg;
3489 char_u *p = *arg;
3490
3491 while (*p == '!' || *p == '-' || *p == '+')
3492 {
3493 char_u *n = skipwhite(p + 1);
3494
3495 // ++, --, -+ and +- are not accepted in Vim9 script
3496 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3497 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003498 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003499 return FAIL;
3500 }
3501 p = n;
3502 }
3503 *arg = p;
3504 return OK;
3505}
3506
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003508 * Check for a predefined value "true", "false" and "null.*".
3509 * Return OK when recognized.
3510 */
3511 int
3512handle_predefined(char_u *s, int len, typval_T *rettv)
3513{
3514 switch (len)
3515 {
3516 case 4: if (STRNCMP(s, "true", 4) == 0)
3517 {
3518 rettv->v_type = VAR_BOOL;
3519 rettv->vval.v_number = VVAL_TRUE;
3520 return OK;
3521 }
3522 if (STRNCMP(s, "null", 4) == 0)
3523 {
3524 rettv->v_type = VAR_SPECIAL;
3525 rettv->vval.v_number = VVAL_NULL;
3526 return OK;
3527 }
3528 break;
3529 case 5: if (STRNCMP(s, "false", 5) == 0)
3530 {
3531 rettv->v_type = VAR_BOOL;
3532 rettv->vval.v_number = VVAL_FALSE;
3533 return OK;
3534 }
3535 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003536 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3537 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003538#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003539 rettv->v_type = VAR_JOB;
3540 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003541#else
3542 rettv->v_type = VAR_SPECIAL;
3543 rettv->vval.v_number = VVAL_NULL;
3544#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003545 return OK;
3546 }
3547 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003548 case 9:
3549 if (STRNCMP(s, "null_", 5) != 0)
3550 break;
3551 if (STRNCMP(s + 5, "list", 4) == 0)
3552 {
3553 rettv->v_type = VAR_LIST;
3554 rettv->vval.v_list = NULL;
3555 return OK;
3556 }
3557 if (STRNCMP(s + 5, "dict", 4) == 0)
3558 {
3559 rettv->v_type = VAR_DICT;
3560 rettv->vval.v_dict = NULL;
3561 return OK;
3562 }
3563 if (STRNCMP(s + 5, "blob", 4) == 0)
3564 {
3565 rettv->v_type = VAR_BLOB;
3566 rettv->vval.v_blob = NULL;
3567 return OK;
3568 }
3569 break;
3570 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3571 {
3572 rettv->v_type = VAR_STRING;
3573 rettv->vval.v_string = NULL;
3574 return OK;
3575 }
3576 break;
3577 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003578 if (STRNCMP(s, "null_channel", 12) == 0)
3579 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003580#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003581 rettv->v_type = VAR_CHANNEL;
3582 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003583#else
3584 rettv->v_type = VAR_SPECIAL;
3585 rettv->vval.v_number = VVAL_NULL;
3586#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003587 return OK;
3588 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003589 if (STRNCMP(s, "null_partial", 12) == 0)
3590 {
3591 rettv->v_type = VAR_PARTIAL;
3592 rettv->vval.v_partial = NULL;
3593 return OK;
3594 }
3595 break;
3596 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3597 {
3598 rettv->v_type = VAR_FUNC;
3599 rettv->vval.v_string = NULL;
3600 return OK;
3601 }
3602 break;
3603 }
3604 return FAIL;
3605}
3606
3607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 * Handle sixth level expression:
3609 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003610 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003611 * "string" string constant
3612 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 * &option-name option value
3614 * @r register contents
3615 * identifier variable value
3616 * function() function call
3617 * $VAR environment variable
3618 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003619 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003621 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003622 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 *
3624 * Also handle:
3625 * ! in front logical NOT
3626 * - in front unary minus
3627 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003628 * trailing [] subscript in String or List
3629 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003630 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 *
3632 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003633 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 *
3635 * Return OK or FAIL.
3636 */
3637 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003638eval7(
3639 char_u **arg,
3640 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003641 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003644 int evaluate = evalarg != NULL
3645 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 int len;
3647 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003648 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 char_u *start_leader, *end_leader;
3650 int ret = OK;
3651 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003652 static int recurse = 0;
3653 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654
3655 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003656 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003657 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003659 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660
3661 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003662 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 */
3664 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003665 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003666 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 end_leader = *arg;
3668
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003669 if (**arg == '.' && (!isdigit(*(*arg + 1))
3670#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003671 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003672#endif
3673 ))
3674 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003675 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003676 ++*arg;
3677 return FAIL;
3678 }
3679
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003680 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003681 // and crash. With MSVC the stack is smaller.
3682 if (recurse ==
3683#ifdef _MSC_VER
3684 300
3685#else
3686 1000
3687#endif
3688 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003689 {
3690 semsg(_(e_expression_too_recursive_str), *arg);
3691 return FAIL;
3692 }
3693 ++recurse;
3694
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 switch (**arg)
3696 {
3697 /*
3698 * Number constant.
3699 */
3700 case '0':
3701 case '1':
3702 case '2':
3703 case '3':
3704 case '4':
3705 case '5':
3706 case '6':
3707 case '7':
3708 case '8':
3709 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003710 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003711
3712 // Apply prefixed "-" and "+" now. Matters especially when
3713 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003714 if (ret == OK && evaluate && end_leader > start_leader
3715 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003716 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 break;
3718
3719 /*
3720 * String constant: "string".
3721 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003722 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 break;
3724
3725 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003726 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003728 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003729 break;
3730
3731 /*
3732 * List: [expr, expr]
3733 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003734 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 break;
3736
3737 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003738 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003739 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003740 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003741 {
3742 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3743 }
3744 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003745 {
3746 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003747 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003748 }
3749 else
3750 ret = NOTDONE;
3751 break;
3752
3753 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003754 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003755 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003756 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003757 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003758 ret = NOTDONE;
3759 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00003760 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003761 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003762 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003763 break;
3764
3765 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003766 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003768 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 break;
3770
3771 /*
3772 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01003773 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 */
LemonBoy2eaef102022-05-06 13:14:50 +01003775 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
3776 ret = eval_interp_string(arg, rettv, evaluate);
3777 else
3778 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 break;
3780
3781 /*
3782 * Register contents: @r.
3783 */
3784 case '@': ++*arg;
3785 if (evaluate)
3786 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003787 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003788 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003789 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003790 emsg_invreg(**arg);
3791 else
3792 {
3793 rettv->v_type = VAR_STRING;
3794 rettv->vval.v_string = get_reg_contents(**arg,
3795 GREG_EXPR_SRC);
3796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 }
3798 if (**arg != NUL)
3799 ++*arg;
3800 break;
3801
3802 /*
3803 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003804 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003806 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003807 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01003808 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003809 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003810 if (ret == OK && evaluate)
3811 {
3812 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3813
Bram Moolenaara9931532021-06-12 15:58:16 +02003814 // Compile it here to get the return type. The return
3815 // type is optional, when it's missing use t_unknown.
3816 // This is recognized in compile_return().
3817 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3818 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00003819 if (compile_def_function(ufunc, FALSE,
3820 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003821 {
3822 clear_tv(rettv);
3823 ret = FAIL;
3824 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003825 }
3826 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003827 if (ret == NOTDONE)
3828 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003829 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003830 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003831
Bram Moolenaar9215f012020-06-27 21:18:00 +02003832 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003833 if (**arg == ')')
3834 ++*arg;
3835 else if (ret == OK)
3836 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003837 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003838 clear_tv(rettv);
3839 ret = FAIL;
3840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 }
3842 break;
3843
Bram Moolenaar8c711452005-01-14 21:53:12 +00003844 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 break;
3846 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003847
3848 if (ret == NOTDONE)
3849 {
3850 /*
3851 * Must be a variable or function name.
3852 * Can also be a curly-braces kind of name: {expr}.
3853 */
3854 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003855 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003856 if (alias != NULL)
3857 s = alias;
3858
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003859 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003860 ret = FAIL;
3861 else
3862 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003863 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3864
Bram Moolenaar4525a572022-02-13 11:57:33 +00003865 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003866 {
3867 emsg(_(e_cannot_use_underscore_here));
3868 ret = FAIL;
3869 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00003870 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00003871 && s[0] == 's' && s[1] == ':')
3872 {
3873 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
3874 ret = FAIL;
3875 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00003876 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003877 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003878 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003879 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003880 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003881 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003882 else if (flags & EVAL_CONSTANT)
3883 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003884 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003885 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003886 // get the value of "true", "false", etc. or a variable
3887 ret = FAIL;
3888 if (vim9script)
3889 ret = handle_predefined(s, len, rettv);
3890 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003891 {
3892 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003893 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003894 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003895 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003896 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003897 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003898 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003899 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003900 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003901 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003902 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003903 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003904 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003905 }
3906
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003907 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3908 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003909 if (ret == OK)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003910 ret = handle_subscript(arg, name_start, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911
3912 /*
3913 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3914 */
3915 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003916 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003917
3918 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003919 return ret;
3920}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003921
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003922/*
3923 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003924 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003925 * Adjusts "end_leaderp" until it is at "start_leader".
3926 */
3927 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003928eval7_leader(
3929 typval_T *rettv,
3930 int numeric_only,
3931 char_u *start_leader,
3932 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003933{
3934 char_u *end_leader = *end_leaderp;
3935 int ret = OK;
3936 int error = FALSE;
3937 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003938 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003939 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003940#ifdef FEAT_FLOAT
3941 float_T f = 0.0;
3942
3943 if (rettv->v_type == VAR_FLOAT)
3944 f = rettv->vval.v_float;
3945 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003946#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003947 {
3948 while (VIM_ISWHITE(end_leader[-1]))
3949 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003950 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003951 val = tv2bool(rettv);
3952 else
3953 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003954 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003955 if (error)
3956 {
3957 clear_tv(rettv);
3958 ret = FAIL;
3959 }
3960 else
3961 {
3962 while (end_leader > start_leader)
3963 {
3964 --end_leader;
3965 if (*end_leader == '!')
3966 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003967 if (numeric_only)
3968 {
3969 ++end_leader;
3970 break;
3971 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003972#ifdef FEAT_FLOAT
3973 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003974 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003975 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003976 {
3977 rettv->v_type = VAR_BOOL;
3978 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3979 }
3980 else
3981 f = !f;
3982 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003983 else
3984#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003985 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003986 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003987 type = VAR_BOOL;
3988 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003989 }
3990 else if (*end_leader == '-')
3991 {
3992#ifdef FEAT_FLOAT
3993 if (rettv->v_type == VAR_FLOAT)
3994 f = -f;
3995 else
3996#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003997 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003998 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003999 type = VAR_NUMBER;
4000 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004001 }
4002 }
4003#ifdef FEAT_FLOAT
4004 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004006 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004007 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004009 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004010#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004011 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004012 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004013 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004014 rettv->v_type = type;
4015 else
4016 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004017 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004020 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 return ret;
4022}
4023
4024/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004025 * Call the function referred to in "rettv".
4026 */
4027 static int
4028call_func_rettv(
4029 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004030 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004031 typval_T *rettv,
4032 int evaluate,
4033 dict_T *selfdict,
4034 typval_T *basetv)
4035{
4036 partial_T *pt = NULL;
4037 funcexe_T funcexe;
4038 typval_T functv;
4039 char_u *s;
4040 int ret;
4041
4042 // need to copy the funcref so that we can clear rettv
4043 if (evaluate)
4044 {
4045 functv = *rettv;
4046 rettv->v_type = VAR_UNKNOWN;
4047
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004048 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004049 if (functv.v_type == VAR_PARTIAL)
4050 {
4051 pt = functv.vval.v_partial;
4052 s = partial_name(pt);
4053 }
4054 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004055 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004056 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004057 if (s == NULL || *s == NUL)
4058 {
4059 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004060 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004061 goto theend;
4062 }
4063 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004064 }
4065 else
4066 s = (char_u *)"";
4067
Bram Moolenaara80faa82020-04-12 19:37:17 +02004068 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004069 funcexe.fe_firstline = curwin->w_cursor.lnum;
4070 funcexe.fe_lastline = curwin->w_cursor.lnum;
4071 funcexe.fe_evaluate = evaluate;
4072 funcexe.fe_partial = pt;
4073 funcexe.fe_selfdict = selfdict;
4074 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004075 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004076
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004077theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004078 // Clear the funcref afterwards, so that deleting it while
4079 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004080 if (evaluate)
4081 clear_tv(&functv);
4082
4083 return ret;
4084}
4085
4086/*
4087 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004088 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004089 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4090 */
4091 static int
4092eval_lambda(
4093 char_u **arg,
4094 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004095 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004096 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004097{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004098 int evaluate = evalarg != NULL
4099 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004100 typval_T base = *rettv;
4101 int ret;
4102
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004103 rettv->v_type = VAR_UNKNOWN;
4104
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004105 if (**arg == '{')
4106 {
4107 // ->{lambda}()
4108 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4109 }
4110 else
4111 {
4112 // ->(lambda)()
4113 ++*arg;
4114 ret = eval1(arg, rettv, evalarg);
4115 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004116 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004117 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004118 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004119 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004120 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004121 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4122 && rettv->v_type != VAR_PARTIAL)
4123 {
4124 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4125 return FAIL;
4126 }
4127 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004128 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004129 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004130 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004131
4132 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004133 {
4134 if (verbose)
4135 {
4136 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004137 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004138 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004139 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004140 }
4141 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004142 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004143 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004144 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004145 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004146
4147 // Clear the funcref afterwards, so that deleting it while
4148 // evaluating the arguments is possible (see test55).
4149 if (evaluate)
4150 clear_tv(&base);
4151
4152 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004153}
4154
4155/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004156 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004157 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004158 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4159 */
4160 static int
4161eval_method(
4162 char_u **arg,
4163 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004164 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004165 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004166{
4167 char_u *name;
4168 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004169 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004170 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004171 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004172 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004173 int evaluate = evalarg != NULL
4174 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004175
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004176 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004177
Bram Moolenaarac92e252019-08-03 21:58:38 +02004178 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004179 len = get_name_len(arg, &alias, evaluate, TRUE);
4180 if (alias != NULL)
4181 name = alias;
4182
4183 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004184 {
4185 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004186 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004187 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004188 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004189 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004190 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004191 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004192
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004193 // If there is no "(" immediately following, but there is further on,
4194 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4195 // Does not handle anything where "(" is part of the expression.
4196 *arg = skipwhite(*arg);
4197
4198 if (**arg != '(' && alias == NULL
4199 && (paren = vim_strchr(*arg, '(')) != NULL)
4200 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004201 char_u *deref;
4202
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004203 *arg = name;
4204 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004205 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4206 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004207 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004208 *arg = name + len;
4209 ret = FAIL;
4210 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004211 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004212 {
4213 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004214 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004215 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004216 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004217 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004218
4219 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004220 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004221 *arg = skipwhite(*arg);
4222
4223 if (**arg != '(')
4224 {
4225 if (verbose)
4226 semsg(_(e_missing_parenthesis_str), name);
4227 ret = FAIL;
4228 }
4229 else if (VIM_ISWHITE((*arg)[-1]))
4230 {
4231 if (verbose)
4232 emsg(_(e_no_white_space_allowed_before_parenthesis));
4233 ret = FAIL;
4234 }
4235 else
4236 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004237 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004238 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004239 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004240
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004241 // Clear the funcref afterwards, so that deleting it while
4242 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004243 if (evaluate)
4244 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004245 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004246
Bram Moolenaarac92e252019-08-03 21:58:38 +02004247 return ret;
4248}
4249
4250/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004251 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4252 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004253 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4254 */
4255 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004256eval_index(
4257 char_u **arg,
4258 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004259 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004260 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004261{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004262 int evaluate = evalarg != NULL
4263 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004264 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004265 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004266 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004267 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004268 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004269 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004270
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004271 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4272 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004273
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004274 init_tv(&var1);
4275 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004276 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004277 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004278 /*
4279 * dict.name
4280 */
4281 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004282 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004283 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004284 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004285 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004286 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004287 }
4288 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004289 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004290 /*
4291 * something[idx]
4292 *
4293 * Get the (first) variable from inside the [].
4294 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004295 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004296 if (**arg == ':')
4297 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004298 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004299 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004300 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004301 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004302 semsg(_(e_white_space_required_before_and_after_str_at_str),
4303 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004304 clear_tv(&var1);
4305 return FAIL;
4306 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004307 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004308 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004309 int error = FALSE;
4310
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004311#ifdef FEAT_FLOAT
4312 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004313 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004314 && var1.v_type == VAR_FLOAT)
4315 {
4316 var1.vval.v_string = typval_tostring(&var1, TRUE);
4317 var1.v_type = VAR_STRING;
4318 }
4319#endif
Bram Moolenaar4525a572022-02-13 11:57:33 +00004320 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004321 tv_get_number_chk(&var1, &error);
4322 else
4323 error = tv_get_string_chk(&var1) == NULL;
4324 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004325 {
4326 // not a number or string
4327 clear_tv(&var1);
4328 return FAIL;
4329 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004330 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004331
4332 /*
4333 * Get the second variable from inside the [:].
4334 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004335 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004336 if (**arg == ':')
4337 {
4338 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004339 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004340 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004341 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004342 semsg(_(e_white_space_required_before_and_after_str_at_str),
4343 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004344 if (!empty1)
4345 clear_tv(&var1);
4346 return FAIL;
4347 }
4348 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004349 if (**arg == ']')
4350 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004351 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004352 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004353 if (!empty1)
4354 clear_tv(&var1);
4355 return FAIL;
4356 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004357 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004358 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004359 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004360 if (!empty1)
4361 clear_tv(&var1);
4362 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004363 return FAIL;
4364 }
4365 }
4366
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004367 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004368 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004369 if (**arg != ']')
4370 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004371 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004372 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004373 clear_tv(&var1);
4374 if (range)
4375 clear_tv(&var2);
4376 return FAIL;
4377 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004378 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004379 }
4380
4381 if (evaluate)
4382 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004383 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004384 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004385 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004386
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004387 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004388 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004389 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004390 clear_tv(&var2);
4391 return res;
4392 }
4393 return OK;
4394}
4395
4396/*
4397 * Check if "rettv" can have an [index] or [sli:ce]
4398 */
4399 int
4400check_can_index(typval_T *rettv, int evaluate, int verbose)
4401{
4402 switch (rettv->v_type)
4403 {
4404 case VAR_FUNC:
4405 case VAR_PARTIAL:
4406 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004407 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004408 return FAIL;
4409 case VAR_FLOAT:
4410#ifdef FEAT_FLOAT
4411 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004412 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004413 return FAIL;
4414#endif
4415 case VAR_BOOL:
4416 case VAR_SPECIAL:
4417 case VAR_JOB:
4418 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004419 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004420 if (verbose)
4421 emsg(_(e_cannot_index_special_variable));
4422 return FAIL;
4423 case VAR_UNKNOWN:
4424 case VAR_ANY:
4425 case VAR_VOID:
4426 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004427 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004428 emsg(_(e_cannot_index_special_variable));
4429 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004430 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004431 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004432
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004433 case VAR_STRING:
4434 case VAR_LIST:
4435 case VAR_DICT:
4436 case VAR_BLOB:
4437 break;
4438 case VAR_NUMBER:
4439 if (in_vim9script())
4440 emsg(_(e_cannot_index_number));
4441 break;
4442 }
4443 return OK;
4444}
4445
4446/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004447 * slice() function
4448 */
4449 void
4450f_slice(typval_T *argvars, typval_T *rettv)
4451{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004452 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004453 && ((argvars[0].v_type != VAR_STRING
4454 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004455 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004456 && check_for_list_arg(argvars, 0) == FAIL)
4457 || check_for_number_arg(argvars, 1) == FAIL
4458 || check_for_opt_number_arg(argvars, 2) == FAIL))
4459 return;
4460
Bram Moolenaar6601b622021-01-13 21:47:15 +01004461 if (check_can_index(argvars, TRUE, FALSE) == OK)
4462 {
4463 copy_tv(argvars, rettv);
4464 eval_index_inner(rettv, TRUE, argvars + 1,
4465 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4466 TRUE, NULL, 0, FALSE);
4467 }
4468}
4469
4470/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004471 * Apply index or range to "rettv".
4472 * "var1" is the first index, NULL for [:expr].
4473 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004474 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4475 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004476 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4477 */
4478 int
4479eval_index_inner(
4480 typval_T *rettv,
4481 int is_range,
4482 typval_T *var1,
4483 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004484 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004485 char_u *key,
4486 int keylen,
4487 int verbose)
4488{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004489 varnumber_T n1, n2 = 0;
4490 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004491
4492 n1 = 0;
4493 if (var1 != NULL && rettv->v_type != VAR_DICT)
4494 n1 = tv_get_number(var1);
4495
4496 if (is_range)
4497 {
4498 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004499 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004500 if (verbose)
4501 emsg(_(e_cannot_slice_dictionary));
4502 return FAIL;
4503 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004504 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004505 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004506 else
4507 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004508 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004509
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004510 switch (rettv->v_type)
4511 {
4512 case VAR_UNKNOWN:
4513 case VAR_ANY:
4514 case VAR_VOID:
4515 case VAR_FUNC:
4516 case VAR_PARTIAL:
4517 case VAR_FLOAT:
4518 case VAR_BOOL:
4519 case VAR_SPECIAL:
4520 case VAR_JOB:
4521 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004522 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004523 break; // not evaluating, skipping over subscript
4524
4525 case VAR_NUMBER:
4526 case VAR_STRING:
4527 {
4528 char_u *s = tv_get_string(rettv);
4529
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004530 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004531 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004532 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004533 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004534 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004535 else
4536 s = char_from_string(s, n1);
4537 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004538 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004539 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004540 // The resulting variable is a substring. If the indexes
4541 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004542 if (n1 < 0)
4543 {
4544 n1 = len + n1;
4545 if (n1 < 0)
4546 n1 = 0;
4547 }
4548 if (n2 < 0)
4549 n2 = len + n2;
4550 else if (n2 >= len)
4551 n2 = len;
4552 if (n1 >= len || n2 < 0 || n1 > n2)
4553 s = NULL;
4554 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004555 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004556 }
4557 else
4558 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004559 // The resulting variable is a string of a single
4560 // character. If the index is too big or negative the
4561 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004562 if (n1 >= len || n1 < 0)
4563 s = NULL;
4564 else
4565 s = vim_strnsave(s + n1, 1);
4566 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004567 clear_tv(rettv);
4568 rettv->v_type = VAR_STRING;
4569 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004570 }
4571 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004572
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004573 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004574 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4575 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004576 break;
4577
4578 case VAR_LIST:
4579 if (var1 == NULL)
4580 n1 = 0;
4581 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004582 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004583 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004584 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004585 return FAIL;
4586 break;
4587
4588 case VAR_DICT:
4589 {
4590 dictitem_T *item;
4591 typval_T tmp;
4592
4593 if (key == NULL)
4594 {
4595 key = tv_get_string_chk(var1);
4596 if (key == NULL)
4597 return FAIL;
4598 }
4599
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004600 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004601
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004602 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004603 {
4604 if (verbose)
4605 {
4606 if (keylen > 0)
4607 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004608 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004609 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004610 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004611 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004612
4613 copy_tv(&item->di_tv, &tmp);
4614 clear_tv(rettv);
4615 *rettv = tmp;
4616 }
4617 break;
4618 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004619 return OK;
4620}
4621
4622/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004623 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004624 */
4625 char_u *
4626partial_name(partial_T *pt)
4627{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004628 if (pt != NULL)
4629 {
4630 if (pt->pt_name != NULL)
4631 return pt->pt_name;
4632 if (pt->pt_func != NULL)
4633 return pt->pt_func->uf_name;
4634 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004635 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004636}
4637
Bram Moolenaarddecc252016-04-06 22:59:37 +02004638 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004639partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004640{
4641 int i;
4642
4643 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004644 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004645 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004646 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004647 if (pt->pt_name != NULL)
4648 {
4649 func_unref(pt->pt_name);
4650 vim_free(pt->pt_name);
4651 }
4652 else
4653 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004654
Bram Moolenaar54656012021-06-09 20:50:46 +02004655 // "out_up" is no longer used, decrement refcount on partial that owns it.
4656 partial_unref(pt->pt_outer.out_up_partial);
4657
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004658 // Using pt_outer from another partial.
4659 partial_unref(pt->pt_outer_partial);
4660
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004661 // Decrease the reference count for the context of a closure. If down
4662 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004663 if (pt->pt_funcstack != NULL)
4664 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004665 --pt->pt_funcstack->fs_refcount;
4666 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004667 }
4668
Bram Moolenaarddecc252016-04-06 22:59:37 +02004669 vim_free(pt);
4670}
4671
4672/*
4673 * Unreference a closure: decrement the reference count and free it when it
4674 * becomes zero.
4675 */
4676 void
4677partial_unref(partial_T *pt)
4678{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004679 if (pt != NULL)
4680 {
4681 if (--pt->pt_refcount <= 0)
4682 partial_free(pt);
4683
4684 // If the reference count goes down to one, the funcstack may be the
4685 // only reference and can be freed if no other partials reference it.
4686 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4687 funcstack_check_refcount(pt->pt_funcstack);
4688 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004689}
4690
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004691/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004692 * Return the next (unique) copy ID.
4693 * Used for serializing nested structures.
4694 */
4695 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004696get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004697{
4698 current_copyID += COPYID_INC;
4699 return current_copyID;
4700}
4701
4702/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004703 * Garbage collection for lists and dictionaries.
4704 *
4705 * We use reference counts to be able to free most items right away when they
4706 * are no longer used. But for composite items it's possible that it becomes
4707 * unused while the reference count is > 0: When there is a recursive
4708 * reference. Example:
4709 * :let l = [1, 2, 3]
4710 * :let d = {9: l}
4711 * :let l[1] = d
4712 *
4713 * Since this is quite unusual we handle this with garbage collection: every
4714 * once in a while find out which lists and dicts are not referenced from any
4715 * variable.
4716 *
4717 * Here is a good reference text about garbage collection (refers to Python
4718 * but it applies to all reference-counting mechanisms):
4719 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004720 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004721
4722/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004723 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004724 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004725 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004726 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004727 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004728garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004729{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004730 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004731 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004732 buf_T *buf;
4733 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004734 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004735 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004736
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004737 if (!testing)
4738 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004739 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004740 want_garbage_collect = FALSE;
4741 may_garbage_collect = FALSE;
4742 garbage_collect_at_exit = FALSE;
4743 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004744
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004745 // The execution stack can grow big, limit the size.
4746 if (exestack.ga_maxlen - exestack.ga_len > 500)
4747 {
4748 size_t new_len;
4749 char_u *pp;
4750 int n;
4751
4752 // Keep 150% of the current size, with a minimum of the growth size.
4753 n = exestack.ga_len / 2;
4754 if (n < exestack.ga_growsize)
4755 n = exestack.ga_growsize;
4756
4757 // Don't make it bigger though.
4758 if (exestack.ga_len + n < exestack.ga_maxlen)
4759 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004760 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004761 pp = vim_realloc(exestack.ga_data, new_len);
4762 if (pp == NULL)
4763 return FAIL;
4764 exestack.ga_maxlen = exestack.ga_len + n;
4765 exestack.ga_data = pp;
4766 }
4767 }
4768
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004769 // We advance by two because we add one for items referenced through
4770 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004771 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004772
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004773 /*
4774 * 1. Go through all accessible variables and mark all lists and dicts
4775 * with copyID.
4776 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004777
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004778 // Don't free variables in the previous_funccal list unless they are only
4779 // referenced through previous_funccal. This must be first, because if
4780 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004781 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004782
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004783 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004784 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004785
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004786 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004787 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004788 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4789 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004790
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004791 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004792 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004793 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4794 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004795 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004796 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4797 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004798#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004799 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004800 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4801 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004802 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004803 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004804 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4805 NULL, NULL);
4806#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004807
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004808 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004809 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004810 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4811 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004812 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004813 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004814
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004815 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004816 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004817
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004818 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004819 abort = abort || set_ref_in_functions(copyID);
4820
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004821 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004822 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004823
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004824 // funcstacks keep variables for closures
4825 abort = abort || set_ref_in_funcstacks(copyID);
4826
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004827 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004828 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004829
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004830 // callbacks in buffers
4831 abort = abort || set_ref_in_buffers(copyID);
4832
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004833 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
4834 abort = abort || set_ref_in_insexpand_funcs(copyID);
4835
4836 // 'operatorfunc' callback
4837 abort = abort || set_ref_in_opfunc(copyID);
4838
4839 // 'tagfunc' callback
4840 abort = abort || set_ref_in_tagfunc(copyID);
4841
4842 // 'imactivatefunc' and 'imstatusfunc' callbacks
4843 abort = abort || set_ref_in_im_funcs(copyID);
4844
Bram Moolenaar1dced572012-04-05 16:54:08 +02004845#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004846 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004847#endif
4848
Bram Moolenaardb913952012-06-29 12:54:53 +02004849#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004850 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004851#endif
4852
4853#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004854 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004855#endif
4856
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004857#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004858 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004859 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004860#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004861#ifdef FEAT_NETBEANS_INTG
4862 abort = abort || set_ref_in_nb_channel(copyID);
4863#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004864
Bram Moolenaare3188e22016-05-31 21:13:04 +02004865#ifdef FEAT_TIMERS
4866 abort = abort || set_ref_in_timer(copyID);
4867#endif
4868
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004869#ifdef FEAT_QUICKFIX
4870 abort = abort || set_ref_in_quickfix(copyID);
4871#endif
4872
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004873#ifdef FEAT_TERMINAL
4874 abort = abort || set_ref_in_term(copyID);
4875#endif
4876
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004877#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004878 abort = abort || set_ref_in_popups(copyID);
4879#endif
4880
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004881 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004882 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004883 /*
4884 * 2. Free lists and dictionaries that are not referenced.
4885 */
4886 did_free = free_unref_items(copyID);
4887
4888 /*
4889 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004890 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004891 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004892 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004893 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004894 else if (p_verbose > 0)
4895 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004896 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004897 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004898
4899 return did_free;
4900}
4901
4902/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004903 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004904 */
4905 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004906free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004907{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004908 int did_free = FALSE;
4909
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004910 // Let all "free" functions know that we are here. This means no
4911 // dictionaries, lists, channels or jobs are to be freed, because we will
4912 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004913 in_free_unref_items = TRUE;
4914
4915 /*
4916 * PASS 1: free the contents of the items. We don't free the items
4917 * themselves yet, so that it is possible to decrement refcount counters
4918 */
4919
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004920 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004921 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004922
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004923 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004924 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004925
4926#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004927 // Go through the list of jobs and free items without the copyID. This
4928 // must happen before doing channels, because jobs refer to channels, but
4929 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004930 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4931
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004932 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004933 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4934#endif
4935
4936 /*
4937 * PASS 2: free the items themselves.
4938 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004939 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004940 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004941
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004942#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004943 // Go through the list of jobs and free items without the copyID. This
4944 // must happen before doing channels, because jobs refer to channels, but
4945 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004946 free_unused_jobs(copyID, COPYID_MASK);
4947
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004948 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004949 free_unused_channels(copyID, COPYID_MASK);
4950#endif
4951
4952 in_free_unref_items = FALSE;
4953
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004954 return did_free;
4955}
4956
4957/*
4958 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004959 * "list_stack" is used to add lists to be marked. Can be NULL.
4960 *
4961 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004962 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004963 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004964set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004965{
4966 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004967 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004968 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004969 hashtab_T *cur_ht;
4970 ht_stack_T *ht_stack = NULL;
4971 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004972
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004973 cur_ht = ht;
4974 for (;;)
4975 {
4976 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004977 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004978 // Mark each item in the hashtab. If the item contains a hashtab
4979 // it is added to ht_stack, if it contains a list it is added to
4980 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004981 todo = (int)cur_ht->ht_used;
4982 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4983 if (!HASHITEM_EMPTY(hi))
4984 {
4985 --todo;
4986 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4987 &ht_stack, list_stack);
4988 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004989 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004990
4991 if (ht_stack == NULL)
4992 break;
4993
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004994 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004995 cur_ht = ht_stack->ht;
4996 tempitem = ht_stack;
4997 ht_stack = ht_stack->prev;
4998 free(tempitem);
4999 }
5000
5001 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005002}
5003
Dominique Pelle748b3082022-01-08 12:41:16 +00005004#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5005 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005006/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005007 * Mark a dict and its items with "copyID".
5008 * Returns TRUE if setting references failed somehow.
5009 */
5010 int
5011set_ref_in_dict(dict_T *d, int copyID)
5012{
5013 if (d != NULL && d->dv_copyID != copyID)
5014 {
5015 d->dv_copyID = copyID;
5016 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5017 }
5018 return FALSE;
5019}
Dominique Pelle748b3082022-01-08 12:41:16 +00005020#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005021
5022/*
5023 * Mark a list and its items with "copyID".
5024 * Returns TRUE if setting references failed somehow.
5025 */
5026 int
5027set_ref_in_list(list_T *ll, int copyID)
5028{
5029 if (ll != NULL && ll->lv_copyID != copyID)
5030 {
5031 ll->lv_copyID = copyID;
5032 return set_ref_in_list_items(ll, copyID, NULL);
5033 }
5034 return FALSE;
5035}
5036
5037/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005038 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005039 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5040 *
5041 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005042 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005043 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005044set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005045{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005046 listitem_T *li;
5047 int abort = FALSE;
5048 list_T *cur_l;
5049 list_stack_T *list_stack = NULL;
5050 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005051
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005052 cur_l = l;
5053 for (;;)
5054 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005055 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005056 // Mark each item in the list. If the item contains a hashtab
5057 // it is added to ht_stack, if it contains a list it is added to
5058 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005059 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5060 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5061 ht_stack, &list_stack);
5062 if (list_stack == NULL)
5063 break;
5064
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005065 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005066 cur_l = list_stack->list;
5067 tempitem = list_stack;
5068 list_stack = list_stack->prev;
5069 free(tempitem);
5070 }
5071
5072 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005073}
5074
5075/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005076 * Mark the partial in callback 'cb' with "copyID".
5077 */
5078 int
5079set_ref_in_callback(callback_T *cb, int copyID)
5080{
5081 typval_T tv;
5082
5083 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5084 return FALSE;
5085
5086 tv.v_type = VAR_PARTIAL;
5087 tv.vval.v_partial = cb->cb_partial;
5088 return set_ref_in_item(&tv, copyID, NULL, NULL);
5089}
5090
5091/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005092 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005093 * "list_stack" is used to add lists to be marked. Can be NULL.
5094 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5095 *
5096 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005097 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005098 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005099set_ref_in_item(
5100 typval_T *tv,
5101 int copyID,
5102 ht_stack_T **ht_stack,
5103 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005104{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005105 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005106
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005107 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005108 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005109 dict_T *dd = tv->vval.v_dict;
5110
Bram Moolenaara03f2332016-02-06 18:09:59 +01005111 if (dd != NULL && dd->dv_copyID != copyID)
5112 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005113 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005114 dd->dv_copyID = copyID;
5115 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005116 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005117 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5118 }
5119 else
5120 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005121 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5122
Bram Moolenaara03f2332016-02-06 18:09:59 +01005123 if (newitem == NULL)
5124 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005125 else
5126 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005127 newitem->ht = &dd->dv_hashtab;
5128 newitem->prev = *ht_stack;
5129 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005130 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005131 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005132 }
5133 }
5134 else if (tv->v_type == VAR_LIST)
5135 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005136 list_T *ll = tv->vval.v_list;
5137
Bram Moolenaara03f2332016-02-06 18:09:59 +01005138 if (ll != NULL && ll->lv_copyID != copyID)
5139 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005140 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005141 ll->lv_copyID = copyID;
5142 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005143 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005144 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005145 }
5146 else
5147 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005148 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5149
Bram Moolenaara03f2332016-02-06 18:09:59 +01005150 if (newitem == NULL)
5151 abort = TRUE;
5152 else
5153 {
5154 newitem->list = ll;
5155 newitem->prev = *list_stack;
5156 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005157 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005158 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005159 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005160 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005161 else if (tv->v_type == VAR_FUNC)
5162 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005163 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005164 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005165 else if (tv->v_type == VAR_PARTIAL)
5166 {
5167 partial_T *pt = tv->vval.v_partial;
5168 int i;
5169
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005170 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005171 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005172 // Didn't see this partial yet.
5173 pt->pt_copyID = copyID;
5174
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005175 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005176
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005177 if (pt->pt_dict != NULL)
5178 {
5179 typval_T dtv;
5180
5181 dtv.v_type = VAR_DICT;
5182 dtv.vval.v_dict = pt->pt_dict;
5183 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5184 }
5185
5186 for (i = 0; i < pt->pt_argc; ++i)
5187 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5188 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005189 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005190 }
5191 }
5192#ifdef FEAT_JOB_CHANNEL
5193 else if (tv->v_type == VAR_JOB)
5194 {
5195 job_T *job = tv->vval.v_job;
5196 typval_T dtv;
5197
5198 if (job != NULL && job->jv_copyID != copyID)
5199 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005200 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005201 if (job->jv_channel != NULL)
5202 {
5203 dtv.v_type = VAR_CHANNEL;
5204 dtv.vval.v_channel = job->jv_channel;
5205 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5206 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005207 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005208 {
5209 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005210 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005211 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5212 }
5213 }
5214 }
5215 else if (tv->v_type == VAR_CHANNEL)
5216 {
5217 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005218 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005219 typval_T dtv;
5220 jsonq_T *jq;
5221 cbq_T *cq;
5222
5223 if (ch != NULL && ch->ch_copyID != copyID)
5224 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005225 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005226 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005227 {
5228 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5229 jq = jq->jq_next)
5230 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5231 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5232 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005233 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005234 {
5235 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005236 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005237 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5238 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005239 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005240 {
5241 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005242 dtv.vval.v_partial =
5243 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005244 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5245 }
5246 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005247 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005248 {
5249 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005250 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005251 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5252 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005253 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005254 {
5255 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005256 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005257 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5258 }
5259 }
5260 }
5261#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005262 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005263}
5264
Bram Moolenaar8c711452005-01-14 21:53:12 +00005265/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005266 * Return a string with the string representation of a variable.
5267 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005268 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005269 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005270 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005271 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005272 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005273 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5274 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005275 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005276 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005277 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005278echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005279 typval_T *tv,
5280 char_u **tofree,
5281 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005282 int copyID,
5283 int echo_style,
5284 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005285 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005286{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005287 static int recurse = 0;
5288 char_u *r = NULL;
5289
Bram Moolenaar33570922005-01-25 22:26:29 +00005290 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005291 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005292 if (!did_echo_string_emsg)
5293 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005294 // Only give this message once for a recursive call to avoid
5295 // flooding the user with errors. And stop iterating over lists
5296 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005297 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005298 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005299 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005300 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005301 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005302 }
5303 ++recurse;
5304
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005305 switch (tv->v_type)
5306 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005307 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005308 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005309 {
5310 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005311 r = tv->vval.v_string;
5312 if (r == NULL)
5313 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005314 }
5315 else
5316 {
5317 *tofree = string_quote(tv->vval.v_string, FALSE);
5318 r = *tofree;
5319 }
5320 break;
5321
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005322 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005323 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005324 char_u buf[MAX_FUNC_NAME_LEN];
5325
5326 if (echo_style)
5327 {
LemonBoya5d35902022-04-29 21:15:02 +01005328 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5329 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005330 buf, MAX_FUNC_NAME_LEN);
5331 if (r == buf)
5332 {
5333 r = vim_strsave(buf);
5334 *tofree = r;
5335 }
5336 else
5337 *tofree = NULL;
5338 }
5339 else
5340 {
5341 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5342 : make_ufunc_name_readable(
5343 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5344 TRUE);
5345 r = *tofree;
5346 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005347 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005348 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005349
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005350 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005351 {
5352 partial_T *pt = tv->vval.v_partial;
5353 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005354 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005355 garray_T ga;
5356 int i;
5357 char_u *tf;
5358
5359 ga_init2(&ga, 1, 100);
5360 ga_concat(&ga, (char_u *)"function(");
5361 if (fname != NULL)
5362 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005363 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005364 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005365 && vim_isupper(fname[1]))
5366 {
5367 ga_concat(&ga, (char_u *)"'g:");
5368 ga_concat(&ga, fname + 1);
5369 }
5370 else
5371 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005372 vim_free(fname);
5373 }
5374 if (pt != NULL && pt->pt_argc > 0)
5375 {
5376 ga_concat(&ga, (char_u *)", [");
5377 for (i = 0; i < pt->pt_argc; ++i)
5378 {
5379 if (i > 0)
5380 ga_concat(&ga, (char_u *)", ");
5381 ga_concat(&ga,
5382 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5383 vim_free(tf);
5384 }
5385 ga_concat(&ga, (char_u *)"]");
5386 }
5387 if (pt != NULL && pt->pt_dict != NULL)
5388 {
5389 typval_T dtv;
5390
5391 ga_concat(&ga, (char_u *)", ");
5392 dtv.v_type = VAR_DICT;
5393 dtv.vval.v_dict = pt->pt_dict;
5394 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5395 vim_free(tf);
5396 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005397 // terminate with ')' and a NUL
5398 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005399
5400 *tofree = ga.ga_data;
5401 r = *tofree;
5402 break;
5403 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005404
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005405 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005406 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005407 break;
5408
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005409 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005410 if (tv->vval.v_list == NULL)
5411 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005412 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005413 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005414 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005415 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005416 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5417 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005418 {
5419 *tofree = NULL;
5420 r = (char_u *)"[...]";
5421 }
5422 else
5423 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005424 int old_copyID = tv->vval.v_list->lv_copyID;
5425
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005426 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005427 *tofree = list2string(tv, copyID, restore_copyID);
5428 if (restore_copyID)
5429 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005430 r = *tofree;
5431 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005432 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005433
Bram Moolenaar8c711452005-01-14 21:53:12 +00005434 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005435 if (tv->vval.v_dict == NULL)
5436 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005437 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005438 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005439 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005440 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005441 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5442 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005443 {
5444 *tofree = NULL;
5445 r = (char_u *)"{...}";
5446 }
5447 else
5448 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005449 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005450
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005451 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005452 *tofree = dict2string(tv, copyID, restore_copyID);
5453 if (restore_copyID)
5454 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005455 r = *tofree;
5456 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005457 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005458
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005459 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005460 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005461 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005462 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005463 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005464 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005465 break;
5466
Bram Moolenaar835dc632016-02-07 14:27:38 +01005467 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005468 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005469#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005470 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005471 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5472 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005473 if (composite_val)
5474 {
5475 *tofree = string_quote(r, FALSE);
5476 r = *tofree;
5477 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005478#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005479 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005480
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005481 case VAR_INSTR:
5482 *tofree = NULL;
5483 r = (char_u *)"instructions";
5484 break;
5485
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005486 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005487#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005488 *tofree = NULL;
5489 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5490 r = numbuf;
5491 break;
5492#endif
5493
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005494 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005495 case VAR_SPECIAL:
5496 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005497 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005498 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005499 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005500
Bram Moolenaar8502c702014-06-17 12:51:16 +02005501 if (--recurse == 0)
5502 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005503 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005504}
5505
5506/*
5507 * Return a string with the string representation of a variable.
5508 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5509 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005510 * Does not put quotes around strings, as ":echo" displays values.
5511 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5512 * May return NULL.
5513 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005514 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005515echo_string(
5516 typval_T *tv,
5517 char_u **tofree,
5518 char_u *numbuf,
5519 int copyID)
5520{
5521 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5522}
5523
5524/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005525 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5526 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005527 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005528 */
5529 int
5530buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5531{
5532 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005533 char_u *t;
5534 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005535
5536 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5537 return -1;
5538
5539 if (lnum > buf->b_ml.ml_line_count)
5540 lnum = buf->b_ml.ml_line_count;
5541
5542 str = ml_get_buf(buf, lnum, FALSE);
5543 if (str == NULL)
5544 return -1;
5545
5546 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005547 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005548
Bram Moolenaar91458462021-01-13 20:08:38 +01005549 // count the number of characters
5550 t = str;
5551 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5552 t += mb_ptr2len(t);
5553
5554 // In insert mode, when the cursor is at the end of a non-empty line,
5555 // byteidx points to the NUL character immediately past the end of the
5556 // string. In this case, add one to the character count.
5557 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5558 count++;
5559
5560 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005561}
5562
5563/*
5564 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005565 * byte index. Works only for loaded buffers. Returns -1 on failure.
5566 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005567 */
5568 int
5569buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5570{
5571 char_u *str;
5572 char_u *t;
5573
5574 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5575 return -1;
5576
5577 if (lnum > buf->b_ml.ml_line_count)
5578 lnum = buf->b_ml.ml_line_count;
5579
5580 str = ml_get_buf(buf, lnum, FALSE);
5581 if (str == NULL)
5582 return -1;
5583
5584 // Convert the character offset to a byte offset
5585 t = str;
5586 while (*t != NUL && --charidx > 0)
5587 t += mb_ptr2len(t);
5588
Bram Moolenaar91458462021-01-13 20:08:38 +01005589 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005590}
5591
5592/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005594 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005596 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005597var2fpos(
5598 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005599 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005600 int *fnum, // set to fnum for '0, 'A, etc.
5601 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005603 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005605 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005607 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005608 if (varp->v_type == VAR_LIST)
5609 {
5610 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005611 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005612 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005613 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005614
5615 l = varp->vval.v_list;
5616 if (l == NULL)
5617 return NULL;
5618
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005619 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005620 pos.lnum = list_find_nr(l, 0L, &error);
5621 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005622 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005623 if (charcol)
5624 len = (long)mb_charlen(ml_get(pos.lnum));
5625 else
5626 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005627
Bram Moolenaarec65d772020-08-20 22:29:12 +02005628 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005629 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005630 li = list_find(l, 1L);
5631 if (li != NULL && li->li_tv.v_type == VAR_STRING
5632 && li->li_tv.vval.v_string != NULL
5633 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005634 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005635 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005636 }
5637 else
5638 {
5639 pos.col = list_find_nr(l, 1L, &error);
5640 if (error)
5641 return NULL;
5642 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005643
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005644 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005645 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005646 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005647 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005648
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005649 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005650 pos.coladd = list_find_nr(l, 2L, &error);
5651 if (error)
5652 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005653
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005654 return &pos;
5655 }
5656
Bram Moolenaarc5809432021-03-27 21:23:30 +01005657 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5658 return NULL;
5659
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005660 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005661 if (name == NULL)
5662 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005663
5664 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005665 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005666 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005667 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005668 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005669 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005670 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005671 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005672 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005673 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005674 pos = VIsual;
5675 else
5676 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005677 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005678 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005679 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005681 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005682 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5684 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005685 pos = *pp;
5686 }
5687 if (pos.lnum != 0)
5688 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005689 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005690 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5691 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005693
Bram Moolenaara5525202006-03-02 22:52:09 +00005694 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005695
Bram Moolenaar477933c2007-07-17 14:32:23 +00005696 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005697 {
5698 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005699 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005700 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005701 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005702 // In silent Ex mode topline is zero, but that's not a valid line
5703 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005704 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005705 return &pos;
5706 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005707 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005708 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005709 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005710 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005711 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005712 return &pos;
5713 }
5714 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005715 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005717 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 {
5719 pos.lnum = curbuf->b_ml.ml_line_count;
5720 pos.col = 0;
5721 }
5722 else
5723 {
5724 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005725 if (charcol)
5726 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5727 else
5728 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 }
5730 return &pos;
5731 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005732 if (in_vim9script())
5733 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 return NULL;
5735}
5736
5737/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005738 * Convert list in "arg" into a position and optional file number.
5739 * When "fnump" is NULL there is no file number, only 3 items.
5740 * Note that the column is passed on as-is, the caller may want to decrement
5741 * it to use 1 for the first column.
5742 * Return FAIL when conversion is not possible, doesn't check the position for
5743 * validity.
5744 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005745 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005746list2fpos(
5747 typval_T *arg,
5748 pos_T *posp,
5749 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005750 colnr_T *curswantp,
5751 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005752{
5753 list_T *l = arg->vval.v_list;
5754 long i = 0;
5755 long n;
5756
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005757 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5758 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005759 if (arg->v_type != VAR_LIST
5760 || l == NULL
5761 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005762 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005763 return FAIL;
5764
5765 if (fnump != NULL)
5766 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005767 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005768 if (n < 0)
5769 return FAIL;
5770 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005771 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005772 *fnump = n;
5773 }
5774
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005775 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005776 if (n < 0)
5777 return FAIL;
5778 posp->lnum = n;
5779
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005780 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005781 if (n < 0)
5782 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005783 // If character position is specified, then convert to byte position
5784 if (charcol)
5785 {
5786 buf_T *buf;
5787
5788 // Get the text for the specified line in a loaded buffer
5789 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5790 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5791 return FAIL;
5792
Bram Moolenaar91458462021-01-13 20:08:38 +01005793 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005794 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005795 posp->col = n;
5796
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005797 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005798 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005799 posp->coladd = 0;
5800 else
5801 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005802
Bram Moolenaar493c1782014-05-28 14:34:46 +02005803 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005804 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005805
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005806 return OK;
5807}
5808
5809/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 * Get the length of an environment variable name.
5811 * Advance "arg" to the first character after the name.
5812 * Return 0 for error.
5813 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005814 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005815get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816{
5817 char_u *p;
5818 int len;
5819
5820 for (p = *arg; vim_isIDc(*p); ++p)
5821 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005822 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 return 0;
5824
5825 len = (int)(p - *arg);
5826 *arg = p;
5827 return len;
5828}
5829
5830/*
5831 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005832 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 * Return 0 if something is wrong.
5834 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005835 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005836get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837{
5838 char_u *p;
5839 int len;
5840
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005841 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005843 {
5844 if (*p == ':')
5845 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005846 // "s:" is start of "s:var", but "n:" is not and can be used in
5847 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005848 len = (int)(p - *arg);
5849 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5850 || len > 1)
5851 break;
5852 }
5853 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005854 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 return 0;
5856
5857 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005858 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859
5860 return len;
5861}
5862
5863/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005864 * Get the length of the name of a variable or function.
5865 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005867 * Return -1 if curly braces expansion failed.
5868 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 * If the name contains 'magic' {}'s, expand them and return the
5870 * expanded name in an allocated string via 'alias' - caller must free.
5871 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005872 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005873get_name_len(
5874 char_u **arg,
5875 char_u **alias,
5876 int evaluate,
5877 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878{
5879 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 char_u *p;
5881 char_u *expr_start;
5882 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005884 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885
5886 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5887 && (*arg)[2] == (int)KE_SNR)
5888 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005889 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 *arg += 3;
5891 return get_id_len(arg) + 3;
5892 }
5893 len = eval_fname_script(*arg);
5894 if (len > 0)
5895 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005896 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 *arg += len;
5898 }
5899
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005901 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005903 p = find_name_end(*arg, &expr_start, &expr_end,
5904 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 if (expr_start != NULL)
5906 {
5907 char_u *temp_string;
5908
5909 if (!evaluate)
5910 {
5911 len += (int)(p - *arg);
5912 *arg = skipwhite(p);
5913 return len;
5914 }
5915
5916 /*
5917 * Include any <SID> etc in the expanded string:
5918 * Thus the -len here.
5919 */
5920 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5921 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005922 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 *alias = temp_string;
5924 *arg = skipwhite(p);
5925 return (int)STRLEN(temp_string);
5926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927
5928 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005929 // Only give an error when there is something, otherwise it will be
5930 // reported at a higher level.
5931 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005932 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933
5934 return len;
5935}
5936
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005937/*
5938 * Find the end of a variable or function name, taking care of magic braces.
5939 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5940 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005941 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005942 * Return a pointer to just after the name. Equal to "arg" if there is no
5943 * valid name.
5944 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005945 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005946find_name_end(
5947 char_u *arg,
5948 char_u **expr_start,
5949 char_u **expr_end,
5950 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005952 int mb_nest = 0;
5953 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005955 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005956 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005958 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005960 *expr_start = NULL;
5961 *expr_end = NULL;
5962 }
5963
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005964 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005965 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5966 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005967 return arg;
5968
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005969 for (p = arg; *p != NUL
5970 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005971 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005972 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005973 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005974 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005975 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005976 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005977 if (*p == '\'')
5978 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005979 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005980 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005981 ;
5982 if (*p == NUL)
5983 break;
5984 }
5985 else if (*p == '"')
5986 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005987 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005988 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005989 if (*p == '\\' && p[1] != NUL)
5990 ++p;
5991 if (*p == NUL)
5992 break;
5993 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005994 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5995 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005996 // "s:" is start of "s:var", but "n:" is not and can be used in
5997 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005998 len = (int)(p - arg);
5999 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006000 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006001 break;
6002 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006003
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006004 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006006 if (*p == '[')
6007 ++br_nest;
6008 else if (*p == ']')
6009 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006011
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006012 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006014 if (*p == '{')
6015 {
6016 mb_nest++;
6017 if (expr_start != NULL && *expr_start == NULL)
6018 *expr_start = p;
6019 }
6020 else if (*p == '}')
6021 {
6022 mb_nest--;
6023 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6024 *expr_end = p;
6025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 }
6028
6029 return p;
6030}
6031
6032/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006033 * Expands out the 'magic' {}'s in a variable/function name.
6034 * Note that this can call itself recursively, to deal with
6035 * constructs like foo{bar}{baz}{bam}
6036 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6037 * "in_start" ^
6038 * "expr_start" ^
6039 * "expr_end" ^
6040 * "in_end" ^
6041 *
6042 * Returns a new allocated string, which the caller must free.
6043 * Returns NULL for failure.
6044 */
6045 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006046make_expanded_name(
6047 char_u *in_start,
6048 char_u *expr_start,
6049 char_u *expr_end,
6050 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006051{
6052 char_u c1;
6053 char_u *retval = NULL;
6054 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006055
6056 if (expr_end == NULL || in_end == NULL)
6057 return NULL;
6058 *expr_start = NUL;
6059 *expr_end = NUL;
6060 c1 = *in_end;
6061 *in_end = NUL;
6062
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006063 temp_result = eval_to_string(expr_start + 1, FALSE);
6064 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006065 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006066 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6067 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006068 if (retval != NULL)
6069 {
6070 STRCPY(retval, in_start);
6071 STRCAT(retval, temp_result);
6072 STRCAT(retval, expr_end + 1);
6073 }
6074 }
6075 vim_free(temp_result);
6076
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006077 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006078 *expr_start = '{';
6079 *expr_end = '}';
6080
6081 if (retval != NULL)
6082 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006083 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006084 if (expr_start != NULL)
6085 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006086 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006087 temp_result = make_expanded_name(retval, expr_start,
6088 expr_end, temp_result);
6089 vim_free(retval);
6090 retval = temp_result;
6091 }
6092 }
6093
6094 return retval;
6095}
6096
6097/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006099 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006101 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006102eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006104 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006105}
6106
6107/*
6108 * Return TRUE if character "c" can be used as the first character in a
6109 * variable or function name (excluding '{' and '}').
6110 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006111 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006112eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006113{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006114 return ASCII_ISALPHA(c) || c == '_';
6115}
6116
6117/*
6118 * Return TRUE if character "c" can be used as the first character of a
6119 * dictionary key.
6120 */
6121 int
6122eval_isdictc(int c)
6123{
6124 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125}
6126
6127/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006128 * Handle:
6129 * - expr[expr], expr[expr:expr] subscript
6130 * - ".name" lookup
6131 * - function call with Funcref variable: func(expr)
6132 * - method call: var->method()
6133 *
6134 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006135 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006136 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006137 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006138handle_subscript(
6139 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006140 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006141 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006142 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006143 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006144{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006145 int evaluate = evalarg != NULL
6146 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006147 int ret = OK;
6148 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006149 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006150 int getnext;
6151 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006152
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006153 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006154 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006155 // When at the end of the line and ".name" or "->{" or "->X" follows in
6156 // the next line then consume the line break.
6157 p = eval_next_non_blank(*arg, evalarg, &getnext);
6158 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006159 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006160 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6161 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6162 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006163 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006164 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006165 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006166 check_white = FALSE;
6167 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006168
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006169 if (rettv->v_type == VAR_ANY)
6170 {
6171 char_u *exp_name;
6172 int cc;
6173 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006174 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006175 type_T *type;
6176
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006177 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006178 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006179 if (**arg != '.')
6180 {
6181 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006182 semsg(_(e_expected_dot_after_name_str),
6183 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006184 ret = FAIL;
6185 break;
6186 }
6187 ++*arg;
6188 if (IS_WHITE_OR_NUL(**arg))
6189 {
6190 if (verbose)
6191 emsg(_(e_no_white_space_allowed_after_dot));
6192 ret = FAIL;
6193 break;
6194 }
6195
6196 // isolate the name
6197 exp_name = *arg;
6198 while (eval_isnamec(**arg))
6199 ++*arg;
6200 cc = **arg;
6201 **arg = NUL;
6202
6203 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00006204 evalarg->eval_cctx, evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006205 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006206
6207 if (idx < 0 && ufunc == NULL)
6208 {
6209 ret = FAIL;
6210 break;
6211 }
6212 if (idx >= 0)
6213 {
6214 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6215 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6216
6217 copy_tv(sv->sv_tv, rettv);
6218 }
6219 else
6220 {
6221 rettv->v_type = VAR_FUNC;
6222 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6223 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006224 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006225 }
6226
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006227 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6228 || rettv->v_type == VAR_PARTIAL))
6229 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006230 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006231 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6232 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006233
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006234 // Stop the expression evaluation when immediately aborting on
6235 // error, or when an interrupt occurred or an exception was thrown
6236 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006237 if (aborting())
6238 {
6239 if (ret == OK)
6240 clear_tv(rettv);
6241 ret = FAIL;
6242 }
6243 dict_unref(selfdict);
6244 selfdict = NULL;
6245 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006246 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006247 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006248 if (in_vim9script())
6249 *arg = skipwhite(p + 2);
6250 else
6251 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006252 if (ret == OK)
6253 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006254 if (VIM_ISWHITE(**arg))
6255 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006256 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006257 ret = FAIL;
6258 }
6259 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006260 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006261 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006262 else
6263 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006264 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006265 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006266 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006267 // "." is ".name" lookup when we found a dict or when evaluating and
6268 // scriptversion is at least 2, where string concatenation is "..".
6269 else if (**arg == '['
6270 || (**arg == '.' && (rettv->v_type == VAR_DICT
6271 || (!evaluate
6272 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006273 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006274 {
6275 dict_unref(selfdict);
6276 if (rettv->v_type == VAR_DICT)
6277 {
6278 selfdict = rettv->vval.v_dict;
6279 if (selfdict != NULL)
6280 ++selfdict->dv_refcount;
6281 }
6282 else
6283 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006284 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006285 {
6286 clear_tv(rettv);
6287 ret = FAIL;
6288 }
6289 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006290 else
6291 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006292 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006293
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006294 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6295 // Don't do this when "Func" is already a partial that was bound
6296 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006297 if (selfdict != NULL
6298 && (rettv->v_type == VAR_FUNC
6299 || (rettv->v_type == VAR_PARTIAL
6300 && (rettv->vval.v_partial->pt_auto
6301 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006302 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006303
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006304 dict_unref(selfdict);
6305 return ret;
6306}
6307
6308/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006309 * Make a copy of an item.
6310 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006311 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006312 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6313 * reference to an already copied list/dict can be used.
6314 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006315 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006316 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006317item_copy(
6318 typval_T *from,
6319 typval_T *to,
6320 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006321 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006322 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006323{
6324 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006325 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006326
Bram Moolenaar33570922005-01-25 22:26:29 +00006327 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006328 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006329 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006330 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006331 }
6332 ++recurse;
6333
6334 switch (from->v_type)
6335 {
6336 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006337 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006338 case VAR_STRING:
6339 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006340 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006341 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006342 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006343 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006344 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006345 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006346 copy_tv(from, to);
6347 break;
6348 case VAR_LIST:
6349 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006350 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006351 if (from->vval.v_list == NULL)
6352 to->vval.v_list = NULL;
6353 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6354 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006355 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006356 to->vval.v_list = from->vval.v_list->lv_copylist;
6357 ++to->vval.v_list->lv_refcount;
6358 }
6359 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006360 to->vval.v_list = list_copy(from->vval.v_list,
6361 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006362 if (to->vval.v_list == NULL)
6363 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006364 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006365 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006366 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006367 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006368 case VAR_DICT:
6369 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006370 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006371 if (from->vval.v_dict == NULL)
6372 to->vval.v_dict = NULL;
6373 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6374 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006375 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006376 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6377 ++to->vval.v_dict->dv_refcount;
6378 }
6379 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006380 to->vval.v_dict = dict_copy(from->vval.v_dict,
6381 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006382 if (to->vval.v_dict == NULL)
6383 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006384 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006385 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006386 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006387 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006388 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006389 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006390 }
6391 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006392 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006393}
6394
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006395 void
6396echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6397{
6398 char_u *tofree;
6399 char_u numbuf[NUMBUFLEN];
6400 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6401
6402 if (*atstart)
6403 {
6404 *atstart = FALSE;
6405 // Call msg_start() after eval1(), evaluating the expression
6406 // may cause a message to appear.
6407 if (with_space)
6408 {
6409 // Mark the saved text as finishing the line, so that what
6410 // follows is displayed on a new line when scrolling back
6411 // at the more prompt.
6412 msg_sb_eol();
6413 msg_start();
6414 }
6415 }
6416 else if (with_space)
6417 msg_puts_attr(" ", echo_attr);
6418
6419 if (p != NULL)
6420 for ( ; *p != NUL && !got_int; ++p)
6421 {
6422 if (*p == '\n' || *p == '\r' || *p == TAB)
6423 {
6424 if (*p != TAB && *needclr)
6425 {
6426 // remove any text still there from the command
6427 msg_clr_eos();
6428 *needclr = FALSE;
6429 }
6430 msg_putchar_attr(*p, echo_attr);
6431 }
6432 else
6433 {
6434 if (has_mbyte)
6435 {
6436 int i = (*mb_ptr2len)(p);
6437
6438 (void)msg_outtrans_len_attr(p, i, echo_attr);
6439 p += i - 1;
6440 }
6441 else
6442 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6443 }
6444 }
6445 vim_free(tofree);
6446}
6447
Bram Moolenaare9a41262005-01-15 22:18:47 +00006448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 * ":echo expr1 ..." print each argument separated with a space, add a
6450 * newline at the end.
6451 * ":echon expr1 ..." print each argument plain.
6452 */
6453 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006454ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455{
6456 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006457 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006458 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 int needclr = TRUE;
6460 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006461 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006462 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006463 evalarg_T evalarg;
6464
Bram Moolenaare707c882020-07-01 13:07:37 +02006465 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466
6467 if (eap->skip)
6468 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006469 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006471 // If eval1() causes an error message the text from the command may
6472 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006473 need_clr_eos = needclr;
6474
Bram Moolenaar68db9962021-05-09 23:19:22 +02006475 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006476 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 {
6478 /*
6479 * Report the invalid expression unless the expression evaluation
6480 * has been cancelled due to an aborting error, an interrupt, or an
6481 * exception.
6482 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006483 if (!aborting() && did_emsg == did_emsg_before
6484 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006485 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006486 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 break;
6488 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006489 need_clr_eos = FALSE;
6490
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006492 {
6493 if (rettv.v_type == VAR_VOID)
6494 {
6495 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6496 break;
6497 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006498 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006499 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006500
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006501 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 arg = skipwhite(arg);
6503 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006504 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006505 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506
6507 if (eap->skip)
6508 --emsg_skip;
6509 else
6510 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006511 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 if (needclr)
6513 msg_clr_eos();
6514 if (eap->cmdidx == CMD_echo)
6515 msg_end();
6516 }
6517}
6518
6519/*
6520 * ":echohl {name}".
6521 */
6522 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006523ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006525 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526}
6527
6528/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006529 * Returns the :echo attribute
6530 */
6531 int
6532get_echo_attr(void)
6533{
6534 return echo_attr;
6535}
6536
6537/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 * ":execute expr1 ..." execute the result of an expression.
6539 * ":echomsg expr1 ..." Print a message
6540 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006541 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 * Each gets spaces around each argument and a newline at the end for
6543 * echo commands
6544 */
6545 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006546ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547{
6548 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006549 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 int ret = OK;
6551 char_u *p;
6552 garray_T ga;
6553 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006554 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555
6556 ga_init2(&ga, 1, 80);
6557
6558 if (eap->skip)
6559 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006560 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006562 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006563 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565
6566 if (!eap->skip)
6567 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006568 char_u buf[NUMBUFLEN];
6569
6570 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006571 {
6572 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6573 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006574 semsg(_(e_using_invalid_value_as_string_str),
6575 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006576 p = NULL;
6577 }
6578 else
6579 p = tv_get_string_buf(&rettv, buf);
6580 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006581 else
6582 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006583 if (p == NULL)
6584 {
6585 clear_tv(&rettv);
6586 ret = FAIL;
6587 break;
6588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 len = (int)STRLEN(p);
6590 if (ga_grow(&ga, len + 2) == FAIL)
6591 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006592 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 ret = FAIL;
6594 break;
6595 }
6596 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 ga.ga_len += len;
6600 }
6601
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006602 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 arg = skipwhite(arg);
6604 }
6605
6606 if (ret != FAIL && ga.ga_data != NULL)
6607 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006608 // use the first line of continuation lines for messages
6609 SOURCING_LNUM = start_lnum;
6610
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006611 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6612 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006613 // Mark the already saved text as finishing the line, so that what
6614 // follows is displayed on a new line when scrolling back at the
6615 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006616 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006617 }
6618
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006620 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006621 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006622 out_flush();
6623 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006624 else if (eap->cmdidx == CMD_echoconsole)
6625 {
6626 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6627 ui_write((char_u *)"\r\n", 2, TRUE);
6628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 else if (eap->cmdidx == CMD_echoerr)
6630 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006631 int save_did_emsg = did_emsg;
6632
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006633 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006634 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 if (!force_abort)
6636 did_emsg = save_did_emsg;
6637 }
6638 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006639 {
6640 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6641
6642 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6643 sticky_cmdmod_flags = cmdmod.cmod_flags
6644 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 do_cmdline((char_u *)ga.ga_data,
6646 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006647 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 }
6650
6651 ga_clear(&ga);
6652
6653 if (eap->skip)
6654 --emsg_skip;
6655
Bram Moolenaar63b91732021-08-05 20:40:03 +02006656 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657}
6658
6659/*
6660 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6661 * "arg" points to the "&" or '+' when called, to "option" when returning.
6662 * Returns NULL when no option name found. Otherwise pointer to the char
6663 * after the option name.
6664 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006665 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006666find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667{
6668 char_u *p = *arg;
6669
6670 ++p;
6671 if (*p == 'g' && p[1] == ':')
6672 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006673 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 p += 2;
6675 }
6676 else if (*p == 'l' && p[1] == ':')
6677 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006678 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 p += 2;
6680 }
6681 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006682 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683
6684 if (!ASCII_ISALPHA(*p))
6685 return NULL;
6686 *arg = p;
6687
6688 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006689 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 else
6691 while (ASCII_ISALPHA(*p))
6692 ++p;
6693 return p;
6694}
6695
6696/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006697 * Display script name where an item was last set.
6698 * Should only be invoked when 'verbose' is non-zero.
6699 */
6700 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006701last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006702{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006703 char_u *p;
6704
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006705 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006706 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006707 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006708 if (p != NULL)
6709 {
6710 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006711 msg_puts(_("\n\tLast set from "));
6712 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006713 if (script_ctx.sc_lnum > 0)
6714 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006715 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006716 msg_outnum((long)script_ctx.sc_lnum);
6717 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006718 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006719 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006720 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006721 }
6722}
6723
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006724#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725
6726/*
6727 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006728 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 * "flags" can be "g" to do a global substitute.
6730 * Returns an allocated string, NULL for error.
6731 */
6732 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006733do_string_sub(
6734 char_u *str,
6735 char_u *pat,
6736 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006737 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006738 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739{
6740 int sublen;
6741 regmatch_T regmatch;
6742 int i;
6743 int do_all;
6744 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006745 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 garray_T ga;
6747 char_u *ret;
6748 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006749 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006751 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006753 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754
6755 ga_init2(&ga, 1, 200);
6756
6757 do_all = (flags[0] == 'g');
6758
6759 regmatch.rm_ic = p_ic;
6760 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6761 if (regmatch.regprog != NULL)
6762 {
6763 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006764 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6766 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006767 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006768 if (regmatch.startp[0] == regmatch.endp[0])
6769 {
6770 if (zero_width == regmatch.startp[0])
6771 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006772 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006773 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006774 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6775 (size_t)i);
6776 ga.ga_len += i;
6777 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006778 continue;
6779 }
6780 zero_width = regmatch.startp[0];
6781 }
6782
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 /*
6784 * Get some space for a temporary buffer to do the substitution
6785 * into. It will contain:
6786 * - The text up to where the match is.
6787 * - The substituted text.
6788 * - The text after the match.
6789 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006790 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006791 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6793 {
6794 ga_clear(&ga);
6795 break;
6796 }
6797
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006798 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 i = (int)(regmatch.startp[0] - tail);
6800 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006801 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006802 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 + ga.ga_len + i, TRUE, TRUE, FALSE);
6804 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006805 tail = regmatch.endp[0];
6806 if (*tail == NUL)
6807 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 if (!do_all)
6809 break;
6810 }
6811
6812 if (ga.ga_data != NULL)
6813 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6814
Bram Moolenaar473de612013-06-08 18:19:48 +02006815 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 }
6817
6818 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6819 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006820 if (p_cpo == empty_option)
6821 p_cpo = save_cpo;
6822 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006823 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006824 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006825 // If it's still empty it was changed and restored, need to restore in
6826 // the complicated way.
6827 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01006828 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006829 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831
6832 return ret;
6833}