blob: 0c6d2e5ac1e29cae6f85ac8f39deec6994fcd929 [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 Moolenaar7a4b8982020-07-08 17:36:21 +02002178 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002179 && (*p == NUL || *p == NL
2180 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002181 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002182 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002183
Bram Moolenaare442d592022-05-05 12:20:28 +01002184 if (*p == NL)
2185 next = p + 1;
2186 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002187 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002188 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002189 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002190
Bram Moolenaar9d489562020-07-30 20:08:50 +02002191 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002192 {
2193 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002194 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002195 }
2196 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002197 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002198}
2199
2200/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002201 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002202 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002203 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002204 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002205eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002206{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002207 garray_T *gap = &evalarg->eval_ga;
2208 char_u *line;
2209
Bram Moolenaare442d592022-05-05 12:20:28 +01002210 if (arg != NULL && *arg == NL)
2211 return skipwhite(arg + 1);
2212
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002213 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002214 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2215 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002216 else
2217 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002218 if (line == NULL)
2219 return NULL;
2220
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002221 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002222 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2223 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002224 char_u *p = skipwhite(line);
2225
2226 // Going to concatenate the lines after parsing. For an empty or
2227 // comment line use an empty string.
2228 if (*p == NUL || vim9_comment_start(p))
2229 {
2230 vim_free(line);
2231 line = vim_strsave((char_u *)"");
2232 }
2233
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002234 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2235 ++gap->ga_len;
2236 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002237 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002238 {
2239 vim_free(evalarg->eval_tofree);
2240 evalarg->eval_tofree = line;
2241 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002242
2243 // Advanced to the next line, "arg" no longer points into the previous
2244 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002245 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002246 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002247}
2248
Bram Moolenaare6b53242020-07-01 17:28:33 +02002249/*
2250 * Call eval_next_non_blank() and get the next line if needed.
2251 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002252 char_u *
2253skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2254{
2255 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002256 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002257
Bram Moolenaar962d7212020-07-04 14:15:00 +02002258 if (evalarg == NULL)
2259 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002260 eval_next_non_blank(p, evalarg, &getnext);
2261 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002262 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002263 return p;
2264}
2265
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002266/*
Bram Moolenaar844fb642021-10-23 13:32:30 +01002267 * Initialize "evalarg" for use.
2268 */
2269 void
2270init_evalarg(evalarg_T *evalarg)
2271{
2272 CLEAR_POINTER(evalarg);
2273 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
2274}
2275
2276/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002277 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002278 */
2279 void
2280clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2281{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002282 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002283 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002284 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002285 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002286 if (eap != NULL)
2287 {
2288 // We may need to keep the original command line, e.g. for
2289 // ":let" it has the variable names. But we may also need the
2290 // new one, "nextcmd" points into it. Keep both.
2291 vim_free(eap->cmdline_tofree);
2292 eap->cmdline_tofree = *eap->cmdlinep;
2293 *eap->cmdlinep = evalarg->eval_tofree;
2294 }
2295 else
2296 vim_free(evalarg->eval_tofree);
2297 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002298 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002299
Bram Moolenaar844fb642021-10-23 13:32:30 +01002300 ga_clear_strings(&evalarg->eval_tofree_ga);
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002301 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002302 }
2303}
2304
2305/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2309 */
2310
2311/*
2312 * Handle zero level expression.
2313 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002315 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002316 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 * Return OK or FAIL.
2318 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002319 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002320eval0(
2321 char_u *arg,
2322 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002323 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002324 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002326 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2327}
2328
2329/*
2330 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2331 * expression and don't check what comes after the expression.
2332 */
2333 int
2334eval0_retarg(
2335 char_u *arg,
2336 typval_T *rettv,
2337 exarg_T *eap,
2338 evalarg_T *evalarg,
2339 char_u **retarg)
2340{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 int ret;
2342 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002343 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002344 int did_emsg_before = did_emsg;
2345 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002346 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002347 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002348 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349
2350 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002351 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002352 expr_end = p;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002353 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002354
Bram Moolenaar02929a32021-12-17 14:46:12 +00002355 // In Vim9 script a command block is not split at NL characters for
2356 // commands using an expression argument. Skip over a '#' comment to check
2357 // for a following NL. Require white space before the '#'.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002358 if (in_vim9script() && p > expr_end && retarg == NULL)
Bram Moolenaar02929a32021-12-17 14:46:12 +00002359 while (*p == '#')
2360 {
2361 char_u *nl = vim_strchr(p, NL);
2362
2363 if (nl == NULL)
2364 break;
2365 p = skipwhite(nl + 1);
2366 if (eap != NULL && *p != NUL)
2367 eap->nextcmd = p;
2368 check_for_end = FALSE;
2369 }
2370
2371 if (ret != FAIL && check_for_end)
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002372 end_error = !ends_excmd2(arg, p);
2373 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 {
2375 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002376 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 /*
2378 * Report the invalid expression unless the expression evaluation has
2379 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002380 * exception, or we already gave a more specific error.
2381 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002383 if (!aborting()
2384 && did_emsg == did_emsg_before
2385 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002386 && (flags & EVAL_CONSTANT) == 0
2387 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002388 {
2389 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002390 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002391 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002392 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002393 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002394
2395 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002396 // a next command to avoid more errors, unless "|" is following, which
2397 // could only be a command separator.
2398 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2399 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002400 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002402
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002403 if (retarg != NULL)
2404 *retarg = p;
2405 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002406 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002407
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 return ret;
2409}
2410
2411/*
2412 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002413 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002414 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 *
2416 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002417 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002419 * Note: "rettv.v_lock" is not set.
2420 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 * Return OK or FAIL.
2422 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002423 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002424eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002426 char_u *p;
2427 int getnext;
2428
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002429 CLEAR_POINTER(rettv);
2430
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 /*
2432 * Get the first variable.
2433 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002434 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 return FAIL;
2436
Bram Moolenaar793648f2020-06-26 21:28:25 +02002437 p = eval_next_non_blank(*arg, evalarg, &getnext);
2438 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002440 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002441 int result;
2442 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002443 evalarg_T *evalarg_used = evalarg;
2444 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002445 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002446 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002447 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002448
2449 if (evalarg == NULL)
2450 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002451 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002452 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002453 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002454 orig_flags = evalarg_used->eval_flags;
2455 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002456
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002457 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002458 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002459 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002460 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002461 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002462 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002463 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002464 clear_tv(rettv);
2465 return FAIL;
2466 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002467 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002468 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002469
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002471 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002473 int error = FALSE;
2474
Bram Moolenaar13106602020-10-04 16:06:05 +02002475 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002476 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002477 else if (vim9script)
2478 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002479 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002481 if (error || !op_falsy || !result)
2482 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002483 if (error)
2484 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 }
2486
2487 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002488 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002490 if (op_falsy)
2491 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002492 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002493 {
mityu4ac198c2021-05-28 17:52:40 +02002494 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002495 clear_tv(rettv);
2496 return FAIL;
2497 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002498 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002499 evalarg_used->eval_flags = (op_falsy ? !result : result)
2500 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2501 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002502 {
2503 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002505 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002506 if (!op_falsy || !result)
2507 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002509 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002511 /*
2512 * Check for the ":".
2513 */
2514 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2515 if (*p != ':')
2516 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002517 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002518 if (evaluate && result)
2519 clear_tv(rettv);
2520 evalarg_used->eval_flags = orig_flags;
2521 return FAIL;
2522 }
2523 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002524 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002525 else
2526 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002527 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002528 {
2529 error_white_both(p, 1);
2530 clear_tv(rettv);
2531 evalarg_used->eval_flags = orig_flags;
2532 return FAIL;
2533 }
2534 *arg = p;
2535 }
2536
2537 /*
2538 * Get the third variable. Recursive!
2539 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002540 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002541 {
mityu4ac198c2021-05-28 17:52:40 +02002542 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002543 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002544 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002545 return FAIL;
2546 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002547 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2548 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002549 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002550 if (eval1(arg, &var2, evalarg_used) == FAIL)
2551 {
2552 if (evaluate && result)
2553 clear_tv(rettv);
2554 evalarg_used->eval_flags = orig_flags;
2555 return FAIL;
2556 }
2557 if (evaluate && !result)
2558 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002560
2561 if (evalarg == NULL)
2562 clear_evalarg(&local_evalarg, NULL);
2563 else
2564 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 }
2566
2567 return OK;
2568}
2569
2570/*
2571 * Handle first level expression:
2572 * expr2 || expr2 || expr2 logical OR
2573 *
2574 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002575 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 *
2577 * Return OK or FAIL.
2578 */
2579 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002580eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002582 char_u *p;
2583 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584
2585 /*
2586 * Get the first variable.
2587 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002588 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 return FAIL;
2590
2591 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002592 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002594 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002595 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002597 evalarg_T *evalarg_used = evalarg;
2598 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002599 int evaluate;
2600 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002601 long result = FALSE;
2602 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002603 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002604 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002605
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002606 if (evalarg == NULL)
2607 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002608 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002609 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002610 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002611 orig_flags = evalarg_used->eval_flags;
2612 evaluate = orig_flags & EVAL_EVALUATE;
2613 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002614 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002615 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002616 result = tv_get_bool_chk(rettv, &error);
2617 else if (tv_get_number_chk(rettv, &error) != 0)
2618 result = TRUE;
2619 clear_tv(rettv);
2620 if (error)
2621 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 }
2623
2624 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002625 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002627 while (p[0] == '|' && p[1] == '|')
2628 {
2629 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002630 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002631 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002632 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002633 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002634 {
2635 error_white_both(p, 2);
2636 clear_tv(rettv);
2637 return FAIL;
2638 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002639 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002640 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002641
2642 /*
2643 * Get the second variable.
2644 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002645 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002646 {
mityu4ac198c2021-05-28 17:52:40 +02002647 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002648 clear_tv(rettv);
2649 return FAIL;
2650 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002651 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2652 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002653 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002654 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002655 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002656
2657 /*
2658 * Compute the result.
2659 */
2660 if (evaluate && !result)
2661 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002662 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002663 result = tv_get_bool_chk(&var2, &error);
2664 else if (tv_get_number_chk(&var2, &error) != 0)
2665 result = TRUE;
2666 clear_tv(&var2);
2667 if (error)
2668 return FAIL;
2669 }
2670 if (evaluate)
2671 {
2672 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002673 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002674 rettv->v_type = VAR_BOOL;
2675 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002676 }
2677 else
2678 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002679 rettv->v_type = VAR_NUMBER;
2680 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002681 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002682 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002683
2684 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002686
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002687 if (evalarg == NULL)
2688 clear_evalarg(&local_evalarg, NULL);
2689 else
2690 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 }
2692
2693 return OK;
2694}
2695
2696/*
2697 * Handle second level expression:
2698 * expr3 && expr3 && expr3 logical AND
2699 *
2700 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002701 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 *
2703 * Return OK or FAIL.
2704 */
2705 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002706eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002708 char_u *p;
2709 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710
2711 /*
2712 * Get the first variable.
2713 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002714 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 return FAIL;
2716
2717 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002718 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002720 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002721 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002723 evalarg_T *evalarg_used = evalarg;
2724 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002725 int orig_flags;
2726 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002727 long result = TRUE;
2728 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002729 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002730 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002731
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002732 if (evalarg == NULL)
2733 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002734 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002735 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002736 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002737 orig_flags = evalarg_used->eval_flags;
2738 evaluate = orig_flags & EVAL_EVALUATE;
2739 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002740 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002741 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002742 result = tv_get_bool_chk(rettv, &error);
2743 else if (tv_get_number_chk(rettv, &error) == 0)
2744 result = FALSE;
2745 clear_tv(rettv);
2746 if (error)
2747 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 }
2749
2750 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002751 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002753 while (p[0] == '&' && p[1] == '&')
2754 {
2755 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002756 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002757 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002758 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002759 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002760 {
2761 error_white_both(p, 2);
2762 clear_tv(rettv);
2763 return FAIL;
2764 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002765 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002766 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002767
2768 /*
2769 * Get the second variable.
2770 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002771 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002772 {
mityu4ac198c2021-05-28 17:52:40 +02002773 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002774 clear_tv(rettv);
2775 return FAIL;
2776 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002777 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2778 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002779 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002780 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002781 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002782 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002783
2784 /*
2785 * Compute the result.
2786 */
2787 if (evaluate && result)
2788 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002789 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002790 result = tv_get_bool_chk(&var2, &error);
2791 else if (tv_get_number_chk(&var2, &error) == 0)
2792 result = FALSE;
2793 clear_tv(&var2);
2794 if (error)
2795 return FAIL;
2796 }
2797 if (evaluate)
2798 {
2799 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002800 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002801 rettv->v_type = VAR_BOOL;
2802 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002803 }
2804 else
2805 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002806 rettv->v_type = VAR_NUMBER;
2807 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002808 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002809 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002810
2811 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002813
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002814 if (evalarg == NULL)
2815 clear_evalarg(&local_evalarg, NULL);
2816 else
2817 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 }
2819
2820 return OK;
2821}
2822
2823/*
2824 * Handle third level expression:
2825 * var1 == var2
2826 * var1 =~ var2
2827 * var1 != var2
2828 * var1 !~ var2
2829 * var1 > var2
2830 * var1 >= var2
2831 * var1 < var2
2832 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002833 * var1 is var2
2834 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 *
2836 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002837 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 *
2839 * Return OK or FAIL.
2840 */
2841 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002842eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002845 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002846 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002848 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849
2850 /*
2851 * Get the first variable.
2852 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002853 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 return FAIL;
2855
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002856 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002857 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858
2859 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002860 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002862 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002864 typval_T var2;
2865 int ic;
2866 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002867 int evaluate = evalarg == NULL
2868 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002869 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002870
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002871 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002872 {
Bram Moolenaare442d592022-05-05 12:20:28 +01002873 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002874 p = *arg;
2875 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002876 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2877 {
mityu4ac198c2021-05-28 17:52:40 +02002878 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002879 clear_tv(rettv);
2880 return FAIL;
2881 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002882
Bram Moolenaar696ba232020-07-29 21:20:41 +02002883 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2884 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002885 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002886 clear_tv(rettv);
2887 return FAIL;
2888 }
2889
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002890 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 if (p[len] == '?')
2892 {
2893 ic = TRUE;
2894 ++len;
2895 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002896 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 else if (p[len] == '#')
2898 {
2899 ic = FALSE;
2900 ++len;
2901 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002902 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002904 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905
2906 /*
2907 * Get the second variable.
2908 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002909 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2910 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002911 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002912 clear_tv(rettv);
2913 return FAIL;
2914 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002915 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002916 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002918 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 return FAIL;
2920 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002921 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002922 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002923 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002924
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002925 // use the line of the comparison for messages
2926 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002927 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002928 {
2929 ret = FAIL;
2930 clear_tv(rettv);
2931 }
2932 else
2933 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002934 clear_tv(&var2);
2935 return ret;
2936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 }
2938
2939 return OK;
2940}
2941
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002942/*
2943 * Make a copy of blob "tv1" and append blob "tv2".
2944 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 void
2946eval_addblob(typval_T *tv1, typval_T *tv2)
2947{
2948 blob_T *b1 = tv1->vval.v_blob;
2949 blob_T *b2 = tv2->vval.v_blob;
2950 blob_T *b = blob_alloc();
2951 int i;
2952
2953 if (b != NULL)
2954 {
2955 for (i = 0; i < blob_len(b1); i++)
2956 ga_append(&b->bv_ga, blob_get(b1, i));
2957 for (i = 0; i < blob_len(b2); i++)
2958 ga_append(&b->bv_ga, blob_get(b2, i));
2959
2960 clear_tv(tv1);
2961 rettv_blob_set(tv1, b);
2962 }
2963}
2964
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002965/*
2966 * Make a copy of list "tv1" and append list "tv2".
2967 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 int
2969eval_addlist(typval_T *tv1, typval_T *tv2)
2970{
2971 typval_T var3;
2972
2973 // concatenate Lists
2974 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2975 {
2976 clear_tv(tv1);
2977 clear_tv(tv2);
2978 return FAIL;
2979 }
2980 clear_tv(tv1);
2981 *tv1 = var3;
2982 return OK;
2983}
2984
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985/*
2986 * Handle fourth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00002987 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002989 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002990 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 *
2992 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002993 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 *
2995 * Return OK or FAIL.
2996 */
2997 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002998eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 /*
3001 * Get the first variable.
3002 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003003 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 return FAIL;
3005
3006 /*
3007 * Repeat computing, until no '+', '-' or '.' is following.
3008 */
3009 for (;;)
3010 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003011 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003012 int getnext;
3013 char_u *p;
3014 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003015 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003016 int concat;
3017 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003018 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003019
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003020 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003021 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003022 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003023 p = eval_next_non_blank(*arg, evalarg, &getnext);
3024 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003025 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003026 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3027 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003029 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3030 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003031
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003032 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003033 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003034 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003035 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003036 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003037 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003038 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003039 {
mityu4ac198c2021-05-28 17:52:40 +02003040 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003041 clear_tv(rettv);
3042 return FAIL;
3043 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003044 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003045 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003046 if ((op != '+' || (rettv->v_type != VAR_LIST
3047 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003048#ifdef FEAT_FLOAT
3049 && (op == '.' || rettv->v_type != VAR_FLOAT)
3050#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003051 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003052 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003053 int error = FALSE;
3054
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003055 // For "list + ...", an illegal use of the first operand as
3056 // a number cannot be determined before evaluating the 2nd
3057 // operand: if this is also a list, all is ok.
3058 // For "something . ...", "something - ..." or "non-list + ...",
3059 // we know that the first operand needs to be a string or number
3060 // without evaluating the 2nd operand. So check before to avoid
3061 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003062 if (op != '.')
3063 tv_get_number_chk(rettv, &error);
3064 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003065 {
3066 clear_tv(rettv);
3067 return FAIL;
3068 }
3069 }
3070
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 /*
3072 * Get the second variable.
3073 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003074 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003075 {
mityu89dcb4d2021-05-28 13:50:17 +02003076 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003077 clear_tv(rettv);
3078 return FAIL;
3079 }
3080 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02003081 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003083 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 return FAIL;
3085 }
3086
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003087 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 {
3089 /*
3090 * Compute the result.
3091 */
3092 if (op == '.')
3093 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003094 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3095 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003096 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003097
Bram Moolenaar2e086612020-08-25 22:37:48 +02003098 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003099 || var2.v_type == VAR_CHANNEL
3100 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003101 semsg(_(e_using_invalid_value_as_string_str),
3102 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003103#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003104 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003105 {
3106 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3107 var2.vval.v_float);
3108 s2 = buf2;
3109 }
3110#endif
3111 else
3112 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003113 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003114 {
3115 clear_tv(rettv);
3116 clear_tv(&var2);
3117 return FAIL;
3118 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003119 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003120 clear_tv(rettv);
3121 rettv->v_type = VAR_STRING;
3122 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003124 else if (op == '+' && rettv->v_type == VAR_BLOB
3125 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003127 else if (op == '+' && rettv->v_type == VAR_LIST
3128 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003129 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003131 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 else
3134 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003135 int error = FALSE;
3136 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003137#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003138 float_T f1 = 0, f2 = 0;
3139
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003140 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003141 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003142 f1 = rettv->vval.v_float;
3143 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003144 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003145 else
3146#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003147 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003148 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003149 if (error)
3150 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003151 // This can only happen for "list + non-list" or
3152 // "blob + non-blob". For "non-list + ..." or
3153 // "something - ...", we returned before evaluating the
3154 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003155 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003156 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003157 return FAIL;
3158 }
3159#ifdef FEAT_FLOAT
3160 if (var2.v_type == VAR_FLOAT)
3161 f1 = n1;
3162#endif
3163 }
3164#ifdef FEAT_FLOAT
3165 if (var2.v_type == VAR_FLOAT)
3166 {
3167 f2 = var2.vval.v_float;
3168 n2 = 0;
3169 }
3170 else
3171#endif
3172 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003173 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003174 if (error)
3175 {
3176 clear_tv(rettv);
3177 clear_tv(&var2);
3178 return FAIL;
3179 }
3180#ifdef FEAT_FLOAT
3181 if (rettv->v_type == VAR_FLOAT)
3182 f2 = n2;
3183#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003184 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003185 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003186
3187#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003188 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003189 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3190 {
3191 if (op == '+')
3192 f1 = f1 + f2;
3193 else
3194 f1 = f1 - f2;
3195 rettv->v_type = VAR_FLOAT;
3196 rettv->vval.v_float = f1;
3197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003199#endif
3200 {
3201 if (op == '+')
3202 n1 = n1 + n2;
3203 else
3204 n1 = n1 - n2;
3205 rettv->v_type = VAR_NUMBER;
3206 rettv->vval.v_number = n1;
3207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003209 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 }
3211 }
3212 return OK;
3213}
3214
3215/*
3216 * Handle fifth level expression:
3217 * * number multiplication
3218 * / number division
3219 * % number modulo
3220 *
3221 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003222 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 *
3224 * Return OK or FAIL.
3225 */
3226 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003227eval6(
3228 char_u **arg,
3229 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003230 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003231 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003233#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003234 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236
3237 /*
3238 * Get the first variable.
3239 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003240 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 return FAIL;
3242
3243 /*
3244 * Repeat computing, until no '*', '/' or '%' is following.
3245 */
3246 for (;;)
3247 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003248 int evaluate;
3249 int getnext;
3250 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003251 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003252 int op;
3253 varnumber_T n1, n2;
3254#ifdef FEAT_FLOAT
3255 float_T f1, f2;
3256#endif
3257 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003258
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003259 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003260 p = eval_next_non_blank(*arg, evalarg, &getnext);
3261 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003262 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003264
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003265 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003266 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003267 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003268 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003269 {
3270 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3271 {
mityu4ac198c2021-05-28 17:52:40 +02003272 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003273 clear_tv(rettv);
3274 return FAIL;
3275 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003276 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003279#ifdef FEAT_FLOAT
3280 f1 = 0;
3281 f2 = 0;
3282#endif
3283 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003284 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003286#ifdef FEAT_FLOAT
3287 if (rettv->v_type == VAR_FLOAT)
3288 {
3289 f1 = rettv->vval.v_float;
3290 use_float = TRUE;
3291 n1 = 0;
3292 }
3293 else
3294#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003295 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003296 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003297 if (error)
3298 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 }
3300 else
3301 n1 = 0;
3302
3303 /*
3304 * Get the second variable.
3305 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003306 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3307 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003308 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003309 clear_tv(rettv);
3310 return FAIL;
3311 }
3312 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003313 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 return FAIL;
3315
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003316 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003318#ifdef FEAT_FLOAT
3319 if (var2.v_type == VAR_FLOAT)
3320 {
3321 if (!use_float)
3322 {
3323 f1 = n1;
3324 use_float = TRUE;
3325 }
3326 f2 = var2.vval.v_float;
3327 n2 = 0;
3328 }
3329 else
3330#endif
3331 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003332 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003333 clear_tv(&var2);
3334 if (error)
3335 return FAIL;
3336#ifdef FEAT_FLOAT
3337 if (use_float)
3338 f2 = n2;
3339#endif
3340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341
3342 /*
3343 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003344 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003346#ifdef FEAT_FLOAT
3347 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003349 if (op == '*')
3350 f1 = f1 * f2;
3351 else if (op == '/')
3352 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003353# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003354 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003355 if (f2 == 0.0)
3356 {
3357 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003358 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003359 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003360 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003361 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003362 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003363 }
3364 else
3365 f1 = f1 / f2;
3366# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003367 // We rely on the floating point library to handle divide
3368 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003369 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003370# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003373 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003374 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003375 return FAIL;
3376 }
3377 rettv->v_type = VAR_FLOAT;
3378 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 }
3380 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003383 int failed = FALSE;
3384
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003385 if (op == '*')
3386 n1 = n1 * n2;
3387 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003388 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003390 n1 = num_modulus(n1, n2, &failed);
3391 if (failed)
3392 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003393
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003394 rettv->v_type = VAR_NUMBER;
3395 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 }
3398 }
3399
3400 return OK;
3401}
3402
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003403/*
3404 * Handle a type cast before a base level expression.
3405 * "arg" must point to the first non-white of the expression.
3406 * "arg" is advanced to just after the recognized expression.
3407 * Return OK or FAIL.
3408 */
3409 static int
3410eval7t(
3411 char_u **arg,
3412 typval_T *rettv,
3413 evalarg_T *evalarg,
3414 int want_string) // after "." operator
3415{
3416 type_T *want_type = NULL;
3417 garray_T type_list; // list of pointers to allocated types
3418 int res;
3419 int evaluate = evalarg == NULL ? 0
3420 : (evalarg->eval_flags & EVAL_EVALUATE);
3421
3422 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003423 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3424 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003425 {
3426 ++*arg;
3427 ga_init2(&type_list, sizeof(type_T *), 10);
3428 want_type = parse_type(arg, &type_list, TRUE);
3429 if (want_type == NULL && (evaluate || **arg != '>'))
3430 {
3431 clear_type_list(&type_list);
3432 return FAIL;
3433 }
3434
3435 if (**arg != '>')
3436 {
3437 if (*skipwhite(*arg) == '>')
3438 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3439 else
3440 emsg(_(e_missing_gt));
3441 clear_type_list(&type_list);
3442 return FAIL;
3443 }
3444 ++*arg;
3445 *arg = skipwhite_and_linebreak(*arg, evalarg);
3446 }
3447
3448 res = eval7(arg, rettv, evalarg, want_string);
3449
3450 if (want_type != NULL && evaluate)
3451 {
3452 if (res == OK)
3453 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003454 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3455 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003456
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003457 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003458 {
3459 if (want_type == &t_bool && actual != &t_bool
3460 && (actual->tt_flags & TTFLAG_BOOL_OK))
3461 {
3462 int n = tv2bool(rettv);
3463
3464 // can use "0" and "1" for boolean in some places
3465 clear_tv(rettv);
3466 rettv->v_type = VAR_BOOL;
3467 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3468 }
3469 else
3470 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003471 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003472
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003473 where.wt_variable = TRUE;
3474 res = check_type(want_type, actual, TRUE, where);
3475 }
3476 }
3477 }
3478 clear_type_list(&type_list);
3479 }
3480
3481 return res;
3482}
3483
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003484 int
3485eval_leader(char_u **arg, int vim9)
3486{
3487 char_u *s = *arg;
3488 char_u *p = *arg;
3489
3490 while (*p == '!' || *p == '-' || *p == '+')
3491 {
3492 char_u *n = skipwhite(p + 1);
3493
3494 // ++, --, -+ and +- are not accepted in Vim9 script
3495 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3496 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003497 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003498 return FAIL;
3499 }
3500 p = n;
3501 }
3502 *arg = p;
3503 return OK;
3504}
3505
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003507 * Check for a predefined value "true", "false" and "null.*".
3508 * Return OK when recognized.
3509 */
3510 int
3511handle_predefined(char_u *s, int len, typval_T *rettv)
3512{
3513 switch (len)
3514 {
3515 case 4: if (STRNCMP(s, "true", 4) == 0)
3516 {
3517 rettv->v_type = VAR_BOOL;
3518 rettv->vval.v_number = VVAL_TRUE;
3519 return OK;
3520 }
3521 if (STRNCMP(s, "null", 4) == 0)
3522 {
3523 rettv->v_type = VAR_SPECIAL;
3524 rettv->vval.v_number = VVAL_NULL;
3525 return OK;
3526 }
3527 break;
3528 case 5: if (STRNCMP(s, "false", 5) == 0)
3529 {
3530 rettv->v_type = VAR_BOOL;
3531 rettv->vval.v_number = VVAL_FALSE;
3532 return OK;
3533 }
3534 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003535 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3536 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003537#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003538 rettv->v_type = VAR_JOB;
3539 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003540#else
3541 rettv->v_type = VAR_SPECIAL;
3542 rettv->vval.v_number = VVAL_NULL;
3543#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003544 return OK;
3545 }
3546 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003547 case 9:
3548 if (STRNCMP(s, "null_", 5) != 0)
3549 break;
3550 if (STRNCMP(s + 5, "list", 4) == 0)
3551 {
3552 rettv->v_type = VAR_LIST;
3553 rettv->vval.v_list = NULL;
3554 return OK;
3555 }
3556 if (STRNCMP(s + 5, "dict", 4) == 0)
3557 {
3558 rettv->v_type = VAR_DICT;
3559 rettv->vval.v_dict = NULL;
3560 return OK;
3561 }
3562 if (STRNCMP(s + 5, "blob", 4) == 0)
3563 {
3564 rettv->v_type = VAR_BLOB;
3565 rettv->vval.v_blob = NULL;
3566 return OK;
3567 }
3568 break;
3569 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3570 {
3571 rettv->v_type = VAR_STRING;
3572 rettv->vval.v_string = NULL;
3573 return OK;
3574 }
3575 break;
3576 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003577 if (STRNCMP(s, "null_channel", 12) == 0)
3578 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003579#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003580 rettv->v_type = VAR_CHANNEL;
3581 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003582#else
3583 rettv->v_type = VAR_SPECIAL;
3584 rettv->vval.v_number = VVAL_NULL;
3585#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003586 return OK;
3587 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003588 if (STRNCMP(s, "null_partial", 12) == 0)
3589 {
3590 rettv->v_type = VAR_PARTIAL;
3591 rettv->vval.v_partial = NULL;
3592 return OK;
3593 }
3594 break;
3595 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3596 {
3597 rettv->v_type = VAR_FUNC;
3598 rettv->vval.v_string = NULL;
3599 return OK;
3600 }
3601 break;
3602 }
3603 return FAIL;
3604}
3605
3606/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 * Handle sixth level expression:
3608 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003609 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003610 * "string" string constant
3611 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 * &option-name option value
3613 * @r register contents
3614 * identifier variable value
3615 * function() function call
3616 * $VAR environment variable
3617 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003618 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003619 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003620 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003621 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 *
3623 * Also handle:
3624 * ! in front logical NOT
3625 * - in front unary minus
3626 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003627 * trailing [] subscript in String or List
3628 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003629 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 *
3631 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003632 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 *
3634 * Return OK or FAIL.
3635 */
3636 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003637eval7(
3638 char_u **arg,
3639 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003640 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003643 int evaluate = evalarg != NULL
3644 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 int len;
3646 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003647 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 char_u *start_leader, *end_leader;
3649 int ret = OK;
3650 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003651 static int recurse = 0;
3652 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653
3654 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003655 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003656 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003658 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659
3660 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003661 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 */
3663 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003664 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003665 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 end_leader = *arg;
3667
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003668 if (**arg == '.' && (!isdigit(*(*arg + 1))
3669#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003670 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003671#endif
3672 ))
3673 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003674 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003675 ++*arg;
3676 return FAIL;
3677 }
3678
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003679 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003680 // and crash. With MSVC the stack is smaller.
3681 if (recurse ==
3682#ifdef _MSC_VER
3683 300
3684#else
3685 1000
3686#endif
3687 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003688 {
3689 semsg(_(e_expression_too_recursive_str), *arg);
3690 return FAIL;
3691 }
3692 ++recurse;
3693
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 switch (**arg)
3695 {
3696 /*
3697 * Number constant.
3698 */
3699 case '0':
3700 case '1':
3701 case '2':
3702 case '3':
3703 case '4':
3704 case '5':
3705 case '6':
3706 case '7':
3707 case '8':
3708 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003709 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003710
3711 // Apply prefixed "-" and "+" now. Matters especially when
3712 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003713 if (ret == OK && evaluate && end_leader > start_leader
3714 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003715 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 break;
3717
3718 /*
3719 * String constant: "string".
3720 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003721 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 break;
3723
3724 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003725 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003727 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003728 break;
3729
3730 /*
3731 * List: [expr, expr]
3732 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003733 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 break;
3735
3736 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003737 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003738 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003739 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003740 {
3741 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3742 }
3743 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003744 {
3745 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003746 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003747 }
3748 else
3749 ret = NOTDONE;
3750 break;
3751
3752 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003753 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003754 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003755 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003756 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003757 ret = NOTDONE;
3758 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00003759 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003760 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003761 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003762 break;
3763
3764 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003765 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003767 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 break;
3769
3770 /*
3771 * Environment variable: $VAR.
3772 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003773 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 break;
3775
3776 /*
3777 * Register contents: @r.
3778 */
3779 case '@': ++*arg;
3780 if (evaluate)
3781 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003782 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003783 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003784 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003785 emsg_invreg(**arg);
3786 else
3787 {
3788 rettv->v_type = VAR_STRING;
3789 rettv->vval.v_string = get_reg_contents(**arg,
3790 GREG_EXPR_SRC);
3791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 }
3793 if (**arg != NUL)
3794 ++*arg;
3795 break;
3796
3797 /*
3798 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003799 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003801 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003802 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01003803 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003804 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003805 if (ret == OK && evaluate)
3806 {
3807 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3808
Bram Moolenaara9931532021-06-12 15:58:16 +02003809 // Compile it here to get the return type. The return
3810 // type is optional, when it's missing use t_unknown.
3811 // This is recognized in compile_return().
3812 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3813 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00003814 if (compile_def_function(ufunc, FALSE,
3815 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003816 {
3817 clear_tv(rettv);
3818 ret = FAIL;
3819 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003820 }
3821 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003822 if (ret == NOTDONE)
3823 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003824 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003825 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003826
Bram Moolenaar9215f012020-06-27 21:18:00 +02003827 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003828 if (**arg == ')')
3829 ++*arg;
3830 else if (ret == OK)
3831 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003832 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003833 clear_tv(rettv);
3834 ret = FAIL;
3835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 }
3837 break;
3838
Bram Moolenaar8c711452005-01-14 21:53:12 +00003839 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 break;
3841 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003842
3843 if (ret == NOTDONE)
3844 {
3845 /*
3846 * Must be a variable or function name.
3847 * Can also be a curly-braces kind of name: {expr}.
3848 */
3849 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003850 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003851 if (alias != NULL)
3852 s = alias;
3853
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003854 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003855 ret = FAIL;
3856 else
3857 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003858 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3859
Bram Moolenaar4525a572022-02-13 11:57:33 +00003860 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003861 {
3862 emsg(_(e_cannot_use_underscore_here));
3863 ret = FAIL;
3864 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00003865 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00003866 && s[0] == 's' && s[1] == ':')
3867 {
3868 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
3869 ret = FAIL;
3870 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00003871 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003872 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003873 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003874 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003875 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003876 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003877 else if (flags & EVAL_CONSTANT)
3878 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003879 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003880 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003881 // get the value of "true", "false", etc. or a variable
3882 ret = FAIL;
3883 if (vim9script)
3884 ret = handle_predefined(s, len, rettv);
3885 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003886 {
3887 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003888 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003889 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003890 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003891 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003892 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003893 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003894 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003895 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003896 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003897 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003898 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003899 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003900 }
3901
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003902 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3903 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003904 if (ret == OK)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003905 ret = handle_subscript(arg, name_start, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906
3907 /*
3908 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3909 */
3910 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003911 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003912
3913 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003914 return ret;
3915}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003916
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003917/*
3918 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003919 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003920 * Adjusts "end_leaderp" until it is at "start_leader".
3921 */
3922 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003923eval7_leader(
3924 typval_T *rettv,
3925 int numeric_only,
3926 char_u *start_leader,
3927 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003928{
3929 char_u *end_leader = *end_leaderp;
3930 int ret = OK;
3931 int error = FALSE;
3932 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003933 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003934 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003935#ifdef FEAT_FLOAT
3936 float_T f = 0.0;
3937
3938 if (rettv->v_type == VAR_FLOAT)
3939 f = rettv->vval.v_float;
3940 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003941#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003942 {
3943 while (VIM_ISWHITE(end_leader[-1]))
3944 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003945 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003946 val = tv2bool(rettv);
3947 else
3948 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003949 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003950 if (error)
3951 {
3952 clear_tv(rettv);
3953 ret = FAIL;
3954 }
3955 else
3956 {
3957 while (end_leader > start_leader)
3958 {
3959 --end_leader;
3960 if (*end_leader == '!')
3961 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003962 if (numeric_only)
3963 {
3964 ++end_leader;
3965 break;
3966 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003967#ifdef FEAT_FLOAT
3968 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003969 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003970 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003971 {
3972 rettv->v_type = VAR_BOOL;
3973 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3974 }
3975 else
3976 f = !f;
3977 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003978 else
3979#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003980 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003981 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003982 type = VAR_BOOL;
3983 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003984 }
3985 else if (*end_leader == '-')
3986 {
3987#ifdef FEAT_FLOAT
3988 if (rettv->v_type == VAR_FLOAT)
3989 f = -f;
3990 else
3991#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003992 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003993 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003994 type = VAR_NUMBER;
3995 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003996 }
3997 }
3998#ifdef FEAT_FLOAT
3999 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004001 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004002 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004004 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004005#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004006 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004007 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004008 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004009 rettv->v_type = type;
4010 else
4011 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004012 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004015 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 return ret;
4017}
4018
4019/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004020 * Call the function referred to in "rettv".
4021 */
4022 static int
4023call_func_rettv(
4024 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004025 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004026 typval_T *rettv,
4027 int evaluate,
4028 dict_T *selfdict,
4029 typval_T *basetv)
4030{
4031 partial_T *pt = NULL;
4032 funcexe_T funcexe;
4033 typval_T functv;
4034 char_u *s;
4035 int ret;
4036
4037 // need to copy the funcref so that we can clear rettv
4038 if (evaluate)
4039 {
4040 functv = *rettv;
4041 rettv->v_type = VAR_UNKNOWN;
4042
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004043 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004044 if (functv.v_type == VAR_PARTIAL)
4045 {
4046 pt = functv.vval.v_partial;
4047 s = partial_name(pt);
4048 }
4049 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004050 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004051 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004052 if (s == NULL || *s == NUL)
4053 {
4054 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004055 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004056 goto theend;
4057 }
4058 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004059 }
4060 else
4061 s = (char_u *)"";
4062
Bram Moolenaara80faa82020-04-12 19:37:17 +02004063 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004064 funcexe.fe_firstline = curwin->w_cursor.lnum;
4065 funcexe.fe_lastline = curwin->w_cursor.lnum;
4066 funcexe.fe_evaluate = evaluate;
4067 funcexe.fe_partial = pt;
4068 funcexe.fe_selfdict = selfdict;
4069 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004070 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004071
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004072theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004073 // Clear the funcref afterwards, so that deleting it while
4074 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004075 if (evaluate)
4076 clear_tv(&functv);
4077
4078 return ret;
4079}
4080
4081/*
4082 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004083 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004084 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4085 */
4086 static int
4087eval_lambda(
4088 char_u **arg,
4089 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004090 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004091 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004092{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004093 int evaluate = evalarg != NULL
4094 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004095 typval_T base = *rettv;
4096 int ret;
4097
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004098 rettv->v_type = VAR_UNKNOWN;
4099
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004100 if (**arg == '{')
4101 {
4102 // ->{lambda}()
4103 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4104 }
4105 else
4106 {
4107 // ->(lambda)()
4108 ++*arg;
4109 ret = eval1(arg, rettv, evalarg);
4110 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004111 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004112 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004113 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004114 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004115 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004116 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4117 && rettv->v_type != VAR_PARTIAL)
4118 {
4119 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4120 return FAIL;
4121 }
4122 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004123 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004124 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004125 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004126
4127 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004128 {
4129 if (verbose)
4130 {
4131 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004132 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004133 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004134 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004135 }
4136 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004137 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004138 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004139 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004140 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004141
4142 // Clear the funcref afterwards, so that deleting it while
4143 // evaluating the arguments is possible (see test55).
4144 if (evaluate)
4145 clear_tv(&base);
4146
4147 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004148}
4149
4150/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004151 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004152 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004153 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4154 */
4155 static int
4156eval_method(
4157 char_u **arg,
4158 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004159 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004160 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004161{
4162 char_u *name;
4163 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004164 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004165 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004166 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004167 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004168 int evaluate = evalarg != NULL
4169 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004170
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004171 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004172
Bram Moolenaarac92e252019-08-03 21:58:38 +02004173 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004174 len = get_name_len(arg, &alias, evaluate, TRUE);
4175 if (alias != NULL)
4176 name = alias;
4177
4178 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004179 {
4180 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004181 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004182 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004183 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004184 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004185 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004186 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004187
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004188 // If there is no "(" immediately following, but there is further on,
4189 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4190 // Does not handle anything where "(" is part of the expression.
4191 *arg = skipwhite(*arg);
4192
4193 if (**arg != '(' && alias == NULL
4194 && (paren = vim_strchr(*arg, '(')) != NULL)
4195 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004196 char_u *deref;
4197
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004198 *arg = name;
4199 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004200 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4201 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004202 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004203 *arg = name + len;
4204 ret = FAIL;
4205 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004206 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004207 {
4208 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004209 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004210 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004211 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004212 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004213
4214 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004215 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004216 *arg = skipwhite(*arg);
4217
4218 if (**arg != '(')
4219 {
4220 if (verbose)
4221 semsg(_(e_missing_parenthesis_str), name);
4222 ret = FAIL;
4223 }
4224 else if (VIM_ISWHITE((*arg)[-1]))
4225 {
4226 if (verbose)
4227 emsg(_(e_no_white_space_allowed_before_parenthesis));
4228 ret = FAIL;
4229 }
4230 else
4231 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004232 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004233 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004234 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004235
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004236 // Clear the funcref afterwards, so that deleting it while
4237 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004238 if (evaluate)
4239 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004240 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004241
Bram Moolenaarac92e252019-08-03 21:58:38 +02004242 return ret;
4243}
4244
4245/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004246 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4247 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004248 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4249 */
4250 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004251eval_index(
4252 char_u **arg,
4253 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004254 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004255 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004256{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004257 int evaluate = evalarg != NULL
4258 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004259 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004260 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004261 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004262 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004263 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004264 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004265
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004266 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4267 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004268
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004269 init_tv(&var1);
4270 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004271 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004272 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004273 /*
4274 * dict.name
4275 */
4276 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004277 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004278 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004279 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004280 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004281 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004282 }
4283 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004284 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004285 /*
4286 * something[idx]
4287 *
4288 * Get the (first) variable from inside the [].
4289 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004290 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004291 if (**arg == ':')
4292 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004293 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004294 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004295 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004296 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004297 semsg(_(e_white_space_required_before_and_after_str_at_str),
4298 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004299 clear_tv(&var1);
4300 return FAIL;
4301 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004302 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004303 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004304 int error = FALSE;
4305
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004306#ifdef FEAT_FLOAT
4307 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004308 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004309 && var1.v_type == VAR_FLOAT)
4310 {
4311 var1.vval.v_string = typval_tostring(&var1, TRUE);
4312 var1.v_type = VAR_STRING;
4313 }
4314#endif
Bram Moolenaar4525a572022-02-13 11:57:33 +00004315 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004316 tv_get_number_chk(&var1, &error);
4317 else
4318 error = tv_get_string_chk(&var1) == NULL;
4319 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004320 {
4321 // not a number or string
4322 clear_tv(&var1);
4323 return FAIL;
4324 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004325 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004326
4327 /*
4328 * Get the second variable from inside the [:].
4329 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004330 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004331 if (**arg == ':')
4332 {
4333 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004334 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004335 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004336 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004337 semsg(_(e_white_space_required_before_and_after_str_at_str),
4338 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004339 if (!empty1)
4340 clear_tv(&var1);
4341 return FAIL;
4342 }
4343 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004344 if (**arg == ']')
4345 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004346 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004347 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004348 if (!empty1)
4349 clear_tv(&var1);
4350 return FAIL;
4351 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004352 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004353 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004354 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004355 if (!empty1)
4356 clear_tv(&var1);
4357 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004358 return FAIL;
4359 }
4360 }
4361
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004362 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004363 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004364 if (**arg != ']')
4365 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004366 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004367 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004368 clear_tv(&var1);
4369 if (range)
4370 clear_tv(&var2);
4371 return FAIL;
4372 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004373 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004374 }
4375
4376 if (evaluate)
4377 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004378 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004379 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004380 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004381
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004382 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004383 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004384 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004385 clear_tv(&var2);
4386 return res;
4387 }
4388 return OK;
4389}
4390
4391/*
4392 * Check if "rettv" can have an [index] or [sli:ce]
4393 */
4394 int
4395check_can_index(typval_T *rettv, int evaluate, int verbose)
4396{
4397 switch (rettv->v_type)
4398 {
4399 case VAR_FUNC:
4400 case VAR_PARTIAL:
4401 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004402 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004403 return FAIL;
4404 case VAR_FLOAT:
4405#ifdef FEAT_FLOAT
4406 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004407 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004408 return FAIL;
4409#endif
4410 case VAR_BOOL:
4411 case VAR_SPECIAL:
4412 case VAR_JOB:
4413 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004414 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004415 if (verbose)
4416 emsg(_(e_cannot_index_special_variable));
4417 return FAIL;
4418 case VAR_UNKNOWN:
4419 case VAR_ANY:
4420 case VAR_VOID:
4421 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004422 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004423 emsg(_(e_cannot_index_special_variable));
4424 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004425 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004426 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004427
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004428 case VAR_STRING:
4429 case VAR_LIST:
4430 case VAR_DICT:
4431 case VAR_BLOB:
4432 break;
4433 case VAR_NUMBER:
4434 if (in_vim9script())
4435 emsg(_(e_cannot_index_number));
4436 break;
4437 }
4438 return OK;
4439}
4440
4441/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004442 * slice() function
4443 */
4444 void
4445f_slice(typval_T *argvars, typval_T *rettv)
4446{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004447 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004448 && ((argvars[0].v_type != VAR_STRING
4449 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004450 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004451 && check_for_list_arg(argvars, 0) == FAIL)
4452 || check_for_number_arg(argvars, 1) == FAIL
4453 || check_for_opt_number_arg(argvars, 2) == FAIL))
4454 return;
4455
Bram Moolenaar6601b622021-01-13 21:47:15 +01004456 if (check_can_index(argvars, TRUE, FALSE) == OK)
4457 {
4458 copy_tv(argvars, rettv);
4459 eval_index_inner(rettv, TRUE, argvars + 1,
4460 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4461 TRUE, NULL, 0, FALSE);
4462 }
4463}
4464
4465/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004466 * Apply index or range to "rettv".
4467 * "var1" is the first index, NULL for [:expr].
4468 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004469 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4470 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004471 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4472 */
4473 int
4474eval_index_inner(
4475 typval_T *rettv,
4476 int is_range,
4477 typval_T *var1,
4478 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004479 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004480 char_u *key,
4481 int keylen,
4482 int verbose)
4483{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004484 varnumber_T n1, n2 = 0;
4485 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004486
4487 n1 = 0;
4488 if (var1 != NULL && rettv->v_type != VAR_DICT)
4489 n1 = tv_get_number(var1);
4490
4491 if (is_range)
4492 {
4493 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004494 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004495 if (verbose)
4496 emsg(_(e_cannot_slice_dictionary));
4497 return FAIL;
4498 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004499 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004500 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004501 else
4502 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004503 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004504
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004505 switch (rettv->v_type)
4506 {
4507 case VAR_UNKNOWN:
4508 case VAR_ANY:
4509 case VAR_VOID:
4510 case VAR_FUNC:
4511 case VAR_PARTIAL:
4512 case VAR_FLOAT:
4513 case VAR_BOOL:
4514 case VAR_SPECIAL:
4515 case VAR_JOB:
4516 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004517 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004518 break; // not evaluating, skipping over subscript
4519
4520 case VAR_NUMBER:
4521 case VAR_STRING:
4522 {
4523 char_u *s = tv_get_string(rettv);
4524
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004525 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004526 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004527 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004528 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004529 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004530 else
4531 s = char_from_string(s, n1);
4532 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004533 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004534 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004535 // The resulting variable is a substring. If the indexes
4536 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004537 if (n1 < 0)
4538 {
4539 n1 = len + n1;
4540 if (n1 < 0)
4541 n1 = 0;
4542 }
4543 if (n2 < 0)
4544 n2 = len + n2;
4545 else if (n2 >= len)
4546 n2 = len;
4547 if (n1 >= len || n2 < 0 || n1 > n2)
4548 s = NULL;
4549 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004550 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004551 }
4552 else
4553 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004554 // The resulting variable is a string of a single
4555 // character. If the index is too big or negative the
4556 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004557 if (n1 >= len || n1 < 0)
4558 s = NULL;
4559 else
4560 s = vim_strnsave(s + n1, 1);
4561 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004562 clear_tv(rettv);
4563 rettv->v_type = VAR_STRING;
4564 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004565 }
4566 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004567
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004568 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004569 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4570 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004571 break;
4572
4573 case VAR_LIST:
4574 if (var1 == NULL)
4575 n1 = 0;
4576 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004577 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004578 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004579 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004580 return FAIL;
4581 break;
4582
4583 case VAR_DICT:
4584 {
4585 dictitem_T *item;
4586 typval_T tmp;
4587
4588 if (key == NULL)
4589 {
4590 key = tv_get_string_chk(var1);
4591 if (key == NULL)
4592 return FAIL;
4593 }
4594
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004595 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004596
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004597 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004598 {
4599 if (verbose)
4600 {
4601 if (keylen > 0)
4602 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004603 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004604 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004605 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004606 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004607
4608 copy_tv(&item->di_tv, &tmp);
4609 clear_tv(rettv);
4610 *rettv = tmp;
4611 }
4612 break;
4613 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004614 return OK;
4615}
4616
4617/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004618 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004619 */
4620 char_u *
4621partial_name(partial_T *pt)
4622{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004623 if (pt != NULL)
4624 {
4625 if (pt->pt_name != NULL)
4626 return pt->pt_name;
4627 if (pt->pt_func != NULL)
4628 return pt->pt_func->uf_name;
4629 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004630 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004631}
4632
Bram Moolenaarddecc252016-04-06 22:59:37 +02004633 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004634partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004635{
4636 int i;
4637
4638 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004639 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004640 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004641 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004642 if (pt->pt_name != NULL)
4643 {
4644 func_unref(pt->pt_name);
4645 vim_free(pt->pt_name);
4646 }
4647 else
4648 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004649
Bram Moolenaar54656012021-06-09 20:50:46 +02004650 // "out_up" is no longer used, decrement refcount on partial that owns it.
4651 partial_unref(pt->pt_outer.out_up_partial);
4652
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004653 // Using pt_outer from another partial.
4654 partial_unref(pt->pt_outer_partial);
4655
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004656 // Decrease the reference count for the context of a closure. If down
4657 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004658 if (pt->pt_funcstack != NULL)
4659 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004660 --pt->pt_funcstack->fs_refcount;
4661 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004662 }
4663
Bram Moolenaarddecc252016-04-06 22:59:37 +02004664 vim_free(pt);
4665}
4666
4667/*
4668 * Unreference a closure: decrement the reference count and free it when it
4669 * becomes zero.
4670 */
4671 void
4672partial_unref(partial_T *pt)
4673{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004674 if (pt != NULL)
4675 {
4676 if (--pt->pt_refcount <= 0)
4677 partial_free(pt);
4678
4679 // If the reference count goes down to one, the funcstack may be the
4680 // only reference and can be freed if no other partials reference it.
4681 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4682 funcstack_check_refcount(pt->pt_funcstack);
4683 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004684}
4685
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004686/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004687 * Return the next (unique) copy ID.
4688 * Used for serializing nested structures.
4689 */
4690 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004691get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004692{
4693 current_copyID += COPYID_INC;
4694 return current_copyID;
4695}
4696
4697/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004698 * Garbage collection for lists and dictionaries.
4699 *
4700 * We use reference counts to be able to free most items right away when they
4701 * are no longer used. But for composite items it's possible that it becomes
4702 * unused while the reference count is > 0: When there is a recursive
4703 * reference. Example:
4704 * :let l = [1, 2, 3]
4705 * :let d = {9: l}
4706 * :let l[1] = d
4707 *
4708 * Since this is quite unusual we handle this with garbage collection: every
4709 * once in a while find out which lists and dicts are not referenced from any
4710 * variable.
4711 *
4712 * Here is a good reference text about garbage collection (refers to Python
4713 * but it applies to all reference-counting mechanisms):
4714 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004715 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004716
4717/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004718 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004719 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004720 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004721 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004722 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004723garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004724{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004725 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004726 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004727 buf_T *buf;
4728 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004729 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004730 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004731
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004732 if (!testing)
4733 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004734 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004735 want_garbage_collect = FALSE;
4736 may_garbage_collect = FALSE;
4737 garbage_collect_at_exit = FALSE;
4738 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004739
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004740 // The execution stack can grow big, limit the size.
4741 if (exestack.ga_maxlen - exestack.ga_len > 500)
4742 {
4743 size_t new_len;
4744 char_u *pp;
4745 int n;
4746
4747 // Keep 150% of the current size, with a minimum of the growth size.
4748 n = exestack.ga_len / 2;
4749 if (n < exestack.ga_growsize)
4750 n = exestack.ga_growsize;
4751
4752 // Don't make it bigger though.
4753 if (exestack.ga_len + n < exestack.ga_maxlen)
4754 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004755 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004756 pp = vim_realloc(exestack.ga_data, new_len);
4757 if (pp == NULL)
4758 return FAIL;
4759 exestack.ga_maxlen = exestack.ga_len + n;
4760 exestack.ga_data = pp;
4761 }
4762 }
4763
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004764 // We advance by two because we add one for items referenced through
4765 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004766 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004767
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004768 /*
4769 * 1. Go through all accessible variables and mark all lists and dicts
4770 * with copyID.
4771 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004772
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004773 // Don't free variables in the previous_funccal list unless they are only
4774 // referenced through previous_funccal. This must be first, because if
4775 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004776 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004777
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004778 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004779 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004780
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004781 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004782 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004783 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4784 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004785
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004786 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004787 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004788 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4789 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004790 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004791 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4792 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004793#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004794 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004795 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4796 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004797 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004798 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004799 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4800 NULL, NULL);
4801#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004802
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004803 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004804 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004805 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4806 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004807 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004808 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004809
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004810 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004811 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004812
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004813 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004814 abort = abort || set_ref_in_functions(copyID);
4815
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004816 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004817 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004818
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004819 // funcstacks keep variables for closures
4820 abort = abort || set_ref_in_funcstacks(copyID);
4821
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004822 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004823 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004824
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004825 // callbacks in buffers
4826 abort = abort || set_ref_in_buffers(copyID);
4827
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004828 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
4829 abort = abort || set_ref_in_insexpand_funcs(copyID);
4830
4831 // 'operatorfunc' callback
4832 abort = abort || set_ref_in_opfunc(copyID);
4833
4834 // 'tagfunc' callback
4835 abort = abort || set_ref_in_tagfunc(copyID);
4836
4837 // 'imactivatefunc' and 'imstatusfunc' callbacks
4838 abort = abort || set_ref_in_im_funcs(copyID);
4839
Bram Moolenaar1dced572012-04-05 16:54:08 +02004840#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004841 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004842#endif
4843
Bram Moolenaardb913952012-06-29 12:54:53 +02004844#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004845 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004846#endif
4847
4848#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004849 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004850#endif
4851
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004852#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004853 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004854 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004855#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004856#ifdef FEAT_NETBEANS_INTG
4857 abort = abort || set_ref_in_nb_channel(copyID);
4858#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004859
Bram Moolenaare3188e22016-05-31 21:13:04 +02004860#ifdef FEAT_TIMERS
4861 abort = abort || set_ref_in_timer(copyID);
4862#endif
4863
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004864#ifdef FEAT_QUICKFIX
4865 abort = abort || set_ref_in_quickfix(copyID);
4866#endif
4867
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004868#ifdef FEAT_TERMINAL
4869 abort = abort || set_ref_in_term(copyID);
4870#endif
4871
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004872#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004873 abort = abort || set_ref_in_popups(copyID);
4874#endif
4875
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004876 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004877 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004878 /*
4879 * 2. Free lists and dictionaries that are not referenced.
4880 */
4881 did_free = free_unref_items(copyID);
4882
4883 /*
4884 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004885 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004886 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004887 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004888 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004889 else if (p_verbose > 0)
4890 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004891 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004892 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004893
4894 return did_free;
4895}
4896
4897/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004898 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004899 */
4900 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004901free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004902{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004903 int did_free = FALSE;
4904
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004905 // Let all "free" functions know that we are here. This means no
4906 // dictionaries, lists, channels or jobs are to be freed, because we will
4907 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004908 in_free_unref_items = TRUE;
4909
4910 /*
4911 * PASS 1: free the contents of the items. We don't free the items
4912 * themselves yet, so that it is possible to decrement refcount counters
4913 */
4914
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004915 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004916 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004917
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004918 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004919 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004920
4921#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004922 // Go through the list of jobs and free items without the copyID. This
4923 // must happen before doing channels, because jobs refer to channels, but
4924 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004925 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4926
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004927 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004928 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4929#endif
4930
4931 /*
4932 * PASS 2: free the items themselves.
4933 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004934 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004935 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004936
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004937#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004938 // Go through the list of jobs and free items without the copyID. This
4939 // must happen before doing channels, because jobs refer to channels, but
4940 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004941 free_unused_jobs(copyID, COPYID_MASK);
4942
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004943 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004944 free_unused_channels(copyID, COPYID_MASK);
4945#endif
4946
4947 in_free_unref_items = FALSE;
4948
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004949 return did_free;
4950}
4951
4952/*
4953 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004954 * "list_stack" is used to add lists to be marked. Can be NULL.
4955 *
4956 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004957 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004958 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004959set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004960{
4961 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004962 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004963 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004964 hashtab_T *cur_ht;
4965 ht_stack_T *ht_stack = NULL;
4966 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004967
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004968 cur_ht = ht;
4969 for (;;)
4970 {
4971 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004972 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004973 // Mark each item in the hashtab. If the item contains a hashtab
4974 // it is added to ht_stack, if it contains a list it is added to
4975 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004976 todo = (int)cur_ht->ht_used;
4977 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4978 if (!HASHITEM_EMPTY(hi))
4979 {
4980 --todo;
4981 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4982 &ht_stack, list_stack);
4983 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004984 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004985
4986 if (ht_stack == NULL)
4987 break;
4988
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004989 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004990 cur_ht = ht_stack->ht;
4991 tempitem = ht_stack;
4992 ht_stack = ht_stack->prev;
4993 free(tempitem);
4994 }
4995
4996 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004997}
4998
Dominique Pelle748b3082022-01-08 12:41:16 +00004999#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5000 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005001/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005002 * Mark a dict and its items with "copyID".
5003 * Returns TRUE if setting references failed somehow.
5004 */
5005 int
5006set_ref_in_dict(dict_T *d, int copyID)
5007{
5008 if (d != NULL && d->dv_copyID != copyID)
5009 {
5010 d->dv_copyID = copyID;
5011 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5012 }
5013 return FALSE;
5014}
Dominique Pelle748b3082022-01-08 12:41:16 +00005015#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005016
5017/*
5018 * Mark a list and its items with "copyID".
5019 * Returns TRUE if setting references failed somehow.
5020 */
5021 int
5022set_ref_in_list(list_T *ll, int copyID)
5023{
5024 if (ll != NULL && ll->lv_copyID != copyID)
5025 {
5026 ll->lv_copyID = copyID;
5027 return set_ref_in_list_items(ll, copyID, NULL);
5028 }
5029 return FALSE;
5030}
5031
5032/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005033 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005034 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5035 *
5036 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005037 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005038 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005039set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005040{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005041 listitem_T *li;
5042 int abort = FALSE;
5043 list_T *cur_l;
5044 list_stack_T *list_stack = NULL;
5045 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005046
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005047 cur_l = l;
5048 for (;;)
5049 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005050 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005051 // Mark each item in the list. If the item contains a hashtab
5052 // it is added to ht_stack, if it contains a list it is added to
5053 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005054 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5055 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5056 ht_stack, &list_stack);
5057 if (list_stack == NULL)
5058 break;
5059
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005060 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005061 cur_l = list_stack->list;
5062 tempitem = list_stack;
5063 list_stack = list_stack->prev;
5064 free(tempitem);
5065 }
5066
5067 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005068}
5069
5070/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005071 * Mark the partial in callback 'cb' with "copyID".
5072 */
5073 int
5074set_ref_in_callback(callback_T *cb, int copyID)
5075{
5076 typval_T tv;
5077
5078 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5079 return FALSE;
5080
5081 tv.v_type = VAR_PARTIAL;
5082 tv.vval.v_partial = cb->cb_partial;
5083 return set_ref_in_item(&tv, copyID, NULL, NULL);
5084}
5085
5086/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005087 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005088 * "list_stack" is used to add lists to be marked. Can be NULL.
5089 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5090 *
5091 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005092 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005093 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005094set_ref_in_item(
5095 typval_T *tv,
5096 int copyID,
5097 ht_stack_T **ht_stack,
5098 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005099{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005100 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005101
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005102 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005103 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005104 dict_T *dd = tv->vval.v_dict;
5105
Bram Moolenaara03f2332016-02-06 18:09:59 +01005106 if (dd != NULL && dd->dv_copyID != copyID)
5107 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005108 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005109 dd->dv_copyID = copyID;
5110 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005111 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005112 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5113 }
5114 else
5115 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005116 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5117
Bram Moolenaara03f2332016-02-06 18:09:59 +01005118 if (newitem == NULL)
5119 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005120 else
5121 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005122 newitem->ht = &dd->dv_hashtab;
5123 newitem->prev = *ht_stack;
5124 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005125 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005126 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005127 }
5128 }
5129 else if (tv->v_type == VAR_LIST)
5130 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005131 list_T *ll = tv->vval.v_list;
5132
Bram Moolenaara03f2332016-02-06 18:09:59 +01005133 if (ll != NULL && ll->lv_copyID != copyID)
5134 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005135 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005136 ll->lv_copyID = copyID;
5137 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005138 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005139 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005140 }
5141 else
5142 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005143 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5144
Bram Moolenaara03f2332016-02-06 18:09:59 +01005145 if (newitem == NULL)
5146 abort = TRUE;
5147 else
5148 {
5149 newitem->list = ll;
5150 newitem->prev = *list_stack;
5151 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005152 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005153 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005154 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005155 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005156 else if (tv->v_type == VAR_FUNC)
5157 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005158 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005159 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005160 else if (tv->v_type == VAR_PARTIAL)
5161 {
5162 partial_T *pt = tv->vval.v_partial;
5163 int i;
5164
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005165 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005166 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005167 // Didn't see this partial yet.
5168 pt->pt_copyID = copyID;
5169
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005170 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005171
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005172 if (pt->pt_dict != NULL)
5173 {
5174 typval_T dtv;
5175
5176 dtv.v_type = VAR_DICT;
5177 dtv.vval.v_dict = pt->pt_dict;
5178 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5179 }
5180
5181 for (i = 0; i < pt->pt_argc; ++i)
5182 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5183 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005184 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005185 }
5186 }
5187#ifdef FEAT_JOB_CHANNEL
5188 else if (tv->v_type == VAR_JOB)
5189 {
5190 job_T *job = tv->vval.v_job;
5191 typval_T dtv;
5192
5193 if (job != NULL && job->jv_copyID != copyID)
5194 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005195 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005196 if (job->jv_channel != NULL)
5197 {
5198 dtv.v_type = VAR_CHANNEL;
5199 dtv.vval.v_channel = job->jv_channel;
5200 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5201 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005202 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005203 {
5204 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005205 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005206 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5207 }
5208 }
5209 }
5210 else if (tv->v_type == VAR_CHANNEL)
5211 {
5212 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005213 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005214 typval_T dtv;
5215 jsonq_T *jq;
5216 cbq_T *cq;
5217
5218 if (ch != NULL && ch->ch_copyID != copyID)
5219 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005220 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005221 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005222 {
5223 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5224 jq = jq->jq_next)
5225 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5226 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5227 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005228 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005229 {
5230 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005231 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005232 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5233 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005234 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005235 {
5236 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005237 dtv.vval.v_partial =
5238 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005239 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5240 }
5241 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005242 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005243 {
5244 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005245 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005246 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5247 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005248 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005249 {
5250 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005251 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005252 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5253 }
5254 }
5255 }
5256#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005257 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005258}
5259
Bram Moolenaar8c711452005-01-14 21:53:12 +00005260/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005261 * Return a string with the string representation of a variable.
5262 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005263 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005264 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005265 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005266 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005267 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005268 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5269 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005270 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005271 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005272 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005273echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005274 typval_T *tv,
5275 char_u **tofree,
5276 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005277 int copyID,
5278 int echo_style,
5279 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005280 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005281{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005282 static int recurse = 0;
5283 char_u *r = NULL;
5284
Bram Moolenaar33570922005-01-25 22:26:29 +00005285 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005286 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005287 if (!did_echo_string_emsg)
5288 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005289 // Only give this message once for a recursive call to avoid
5290 // flooding the user with errors. And stop iterating over lists
5291 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005292 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005293 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005294 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005295 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005296 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005297 }
5298 ++recurse;
5299
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300 switch (tv->v_type)
5301 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005302 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005303 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005304 {
5305 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005306 r = tv->vval.v_string;
5307 if (r == NULL)
5308 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005309 }
5310 else
5311 {
5312 *tofree = string_quote(tv->vval.v_string, FALSE);
5313 r = *tofree;
5314 }
5315 break;
5316
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005317 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005318 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005319 char_u buf[MAX_FUNC_NAME_LEN];
5320
5321 if (echo_style)
5322 {
LemonBoya5d35902022-04-29 21:15:02 +01005323 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5324 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005325 buf, MAX_FUNC_NAME_LEN);
5326 if (r == buf)
5327 {
5328 r = vim_strsave(buf);
5329 *tofree = r;
5330 }
5331 else
5332 *tofree = NULL;
5333 }
5334 else
5335 {
5336 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5337 : make_ufunc_name_readable(
5338 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5339 TRUE);
5340 r = *tofree;
5341 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005342 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005343 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005344
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005345 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005346 {
5347 partial_T *pt = tv->vval.v_partial;
5348 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005349 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005350 garray_T ga;
5351 int i;
5352 char_u *tf;
5353
5354 ga_init2(&ga, 1, 100);
5355 ga_concat(&ga, (char_u *)"function(");
5356 if (fname != NULL)
5357 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005358 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005359 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005360 && vim_isupper(fname[1]))
5361 {
5362 ga_concat(&ga, (char_u *)"'g:");
5363 ga_concat(&ga, fname + 1);
5364 }
5365 else
5366 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005367 vim_free(fname);
5368 }
5369 if (pt != NULL && pt->pt_argc > 0)
5370 {
5371 ga_concat(&ga, (char_u *)", [");
5372 for (i = 0; i < pt->pt_argc; ++i)
5373 {
5374 if (i > 0)
5375 ga_concat(&ga, (char_u *)", ");
5376 ga_concat(&ga,
5377 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5378 vim_free(tf);
5379 }
5380 ga_concat(&ga, (char_u *)"]");
5381 }
5382 if (pt != NULL && pt->pt_dict != NULL)
5383 {
5384 typval_T dtv;
5385
5386 ga_concat(&ga, (char_u *)", ");
5387 dtv.v_type = VAR_DICT;
5388 dtv.vval.v_dict = pt->pt_dict;
5389 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5390 vim_free(tf);
5391 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005392 // terminate with ')' and a NUL
5393 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005394
5395 *tofree = ga.ga_data;
5396 r = *tofree;
5397 break;
5398 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005399
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005400 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005401 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005402 break;
5403
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005405 if (tv->vval.v_list == NULL)
5406 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005407 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005408 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005409 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005410 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005411 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5412 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005413 {
5414 *tofree = NULL;
5415 r = (char_u *)"[...]";
5416 }
5417 else
5418 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005419 int old_copyID = tv->vval.v_list->lv_copyID;
5420
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005421 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005422 *tofree = list2string(tv, copyID, restore_copyID);
5423 if (restore_copyID)
5424 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005425 r = *tofree;
5426 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005427 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005428
Bram Moolenaar8c711452005-01-14 21:53:12 +00005429 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005430 if (tv->vval.v_dict == NULL)
5431 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005432 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005433 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005434 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005435 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005436 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5437 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005438 {
5439 *tofree = NULL;
5440 r = (char_u *)"{...}";
5441 }
5442 else
5443 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005444 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005445
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005446 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005447 *tofree = dict2string(tv, copyID, restore_copyID);
5448 if (restore_copyID)
5449 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005450 r = *tofree;
5451 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005452 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005453
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005454 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005455 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005456 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005457 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005458 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005459 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005460 break;
5461
Bram Moolenaar835dc632016-02-07 14:27:38 +01005462 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005463 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005464#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005465 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005466 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5467 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005468 if (composite_val)
5469 {
5470 *tofree = string_quote(r, FALSE);
5471 r = *tofree;
5472 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005473#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005474 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005475
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005476 case VAR_INSTR:
5477 *tofree = NULL;
5478 r = (char_u *)"instructions";
5479 break;
5480
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005481 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005482#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005483 *tofree = NULL;
5484 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5485 r = numbuf;
5486 break;
5487#endif
5488
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005489 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005490 case VAR_SPECIAL:
5491 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005492 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005493 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005494 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005495
Bram Moolenaar8502c702014-06-17 12:51:16 +02005496 if (--recurse == 0)
5497 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005498 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005499}
5500
5501/*
5502 * Return a string with the string representation of a variable.
5503 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5504 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005505 * Does not put quotes around strings, as ":echo" displays values.
5506 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5507 * May return NULL.
5508 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005509 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005510echo_string(
5511 typval_T *tv,
5512 char_u **tofree,
5513 char_u *numbuf,
5514 int copyID)
5515{
5516 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5517}
5518
5519/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005520 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5521 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005522 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005523 */
5524 int
5525buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5526{
5527 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005528 char_u *t;
5529 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005530
5531 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5532 return -1;
5533
5534 if (lnum > buf->b_ml.ml_line_count)
5535 lnum = buf->b_ml.ml_line_count;
5536
5537 str = ml_get_buf(buf, lnum, FALSE);
5538 if (str == NULL)
5539 return -1;
5540
5541 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005542 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005543
Bram Moolenaar91458462021-01-13 20:08:38 +01005544 // count the number of characters
5545 t = str;
5546 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5547 t += mb_ptr2len(t);
5548
5549 // In insert mode, when the cursor is at the end of a non-empty line,
5550 // byteidx points to the NUL character immediately past the end of the
5551 // string. In this case, add one to the character count.
5552 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5553 count++;
5554
5555 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005556}
5557
5558/*
5559 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005560 * byte index. Works only for loaded buffers. Returns -1 on failure.
5561 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005562 */
5563 int
5564buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5565{
5566 char_u *str;
5567 char_u *t;
5568
5569 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5570 return -1;
5571
5572 if (lnum > buf->b_ml.ml_line_count)
5573 lnum = buf->b_ml.ml_line_count;
5574
5575 str = ml_get_buf(buf, lnum, FALSE);
5576 if (str == NULL)
5577 return -1;
5578
5579 // Convert the character offset to a byte offset
5580 t = str;
5581 while (*t != NUL && --charidx > 0)
5582 t += mb_ptr2len(t);
5583
Bram Moolenaar91458462021-01-13 20:08:38 +01005584 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005585}
5586
5587/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005589 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005591 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005592var2fpos(
5593 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005594 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005595 int *fnum, // set to fnum for '0, 'A, etc.
5596 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005598 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005600 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005602 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005603 if (varp->v_type == VAR_LIST)
5604 {
5605 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005606 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005607 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005608 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005609
5610 l = varp->vval.v_list;
5611 if (l == NULL)
5612 return NULL;
5613
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005614 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005615 pos.lnum = list_find_nr(l, 0L, &error);
5616 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005617 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005618 if (charcol)
5619 len = (long)mb_charlen(ml_get(pos.lnum));
5620 else
5621 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005622
Bram Moolenaarec65d772020-08-20 22:29:12 +02005623 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005624 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005625 li = list_find(l, 1L);
5626 if (li != NULL && li->li_tv.v_type == VAR_STRING
5627 && li->li_tv.vval.v_string != NULL
5628 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005629 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005630 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005631 }
5632 else
5633 {
5634 pos.col = list_find_nr(l, 1L, &error);
5635 if (error)
5636 return NULL;
5637 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005638
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005639 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005640 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005641 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005642 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005643
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005644 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005645 pos.coladd = list_find_nr(l, 2L, &error);
5646 if (error)
5647 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005648
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005649 return &pos;
5650 }
5651
Bram Moolenaarc5809432021-03-27 21:23:30 +01005652 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5653 return NULL;
5654
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005655 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005656 if (name == NULL)
5657 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005658
5659 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005660 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005661 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005662 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005663 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005664 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005665 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005666 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005667 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005668 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005669 pos = VIsual;
5670 else
5671 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005672 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005673 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005674 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005676 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005677 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5679 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005680 pos = *pp;
5681 }
5682 if (pos.lnum != 0)
5683 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005684 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005685 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5686 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005688
Bram Moolenaara5525202006-03-02 22:52:09 +00005689 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005690
Bram Moolenaar477933c2007-07-17 14:32:23 +00005691 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005692 {
5693 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005694 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005695 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005696 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005697 // In silent Ex mode topline is zero, but that's not a valid line
5698 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005699 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005700 return &pos;
5701 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005702 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005703 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005704 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005705 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005706 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005707 return &pos;
5708 }
5709 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005710 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005712 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 {
5714 pos.lnum = curbuf->b_ml.ml_line_count;
5715 pos.col = 0;
5716 }
5717 else
5718 {
5719 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005720 if (charcol)
5721 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5722 else
5723 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 }
5725 return &pos;
5726 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005727 if (in_vim9script())
5728 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 return NULL;
5730}
5731
5732/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005733 * Convert list in "arg" into a position and optional file number.
5734 * When "fnump" is NULL there is no file number, only 3 items.
5735 * Note that the column is passed on as-is, the caller may want to decrement
5736 * it to use 1 for the first column.
5737 * Return FAIL when conversion is not possible, doesn't check the position for
5738 * validity.
5739 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005740 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005741list2fpos(
5742 typval_T *arg,
5743 pos_T *posp,
5744 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005745 colnr_T *curswantp,
5746 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005747{
5748 list_T *l = arg->vval.v_list;
5749 long i = 0;
5750 long n;
5751
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005752 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5753 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005754 if (arg->v_type != VAR_LIST
5755 || l == NULL
5756 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005757 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005758 return FAIL;
5759
5760 if (fnump != NULL)
5761 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005762 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005763 if (n < 0)
5764 return FAIL;
5765 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005766 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005767 *fnump = n;
5768 }
5769
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005770 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005771 if (n < 0)
5772 return FAIL;
5773 posp->lnum = n;
5774
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005775 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005776 if (n < 0)
5777 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005778 // If character position is specified, then convert to byte position
5779 if (charcol)
5780 {
5781 buf_T *buf;
5782
5783 // Get the text for the specified line in a loaded buffer
5784 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5785 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5786 return FAIL;
5787
Bram Moolenaar91458462021-01-13 20:08:38 +01005788 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005789 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005790 posp->col = n;
5791
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005792 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005793 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005794 posp->coladd = 0;
5795 else
5796 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005797
Bram Moolenaar493c1782014-05-28 14:34:46 +02005798 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005799 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005800
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005801 return OK;
5802}
5803
5804/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 * Get the length of an environment variable name.
5806 * Advance "arg" to the first character after the name.
5807 * Return 0 for error.
5808 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005809 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005810get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811{
5812 char_u *p;
5813 int len;
5814
5815 for (p = *arg; vim_isIDc(*p); ++p)
5816 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005817 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 return 0;
5819
5820 len = (int)(p - *arg);
5821 *arg = p;
5822 return len;
5823}
5824
5825/*
5826 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005827 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 * Return 0 if something is wrong.
5829 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005830 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005831get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832{
5833 char_u *p;
5834 int len;
5835
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005836 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005838 {
5839 if (*p == ':')
5840 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005841 // "s:" is start of "s:var", but "n:" is not and can be used in
5842 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005843 len = (int)(p - *arg);
5844 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5845 || len > 1)
5846 break;
5847 }
5848 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005849 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 return 0;
5851
5852 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005853 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854
5855 return len;
5856}
5857
5858/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005859 * Get the length of the name of a variable or function.
5860 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005862 * Return -1 if curly braces expansion failed.
5863 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 * If the name contains 'magic' {}'s, expand them and return the
5865 * expanded name in an allocated string via 'alias' - caller must free.
5866 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005867 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005868get_name_len(
5869 char_u **arg,
5870 char_u **alias,
5871 int evaluate,
5872 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873{
5874 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 char_u *p;
5876 char_u *expr_start;
5877 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005879 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880
5881 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5882 && (*arg)[2] == (int)KE_SNR)
5883 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005884 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 *arg += 3;
5886 return get_id_len(arg) + 3;
5887 }
5888 len = eval_fname_script(*arg);
5889 if (len > 0)
5890 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005891 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 *arg += len;
5893 }
5894
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005896 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005898 p = find_name_end(*arg, &expr_start, &expr_end,
5899 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 if (expr_start != NULL)
5901 {
5902 char_u *temp_string;
5903
5904 if (!evaluate)
5905 {
5906 len += (int)(p - *arg);
5907 *arg = skipwhite(p);
5908 return len;
5909 }
5910
5911 /*
5912 * Include any <SID> etc in the expanded string:
5913 * Thus the -len here.
5914 */
5915 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5916 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005917 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 *alias = temp_string;
5919 *arg = skipwhite(p);
5920 return (int)STRLEN(temp_string);
5921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922
5923 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005924 // Only give an error when there is something, otherwise it will be
5925 // reported at a higher level.
5926 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005927 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928
5929 return len;
5930}
5931
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005932/*
5933 * Find the end of a variable or function name, taking care of magic braces.
5934 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5935 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005936 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005937 * Return a pointer to just after the name. Equal to "arg" if there is no
5938 * valid name.
5939 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005940 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005941find_name_end(
5942 char_u *arg,
5943 char_u **expr_start,
5944 char_u **expr_end,
5945 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005947 int mb_nest = 0;
5948 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005950 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005951 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005953 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005955 *expr_start = NULL;
5956 *expr_end = NULL;
5957 }
5958
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005959 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005960 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5961 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005962 return arg;
5963
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005964 for (p = arg; *p != NUL
5965 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005966 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005967 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005968 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005969 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005970 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005971 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005972 if (*p == '\'')
5973 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005974 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005975 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005976 ;
5977 if (*p == NUL)
5978 break;
5979 }
5980 else if (*p == '"')
5981 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005982 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005983 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005984 if (*p == '\\' && p[1] != NUL)
5985 ++p;
5986 if (*p == NUL)
5987 break;
5988 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005989 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5990 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005991 // "s:" is start of "s:var", but "n:" is not and can be used in
5992 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005993 len = (int)(p - arg);
5994 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005995 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005996 break;
5997 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005998
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005999 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006001 if (*p == '[')
6002 ++br_nest;
6003 else if (*p == ']')
6004 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006006
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006007 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006009 if (*p == '{')
6010 {
6011 mb_nest++;
6012 if (expr_start != NULL && *expr_start == NULL)
6013 *expr_start = p;
6014 }
6015 else if (*p == '}')
6016 {
6017 mb_nest--;
6018 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6019 *expr_end = p;
6020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 }
6023
6024 return p;
6025}
6026
6027/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006028 * Expands out the 'magic' {}'s in a variable/function name.
6029 * Note that this can call itself recursively, to deal with
6030 * constructs like foo{bar}{baz}{bam}
6031 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6032 * "in_start" ^
6033 * "expr_start" ^
6034 * "expr_end" ^
6035 * "in_end" ^
6036 *
6037 * Returns a new allocated string, which the caller must free.
6038 * Returns NULL for failure.
6039 */
6040 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006041make_expanded_name(
6042 char_u *in_start,
6043 char_u *expr_start,
6044 char_u *expr_end,
6045 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006046{
6047 char_u c1;
6048 char_u *retval = NULL;
6049 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006050
6051 if (expr_end == NULL || in_end == NULL)
6052 return NULL;
6053 *expr_start = NUL;
6054 *expr_end = NUL;
6055 c1 = *in_end;
6056 *in_end = NUL;
6057
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006058 temp_result = eval_to_string(expr_start + 1, FALSE);
6059 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006060 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006061 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6062 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006063 if (retval != NULL)
6064 {
6065 STRCPY(retval, in_start);
6066 STRCAT(retval, temp_result);
6067 STRCAT(retval, expr_end + 1);
6068 }
6069 }
6070 vim_free(temp_result);
6071
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006072 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006073 *expr_start = '{';
6074 *expr_end = '}';
6075
6076 if (retval != NULL)
6077 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006078 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006079 if (expr_start != NULL)
6080 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006081 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006082 temp_result = make_expanded_name(retval, expr_start,
6083 expr_end, temp_result);
6084 vim_free(retval);
6085 retval = temp_result;
6086 }
6087 }
6088
6089 return retval;
6090}
6091
6092/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006094 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006096 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006097eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006099 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006100}
6101
6102/*
6103 * Return TRUE if character "c" can be used as the first character in a
6104 * variable or function name (excluding '{' and '}').
6105 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006106 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006107eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006108{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006109 return ASCII_ISALPHA(c) || c == '_';
6110}
6111
6112/*
6113 * Return TRUE if character "c" can be used as the first character of a
6114 * dictionary key.
6115 */
6116 int
6117eval_isdictc(int c)
6118{
6119 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120}
6121
6122/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006123 * Handle:
6124 * - expr[expr], expr[expr:expr] subscript
6125 * - ".name" lookup
6126 * - function call with Funcref variable: func(expr)
6127 * - method call: var->method()
6128 *
6129 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006130 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006131 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006132 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006133handle_subscript(
6134 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006135 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006136 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006137 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006138 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006139{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006140 int evaluate = evalarg != NULL
6141 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006142 int ret = OK;
6143 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006144 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006145 int getnext;
6146 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006147
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006148 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006149 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006150 // When at the end of the line and ".name" or "->{" or "->X" follows in
6151 // the next line then consume the line break.
6152 p = eval_next_non_blank(*arg, evalarg, &getnext);
6153 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006154 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006155 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6156 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6157 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006158 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006159 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006160 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006161 check_white = FALSE;
6162 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006163
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006164 if (rettv->v_type == VAR_ANY)
6165 {
6166 char_u *exp_name;
6167 int cc;
6168 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006169 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006170 type_T *type;
6171
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006172 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006173 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006174 if (**arg != '.')
6175 {
6176 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006177 semsg(_(e_expected_dot_after_name_str),
6178 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006179 ret = FAIL;
6180 break;
6181 }
6182 ++*arg;
6183 if (IS_WHITE_OR_NUL(**arg))
6184 {
6185 if (verbose)
6186 emsg(_(e_no_white_space_allowed_after_dot));
6187 ret = FAIL;
6188 break;
6189 }
6190
6191 // isolate the name
6192 exp_name = *arg;
6193 while (eval_isnamec(**arg))
6194 ++*arg;
6195 cc = **arg;
6196 **arg = NUL;
6197
6198 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00006199 evalarg->eval_cctx, evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006200 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006201
6202 if (idx < 0 && ufunc == NULL)
6203 {
6204 ret = FAIL;
6205 break;
6206 }
6207 if (idx >= 0)
6208 {
6209 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6210 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6211
6212 copy_tv(sv->sv_tv, rettv);
6213 }
6214 else
6215 {
6216 rettv->v_type = VAR_FUNC;
6217 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6218 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006219 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006220 }
6221
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006222 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6223 || rettv->v_type == VAR_PARTIAL))
6224 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006225 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006226 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6227 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006228
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006229 // Stop the expression evaluation when immediately aborting on
6230 // error, or when an interrupt occurred or an exception was thrown
6231 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006232 if (aborting())
6233 {
6234 if (ret == OK)
6235 clear_tv(rettv);
6236 ret = FAIL;
6237 }
6238 dict_unref(selfdict);
6239 selfdict = NULL;
6240 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006241 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006242 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006243 if (in_vim9script())
6244 *arg = skipwhite(p + 2);
6245 else
6246 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006247 if (ret == OK)
6248 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006249 if (VIM_ISWHITE(**arg))
6250 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006251 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006252 ret = FAIL;
6253 }
6254 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006255 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006256 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006257 else
6258 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006259 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006260 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006261 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006262 // "." is ".name" lookup when we found a dict or when evaluating and
6263 // scriptversion is at least 2, where string concatenation is "..".
6264 else if (**arg == '['
6265 || (**arg == '.' && (rettv->v_type == VAR_DICT
6266 || (!evaluate
6267 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006268 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006269 {
6270 dict_unref(selfdict);
6271 if (rettv->v_type == VAR_DICT)
6272 {
6273 selfdict = rettv->vval.v_dict;
6274 if (selfdict != NULL)
6275 ++selfdict->dv_refcount;
6276 }
6277 else
6278 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006279 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006280 {
6281 clear_tv(rettv);
6282 ret = FAIL;
6283 }
6284 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006285 else
6286 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006287 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006288
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006289 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6290 // Don't do this when "Func" is already a partial that was bound
6291 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006292 if (selfdict != NULL
6293 && (rettv->v_type == VAR_FUNC
6294 || (rettv->v_type == VAR_PARTIAL
6295 && (rettv->vval.v_partial->pt_auto
6296 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006297 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006298
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006299 dict_unref(selfdict);
6300 return ret;
6301}
6302
6303/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006304 * Make a copy of an item.
6305 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006306 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006307 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6308 * reference to an already copied list/dict can be used.
6309 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006310 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006311 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006312item_copy(
6313 typval_T *from,
6314 typval_T *to,
6315 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006316 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006317 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006318{
6319 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006320 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006321
Bram Moolenaar33570922005-01-25 22:26:29 +00006322 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006323 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006324 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006325 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006326 }
6327 ++recurse;
6328
6329 switch (from->v_type)
6330 {
6331 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006332 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006333 case VAR_STRING:
6334 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006335 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006336 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006337 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006338 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006339 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006340 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006341 copy_tv(from, to);
6342 break;
6343 case VAR_LIST:
6344 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006345 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006346 if (from->vval.v_list == NULL)
6347 to->vval.v_list = NULL;
6348 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6349 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006350 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006351 to->vval.v_list = from->vval.v_list->lv_copylist;
6352 ++to->vval.v_list->lv_refcount;
6353 }
6354 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006355 to->vval.v_list = list_copy(from->vval.v_list,
6356 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006357 if (to->vval.v_list == NULL)
6358 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006359 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006360 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006361 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006362 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006363 case VAR_DICT:
6364 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006365 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006366 if (from->vval.v_dict == NULL)
6367 to->vval.v_dict = NULL;
6368 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6369 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006370 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006371 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6372 ++to->vval.v_dict->dv_refcount;
6373 }
6374 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006375 to->vval.v_dict = dict_copy(from->vval.v_dict,
6376 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006377 if (to->vval.v_dict == NULL)
6378 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006379 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006380 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006381 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006382 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006383 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006384 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006385 }
6386 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006387 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006388}
6389
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006390 void
6391echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6392{
6393 char_u *tofree;
6394 char_u numbuf[NUMBUFLEN];
6395 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6396
6397 if (*atstart)
6398 {
6399 *atstart = FALSE;
6400 // Call msg_start() after eval1(), evaluating the expression
6401 // may cause a message to appear.
6402 if (with_space)
6403 {
6404 // Mark the saved text as finishing the line, so that what
6405 // follows is displayed on a new line when scrolling back
6406 // at the more prompt.
6407 msg_sb_eol();
6408 msg_start();
6409 }
6410 }
6411 else if (with_space)
6412 msg_puts_attr(" ", echo_attr);
6413
6414 if (p != NULL)
6415 for ( ; *p != NUL && !got_int; ++p)
6416 {
6417 if (*p == '\n' || *p == '\r' || *p == TAB)
6418 {
6419 if (*p != TAB && *needclr)
6420 {
6421 // remove any text still there from the command
6422 msg_clr_eos();
6423 *needclr = FALSE;
6424 }
6425 msg_putchar_attr(*p, echo_attr);
6426 }
6427 else
6428 {
6429 if (has_mbyte)
6430 {
6431 int i = (*mb_ptr2len)(p);
6432
6433 (void)msg_outtrans_len_attr(p, i, echo_attr);
6434 p += i - 1;
6435 }
6436 else
6437 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6438 }
6439 }
6440 vim_free(tofree);
6441}
6442
Bram Moolenaare9a41262005-01-15 22:18:47 +00006443/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 * ":echo expr1 ..." print each argument separated with a space, add a
6445 * newline at the end.
6446 * ":echon expr1 ..." print each argument plain.
6447 */
6448 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006449ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450{
6451 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006452 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006453 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 int needclr = TRUE;
6455 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006456 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006457 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006458 evalarg_T evalarg;
6459
Bram Moolenaare707c882020-07-01 13:07:37 +02006460 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461
6462 if (eap->skip)
6463 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006464 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006466 // If eval1() causes an error message the text from the command may
6467 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006468 need_clr_eos = needclr;
6469
Bram Moolenaar68db9962021-05-09 23:19:22 +02006470 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006471 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 {
6473 /*
6474 * Report the invalid expression unless the expression evaluation
6475 * has been cancelled due to an aborting error, an interrupt, or an
6476 * exception.
6477 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006478 if (!aborting() && did_emsg == did_emsg_before
6479 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006480 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006481 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 break;
6483 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006484 need_clr_eos = FALSE;
6485
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006487 {
6488 if (rettv.v_type == VAR_VOID)
6489 {
6490 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6491 break;
6492 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006493 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006494 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006495
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006496 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 arg = skipwhite(arg);
6498 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006499 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006500 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501
6502 if (eap->skip)
6503 --emsg_skip;
6504 else
6505 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006506 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 if (needclr)
6508 msg_clr_eos();
6509 if (eap->cmdidx == CMD_echo)
6510 msg_end();
6511 }
6512}
6513
6514/*
6515 * ":echohl {name}".
6516 */
6517 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006518ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006520 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521}
6522
6523/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006524 * Returns the :echo attribute
6525 */
6526 int
6527get_echo_attr(void)
6528{
6529 return echo_attr;
6530}
6531
6532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 * ":execute expr1 ..." execute the result of an expression.
6534 * ":echomsg expr1 ..." Print a message
6535 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006536 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 * Each gets spaces around each argument and a newline at the end for
6538 * echo commands
6539 */
6540 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006541ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542{
6543 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006544 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 int ret = OK;
6546 char_u *p;
6547 garray_T ga;
6548 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006549 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550
6551 ga_init2(&ga, 1, 80);
6552
6553 if (eap->skip)
6554 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006555 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006557 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006558 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560
6561 if (!eap->skip)
6562 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006563 char_u buf[NUMBUFLEN];
6564
6565 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006566 {
6567 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6568 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006569 semsg(_(e_using_invalid_value_as_string_str),
6570 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006571 p = NULL;
6572 }
6573 else
6574 p = tv_get_string_buf(&rettv, buf);
6575 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006576 else
6577 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006578 if (p == NULL)
6579 {
6580 clear_tv(&rettv);
6581 ret = FAIL;
6582 break;
6583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 len = (int)STRLEN(p);
6585 if (ga_grow(&ga, len + 2) == FAIL)
6586 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006587 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 ret = FAIL;
6589 break;
6590 }
6591 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 ga.ga_len += len;
6595 }
6596
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006597 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 arg = skipwhite(arg);
6599 }
6600
6601 if (ret != FAIL && ga.ga_data != NULL)
6602 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006603 // use the first line of continuation lines for messages
6604 SOURCING_LNUM = start_lnum;
6605
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006606 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6607 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006608 // Mark the already saved text as finishing the line, so that what
6609 // follows is displayed on a new line when scrolling back at the
6610 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006611 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006612 }
6613
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006615 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006616 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006617 out_flush();
6618 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006619 else if (eap->cmdidx == CMD_echoconsole)
6620 {
6621 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6622 ui_write((char_u *)"\r\n", 2, TRUE);
6623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 else if (eap->cmdidx == CMD_echoerr)
6625 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006626 int save_did_emsg = did_emsg;
6627
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006628 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006629 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 if (!force_abort)
6631 did_emsg = save_did_emsg;
6632 }
6633 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006634 {
6635 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6636
6637 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6638 sticky_cmdmod_flags = cmdmod.cmod_flags
6639 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 do_cmdline((char_u *)ga.ga_data,
6641 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006642 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 }
6645
6646 ga_clear(&ga);
6647
6648 if (eap->skip)
6649 --emsg_skip;
6650
Bram Moolenaar63b91732021-08-05 20:40:03 +02006651 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652}
6653
6654/*
6655 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6656 * "arg" points to the "&" or '+' when called, to "option" when returning.
6657 * Returns NULL when no option name found. Otherwise pointer to the char
6658 * after the option name.
6659 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006660 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006661find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662{
6663 char_u *p = *arg;
6664
6665 ++p;
6666 if (*p == 'g' && p[1] == ':')
6667 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006668 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 p += 2;
6670 }
6671 else if (*p == 'l' && p[1] == ':')
6672 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006673 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 p += 2;
6675 }
6676 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006677 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678
6679 if (!ASCII_ISALPHA(*p))
6680 return NULL;
6681 *arg = p;
6682
6683 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006684 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 else
6686 while (ASCII_ISALPHA(*p))
6687 ++p;
6688 return p;
6689}
6690
6691/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006692 * Display script name where an item was last set.
6693 * Should only be invoked when 'verbose' is non-zero.
6694 */
6695 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006696last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006697{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006698 char_u *p;
6699
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006700 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006701 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006702 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006703 if (p != NULL)
6704 {
6705 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006706 msg_puts(_("\n\tLast set from "));
6707 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006708 if (script_ctx.sc_lnum > 0)
6709 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006710 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006711 msg_outnum((long)script_ctx.sc_lnum);
6712 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006713 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006714 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006715 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006716 }
6717}
6718
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006719#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720
6721/*
6722 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006723 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 * "flags" can be "g" to do a global substitute.
6725 * Returns an allocated string, NULL for error.
6726 */
6727 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006728do_string_sub(
6729 char_u *str,
6730 char_u *pat,
6731 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006732 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006733 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734{
6735 int sublen;
6736 regmatch_T regmatch;
6737 int i;
6738 int do_all;
6739 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006740 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 garray_T ga;
6742 char_u *ret;
6743 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006744 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006746 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006748 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749
6750 ga_init2(&ga, 1, 200);
6751
6752 do_all = (flags[0] == 'g');
6753
6754 regmatch.rm_ic = p_ic;
6755 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6756 if (regmatch.regprog != NULL)
6757 {
6758 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006759 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6761 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006762 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006763 if (regmatch.startp[0] == regmatch.endp[0])
6764 {
6765 if (zero_width == regmatch.startp[0])
6766 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006767 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006768 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006769 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6770 (size_t)i);
6771 ga.ga_len += i;
6772 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006773 continue;
6774 }
6775 zero_width = regmatch.startp[0];
6776 }
6777
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 /*
6779 * Get some space for a temporary buffer to do the substitution
6780 * into. It will contain:
6781 * - The text up to where the match is.
6782 * - The substituted text.
6783 * - The text after the match.
6784 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006785 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006786 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6788 {
6789 ga_clear(&ga);
6790 break;
6791 }
6792
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006793 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 i = (int)(regmatch.startp[0] - tail);
6795 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006796 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006797 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 + ga.ga_len + i, TRUE, TRUE, FALSE);
6799 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006800 tail = regmatch.endp[0];
6801 if (*tail == NUL)
6802 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 if (!do_all)
6804 break;
6805 }
6806
6807 if (ga.ga_data != NULL)
6808 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6809
Bram Moolenaar473de612013-06-08 18:19:48 +02006810 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 }
6812
6813 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6814 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006815 if (p_cpo == empty_option)
6816 p_cpo = save_cpo;
6817 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006818 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006819 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006820 // If it's still empty it was changed and restored, need to restore in
6821 // the complicated way.
6822 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01006823 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006824 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826
6827 return ret;
6828}