blob: 41094cd69f055b4cdccb36c062b441be4ce03617 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar071d4272004-06-13 20:20:40 +000032/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000033 * Info used by a ":for" loop.
34 */
Bram Moolenaar33570922005-01-25 22:26:29 +000035typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000036{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010037 int fi_semicolon; // TRUE if ending in '; var]'
38 int fi_varcount; // nr of variables in the list
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020039 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010040 listwatch_T fi_lw; // keep an eye on the item used.
41 list_T *fi_list; // list being used
42 int fi_bi; // index of blob
43 blob_T *fi_blob; // blob being used
Bram Moolenaar74e54fc2021-03-26 20:41:29 +010044 char_u *fi_string; // copy of string being used
45 int fi_byte_idx; // byte index in fi_string
Bram Moolenaar33570922005-01-25 22:26:29 +000046} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000047
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020048static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
49static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
52static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +020053static int eval7t(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020054static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020055static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000056
Bram Moolenaar48e697e2016-01-23 22:17:30 +010057static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020058static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020059
Bram Moolenaare21c1582019-03-02 11:57:09 +010060/*
61 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010062 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010063 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020064 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010065num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010066{
67 varnumber_T result;
68
Bram Moolenaar99880f92021-01-20 21:23:14 +010069 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010070 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010071 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010072 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010073 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010074 if (failed != NULL)
75 *failed = TRUE;
76 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010077 if (n1 == 0)
78 result = VARNUM_MIN; // similar to NaN
79 else if (n1 < 0)
80 result = -VARNUM_MAX;
81 else
82 result = VARNUM_MAX;
83 }
84 else
85 result = n1 / n2;
86
87 return result;
88}
89
90/*
91 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010092 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010093 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020094 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010095num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010096{
Bram Moolenaar99880f92021-01-20 21:23:14 +010097 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010098 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010099 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +0100100 if (failed != NULL)
101 *failed = TRUE;
102 }
Bram Moolenaare21c1582019-03-02 11:57:09 +0100103 return (n2 == 0) ? 0 : (n1 % n2);
104}
105
Bram Moolenaar33570922005-01-25 22:26:29 +0000106/*
107 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000108 */
109 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100110eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000111{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200112 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200113 func_init();
Bram Moolenaara7043832005-01-21 11:56:39 +0000114}
115
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000116#if defined(EXITFREE) || defined(PROTO)
117 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100118eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000119{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200120 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100121 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200122 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000123
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200124 // autoloaded script names
125 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000126
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200127 // unreferenced lists and dicts
128 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200129
130 // functions not garbage collected
131 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000132}
133#endif
134
Bram Moolenaare6b53242020-07-01 17:28:33 +0200135 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200136fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
137{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100138 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200139 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200140 if (eap != NULL)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200141 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200142 evalarg->eval_cstack = eap->cstack;
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +0000143 if (sourcing_a_script(eap))
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200144 {
145 evalarg->eval_getline = eap->getline;
146 evalarg->eval_cookie = eap->cookie;
147 }
Bram Moolenaar37c83712020-06-30 21:18:36 +0200148 }
149}
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
152 * Top level evaluation function, returning a boolean.
153 * Sets "error" to TRUE if there was an error.
154 * Return TRUE or FALSE.
155 */
156 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100157eval_to_bool(
158 char_u *arg,
159 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200160 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100161 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162{
Bram Moolenaar33570922005-01-25 22:26:29 +0000163 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200164 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200165 evalarg_T evalarg;
166
Bram Moolenaar37c83712020-06-30 21:18:36 +0200167 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168
169 if (skip)
170 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200171 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 else
174 {
175 *error = FALSE;
176 if (!skip)
177 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200178 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200179 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200180 else
181 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000182 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 }
184 }
185 if (skip)
186 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200187 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200189 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190}
191
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100192/*
193 * Call eval1() and give an error message if not done at a lower level.
194 */
195 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200196eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100197{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100198 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100199 int ret;
200 int did_emsg_before = did_emsg;
201 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200202 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100203
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200204 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
205
206 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100207 if (ret == FAIL)
208 {
209 // Report the invalid expression unless the expression evaluation has
210 // been cancelled due to an aborting error, an interrupt, or an
211 // exception, or we already gave a more specific error.
212 // Also check called_emsg for when using assert_fails().
213 if (!aborting() && did_emsg == did_emsg_before
214 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200215 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100216 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200217 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100218 return ret;
219}
220
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100221/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200222 * Return whether a typval is a valid expression to pass to eval_expr_typval()
223 * or eval_expr_to_bool(). An empty string returns FALSE;
224 */
225 int
226eval_expr_valid_arg(typval_T *tv)
227{
228 return tv->v_type != VAR_UNKNOWN
229 && (tv->v_type != VAR_STRING
230 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
231}
232
233/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100234 * Evaluate an expression, which can be a function, partial or string.
235 * Pass arguments "argv[argc]".
236 * Return the result in "rettv" and OK or FAIL.
237 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200238 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100239eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
240{
241 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100242 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200243 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100244
245 if (expr->v_type == VAR_FUNC)
246 {
247 s = expr->vval.v_string;
248 if (s == NULL || *s == NUL)
249 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200250 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000251 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200252 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100253 return FAIL;
254 }
255 else if (expr->v_type == VAR_PARTIAL)
256 {
257 partial_T *partial = expr->vval.v_partial;
258
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200259 if (partial == NULL)
260 return FAIL;
261
Bram Moolenaar822ba242020-05-24 23:00:18 +0200262 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200263 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100264 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200265 if (call_def_function(partial->pt_func, argc, argv,
266 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100267 return FAIL;
268 }
269 else
270 {
271 s = partial_name(partial);
272 if (s == NULL || *s == NUL)
273 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200274 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000275 funcexe.fe_evaluate = TRUE;
276 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
278 return FAIL;
279 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100280 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200281 else if (expr->v_type == VAR_INSTR)
282 {
283 return exe_typval_instr(expr, rettv);
284 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100285 else
286 {
Bram Moolenaarfc4c4482022-01-26 21:17:04 +0000287 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
Bram Moolenaar48570482017-10-30 21:48:41 +0100288 if (s == NULL)
289 return FAIL;
290 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200291 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100292 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200293 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100294 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200295 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200296 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100297 return FAIL;
298 }
299 }
300 return OK;
301}
302
303/*
304 * Like eval_to_bool() but using a typval_T instead of a string.
305 * Works for string, funcref and partial.
306 */
307 int
308eval_expr_to_bool(typval_T *expr, int *error)
309{
310 typval_T rettv;
311 int res;
312
313 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
314 {
315 *error = TRUE;
316 return FALSE;
317 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200318 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100319 clear_tv(&rettv);
320 return res;
321}
322
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323/*
324 * Top level evaluation function, returning a string. If "skip" is TRUE,
325 * only parsing to "nextcmd" is done, without reporting errors. Return
326 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
327 */
328 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100329eval_to_string_skip(
330 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200331 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100332 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333{
Bram Moolenaar33570922005-01-25 22:26:29 +0000334 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200336 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337
Bram Moolenaar37c83712020-06-30 21:18:36 +0200338 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 if (skip)
340 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200341 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 retval = NULL;
343 else
344 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100345 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000346 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 }
348 if (skip)
349 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200350 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351
352 return retval;
353}
354
355/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000356 * Skip over an expression at "*pp".
357 * Return FAIL for an error, OK otherwise.
358 */
359 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200360skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000361{
Bram Moolenaar33570922005-01-25 22:26:29 +0000362 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000363
364 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200365 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000366}
367
368/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200369 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200370 * If in Vim9 script and line breaks are encountered, the lines are
371 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200372 * "arg" is advanced to just after the expression.
373 * "start" is set to the start of the expression, "end" to just after the end.
374 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200375 * Return FAIL for an error, OK otherwise.
376 */
377 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200378skip_expr_concatenate(
379 char_u **arg,
380 char_u **start,
381 char_u **end,
382 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200383{
384 typval_T rettv;
385 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200386 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200387 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200388 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200389 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200390 int evaluate = evalarg == NULL
391 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200392
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200393 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200394 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200395 {
396 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200397 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200398 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200399 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200400 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200401 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200402 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200403
404 // Don't evaluate the expression.
405 if (evalarg != NULL)
406 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200407 *arg = skipwhite(*arg);
408 res = eval1(arg, &rettv, evalarg);
409 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200410 if (evalarg != NULL)
411 evalarg->eval_flags = save_flags;
412
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200413 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200414 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200415 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200416 if (evalarg->eval_ga.ga_len == 1)
417 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200418 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200419 ga_clear(gap);
420 gap->ga_itemsize = 0;
421 }
422 else
423 {
424 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200425 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200426
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200427 // Line breaks encountered, concatenate all the lines.
428 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200429 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200430
431 // free the lines only when using getsourceline()
432 if (evalarg->eval_cookie != NULL)
433 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200434 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200435 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200436 // Do not free the last line, "arg" points into it, free it
437 // later.
438 vim_free(evalarg->eval_tofree);
439 evalarg->eval_tofree =
440 ((char_u **)gap->ga_data)[gap->ga_len - 1];
441 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200442 ga_clear_strings(gap);
443 }
444 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200445 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200446 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200447
448 // free lines that were explicitly marked for freeing
449 ga_clear_strings(freegap);
450 }
451
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200452 gap->ga_itemsize = 0;
453 if (p == NULL)
454 return FAIL;
455 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200456 vim_free(evalarg->eval_tofree_lambda);
457 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200458 // Compute "end" relative to the end.
459 *end = *start + STRLEN(*start) - endoff;
460 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200461 }
462
463 return res;
464}
465
466/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100467 * Convert "tv" to a string.
468 * When "convert" is TRUE convert a List into a sequence of lines and convert
469 * a Float to a String.
470 * Returns an allocated string (NULL when out of memory).
471 */
472 char_u *
473typval2string(typval_T *tv, int convert)
474{
475 garray_T ga;
476 char_u *retval;
477#ifdef FEAT_FLOAT
478 char_u numbuf[NUMBUFLEN];
479#endif
480
481 if (convert && tv->v_type == VAR_LIST)
482 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000483 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100484 if (tv->vval.v_list != NULL)
485 {
486 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
487 if (tv->vval.v_list->lv_len > 0)
488 ga_append(&ga, NL);
489 }
490 ga_append(&ga, NUL);
491 retval = (char_u *)ga.ga_data;
492 }
493#ifdef FEAT_FLOAT
494 else if (convert && tv->v_type == VAR_FLOAT)
495 {
496 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
497 retval = vim_strsave(numbuf);
498 }
499#endif
500 else
501 retval = vim_strsave(tv_get_string(tv));
502 return retval;
503}
504
505/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200506 * Top level evaluation function, returning a string. Does not handle line
507 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000508 * When "convert" is TRUE convert a List into a sequence of lines and convert
509 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 * Return pointer to allocated memory, or NULL for failure.
511 */
512 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100513eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100514 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100515 int convert,
516 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517{
Bram Moolenaar33570922005-01-25 22:26:29 +0000518 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100520 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100522 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
523 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 retval = NULL;
525 else
526 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100527 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000528 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100530 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531
532 return retval;
533}
534
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100535 char_u *
536eval_to_string(
537 char_u *arg,
538 int convert)
539{
540 return eval_to_string_eap(arg, convert, NULL);
541}
542
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000544 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200545 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200546 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 */
548 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100549eval_to_string_safe(
550 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000551 int use_sandbox,
552 int keep_script_version)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553{
554 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200555 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200556 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200557 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558
Bram Moolenaar9530b582022-01-22 13:39:08 +0000559 if (!keep_script_version)
560 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200561 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000562 if (use_sandbox)
563 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200564 ++textwinlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200565 may_garbage_collect = FALSE;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200566 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000567 if (use_sandbox)
568 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200569 --textwinlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200570 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200571 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200572 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 return retval;
574}
575
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576/*
577 * Top level evaluation function, returning a number.
578 * Evaluates "expr" silently.
579 * Returns -1 for an error.
580 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200581 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100582eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583{
Bram Moolenaar33570922005-01-25 22:26:29 +0000584 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200585 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000586 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587
588 ++emsg_off;
589
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200590 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 retval = -1;
592 else
593 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100594 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000595 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 }
597 --emsg_off;
598
599 return retval;
600}
601
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000602/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000603 * Top level evaluation function.
604 * Returns an allocated typval_T with the result.
605 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000606 */
607 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200608eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000609{
610 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200611 evalarg_T evalarg;
612
613 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000614
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200615 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200616 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100617 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000618
Bram Moolenaar37c83712020-06-30 21:18:36 +0200619 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000620 return tv;
621}
622
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000624 * "*arg" points to what can be a function name in the form of "import.Name" or
625 * "Funcref". Return the name of the function. Set "tofree" to something that
626 * was allocated.
627 * If "verbose" is FALSE no errors are given.
628 * Return NULL for any failure.
629 */
630 static char_u *
631deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000632 char_u **arg,
633 char_u **tofree,
634 evalarg_T *evalarg,
635 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000636{
637 typval_T ref;
638 char_u *name = *arg;
639
640 ref.v_type = VAR_UNKNOWN;
641 if (eval7(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100642 {
643 dictitem_T *v;
644
645 // If <SID>VarName was used it would not be found, try another way.
646 v = find_var_also_in_script(name, NULL, FALSE);
647 if (v == NULL)
648 return NULL;
649 copy_tv(&v->di_tv, &ref);
650 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000651 if (*skipwhite(*arg) != NUL)
652 {
653 if (verbose)
654 semsg(_(e_trailing_characters_str), *arg);
655 name = NULL;
656 }
657 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
658 {
659 name = ref.vval.v_string;
660 ref.vval.v_string = NULL;
661 *tofree = name;
662 }
663 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
664 {
665 if (ref.vval.v_partial->pt_argc > 0
666 || ref.vval.v_partial->pt_dict != NULL)
667 {
668 if (verbose)
669 emsg(_(e_cannot_use_partial_here));
670 name = NULL;
671 }
672 else
673 {
674 name = vim_strsave(partial_name(ref.vval.v_partial));
675 *tofree = name;
676 }
677 }
678 else
679 {
680 if (verbose)
681 semsg(_(e_not_callable_type_str), name);
682 name = NULL;
683 }
684 clear_tv(&ref);
685 return name;
686}
687
688/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100689 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200690 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
691 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000692 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200694 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100695call_vim_function(
696 char_u *func,
697 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200698 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200699 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000701 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200702 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000703 char_u *arg;
704 char_u *name;
705 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000706 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100708 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200709 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000710 funcexe.fe_firstline = curwin->w_cursor.lnum;
711 funcexe.fe_lastline = curwin->w_cursor.lnum;
712 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000713
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000714 // The name might be "import.Func" or "Funcref". We don't know, we need to
715 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000716 // autoload script has errors. Guess that when there is a dot in the name
717 // showing errors is the right choice.
718 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000719 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000720 if (ignore_errors)
721 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000722 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000723 if (ignore_errors)
724 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000725 if (name == NULL)
726 name = func;
727
728 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
729
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000730 if (ret == FAIL)
731 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000732 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000733
734 return ret;
735}
736
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100737/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100738 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000739 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
740 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000741 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000742 */
743 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100744call_func_retstr(
745 char_u *func,
746 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200747 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000748{
749 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000750 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000751
Bram Moolenaarded27a12018-08-01 19:06:03 +0200752 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000753 return NULL;
754
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100755 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000756 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 return retval;
758}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000759
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000760/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100761 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000762 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000763 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000764 */
765 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100766call_func_retlist(
767 char_u *func,
768 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200769 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000770{
771 typval_T rettv;
772
Bram Moolenaarded27a12018-08-01 19:06:03 +0200773 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000774 return NULL;
775
776 if (rettv.v_type != VAR_LIST)
777 {
778 clear_tv(&rettv);
779 return NULL;
780 }
781
782 return rettv.vval.v_list;
783}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784
Bram Moolenaare70dd112022-01-21 16:31:11 +0000785#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100787 * Evaluate "arg", which is 'foldexpr'.
788 * Note: caller must set "curwin" to match "arg".
789 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
790 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 */
792 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000793eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000795 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000796 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200797 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000799 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000800 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
801 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802
Bram Moolenaare70dd112022-01-21 16:31:11 +0000803 arg = wp->w_p_fde;
804 current_sctx = wp->w_p_script_ctx[WV_FDE];
805
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000807 if (use_sandbox)
808 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200809 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200811 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 retval = 0;
813 else
814 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100815 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000816 if (tv.v_type == VAR_NUMBER)
817 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000818 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 retval = 0;
820 else
821 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100822 // If the result is a string, check if there is a non-digit before
823 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000824 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 if (!VIM_ISDIGIT(*s) && *s != '-')
826 *cp = *s++;
827 retval = atol((char *)s);
828 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000829 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 }
831 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000832 if (use_sandbox)
833 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200834 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200835 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000836 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200838 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839}
840#endif
841
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000843 * Get an lval: variable, Dict item or List item that can be assigned a value
844 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
845 * "name.key", "name.key[expr]" etc.
846 * Indexing only works if "name" is an existing List or Dictionary.
847 * "name" points to the start of the name.
848 * If "rettv" is not NULL it points to the value to be assigned.
849 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
850 * wrong; must end in space or cmd separator.
851 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100852 * flags:
853 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100854 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100855 * GLV_NO_AUTOLOAD: do not use script autoloading
856 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000857 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000858 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000859 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000860 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200861 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100862get_lval(
863 char_u *name,
864 typval_T *rettv,
865 lval_T *lp,
866 int unlet,
867 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100868 int flags, // GLV_ values
869 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000870{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000871 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000872 char_u *expr_start, *expr_end;
873 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000874 dictitem_T *v;
875 typval_T var1;
876 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000877 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000878 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000879 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100880 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100881 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100882 int writing;
Bram Moolenaar4525a572022-02-13 11:57:33 +0000883 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000884
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100885 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200886 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000887
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100888 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000889 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100890 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000891 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200892 lp->ll_name_end = find_name_end(name, NULL, NULL,
893 FNE_INCL_BR | fne_flags);
894 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000895 }
896
Bram Moolenaara749a422022-02-12 19:52:25 +0000897 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +0000898 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +0000899 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
900 {
901 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
902 return NULL;
903 }
904
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100905 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000906 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100907 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000908 if (expr_start != NULL)
909 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100910 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100911 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000912 && *p != '[' && *p != '.')
913 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000914 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000915 return NULL;
916 }
917
918 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
919 if (lp->ll_exp_name == NULL)
920 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100921 // Report an invalid expression in braces, unless the
922 // expression evaluation has been cancelled due to an
923 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000924 if (!aborting() && !quiet)
925 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000926 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000927 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000928 return NULL;
929 }
930 }
931 lp->ll_name = lp->ll_exp_name;
932 }
933 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000935 lp->ll_name = name;
936
Bram Moolenaar4525a572022-02-13 11:57:33 +0000937 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100938 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200939 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +0000940 // However, "g:[key]" is indexing a dictionary.
941 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200942 {
943 --p;
944 lp->ll_name_end = p;
945 }
946 if (*p == ':')
947 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000948 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +0200949
950 if (tp == p + 1 && !quiet)
951 {
952 semsg(_(e_white_space_required_after_str_str), ":", p);
953 return NULL;
954 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100955
Bram Moolenaar4c8b5462022-03-16 20:01:39 +0000956 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000957 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +0000958 semsg(_(e_using_type_not_in_script_context_str), p);
959 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000960 }
961
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200962 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +0000963 lp->ll_type = parse_type(&tp,
964 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
965 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100966 if (lp->ll_type == NULL && !quiet)
967 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200968 lp->ll_name_end = tp;
969 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100970 }
971 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000972 if (lp->ll_name == NULL)
973 return p;
974
Bram Moolenaar62aec932022-01-29 21:45:34 +0000975 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000976 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000977 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000978
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000979 if (import != NULL)
980 {
981 ufunc_T *ufunc;
982 type_T *type;
983
984 lp->ll_sid = import->imp_sid;
985 lp->ll_name = skipwhite(p + 1);
986 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
987 lp->ll_name_end = p;
988
989 // check the item is exported
990 cc = *p;
991 *p = NUL;
992 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000993 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000994 {
995 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +0000996 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000997 }
998 *p = cc;
999 }
1000 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001001
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001002 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001003 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001004 return p;
1005
Bram Moolenaar4525a572022-02-13 11:57:33 +00001006 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001007 {
1008 // using local variable
1009 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001010 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001011 }
1012 else
1013 {
1014 cc = *p;
1015 *p = NUL;
1016 // When we would write to the variable pass &ht and prevent autoload.
1017 writing = !(flags & GLV_READ_ONLY);
1018 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001019 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001020 if (v == NULL && !quiet)
1021 semsg(_(e_undefined_variable_str), lp->ll_name);
1022 *p = cc;
1023 if (v == NULL)
1024 return NULL;
1025 lp->ll_tv = &v->di_tv;
1026 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001027
Bram Moolenaar4525a572022-02-13 11:57:33 +00001028 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001029 {
1030 if (!quiet)
1031 semsg(_(e_variable_already_declared), lp->ll_name);
1032 return NULL;
1033 }
1034
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001035 /*
1036 * Loop until no more [idx] or .key is following.
1037 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001038 var1.v_type = VAR_UNKNOWN;
1039 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001040 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001041 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001042 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1043 {
1044 if (!quiet)
1045 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1046 return NULL;
1047 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001048 if (lp->ll_tv->v_type != VAR_LIST
1049 && lp->ll_tv->v_type != VAR_DICT
1050 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001051 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001052 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001053 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001054 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001055 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001056
1057 // a NULL list/blob works like an empty list/blob, allocate one now.
1058 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1059 rettv_list_alloc(lp->ll_tv);
1060 else if (lp->ll_tv->v_type == VAR_BLOB
1061 && lp->ll_tv->vval.v_blob == NULL)
1062 rettv_blob_alloc(lp->ll_tv);
1063
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001064 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001065 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001066 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001067 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001068 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001069 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001070
Bram Moolenaar4525a572022-02-13 11:57:33 +00001071 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001072 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001073 && lp->ll_tv == &v->di_tv
1074 && ht != NULL && ht == get_script_local_ht())
1075 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001076 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001077
1078 // Vim9 script local variable: get the type
1079 if (sv != NULL)
1080 lp->ll_valtype = sv->sv_type;
1081 }
1082
Bram Moolenaar8c711452005-01-14 21:53:12 +00001083 len = -1;
1084 if (*p == '.')
1085 {
1086 key = p + 1;
1087 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1088 ;
1089 if (len == 0)
1090 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001091 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001092 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001093 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001094 }
1095 p = key + len;
1096 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001097 else
1098 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001099 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001100 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001101 if (*p == ':')
1102 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001103 else
1104 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001105 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001106 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001107 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001108 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001109 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001110 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001111 clear_tv(&var1);
1112 return NULL;
1113 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001114 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001115 }
1116
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001117 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001118 if (*p == ':')
1119 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001120 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001121 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001122 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001123 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001124 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001125 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001126 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001127 if (rettv != NULL
1128 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001129 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001130 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001131 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001132 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001133 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001134 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001135 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001136 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001137 }
1138 p = skipwhite(p + 1);
1139 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001140 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001141 else
1142 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001143 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001144 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001145 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001146 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001147 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001148 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001149 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001150 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001151 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001152 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001153 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001154 clear_tv(&var2);
1155 return NULL;
1156 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001157 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001158 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001159 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001160 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001161 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001162
Bram Moolenaar8c711452005-01-14 21:53:12 +00001163 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001164 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001165 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001166 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001167 clear_tv(&var1);
1168 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001169 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001170 }
1171
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001172 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001173 ++p;
1174 }
1175
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001176 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001177 {
1178 if (len == -1)
1179 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001180 // "[key]": get key from "var1"
1181 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001182 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001183 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001184 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001185 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001186 }
1187 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001188 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001189
1190 // a NULL dict is equivalent with an empty dict
1191 if (lp->ll_tv->vval.v_dict == NULL)
1192 {
1193 lp->ll_tv->vval.v_dict = dict_alloc();
1194 if (lp->ll_tv->vval.v_dict == NULL)
1195 {
1196 clear_tv(&var1);
1197 return NULL;
1198 }
1199 ++lp->ll_tv->vval.v_dict->dv_refcount;
1200 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001201 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001202
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001203 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001204
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001205 // When assigning to a scope dictionary check that a function and
1206 // variable name is valid (only variable name unless it is l: or
1207 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001208 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001209 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001210 int prevval;
1211 int wrong;
1212
1213 if (len != -1)
1214 {
1215 prevval = key[len];
1216 key[len] = NUL;
1217 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001218 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001219 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001220 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1221 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001222 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001223 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001224 if (len != -1)
1225 key[len] = prevval;
1226 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001227 {
1228 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001229 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001230 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001231 }
1232
Bram Moolenaar10c65862020-10-08 21:16:42 +02001233 if (lp->ll_valtype != NULL)
1234 // use the type of the member
1235 lp->ll_valtype = lp->ll_valtype->tt_member;
1236
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001237 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001238 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001239 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001240 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001241 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001242 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001243 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001244 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001245 return NULL;
1246 }
1247
Bram Moolenaar31b81602019-02-10 22:14:27 +01001248 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001249 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001250 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001251 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001252 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001253 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001254 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001255 }
1256 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001257 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001258 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001259 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001260 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001261 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001262 p = NULL;
1263 break;
1264 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001265 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001266 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001267 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1268 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001269 {
1270 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001271 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001272 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001273
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001274 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001275 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001276 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001277 else if (lp->ll_tv->v_type == VAR_BLOB)
1278 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001279 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1280
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001281 /*
1282 * Get the number and item for the only or first index of the List.
1283 */
1284 if (empty1)
1285 lp->ll_n1 = 0;
1286 else
1287 // is number or string
1288 lp->ll_n1 = (long)tv_get_number(&var1);
1289 clear_tv(&var1);
1290
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001291 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001292 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001293 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001294 return NULL;
1295 }
1296 if (lp->ll_range && !lp->ll_empty2)
1297 {
1298 lp->ll_n2 = (long)tv_get_number(&var2);
1299 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001300 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1301 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001302 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001303 }
1304 lp->ll_blob = lp->ll_tv->vval.v_blob;
1305 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001306 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001307 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001308 else
1309 {
1310 /*
1311 * Get the number and item for the only or first index of the List.
1312 */
1313 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001314 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001315 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001316 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001317 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001318 clear_tv(&var1);
1319
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001320 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001321 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001322 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1323 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001324 if (lp->ll_li == NULL)
1325 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001326 clear_tv(&var2);
1327 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001328 }
1329
Bram Moolenaar10c65862020-10-08 21:16:42 +02001330 if (lp->ll_valtype != NULL)
1331 // use the type of the member
1332 lp->ll_valtype = lp->ll_valtype->tt_member;
1333
Bram Moolenaar8c711452005-01-14 21:53:12 +00001334 /*
1335 * May need to find the item or absolute index for the second
1336 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001337 * When no index given: "lp->ll_empty2" is TRUE.
1338 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001339 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001340 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001341 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001342 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001343 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001344 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001345 if (check_range_index_two(lp->ll_list,
1346 &lp->ll_n1, lp->ll_li,
1347 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001348 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001349 }
1350
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001351 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001352 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001353 }
1354
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001355 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001357 return p;
1358}
1359
1360/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001361 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001362 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001363 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001364clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001365{
1366 vim_free(lp->ll_exp_name);
1367 vim_free(lp->ll_newkey);
1368}
1369
1370/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001371 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001372 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001373 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1374 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001375 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001376 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001377set_var_lval(
1378 lval_T *lp,
1379 char_u *endp,
1380 typval_T *rettv,
1381 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001382 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1383 char_u *op,
1384 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001385{
1386 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001387 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001388
1389 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001390 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001391 cc = *endp;
1392 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001393 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1394 return;
1395
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001396 if (lp->ll_blob != NULL)
1397 {
1398 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001399
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001400 if (op != NULL && *op != '=')
1401 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001402 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001403 return;
1404 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001405 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001406 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001407
1408 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1409 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001410 if (lp->ll_empty2)
1411 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1412
Bram Moolenaar68452172021-04-12 21:21:02 +02001413 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1414 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001415 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001416 }
1417 else
1418 {
1419 val = (int)tv_get_number_chk(rettv, &error);
1420 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001421 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001422 }
1423 }
1424 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001425 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001426 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001427
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001428 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1429 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001430 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001431 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001432 *endp = cc;
1433 return;
1434 }
1435
Bram Moolenaarff697e62019-02-12 22:28:33 +01001436 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001437 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001438 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001439 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001440 {
1441 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001442 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1443 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001444 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001445 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001446 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001447 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001448 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001449 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001450 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001451 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001452 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1453 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001454 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001455 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001456 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001457 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001458 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001459 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001460 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001461 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001462 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001463 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001464 else if (lp->ll_range)
1465 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001466 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1467 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001468 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001469 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001470 return;
1471 }
1472
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001473 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1474 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001475 }
1476 else
1477 {
1478 /*
1479 * Assign to a List or Dictionary item.
1480 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001481 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1482 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001483 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001484 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001485 return;
1486 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001487
1488 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001489 && check_typval_arg_type(lp->ll_valtype, rettv,
1490 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001491 return;
1492
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001493 if (lp->ll_newkey != NULL)
1494 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001495 if (op != NULL && *op != '=')
1496 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001497 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001498 return;
1499 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001500 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1501 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001502 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001503
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001504 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001505 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001506 if (di == NULL)
1507 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001508 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1509 {
1510 vim_free(di);
1511 return;
1512 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001513 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001514 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001515 else if (op != NULL && *op != '=')
1516 {
1517 tv_op(lp->ll_tv, rettv, op);
1518 return;
1519 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001520 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001521 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001522
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001523 /*
1524 * Assign the value to the variable or list item.
1525 */
1526 if (copy)
1527 copy_tv(rettv, lp->ll_tv);
1528 else
1529 {
1530 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001531 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001532 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001533 }
1534 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001535}
1536
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001537/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001538 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1539 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001540 * Returns OK or FAIL.
1541 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001542 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001543tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001544{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001545 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001546 char_u numbuf[NUMBUFLEN];
1547 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001548 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001549
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001550 // Can't do anything with a Funcref or Dict on the right.
1551 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001552 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001553 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1554 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001555 {
1556 switch (tv1->v_type)
1557 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001558 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001559 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001561 case VAR_DICT:
1562 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001563 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001564 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001565 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001566 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001567 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001568 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001569 break;
1570
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001571 case VAR_BLOB:
1572 if (*op != '+' || tv2->v_type != VAR_BLOB)
1573 break;
1574 // BLOB += BLOB
1575 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1576 {
1577 blob_T *b1 = tv1->vval.v_blob;
1578 blob_T *b2 = tv2->vval.v_blob;
1579 int i, len = blob_len(b2);
1580 for (i = 0; i < len; i++)
1581 ga_append(&b1->bv_ga, blob_get(b2, i));
1582 }
1583 return OK;
1584
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001585 case VAR_LIST:
1586 if (*op != '+' || tv2->v_type != VAR_LIST)
1587 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001588 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001589 if (tv2->vval.v_list != NULL)
1590 {
1591 if (tv1->vval.v_list == NULL)
1592 {
1593 tv1->vval.v_list = tv2->vval.v_list;
1594 ++tv1->vval.v_list->lv_refcount;
1595 }
1596 else
1597 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1598 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001599 return OK;
1600
1601 case VAR_NUMBER:
1602 case VAR_STRING:
1603 if (tv2->v_type == VAR_LIST)
1604 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001605 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001606 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001607 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001608 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001609#ifdef FEAT_FLOAT
1610 if (tv2->v_type == VAR_FLOAT)
1611 {
1612 float_T f = n;
1613
Bram Moolenaarff697e62019-02-12 22:28:33 +01001614 if (*op == '%')
1615 break;
1616 switch (*op)
1617 {
1618 case '+': f += tv2->vval.v_float; break;
1619 case '-': f -= tv2->vval.v_float; break;
1620 case '*': f *= tv2->vval.v_float; break;
1621 case '/': f /= tv2->vval.v_float; break;
1622 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001623 clear_tv(tv1);
1624 tv1->v_type = VAR_FLOAT;
1625 tv1->vval.v_float = f;
1626 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001627 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001628#endif
1629 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001630 switch (*op)
1631 {
1632 case '+': n += tv_get_number(tv2); break;
1633 case '-': n -= tv_get_number(tv2); break;
1634 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001635 case '/': n = num_divide(n, tv_get_number(tv2),
1636 &failed); break;
1637 case '%': n = num_modulus(n, tv_get_number(tv2),
1638 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001639 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001640 clear_tv(tv1);
1641 tv1->v_type = VAR_NUMBER;
1642 tv1->vval.v_number = n;
1643 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001644 }
1645 else
1646 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001647 if (tv2->v_type == VAR_FLOAT)
1648 break;
1649
Bram Moolenaarff697e62019-02-12 22:28:33 +01001650 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001651 s = tv_get_string(tv1);
1652 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001653 clear_tv(tv1);
1654 tv1->v_type = VAR_STRING;
1655 tv1->vval.v_string = s;
1656 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001657 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001658
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001659 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001660#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001661 {
1662 float_T f;
1663
Bram Moolenaarff697e62019-02-12 22:28:33 +01001664 if (*op == '%' || *op == '.'
1665 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001666 && tv2->v_type != VAR_NUMBER
1667 && tv2->v_type != VAR_STRING))
1668 break;
1669 if (tv2->v_type == VAR_FLOAT)
1670 f = tv2->vval.v_float;
1671 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001672 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001673 switch (*op)
1674 {
1675 case '+': tv1->vval.v_float += f; break;
1676 case '-': tv1->vval.v_float -= f; break;
1677 case '*': tv1->vval.v_float *= f; break;
1678 case '/': tv1->vval.v_float /= f; break;
1679 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001680 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001681#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001682 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001683 }
1684 }
1685
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001686 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001687 return FAIL;
1688}
1689
1690/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001691 * Evaluate the expression used in a ":for var in expr" command.
1692 * "arg" points to "var".
1693 * Set "*errp" to TRUE for an error, FALSE otherwise;
1694 * Return a pointer that holds the info. Null when there is an error.
1695 */
1696 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001697eval_for_line(
1698 char_u *arg,
1699 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001700 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001701 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001702{
Bram Moolenaar33570922005-01-25 22:26:29 +00001703 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001704 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001705 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001706 typval_T tv;
1707 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001708 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001709
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001710 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001711
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001712 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001713 if (fi == NULL)
1714 return NULL;
1715
Bram Moolenaar404557e2021-07-05 21:41:48 +02001716 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1717 &fi->fi_semicolon, FALSE);
1718 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001719 return fi;
1720
Bram Moolenaar404557e2021-07-05 21:41:48 +02001721 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001722 if (expr[0] != 'i' || expr[1] != 'n'
1723 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001724 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001725 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1726 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1727 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001728 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001729 return fi;
1730 }
1731
1732 if (skip)
1733 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001734 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1735 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001736 {
1737 *errp = FALSE;
1738 if (!skip)
1739 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001740 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001741 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001742 l = tv.vval.v_list;
1743 if (l == NULL)
1744 {
1745 // a null list is like an empty list: do nothing
1746 clear_tv(&tv);
1747 }
1748 else
1749 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001751 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001753 // No need to increment the refcount, it's already set for
1754 // the list being used in "tv".
1755 fi->fi_list = l;
1756 list_add_watch(l, &fi->fi_lw);
1757 fi->fi_lw.lw_item = l->lv_first;
1758 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001759 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001760 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001761 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001762 fi->fi_bi = 0;
1763 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001764 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001765 typval_T btv;
1766
1767 // Make a copy, so that the iteration still works when the
1768 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001769 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001770 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001771 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001772 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001773 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001774 else if (tv.v_type == VAR_STRING)
1775 {
1776 fi->fi_byte_idx = 0;
1777 fi->fi_string = tv.vval.v_string;
1778 tv.vval.v_string = NULL;
1779 if (fi->fi_string == NULL)
1780 fi->fi_string = vim_strsave((char_u *)"");
1781 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001782 else
1783 {
Sean Dewar80d73952021-08-04 19:25:54 +02001784 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001785 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001786 }
1787 }
1788 }
1789 if (skip)
1790 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001791 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001792
1793 return fi;
1794}
1795
1796/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001797 * Used when looping over a :for line, skip the "in expr" part.
1798 */
1799 void
1800skip_for_lines(void *fi_void, evalarg_T *evalarg)
1801{
1802 forinfo_T *fi = (forinfo_T *)fi_void;
1803 int i;
1804
1805 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01001806 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001807}
1808
1809/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001810 * Use the first item in a ":for" list. Advance to the next.
1811 * Assign the values to the variable (list). "arg" points to the first one.
1812 * Return TRUE when a valid item was found, FALSE when at end of list or
1813 * something wrong.
1814 */
1815 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001816next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001817{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001818 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001819 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001820 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001821 ? (ASSIGN_FINAL
1822 // first round: error if variable exists
1823 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1824 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001825 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001826 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001827 int skip_assign = in_vim9script() && arg[0] == '_'
1828 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001829
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001830 if (fi->fi_blob != NULL)
1831 {
1832 typval_T tv;
1833
1834 if (fi->fi_bi >= blob_len(fi->fi_blob))
1835 return FALSE;
1836 tv.v_type = VAR_NUMBER;
1837 tv.v_lock = VAR_FIXED;
1838 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1839 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001840 if (skip_assign)
1841 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001842 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001843 fi->fi_varcount, flag, NULL) == OK;
1844 }
1845
1846 if (fi->fi_string != NULL)
1847 {
1848 typval_T tv;
1849 int len;
1850
1851 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1852 if (len == 0)
1853 return FALSE;
1854 tv.v_type = VAR_STRING;
1855 tv.v_lock = VAR_FIXED;
1856 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1857 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001858 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001859 if (skip_assign)
1860 result = TRUE;
1861 else
1862 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001863 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001864 vim_free(tv.vval.v_string);
1865 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001866 }
1867
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001868 item = fi->fi_lw.lw_item;
1869 if (item == NULL)
1870 result = FALSE;
1871 else
1872 {
1873 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001874 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001875 if (skip_assign)
1876 result = TRUE;
1877 else
1878 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001879 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001880 }
1881 return result;
1882}
1883
1884/*
1885 * Free the structure used to store info used by ":for".
1886 */
1887 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001888free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001889{
Bram Moolenaar33570922005-01-25 22:26:29 +00001890 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001891
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001892 if (fi == NULL)
1893 return;
1894 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001895 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001896 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001897 list_unref(fi->fi_list);
1898 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001899 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001900 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001901 else
1902 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001903 vim_free(fi);
1904}
1905
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001907set_context_for_expression(
1908 expand_T *xp,
1909 char_u *arg,
1910 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001912 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001914 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001916 if (cmdidx == CMD_let || cmdidx == CMD_var
1917 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001918 {
1919 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001920 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001922 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001923 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001924 {
1925 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001926 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001927 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928 break;
1929 }
1930 return;
1931 }
1932 }
1933 else
1934 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1935 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 while ((xp->xp_pattern = vim_strpbrk(arg,
1937 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1938 {
1939 c = *xp->xp_pattern;
1940 if (c == '&')
1941 {
1942 c = xp->xp_pattern[1];
1943 if (c == '&')
1944 {
1945 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001946 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 }
1948 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001949 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001951 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1952 xp->xp_pattern += 2;
1953
1954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 }
1956 else if (c == '$')
1957 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001958 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 xp->xp_context = EXPAND_ENV_VARS;
1960 }
1961 else if (c == '=')
1962 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001963 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 xp->xp_context = EXPAND_EXPRESSION;
1965 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001966 else if (c == '#'
1967 && xp->xp_context == EXPAND_EXPRESSION)
1968 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001969 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001970 break;
1971 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001972 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 && xp->xp_context == EXPAND_FUNCTIONS
1974 && vim_strchr(xp->xp_pattern, '(') == NULL)
1975 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001976 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 break;
1978 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001979 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001981 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 {
1983 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1984 if (c == '\\' && xp->xp_pattern[1] != NUL)
1985 ++xp->xp_pattern;
1986 xp->xp_context = EXPAND_NOTHING;
1987 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001988 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001990 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1992 /* skip */ ;
1993 xp->xp_context = EXPAND_NOTHING;
1994 }
1995 else if (c == '|')
1996 {
1997 if (xp->xp_pattern[1] == '|')
1998 {
1999 ++xp->xp_pattern;
2000 xp->xp_context = EXPAND_EXPRESSION;
2001 }
2002 else
2003 xp->xp_context = EXPAND_COMMANDS;
2004 }
2005 else
2006 xp->xp_context = EXPAND_EXPRESSION;
2007 }
2008 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002009 // Doesn't look like something valid, expand as an expression
2010 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002011 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 arg = xp->xp_pattern;
2013 if (*arg != NUL)
2014 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2015 /* skip */ ;
2016 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002017
2018 // ":exe one two" completes "two"
2019 if ((cmdidx == CMD_execute
2020 || cmdidx == CMD_echo
2021 || cmdidx == CMD_echon
2022 || cmdidx == CMD_echomsg)
2023 && xp->xp_context == EXPAND_EXPRESSION)
2024 {
2025 for (;;)
2026 {
2027 char_u *n = skiptowhite(arg);
2028
2029 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2030 break;
2031 arg = skipwhite(n);
2032 }
2033 }
2034
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 xp->xp_pattern = arg;
2036}
2037
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002039 * Return TRUE if "pat" matches "text".
2040 * Does not use 'cpo' and always uses 'magic'.
2041 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002042 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002043pattern_match(char_u *pat, char_u *text, int ic)
2044{
2045 int matches = FALSE;
2046 char_u *save_cpo;
2047 regmatch_T regmatch;
2048
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002049 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002050 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002051 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002052 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2053 if (regmatch.regprog != NULL)
2054 {
2055 regmatch.rm_ic = ic;
2056 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2057 vim_regfree(regmatch.regprog);
2058 }
2059 p_cpo = save_cpo;
2060 return matches;
2061}
2062
2063/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002064 * Handle a name followed by "(". Both for just "name(arg)" and for
2065 * "expr->name(arg)".
2066 * Returns OK or FAIL.
2067 */
2068 static int
2069eval_func(
2070 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002071 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002072 char_u *name,
2073 int name_len,
2074 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002075 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002076 typval_T *basetv) // "expr" for "expr->name(arg)"
2077{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002078 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002079 char_u *s = name;
2080 int len = name_len;
2081 partial_T *partial;
2082 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002083 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002084 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002085
2086 if (!evaluate)
2087 check_vars(s, len);
2088
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002089 // If "s" is the name of a variable of type VAR_FUNC
2090 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002091 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002092 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002093
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002094 // Need to make a copy, in case evaluating the arguments makes
2095 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002096 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002097 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002098 ret = FAIL;
2099 else
2100 {
2101 funcexe_T funcexe;
2102
2103 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002104 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002105 funcexe.fe_firstline = curwin->w_cursor.lnum;
2106 funcexe.fe_lastline = curwin->w_cursor.lnum;
2107 funcexe.fe_evaluate = evaluate;
2108 funcexe.fe_partial = partial;
2109 funcexe.fe_basetv = basetv;
2110 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002111 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002112 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002113 }
2114 vim_free(s);
2115
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002116 // If evaluate is FALSE rettv->v_type was not set in
2117 // get_func_tv, but it's needed in handle_subscript() to parse
2118 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002119 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2120 {
2121 rettv->vval.v_string = NULL;
2122 rettv->v_type = VAR_FUNC;
2123 }
2124
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002125 // Stop the expression evaluation when immediately
2126 // aborting on error, or when an interrupt occurred or
2127 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002128 if (evaluate && aborting())
2129 {
2130 if (ret == OK)
2131 clear_tv(rettv);
2132 ret = FAIL;
2133 }
2134 return ret;
2135}
2136
2137/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002138 * Get the next line source line without advancing. But do skip over comment
2139 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002140 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002141 */
2142 static char_u *
2143getline_peek_skip_comments(evalarg_T *evalarg)
2144{
2145 for (;;)
2146 {
2147 char_u *next = getline_peek(evalarg->eval_getline,
2148 evalarg->eval_cookie);
2149 char_u *p;
2150
2151 if (next == NULL)
2152 break;
2153 p = skipwhite(next);
2154 if (*p != NUL && !vim9_comment_start(p))
2155 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002156 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002157 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002158 }
2159 return NULL;
2160}
2161
2162/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002163 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2164 * comment) and there is a next line, return the next line (skipping blanks)
2165 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002166 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2167 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002168 * "arg" must point somewhere inside a line, not at the start.
2169 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002170 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002171eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2172{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002173 char_u *p = skipwhite(arg);
2174
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002175 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002176 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002177 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002178 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2179 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002180 && (*p == NUL || *p == NL
2181 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002182 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002183 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002184
Bram Moolenaare442d592022-05-05 12:20:28 +01002185 if (*p == NL)
2186 next = p + 1;
2187 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002188 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002189 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002190 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002191
Bram Moolenaar9d489562020-07-30 20:08:50 +02002192 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002193 {
2194 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002195 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002196 }
2197 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002198 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002199}
2200
2201/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002202 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002203 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002204 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002205 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002206eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002207{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002208 garray_T *gap = &evalarg->eval_ga;
2209 char_u *line;
2210
Bram Moolenaar39be4982022-05-06 21:51:50 +01002211 if (arg != NULL)
2212 {
2213 if (*arg == NL)
2214 return skipwhite(arg + 1);
2215 // Truncate before a trailing comment, so that concatenating the lines
2216 // won't turn the rest into a comment.
2217 if (*skipwhite(arg) == '#')
2218 *arg = NUL;
2219 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002220
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002221 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002222 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2223 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002224 else
2225 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002226 if (line == NULL)
2227 return NULL;
2228
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002229 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002230 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2231 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002232 char_u *p = skipwhite(line);
2233
2234 // Going to concatenate the lines after parsing. For an empty or
2235 // comment line use an empty string.
2236 if (*p == NUL || vim9_comment_start(p))
2237 {
2238 vim_free(line);
2239 line = vim_strsave((char_u *)"");
2240 }
2241
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002242 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2243 ++gap->ga_len;
2244 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002245 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002246 {
2247 vim_free(evalarg->eval_tofree);
2248 evalarg->eval_tofree = line;
2249 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002250
2251 // Advanced to the next line, "arg" no longer points into the previous
2252 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002253 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002254 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002255}
2256
Bram Moolenaare6b53242020-07-01 17:28:33 +02002257/*
2258 * Call eval_next_non_blank() and get the next line if needed.
2259 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002260 char_u *
2261skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2262{
2263 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002264 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002265
Bram Moolenaar962d7212020-07-04 14:15:00 +02002266 if (evalarg == NULL)
2267 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002268 eval_next_non_blank(p, evalarg, &getnext);
2269 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002270 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002271 return p;
2272}
2273
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002274/*
Bram Moolenaar844fb642021-10-23 13:32:30 +01002275 * Initialize "evalarg" for use.
2276 */
2277 void
2278init_evalarg(evalarg_T *evalarg)
2279{
2280 CLEAR_POINTER(evalarg);
2281 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
2282}
2283
2284/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002285 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002286 */
2287 void
2288clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2289{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002290 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002291 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002292 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002293 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002294 if (eap != NULL)
2295 {
2296 // We may need to keep the original command line, e.g. for
2297 // ":let" it has the variable names. But we may also need the
2298 // new one, "nextcmd" points into it. Keep both.
2299 vim_free(eap->cmdline_tofree);
2300 eap->cmdline_tofree = *eap->cmdlinep;
2301 *eap->cmdlinep = evalarg->eval_tofree;
2302 }
2303 else
2304 vim_free(evalarg->eval_tofree);
2305 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002306 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002307
Bram Moolenaar844fb642021-10-23 13:32:30 +01002308 ga_clear_strings(&evalarg->eval_tofree_ga);
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002309 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002310 }
2311}
2312
2313/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2317 */
2318
2319/*
2320 * Handle zero level expression.
2321 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002322 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002323 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002324 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 * Return OK or FAIL.
2326 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002327 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002328eval0(
2329 char_u *arg,
2330 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002331 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002332 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002334 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2335}
2336
2337/*
2338 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2339 * expression and don't check what comes after the expression.
2340 */
2341 int
2342eval0_retarg(
2343 char_u *arg,
2344 typval_T *rettv,
2345 exarg_T *eap,
2346 evalarg_T *evalarg,
2347 char_u **retarg)
2348{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 int ret;
2350 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002351 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002352 int did_emsg_before = did_emsg;
2353 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002354 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002355 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002356 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357
2358 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002359 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002360 expr_end = p;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002361 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002362
Bram Moolenaar02929a32021-12-17 14:46:12 +00002363 // In Vim9 script a command block is not split at NL characters for
2364 // commands using an expression argument. Skip over a '#' comment to check
2365 // for a following NL. Require white space before the '#'.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002366 if (in_vim9script() && p > expr_end && retarg == NULL)
Bram Moolenaar02929a32021-12-17 14:46:12 +00002367 while (*p == '#')
2368 {
2369 char_u *nl = vim_strchr(p, NL);
2370
2371 if (nl == NULL)
2372 break;
2373 p = skipwhite(nl + 1);
2374 if (eap != NULL && *p != NUL)
2375 eap->nextcmd = p;
2376 check_for_end = FALSE;
2377 }
2378
2379 if (ret != FAIL && check_for_end)
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002380 end_error = !ends_excmd2(arg, p);
2381 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 {
2383 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002384 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 /*
2386 * Report the invalid expression unless the expression evaluation has
2387 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002388 * exception, or we already gave a more specific error.
2389 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002391 if (!aborting()
2392 && did_emsg == did_emsg_before
2393 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002394 && (flags & EVAL_CONSTANT) == 0
2395 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002396 {
2397 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002398 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002399 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002400 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002401 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002402
2403 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002404 // a next command to avoid more errors, unless "|" is following, which
2405 // could only be a command separator.
2406 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2407 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002408 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002410
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002411 if (retarg != NULL)
2412 *retarg = p;
2413 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002414 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002415
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 return ret;
2417}
2418
2419/*
2420 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002421 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002422 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 *
2424 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002425 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002427 * Note: "rettv.v_lock" is not set.
2428 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 * Return OK or FAIL.
2430 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002431 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002432eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002434 char_u *p;
2435 int getnext;
2436
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002437 CLEAR_POINTER(rettv);
2438
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 /*
2440 * Get the first variable.
2441 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002442 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 return FAIL;
2444
Bram Moolenaar793648f2020-06-26 21:28:25 +02002445 p = eval_next_non_blank(*arg, evalarg, &getnext);
2446 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002448 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002449 int result;
2450 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002451 evalarg_T *evalarg_used = evalarg;
2452 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002453 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002454 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002455 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002456
2457 if (evalarg == NULL)
2458 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002459 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002460 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002461 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002462 orig_flags = evalarg_used->eval_flags;
2463 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002464
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002465 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002466 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002467 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002468 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002469 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002470 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002471 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002472 clear_tv(rettv);
2473 return FAIL;
2474 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002475 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002476 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002477
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002479 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002481 int error = FALSE;
2482
Bram Moolenaar13106602020-10-04 16:06:05 +02002483 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002484 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002485 else if (vim9script)
2486 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002487 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002489 if (error || !op_falsy || !result)
2490 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002491 if (error)
2492 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 }
2494
2495 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002496 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002498 if (op_falsy)
2499 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002500 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002501 {
mityu4ac198c2021-05-28 17:52:40 +02002502 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002503 clear_tv(rettv);
2504 return FAIL;
2505 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002506 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002507 evalarg_used->eval_flags = (op_falsy ? !result : result)
2508 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2509 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002510 {
2511 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002513 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002514 if (!op_falsy || !result)
2515 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002517 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002519 /*
2520 * Check for the ":".
2521 */
2522 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2523 if (*p != ':')
2524 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002525 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002526 if (evaluate && result)
2527 clear_tv(rettv);
2528 evalarg_used->eval_flags = orig_flags;
2529 return FAIL;
2530 }
2531 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002532 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002533 else
2534 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002535 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002536 {
2537 error_white_both(p, 1);
2538 clear_tv(rettv);
2539 evalarg_used->eval_flags = orig_flags;
2540 return FAIL;
2541 }
2542 *arg = p;
2543 }
2544
2545 /*
2546 * Get the third variable. Recursive!
2547 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002548 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002549 {
mityu4ac198c2021-05-28 17:52:40 +02002550 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002551 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002552 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002553 return FAIL;
2554 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002555 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2556 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002557 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002558 if (eval1(arg, &var2, evalarg_used) == FAIL)
2559 {
2560 if (evaluate && result)
2561 clear_tv(rettv);
2562 evalarg_used->eval_flags = orig_flags;
2563 return FAIL;
2564 }
2565 if (evaluate && !result)
2566 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002568
2569 if (evalarg == NULL)
2570 clear_evalarg(&local_evalarg, NULL);
2571 else
2572 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 }
2574
2575 return OK;
2576}
2577
2578/*
2579 * Handle first level expression:
2580 * expr2 || expr2 || expr2 logical OR
2581 *
2582 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002583 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 *
2585 * Return OK or FAIL.
2586 */
2587 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002588eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002590 char_u *p;
2591 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592
2593 /*
2594 * Get the first variable.
2595 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002596 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 return FAIL;
2598
2599 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002600 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002602 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002603 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002605 evalarg_T *evalarg_used = evalarg;
2606 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002607 int evaluate;
2608 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002609 long result = FALSE;
2610 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002611 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002612 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002613
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002614 if (evalarg == NULL)
2615 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002616 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002617 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002618 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002619 orig_flags = evalarg_used->eval_flags;
2620 evaluate = orig_flags & EVAL_EVALUATE;
2621 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002622 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002623 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002624 result = tv_get_bool_chk(rettv, &error);
2625 else if (tv_get_number_chk(rettv, &error) != 0)
2626 result = TRUE;
2627 clear_tv(rettv);
2628 if (error)
2629 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 }
2631
2632 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002633 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002635 while (p[0] == '|' && p[1] == '|')
2636 {
2637 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002638 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002639 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002640 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002641 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002642 {
2643 error_white_both(p, 2);
2644 clear_tv(rettv);
2645 return FAIL;
2646 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002647 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002648 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002649
2650 /*
2651 * Get the second variable.
2652 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002653 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002654 {
mityu4ac198c2021-05-28 17:52:40 +02002655 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002656 clear_tv(rettv);
2657 return FAIL;
2658 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002659 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2660 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002661 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002662 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002663 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002664
2665 /*
2666 * Compute the result.
2667 */
2668 if (evaluate && !result)
2669 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002670 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002671 result = tv_get_bool_chk(&var2, &error);
2672 else if (tv_get_number_chk(&var2, &error) != 0)
2673 result = TRUE;
2674 clear_tv(&var2);
2675 if (error)
2676 return FAIL;
2677 }
2678 if (evaluate)
2679 {
2680 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002681 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002682 rettv->v_type = VAR_BOOL;
2683 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002684 }
2685 else
2686 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002687 rettv->v_type = VAR_NUMBER;
2688 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002689 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002690 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002691
2692 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002694
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002695 if (evalarg == NULL)
2696 clear_evalarg(&local_evalarg, NULL);
2697 else
2698 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 }
2700
2701 return OK;
2702}
2703
2704/*
2705 * Handle second level expression:
2706 * expr3 && expr3 && expr3 logical AND
2707 *
2708 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002709 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 *
2711 * Return OK or FAIL.
2712 */
2713 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002714eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002716 char_u *p;
2717 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718
2719 /*
2720 * Get the first variable.
2721 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002722 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 return FAIL;
2724
2725 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002726 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002728 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002729 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002731 evalarg_T *evalarg_used = evalarg;
2732 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002733 int orig_flags;
2734 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002735 long result = TRUE;
2736 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002737 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002738 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002739
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002740 if (evalarg == NULL)
2741 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002742 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002743 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002744 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002745 orig_flags = evalarg_used->eval_flags;
2746 evaluate = orig_flags & EVAL_EVALUATE;
2747 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002748 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002749 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002750 result = tv_get_bool_chk(rettv, &error);
2751 else if (tv_get_number_chk(rettv, &error) == 0)
2752 result = FALSE;
2753 clear_tv(rettv);
2754 if (error)
2755 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 }
2757
2758 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002759 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002761 while (p[0] == '&' && p[1] == '&')
2762 {
2763 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002764 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002765 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002766 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002767 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002768 {
2769 error_white_both(p, 2);
2770 clear_tv(rettv);
2771 return FAIL;
2772 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002773 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002774 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002775
2776 /*
2777 * Get the second variable.
2778 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002779 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002780 {
mityu4ac198c2021-05-28 17:52:40 +02002781 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002782 clear_tv(rettv);
2783 return FAIL;
2784 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002785 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2786 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002787 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002788 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002789 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002790 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002791
2792 /*
2793 * Compute the result.
2794 */
2795 if (evaluate && result)
2796 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002797 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002798 result = tv_get_bool_chk(&var2, &error);
2799 else if (tv_get_number_chk(&var2, &error) == 0)
2800 result = FALSE;
2801 clear_tv(&var2);
2802 if (error)
2803 return FAIL;
2804 }
2805 if (evaluate)
2806 {
2807 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002808 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002809 rettv->v_type = VAR_BOOL;
2810 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002811 }
2812 else
2813 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002814 rettv->v_type = VAR_NUMBER;
2815 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002816 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002817 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002818
2819 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002821
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002822 if (evalarg == NULL)
2823 clear_evalarg(&local_evalarg, NULL);
2824 else
2825 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 }
2827
2828 return OK;
2829}
2830
2831/*
2832 * Handle third level expression:
2833 * var1 == var2
2834 * var1 =~ var2
2835 * var1 != var2
2836 * var1 !~ var2
2837 * var1 > var2
2838 * var1 >= var2
2839 * var1 < var2
2840 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002841 * var1 is var2
2842 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 *
2844 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002845 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 *
2847 * Return OK or FAIL.
2848 */
2849 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002850eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002853 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002854 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002856 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857
2858 /*
2859 * Get the first variable.
2860 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002861 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 return FAIL;
2863
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002864 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002865 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866
2867 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002868 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002870 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002872 typval_T var2;
2873 int ic;
2874 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002875 int evaluate = evalarg == NULL
2876 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002877 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002878
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002879 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002880 {
Bram Moolenaare442d592022-05-05 12:20:28 +01002881 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002882 p = *arg;
2883 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002884 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2885 {
mityu4ac198c2021-05-28 17:52:40 +02002886 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002887 clear_tv(rettv);
2888 return FAIL;
2889 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002890
Bram Moolenaar696ba232020-07-29 21:20:41 +02002891 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2892 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002893 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002894 clear_tv(rettv);
2895 return FAIL;
2896 }
2897
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002898 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 if (p[len] == '?')
2900 {
2901 ic = TRUE;
2902 ++len;
2903 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002904 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 else if (p[len] == '#')
2906 {
2907 ic = FALSE;
2908 ++len;
2909 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002910 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002912 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913
2914 /*
2915 * Get the second variable.
2916 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002917 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2918 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002919 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002920 clear_tv(rettv);
2921 return FAIL;
2922 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002923 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002924 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002926 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 return FAIL;
2928 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002929 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002930 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002931 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002932
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002933 // use the line of the comparison for messages
2934 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002935 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002936 {
2937 ret = FAIL;
2938 clear_tv(rettv);
2939 }
2940 else
2941 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002942 clear_tv(&var2);
2943 return ret;
2944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 }
2946
2947 return OK;
2948}
2949
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002950/*
2951 * Make a copy of blob "tv1" and append blob "tv2".
2952 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953 void
2954eval_addblob(typval_T *tv1, typval_T *tv2)
2955{
2956 blob_T *b1 = tv1->vval.v_blob;
2957 blob_T *b2 = tv2->vval.v_blob;
2958 blob_T *b = blob_alloc();
2959 int i;
2960
2961 if (b != NULL)
2962 {
2963 for (i = 0; i < blob_len(b1); i++)
2964 ga_append(&b->bv_ga, blob_get(b1, i));
2965 for (i = 0; i < blob_len(b2); i++)
2966 ga_append(&b->bv_ga, blob_get(b2, i));
2967
2968 clear_tv(tv1);
2969 rettv_blob_set(tv1, b);
2970 }
2971}
2972
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002973/*
2974 * Make a copy of list "tv1" and append list "tv2".
2975 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976 int
2977eval_addlist(typval_T *tv1, typval_T *tv2)
2978{
2979 typval_T var3;
2980
2981 // concatenate Lists
2982 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2983 {
2984 clear_tv(tv1);
2985 clear_tv(tv2);
2986 return FAIL;
2987 }
2988 clear_tv(tv1);
2989 *tv1 = var3;
2990 return OK;
2991}
2992
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993/*
2994 * Handle fourth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00002995 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002997 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002998 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 *
3000 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003001 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 *
3003 * Return OK or FAIL.
3004 */
3005 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003006eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 /*
3009 * Get the first variable.
3010 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003011 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 return FAIL;
3013
3014 /*
3015 * Repeat computing, until no '+', '-' or '.' is following.
3016 */
3017 for (;;)
3018 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003019 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003020 int getnext;
3021 char_u *p;
3022 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003023 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003024 int concat;
3025 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003026 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003027
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003028 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003029 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003030 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003031 p = eval_next_non_blank(*arg, evalarg, &getnext);
3032 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003033 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003034 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3035 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003037 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3038 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003039
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003040 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003041 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003042 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003043 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003044 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003045 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003046 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003047 {
mityu4ac198c2021-05-28 17:52:40 +02003048 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003049 clear_tv(rettv);
3050 return FAIL;
3051 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003052 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003053 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003054 if ((op != '+' || (rettv->v_type != VAR_LIST
3055 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003056#ifdef FEAT_FLOAT
3057 && (op == '.' || rettv->v_type != VAR_FLOAT)
3058#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003059 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003060 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003061 int error = FALSE;
3062
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003063 // For "list + ...", an illegal use of the first operand as
3064 // a number cannot be determined before evaluating the 2nd
3065 // operand: if this is also a list, all is ok.
3066 // For "something . ...", "something - ..." or "non-list + ...",
3067 // we know that the first operand needs to be a string or number
3068 // without evaluating the 2nd operand. So check before to avoid
3069 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003070 if (op != '.')
3071 tv_get_number_chk(rettv, &error);
3072 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003073 {
3074 clear_tv(rettv);
3075 return FAIL;
3076 }
3077 }
3078
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 /*
3080 * Get the second variable.
3081 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003082 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003083 {
mityu89dcb4d2021-05-28 13:50:17 +02003084 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003085 clear_tv(rettv);
3086 return FAIL;
3087 }
3088 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02003089 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003091 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 return FAIL;
3093 }
3094
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003095 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 {
3097 /*
3098 * Compute the result.
3099 */
3100 if (op == '.')
3101 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003102 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3103 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003104 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003105
Bram Moolenaar2e086612020-08-25 22:37:48 +02003106 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003107 || var2.v_type == VAR_CHANNEL
3108 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003109 semsg(_(e_using_invalid_value_as_string_str),
3110 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003111#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003112 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003113 {
3114 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3115 var2.vval.v_float);
3116 s2 = buf2;
3117 }
3118#endif
3119 else
3120 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003121 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003122 {
3123 clear_tv(rettv);
3124 clear_tv(&var2);
3125 return FAIL;
3126 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003127 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003128 clear_tv(rettv);
3129 rettv->v_type = VAR_STRING;
3130 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003132 else if (op == '+' && rettv->v_type == VAR_BLOB
3133 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003135 else if (op == '+' && rettv->v_type == VAR_LIST
3136 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003137 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003139 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 else
3142 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003143 int error = FALSE;
3144 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003145#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003146 float_T f1 = 0, f2 = 0;
3147
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003148 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003149 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003150 f1 = rettv->vval.v_float;
3151 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003152 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003153 else
3154#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003155 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003156 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003157 if (error)
3158 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003159 // This can only happen for "list + non-list" or
3160 // "blob + non-blob". For "non-list + ..." or
3161 // "something - ...", we returned before evaluating the
3162 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003163 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003164 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003165 return FAIL;
3166 }
3167#ifdef FEAT_FLOAT
3168 if (var2.v_type == VAR_FLOAT)
3169 f1 = n1;
3170#endif
3171 }
3172#ifdef FEAT_FLOAT
3173 if (var2.v_type == VAR_FLOAT)
3174 {
3175 f2 = var2.vval.v_float;
3176 n2 = 0;
3177 }
3178 else
3179#endif
3180 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003181 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003182 if (error)
3183 {
3184 clear_tv(rettv);
3185 clear_tv(&var2);
3186 return FAIL;
3187 }
3188#ifdef FEAT_FLOAT
3189 if (rettv->v_type == VAR_FLOAT)
3190 f2 = n2;
3191#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003192 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003193 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003194
3195#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003196 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003197 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3198 {
3199 if (op == '+')
3200 f1 = f1 + f2;
3201 else
3202 f1 = f1 - f2;
3203 rettv->v_type = VAR_FLOAT;
3204 rettv->vval.v_float = f1;
3205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003207#endif
3208 {
3209 if (op == '+')
3210 n1 = n1 + n2;
3211 else
3212 n1 = n1 - n2;
3213 rettv->v_type = VAR_NUMBER;
3214 rettv->vval.v_number = n1;
3215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003217 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 }
3219 }
3220 return OK;
3221}
3222
3223/*
3224 * Handle fifth level expression:
3225 * * number multiplication
3226 * / number division
3227 * % number modulo
3228 *
3229 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003230 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 *
3232 * Return OK or FAIL.
3233 */
3234 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003235eval6(
3236 char_u **arg,
3237 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003238 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003239 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003241#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003242 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244
3245 /*
3246 * Get the first variable.
3247 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003248 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 return FAIL;
3250
3251 /*
3252 * Repeat computing, until no '*', '/' or '%' is following.
3253 */
3254 for (;;)
3255 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003256 int evaluate;
3257 int getnext;
3258 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003259 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003260 int op;
3261 varnumber_T n1, n2;
3262#ifdef FEAT_FLOAT
3263 float_T f1, f2;
3264#endif
3265 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003266
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003267 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003268 p = eval_next_non_blank(*arg, evalarg, &getnext);
3269 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003270 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003272
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003273 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003274 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003275 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003276 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003277 {
3278 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3279 {
mityu4ac198c2021-05-28 17:52:40 +02003280 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003281 clear_tv(rettv);
3282 return FAIL;
3283 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003284 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003287#ifdef FEAT_FLOAT
3288 f1 = 0;
3289 f2 = 0;
3290#endif
3291 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003292 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003294#ifdef FEAT_FLOAT
3295 if (rettv->v_type == VAR_FLOAT)
3296 {
3297 f1 = rettv->vval.v_float;
3298 use_float = TRUE;
3299 n1 = 0;
3300 }
3301 else
3302#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003303 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003304 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003305 if (error)
3306 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 }
3308 else
3309 n1 = 0;
3310
3311 /*
3312 * Get the second variable.
3313 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003314 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3315 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003316 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003317 clear_tv(rettv);
3318 return FAIL;
3319 }
3320 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003321 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 return FAIL;
3323
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003324 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003326#ifdef FEAT_FLOAT
3327 if (var2.v_type == VAR_FLOAT)
3328 {
3329 if (!use_float)
3330 {
3331 f1 = n1;
3332 use_float = TRUE;
3333 }
3334 f2 = var2.vval.v_float;
3335 n2 = 0;
3336 }
3337 else
3338#endif
3339 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003340 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003341 clear_tv(&var2);
3342 if (error)
3343 return FAIL;
3344#ifdef FEAT_FLOAT
3345 if (use_float)
3346 f2 = n2;
3347#endif
3348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349
3350 /*
3351 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003352 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003354#ifdef FEAT_FLOAT
3355 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003357 if (op == '*')
3358 f1 = f1 * f2;
3359 else if (op == '/')
3360 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003361# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003362 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003363 if (f2 == 0.0)
3364 {
3365 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003366 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003367 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003368 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003369 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003370 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003371 }
3372 else
3373 f1 = f1 / f2;
3374# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003375 // We rely on the floating point library to handle divide
3376 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003377 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003378# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003381 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003382 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003383 return FAIL;
3384 }
3385 rettv->v_type = VAR_FLOAT;
3386 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 }
3388 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003391 int failed = FALSE;
3392
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003393 if (op == '*')
3394 n1 = n1 * n2;
3395 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003396 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003398 n1 = num_modulus(n1, n2, &failed);
3399 if (failed)
3400 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003401
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003402 rettv->v_type = VAR_NUMBER;
3403 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 }
3406 }
3407
3408 return OK;
3409}
3410
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003411/*
3412 * Handle a type cast before a base level expression.
3413 * "arg" must point to the first non-white of the expression.
3414 * "arg" is advanced to just after the recognized expression.
3415 * Return OK or FAIL.
3416 */
3417 static int
3418eval7t(
3419 char_u **arg,
3420 typval_T *rettv,
3421 evalarg_T *evalarg,
3422 int want_string) // after "." operator
3423{
3424 type_T *want_type = NULL;
3425 garray_T type_list; // list of pointers to allocated types
3426 int res;
3427 int evaluate = evalarg == NULL ? 0
3428 : (evalarg->eval_flags & EVAL_EVALUATE);
3429
3430 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003431 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3432 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003433 {
3434 ++*arg;
3435 ga_init2(&type_list, sizeof(type_T *), 10);
3436 want_type = parse_type(arg, &type_list, TRUE);
3437 if (want_type == NULL && (evaluate || **arg != '>'))
3438 {
3439 clear_type_list(&type_list);
3440 return FAIL;
3441 }
3442
3443 if (**arg != '>')
3444 {
3445 if (*skipwhite(*arg) == '>')
3446 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3447 else
3448 emsg(_(e_missing_gt));
3449 clear_type_list(&type_list);
3450 return FAIL;
3451 }
3452 ++*arg;
3453 *arg = skipwhite_and_linebreak(*arg, evalarg);
3454 }
3455
3456 res = eval7(arg, rettv, evalarg, want_string);
3457
3458 if (want_type != NULL && evaluate)
3459 {
3460 if (res == OK)
3461 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003462 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3463 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003464
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003465 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003466 {
3467 if (want_type == &t_bool && actual != &t_bool
3468 && (actual->tt_flags & TTFLAG_BOOL_OK))
3469 {
3470 int n = tv2bool(rettv);
3471
3472 // can use "0" and "1" for boolean in some places
3473 clear_tv(rettv);
3474 rettv->v_type = VAR_BOOL;
3475 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3476 }
3477 else
3478 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003479 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003480
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003481 where.wt_variable = TRUE;
3482 res = check_type(want_type, actual, TRUE, where);
3483 }
3484 }
3485 }
3486 clear_type_list(&type_list);
3487 }
3488
3489 return res;
3490}
3491
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003492 int
3493eval_leader(char_u **arg, int vim9)
3494{
3495 char_u *s = *arg;
3496 char_u *p = *arg;
3497
3498 while (*p == '!' || *p == '-' || *p == '+')
3499 {
3500 char_u *n = skipwhite(p + 1);
3501
3502 // ++, --, -+ and +- are not accepted in Vim9 script
3503 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3504 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003505 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003506 return FAIL;
3507 }
3508 p = n;
3509 }
3510 *arg = p;
3511 return OK;
3512}
3513
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003515 * Check for a predefined value "true", "false" and "null.*".
3516 * Return OK when recognized.
3517 */
3518 int
3519handle_predefined(char_u *s, int len, typval_T *rettv)
3520{
3521 switch (len)
3522 {
3523 case 4: if (STRNCMP(s, "true", 4) == 0)
3524 {
3525 rettv->v_type = VAR_BOOL;
3526 rettv->vval.v_number = VVAL_TRUE;
3527 return OK;
3528 }
3529 if (STRNCMP(s, "null", 4) == 0)
3530 {
3531 rettv->v_type = VAR_SPECIAL;
3532 rettv->vval.v_number = VVAL_NULL;
3533 return OK;
3534 }
3535 break;
3536 case 5: if (STRNCMP(s, "false", 5) == 0)
3537 {
3538 rettv->v_type = VAR_BOOL;
3539 rettv->vval.v_number = VVAL_FALSE;
3540 return OK;
3541 }
3542 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003543 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3544 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003545#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003546 rettv->v_type = VAR_JOB;
3547 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003548#else
3549 rettv->v_type = VAR_SPECIAL;
3550 rettv->vval.v_number = VVAL_NULL;
3551#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003552 return OK;
3553 }
3554 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003555 case 9:
3556 if (STRNCMP(s, "null_", 5) != 0)
3557 break;
3558 if (STRNCMP(s + 5, "list", 4) == 0)
3559 {
3560 rettv->v_type = VAR_LIST;
3561 rettv->vval.v_list = NULL;
3562 return OK;
3563 }
3564 if (STRNCMP(s + 5, "dict", 4) == 0)
3565 {
3566 rettv->v_type = VAR_DICT;
3567 rettv->vval.v_dict = NULL;
3568 return OK;
3569 }
3570 if (STRNCMP(s + 5, "blob", 4) == 0)
3571 {
3572 rettv->v_type = VAR_BLOB;
3573 rettv->vval.v_blob = NULL;
3574 return OK;
3575 }
3576 break;
3577 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3578 {
3579 rettv->v_type = VAR_STRING;
3580 rettv->vval.v_string = NULL;
3581 return OK;
3582 }
3583 break;
3584 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003585 if (STRNCMP(s, "null_channel", 12) == 0)
3586 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003587#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003588 rettv->v_type = VAR_CHANNEL;
3589 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003590#else
3591 rettv->v_type = VAR_SPECIAL;
3592 rettv->vval.v_number = VVAL_NULL;
3593#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003594 return OK;
3595 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003596 if (STRNCMP(s, "null_partial", 12) == 0)
3597 {
3598 rettv->v_type = VAR_PARTIAL;
3599 rettv->vval.v_partial = NULL;
3600 return OK;
3601 }
3602 break;
3603 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3604 {
3605 rettv->v_type = VAR_FUNC;
3606 rettv->vval.v_string = NULL;
3607 return OK;
3608 }
3609 break;
3610 }
3611 return FAIL;
3612}
3613
3614/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 * Handle sixth level expression:
3616 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003617 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003618 * "string" string constant
3619 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 * &option-name option value
3621 * @r register contents
3622 * identifier variable value
3623 * function() function call
3624 * $VAR environment variable
3625 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003626 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003628 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003629 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 *
3631 * Also handle:
3632 * ! in front logical NOT
3633 * - in front unary minus
3634 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003635 * trailing [] subscript in String or List
3636 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003637 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 *
3639 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003640 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 *
3642 * Return OK or FAIL.
3643 */
3644 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003645eval7(
3646 char_u **arg,
3647 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003648 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003651 int evaluate = evalarg != NULL
3652 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 int len;
3654 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003655 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 char_u *start_leader, *end_leader;
3657 int ret = OK;
3658 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003659 static int recurse = 0;
3660 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661
3662 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003663 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003664 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003666 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667
3668 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003669 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 */
3671 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003672 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003673 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 end_leader = *arg;
3675
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003676 if (**arg == '.' && (!isdigit(*(*arg + 1))
3677#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003678 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003679#endif
3680 ))
3681 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003682 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003683 ++*arg;
3684 return FAIL;
3685 }
3686
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003687 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003688 // and crash. With MSVC the stack is smaller.
3689 if (recurse ==
3690#ifdef _MSC_VER
3691 300
3692#else
3693 1000
3694#endif
3695 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003696 {
3697 semsg(_(e_expression_too_recursive_str), *arg);
3698 return FAIL;
3699 }
3700 ++recurse;
3701
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 switch (**arg)
3703 {
3704 /*
3705 * Number constant.
3706 */
3707 case '0':
3708 case '1':
3709 case '2':
3710 case '3':
3711 case '4':
3712 case '5':
3713 case '6':
3714 case '7':
3715 case '8':
3716 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003717 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003718
3719 // Apply prefixed "-" and "+" now. Matters especially when
3720 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003721 if (ret == OK && evaluate && end_leader > start_leader
3722 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003723 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 break;
3725
3726 /*
3727 * String constant: "string".
3728 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003729 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 break;
3731
3732 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003733 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003735 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003736 break;
3737
3738 /*
3739 * List: [expr, expr]
3740 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003741 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 break;
3743
3744 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003745 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003746 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003747 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003748 {
3749 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3750 }
3751 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003752 {
3753 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003754 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003755 }
3756 else
3757 ret = NOTDONE;
3758 break;
3759
3760 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003761 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003762 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003763 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003764 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003765 ret = NOTDONE;
3766 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00003767 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003768 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003769 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003770 break;
3771
3772 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003773 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003775 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 break;
3777
3778 /*
3779 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01003780 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 */
LemonBoy2eaef102022-05-06 13:14:50 +01003782 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
3783 ret = eval_interp_string(arg, rettv, evaluate);
3784 else
3785 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 break;
3787
3788 /*
3789 * Register contents: @r.
3790 */
3791 case '@': ++*arg;
3792 if (evaluate)
3793 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003794 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003795 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003796 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003797 emsg_invreg(**arg);
3798 else
3799 {
3800 rettv->v_type = VAR_STRING;
3801 rettv->vval.v_string = get_reg_contents(**arg,
3802 GREG_EXPR_SRC);
3803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 }
3805 if (**arg != NUL)
3806 ++*arg;
3807 break;
3808
3809 /*
3810 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003811 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003813 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003814 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01003815 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003816 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003817 if (ret == OK && evaluate)
3818 {
3819 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3820
Bram Moolenaara9931532021-06-12 15:58:16 +02003821 // Compile it here to get the return type. The return
3822 // type is optional, when it's missing use t_unknown.
3823 // This is recognized in compile_return().
3824 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3825 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00003826 if (compile_def_function(ufunc, FALSE,
3827 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003828 {
3829 clear_tv(rettv);
3830 ret = FAIL;
3831 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003832 }
3833 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003834 if (ret == NOTDONE)
3835 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003836 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003837 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003838
Bram Moolenaar9215f012020-06-27 21:18:00 +02003839 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003840 if (**arg == ')')
3841 ++*arg;
3842 else if (ret == OK)
3843 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003844 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003845 clear_tv(rettv);
3846 ret = FAIL;
3847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 }
3849 break;
3850
Bram Moolenaar8c711452005-01-14 21:53:12 +00003851 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 break;
3853 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003854
3855 if (ret == NOTDONE)
3856 {
3857 /*
3858 * Must be a variable or function name.
3859 * Can also be a curly-braces kind of name: {expr}.
3860 */
3861 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003862 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003863 if (alias != NULL)
3864 s = alias;
3865
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003866 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003867 ret = FAIL;
3868 else
3869 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003870 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3871
Bram Moolenaar4525a572022-02-13 11:57:33 +00003872 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003873 {
3874 emsg(_(e_cannot_use_underscore_here));
3875 ret = FAIL;
3876 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00003877 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00003878 && s[0] == 's' && s[1] == ':')
3879 {
3880 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
3881 ret = FAIL;
3882 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00003883 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003884 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003885 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003886 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003887 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003888 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003889 else if (flags & EVAL_CONSTANT)
3890 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003891 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003892 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003893 // get the value of "true", "false", etc. or a variable
3894 ret = FAIL;
3895 if (vim9script)
3896 ret = handle_predefined(s, len, rettv);
3897 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003898 {
3899 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003900 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003901 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003902 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003903 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003904 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003905 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003906 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003907 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003908 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003909 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003910 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003911 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003912 }
3913
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003914 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3915 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003916 if (ret == OK)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003917 ret = handle_subscript(arg, name_start, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918
3919 /*
3920 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3921 */
3922 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003923 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003924
3925 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003926 return ret;
3927}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003928
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003929/*
3930 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003931 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003932 * Adjusts "end_leaderp" until it is at "start_leader".
3933 */
3934 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003935eval7_leader(
3936 typval_T *rettv,
3937 int numeric_only,
3938 char_u *start_leader,
3939 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003940{
3941 char_u *end_leader = *end_leaderp;
3942 int ret = OK;
3943 int error = FALSE;
3944 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003945 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003946 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003947#ifdef FEAT_FLOAT
3948 float_T f = 0.0;
3949
3950 if (rettv->v_type == VAR_FLOAT)
3951 f = rettv->vval.v_float;
3952 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003953#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003954 {
3955 while (VIM_ISWHITE(end_leader[-1]))
3956 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003957 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003958 val = tv2bool(rettv);
3959 else
3960 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003961 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003962 if (error)
3963 {
3964 clear_tv(rettv);
3965 ret = FAIL;
3966 }
3967 else
3968 {
3969 while (end_leader > start_leader)
3970 {
3971 --end_leader;
3972 if (*end_leader == '!')
3973 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003974 if (numeric_only)
3975 {
3976 ++end_leader;
3977 break;
3978 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003979#ifdef FEAT_FLOAT
3980 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003981 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003982 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003983 {
3984 rettv->v_type = VAR_BOOL;
3985 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3986 }
3987 else
3988 f = !f;
3989 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003990 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_BOOL;
3995 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003996 }
3997 else if (*end_leader == '-')
3998 {
3999#ifdef FEAT_FLOAT
4000 if (rettv->v_type == VAR_FLOAT)
4001 f = -f;
4002 else
4003#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004004 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004005 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004006 type = VAR_NUMBER;
4007 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004008 }
4009 }
4010#ifdef FEAT_FLOAT
4011 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004013 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004014 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004016 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004017#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004018 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004019 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004020 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004021 rettv->v_type = type;
4022 else
4023 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004024 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004027 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 return ret;
4029}
4030
4031/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004032 * Call the function referred to in "rettv".
4033 */
4034 static int
4035call_func_rettv(
4036 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004037 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004038 typval_T *rettv,
4039 int evaluate,
4040 dict_T *selfdict,
4041 typval_T *basetv)
4042{
4043 partial_T *pt = NULL;
4044 funcexe_T funcexe;
4045 typval_T functv;
4046 char_u *s;
4047 int ret;
4048
4049 // need to copy the funcref so that we can clear rettv
4050 if (evaluate)
4051 {
4052 functv = *rettv;
4053 rettv->v_type = VAR_UNKNOWN;
4054
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004055 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004056 if (functv.v_type == VAR_PARTIAL)
4057 {
4058 pt = functv.vval.v_partial;
4059 s = partial_name(pt);
4060 }
4061 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004062 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004063 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004064 if (s == NULL || *s == NUL)
4065 {
4066 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004067 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004068 goto theend;
4069 }
4070 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004071 }
4072 else
4073 s = (char_u *)"";
4074
Bram Moolenaara80faa82020-04-12 19:37:17 +02004075 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004076 funcexe.fe_firstline = curwin->w_cursor.lnum;
4077 funcexe.fe_lastline = curwin->w_cursor.lnum;
4078 funcexe.fe_evaluate = evaluate;
4079 funcexe.fe_partial = pt;
4080 funcexe.fe_selfdict = selfdict;
4081 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004082 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004083
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004084theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004085 // Clear the funcref afterwards, so that deleting it while
4086 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004087 if (evaluate)
4088 clear_tv(&functv);
4089
4090 return ret;
4091}
4092
4093/*
4094 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004095 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004096 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4097 */
4098 static int
4099eval_lambda(
4100 char_u **arg,
4101 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004102 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004103 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004104{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004105 int evaluate = evalarg != NULL
4106 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004107 typval_T base = *rettv;
4108 int ret;
4109
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004110 rettv->v_type = VAR_UNKNOWN;
4111
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004112 if (**arg == '{')
4113 {
4114 // ->{lambda}()
4115 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4116 }
4117 else
4118 {
4119 // ->(lambda)()
4120 ++*arg;
4121 ret = eval1(arg, rettv, evalarg);
4122 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004123 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004124 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004125 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004126 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004127 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004128 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4129 && rettv->v_type != VAR_PARTIAL)
4130 {
4131 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4132 return FAIL;
4133 }
4134 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004135 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004136 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004137 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004138
4139 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004140 {
4141 if (verbose)
4142 {
4143 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004144 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004145 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004146 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004147 }
4148 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004149 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004150 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004151 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004152 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004153
4154 // Clear the funcref afterwards, so that deleting it while
4155 // evaluating the arguments is possible (see test55).
4156 if (evaluate)
4157 clear_tv(&base);
4158
4159 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004160}
4161
4162/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004163 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004164 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004165 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4166 */
4167 static int
4168eval_method(
4169 char_u **arg,
4170 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004171 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004172 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004173{
4174 char_u *name;
4175 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004176 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004177 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004178 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004179 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004180 int evaluate = evalarg != NULL
4181 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004182
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004183 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004184
Bram Moolenaarac92e252019-08-03 21:58:38 +02004185 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004186 len = get_name_len(arg, &alias, evaluate, TRUE);
4187 if (alias != NULL)
4188 name = alias;
4189
4190 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004191 {
4192 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004193 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004194 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004195 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004196 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004197 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004198 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004199
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004200 // If there is no "(" immediately following, but there is further on,
4201 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4202 // Does not handle anything where "(" is part of the expression.
4203 *arg = skipwhite(*arg);
4204
4205 if (**arg != '(' && alias == NULL
4206 && (paren = vim_strchr(*arg, '(')) != NULL)
4207 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004208 char_u *deref;
4209
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004210 *arg = name;
4211 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004212 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4213 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004214 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004215 *arg = name + len;
4216 ret = FAIL;
4217 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004218 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004219 {
4220 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004221 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004222 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004223 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004224 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004225
4226 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004227 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004228 *arg = skipwhite(*arg);
4229
4230 if (**arg != '(')
4231 {
4232 if (verbose)
4233 semsg(_(e_missing_parenthesis_str), name);
4234 ret = FAIL;
4235 }
4236 else if (VIM_ISWHITE((*arg)[-1]))
4237 {
4238 if (verbose)
4239 emsg(_(e_no_white_space_allowed_before_parenthesis));
4240 ret = FAIL;
4241 }
4242 else
4243 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004244 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004245 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004246 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004247
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004248 // Clear the funcref afterwards, so that deleting it while
4249 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004250 if (evaluate)
4251 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004252 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004253
Bram Moolenaarac92e252019-08-03 21:58:38 +02004254 return ret;
4255}
4256
4257/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004258 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4259 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004260 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4261 */
4262 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004263eval_index(
4264 char_u **arg,
4265 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004266 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004267 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004268{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004269 int evaluate = evalarg != NULL
4270 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004271 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004272 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004273 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004274 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004275 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004276 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004277
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004278 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4279 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004280
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004281 init_tv(&var1);
4282 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004283 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004284 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004285 /*
4286 * dict.name
4287 */
4288 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004289 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004290 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004291 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004292 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004293 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004294 }
4295 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004296 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004297 /*
4298 * something[idx]
4299 *
4300 * Get the (first) variable from inside the [].
4301 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004302 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004303 if (**arg == ':')
4304 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004305 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004306 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004307 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004308 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004309 semsg(_(e_white_space_required_before_and_after_str_at_str),
4310 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004311 clear_tv(&var1);
4312 return FAIL;
4313 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004314 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004315 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004316 int error = FALSE;
4317
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004318#ifdef FEAT_FLOAT
4319 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004320 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004321 && var1.v_type == VAR_FLOAT)
4322 {
4323 var1.vval.v_string = typval_tostring(&var1, TRUE);
4324 var1.v_type = VAR_STRING;
4325 }
4326#endif
Bram Moolenaar4525a572022-02-13 11:57:33 +00004327 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004328 tv_get_number_chk(&var1, &error);
4329 else
4330 error = tv_get_string_chk(&var1) == NULL;
4331 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004332 {
4333 // not a number or string
4334 clear_tv(&var1);
4335 return FAIL;
4336 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004337 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004338
4339 /*
4340 * Get the second variable from inside the [:].
4341 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004342 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004343 if (**arg == ':')
4344 {
4345 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004346 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004347 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004348 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004349 semsg(_(e_white_space_required_before_and_after_str_at_str),
4350 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004351 if (!empty1)
4352 clear_tv(&var1);
4353 return FAIL;
4354 }
4355 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004356 if (**arg == ']')
4357 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004358 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004359 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004360 if (!empty1)
4361 clear_tv(&var1);
4362 return FAIL;
4363 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004364 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004365 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004366 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004367 if (!empty1)
4368 clear_tv(&var1);
4369 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004370 return FAIL;
4371 }
4372 }
4373
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004374 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004375 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004376 if (**arg != ']')
4377 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004378 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004379 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004380 clear_tv(&var1);
4381 if (range)
4382 clear_tv(&var2);
4383 return FAIL;
4384 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004385 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004386 }
4387
4388 if (evaluate)
4389 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004390 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004391 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004392 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004393
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004394 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004395 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004396 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004397 clear_tv(&var2);
4398 return res;
4399 }
4400 return OK;
4401}
4402
4403/*
4404 * Check if "rettv" can have an [index] or [sli:ce]
4405 */
4406 int
4407check_can_index(typval_T *rettv, int evaluate, int verbose)
4408{
4409 switch (rettv->v_type)
4410 {
4411 case VAR_FUNC:
4412 case VAR_PARTIAL:
4413 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004414 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004415 return FAIL;
4416 case VAR_FLOAT:
4417#ifdef FEAT_FLOAT
4418 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004419 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004420 return FAIL;
4421#endif
4422 case VAR_BOOL:
4423 case VAR_SPECIAL:
4424 case VAR_JOB:
4425 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004426 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004427 if (verbose)
4428 emsg(_(e_cannot_index_special_variable));
4429 return FAIL;
4430 case VAR_UNKNOWN:
4431 case VAR_ANY:
4432 case VAR_VOID:
4433 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004434 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004435 emsg(_(e_cannot_index_special_variable));
4436 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004437 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004438 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004439
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004440 case VAR_STRING:
4441 case VAR_LIST:
4442 case VAR_DICT:
4443 case VAR_BLOB:
4444 break;
4445 case VAR_NUMBER:
4446 if (in_vim9script())
4447 emsg(_(e_cannot_index_number));
4448 break;
4449 }
4450 return OK;
4451}
4452
4453/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004454 * slice() function
4455 */
4456 void
4457f_slice(typval_T *argvars, typval_T *rettv)
4458{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004459 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004460 && ((argvars[0].v_type != VAR_STRING
4461 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004462 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004463 && check_for_list_arg(argvars, 0) == FAIL)
4464 || check_for_number_arg(argvars, 1) == FAIL
4465 || check_for_opt_number_arg(argvars, 2) == FAIL))
4466 return;
4467
Bram Moolenaar6601b622021-01-13 21:47:15 +01004468 if (check_can_index(argvars, TRUE, FALSE) == OK)
4469 {
4470 copy_tv(argvars, rettv);
4471 eval_index_inner(rettv, TRUE, argvars + 1,
4472 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4473 TRUE, NULL, 0, FALSE);
4474 }
4475}
4476
4477/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004478 * Apply index or range to "rettv".
4479 * "var1" is the first index, NULL for [:expr].
4480 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004481 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4482 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004483 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4484 */
4485 int
4486eval_index_inner(
4487 typval_T *rettv,
4488 int is_range,
4489 typval_T *var1,
4490 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004491 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004492 char_u *key,
4493 int keylen,
4494 int verbose)
4495{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004496 varnumber_T n1, n2 = 0;
4497 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004498
4499 n1 = 0;
4500 if (var1 != NULL && rettv->v_type != VAR_DICT)
4501 n1 = tv_get_number(var1);
4502
4503 if (is_range)
4504 {
4505 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004506 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004507 if (verbose)
4508 emsg(_(e_cannot_slice_dictionary));
4509 return FAIL;
4510 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004511 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004512 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004513 else
4514 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004515 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004516
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004517 switch (rettv->v_type)
4518 {
4519 case VAR_UNKNOWN:
4520 case VAR_ANY:
4521 case VAR_VOID:
4522 case VAR_FUNC:
4523 case VAR_PARTIAL:
4524 case VAR_FLOAT:
4525 case VAR_BOOL:
4526 case VAR_SPECIAL:
4527 case VAR_JOB:
4528 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004529 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004530 break; // not evaluating, skipping over subscript
4531
4532 case VAR_NUMBER:
4533 case VAR_STRING:
4534 {
4535 char_u *s = tv_get_string(rettv);
4536
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004537 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004538 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004539 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004540 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004541 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004542 else
4543 s = char_from_string(s, n1);
4544 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004545 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004546 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004547 // The resulting variable is a substring. If the indexes
4548 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004549 if (n1 < 0)
4550 {
4551 n1 = len + n1;
4552 if (n1 < 0)
4553 n1 = 0;
4554 }
4555 if (n2 < 0)
4556 n2 = len + n2;
4557 else if (n2 >= len)
4558 n2 = len;
4559 if (n1 >= len || n2 < 0 || n1 > n2)
4560 s = NULL;
4561 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004562 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004563 }
4564 else
4565 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004566 // The resulting variable is a string of a single
4567 // character. If the index is too big or negative the
4568 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004569 if (n1 >= len || n1 < 0)
4570 s = NULL;
4571 else
4572 s = vim_strnsave(s + n1, 1);
4573 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004574 clear_tv(rettv);
4575 rettv->v_type = VAR_STRING;
4576 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004577 }
4578 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004579
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004580 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004581 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4582 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004583 break;
4584
4585 case VAR_LIST:
4586 if (var1 == NULL)
4587 n1 = 0;
4588 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004589 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004590 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004591 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004592 return FAIL;
4593 break;
4594
4595 case VAR_DICT:
4596 {
4597 dictitem_T *item;
4598 typval_T tmp;
4599
4600 if (key == NULL)
4601 {
4602 key = tv_get_string_chk(var1);
4603 if (key == NULL)
4604 return FAIL;
4605 }
4606
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004607 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004608
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004609 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004610 {
4611 if (verbose)
4612 {
4613 if (keylen > 0)
4614 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004615 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004616 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004617 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004618 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004619
4620 copy_tv(&item->di_tv, &tmp);
4621 clear_tv(rettv);
4622 *rettv = tmp;
4623 }
4624 break;
4625 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004626 return OK;
4627}
4628
4629/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004630 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004631 */
4632 char_u *
4633partial_name(partial_T *pt)
4634{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004635 if (pt != NULL)
4636 {
4637 if (pt->pt_name != NULL)
4638 return pt->pt_name;
4639 if (pt->pt_func != NULL)
4640 return pt->pt_func->uf_name;
4641 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004642 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004643}
4644
Bram Moolenaarddecc252016-04-06 22:59:37 +02004645 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004646partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004647{
4648 int i;
4649
4650 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004651 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004652 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004653 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004654 if (pt->pt_name != NULL)
4655 {
4656 func_unref(pt->pt_name);
4657 vim_free(pt->pt_name);
4658 }
4659 else
4660 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004661
Bram Moolenaar54656012021-06-09 20:50:46 +02004662 // "out_up" is no longer used, decrement refcount on partial that owns it.
4663 partial_unref(pt->pt_outer.out_up_partial);
4664
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004665 // Using pt_outer from another partial.
4666 partial_unref(pt->pt_outer_partial);
4667
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004668 // Decrease the reference count for the context of a closure. If down
4669 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004670 if (pt->pt_funcstack != NULL)
4671 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004672 --pt->pt_funcstack->fs_refcount;
4673 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004674 }
4675
Bram Moolenaarddecc252016-04-06 22:59:37 +02004676 vim_free(pt);
4677}
4678
4679/*
4680 * Unreference a closure: decrement the reference count and free it when it
4681 * becomes zero.
4682 */
4683 void
4684partial_unref(partial_T *pt)
4685{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004686 if (pt != NULL)
4687 {
4688 if (--pt->pt_refcount <= 0)
4689 partial_free(pt);
4690
4691 // If the reference count goes down to one, the funcstack may be the
4692 // only reference and can be freed if no other partials reference it.
4693 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4694 funcstack_check_refcount(pt->pt_funcstack);
4695 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004696}
4697
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004698/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004699 * Return the next (unique) copy ID.
4700 * Used for serializing nested structures.
4701 */
4702 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004703get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004704{
4705 current_copyID += COPYID_INC;
4706 return current_copyID;
4707}
4708
4709/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004710 * Garbage collection for lists and dictionaries.
4711 *
4712 * We use reference counts to be able to free most items right away when they
4713 * are no longer used. But for composite items it's possible that it becomes
4714 * unused while the reference count is > 0: When there is a recursive
4715 * reference. Example:
4716 * :let l = [1, 2, 3]
4717 * :let d = {9: l}
4718 * :let l[1] = d
4719 *
4720 * Since this is quite unusual we handle this with garbage collection: every
4721 * once in a while find out which lists and dicts are not referenced from any
4722 * variable.
4723 *
4724 * Here is a good reference text about garbage collection (refers to Python
4725 * but it applies to all reference-counting mechanisms):
4726 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004727 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004728
4729/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004730 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004731 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004732 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004733 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004734 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004735garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004736{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004737 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004738 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004739 buf_T *buf;
4740 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004741 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004742 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004743
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004744 if (!testing)
4745 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004746 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004747 want_garbage_collect = FALSE;
4748 may_garbage_collect = FALSE;
4749 garbage_collect_at_exit = FALSE;
4750 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004751
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004752 // The execution stack can grow big, limit the size.
4753 if (exestack.ga_maxlen - exestack.ga_len > 500)
4754 {
4755 size_t new_len;
4756 char_u *pp;
4757 int n;
4758
4759 // Keep 150% of the current size, with a minimum of the growth size.
4760 n = exestack.ga_len / 2;
4761 if (n < exestack.ga_growsize)
4762 n = exestack.ga_growsize;
4763
4764 // Don't make it bigger though.
4765 if (exestack.ga_len + n < exestack.ga_maxlen)
4766 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004767 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004768 pp = vim_realloc(exestack.ga_data, new_len);
4769 if (pp == NULL)
4770 return FAIL;
4771 exestack.ga_maxlen = exestack.ga_len + n;
4772 exestack.ga_data = pp;
4773 }
4774 }
4775
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004776 // We advance by two because we add one for items referenced through
4777 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004778 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004779
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004780 /*
4781 * 1. Go through all accessible variables and mark all lists and dicts
4782 * with copyID.
4783 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004784
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004785 // Don't free variables in the previous_funccal list unless they are only
4786 // referenced through previous_funccal. This must be first, because if
4787 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004788 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004789
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004790 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004791 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004792
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004793 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004794 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004795 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4796 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004797
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004798 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004799 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004800 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4801 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004802 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004803 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4804 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004805#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004806 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004807 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4808 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004809 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004810 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004811 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4812 NULL, NULL);
4813#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004814
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004815 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004816 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004817 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4818 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004819 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004820 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004821
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004822 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004823 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004824
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004825 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004826 abort = abort || set_ref_in_functions(copyID);
4827
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004828 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004829 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004830
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004831 // funcstacks keep variables for closures
4832 abort = abort || set_ref_in_funcstacks(copyID);
4833
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004834 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004835 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004836
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004837 // callbacks in buffers
4838 abort = abort || set_ref_in_buffers(copyID);
4839
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004840 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
4841 abort = abort || set_ref_in_insexpand_funcs(copyID);
4842
4843 // 'operatorfunc' callback
4844 abort = abort || set_ref_in_opfunc(copyID);
4845
4846 // 'tagfunc' callback
4847 abort = abort || set_ref_in_tagfunc(copyID);
4848
4849 // 'imactivatefunc' and 'imstatusfunc' callbacks
4850 abort = abort || set_ref_in_im_funcs(copyID);
4851
Bram Moolenaar1dced572012-04-05 16:54:08 +02004852#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004853 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004854#endif
4855
Bram Moolenaardb913952012-06-29 12:54:53 +02004856#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004857 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004858#endif
4859
4860#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004861 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004862#endif
4863
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004864#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004865 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004866 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004867#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004868#ifdef FEAT_NETBEANS_INTG
4869 abort = abort || set_ref_in_nb_channel(copyID);
4870#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004871
Bram Moolenaare3188e22016-05-31 21:13:04 +02004872#ifdef FEAT_TIMERS
4873 abort = abort || set_ref_in_timer(copyID);
4874#endif
4875
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004876#ifdef FEAT_QUICKFIX
4877 abort = abort || set_ref_in_quickfix(copyID);
4878#endif
4879
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004880#ifdef FEAT_TERMINAL
4881 abort = abort || set_ref_in_term(copyID);
4882#endif
4883
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004884#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004885 abort = abort || set_ref_in_popups(copyID);
4886#endif
4887
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004888 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004889 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004890 /*
4891 * 2. Free lists and dictionaries that are not referenced.
4892 */
4893 did_free = free_unref_items(copyID);
4894
4895 /*
4896 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004897 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004898 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004899 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004900 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004901 else if (p_verbose > 0)
4902 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004903 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004904 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004905
4906 return did_free;
4907}
4908
4909/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004910 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004911 */
4912 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004913free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004914{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004915 int did_free = FALSE;
4916
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004917 // Let all "free" functions know that we are here. This means no
4918 // dictionaries, lists, channels or jobs are to be freed, because we will
4919 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004920 in_free_unref_items = TRUE;
4921
4922 /*
4923 * PASS 1: free the contents of the items. We don't free the items
4924 * themselves yet, so that it is possible to decrement refcount counters
4925 */
4926
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004927 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004928 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004929
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004930 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004931 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004932
4933#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004934 // Go through the list of jobs and free items without the copyID. This
4935 // must happen before doing channels, because jobs refer to channels, but
4936 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004937 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4938
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004939 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004940 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4941#endif
4942
4943 /*
4944 * PASS 2: free the items themselves.
4945 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004946 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004947 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004948
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004949#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004950 // Go through the list of jobs and free items without the copyID. This
4951 // must happen before doing channels, because jobs refer to channels, but
4952 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004953 free_unused_jobs(copyID, COPYID_MASK);
4954
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004955 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004956 free_unused_channels(copyID, COPYID_MASK);
4957#endif
4958
4959 in_free_unref_items = FALSE;
4960
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004961 return did_free;
4962}
4963
4964/*
4965 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004966 * "list_stack" is used to add lists to be marked. Can be NULL.
4967 *
4968 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004969 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004970 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004971set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004972{
4973 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004974 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004975 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004976 hashtab_T *cur_ht;
4977 ht_stack_T *ht_stack = NULL;
4978 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004979
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004980 cur_ht = ht;
4981 for (;;)
4982 {
4983 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004984 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004985 // Mark each item in the hashtab. If the item contains a hashtab
4986 // it is added to ht_stack, if it contains a list it is added to
4987 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004988 todo = (int)cur_ht->ht_used;
4989 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4990 if (!HASHITEM_EMPTY(hi))
4991 {
4992 --todo;
4993 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4994 &ht_stack, list_stack);
4995 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004996 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004997
4998 if (ht_stack == NULL)
4999 break;
5000
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005001 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005002 cur_ht = ht_stack->ht;
5003 tempitem = ht_stack;
5004 ht_stack = ht_stack->prev;
5005 free(tempitem);
5006 }
5007
5008 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005009}
5010
Dominique Pelle748b3082022-01-08 12:41:16 +00005011#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5012 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005013/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005014 * Mark a dict and its items with "copyID".
5015 * Returns TRUE if setting references failed somehow.
5016 */
5017 int
5018set_ref_in_dict(dict_T *d, int copyID)
5019{
5020 if (d != NULL && d->dv_copyID != copyID)
5021 {
5022 d->dv_copyID = copyID;
5023 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5024 }
5025 return FALSE;
5026}
Dominique Pelle748b3082022-01-08 12:41:16 +00005027#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005028
5029/*
5030 * Mark a list and its items with "copyID".
5031 * Returns TRUE if setting references failed somehow.
5032 */
5033 int
5034set_ref_in_list(list_T *ll, int copyID)
5035{
5036 if (ll != NULL && ll->lv_copyID != copyID)
5037 {
5038 ll->lv_copyID = copyID;
5039 return set_ref_in_list_items(ll, copyID, NULL);
5040 }
5041 return FALSE;
5042}
5043
5044/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005045 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005046 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5047 *
5048 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005049 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005050 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005051set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005052{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005053 listitem_T *li;
5054 int abort = FALSE;
5055 list_T *cur_l;
5056 list_stack_T *list_stack = NULL;
5057 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005058
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005059 cur_l = l;
5060 for (;;)
5061 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005062 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005063 // Mark each item in the list. If the item contains a hashtab
5064 // it is added to ht_stack, if it contains a list it is added to
5065 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005066 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5067 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5068 ht_stack, &list_stack);
5069 if (list_stack == NULL)
5070 break;
5071
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005072 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005073 cur_l = list_stack->list;
5074 tempitem = list_stack;
5075 list_stack = list_stack->prev;
5076 free(tempitem);
5077 }
5078
5079 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005080}
5081
5082/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005083 * Mark the partial in callback 'cb' with "copyID".
5084 */
5085 int
5086set_ref_in_callback(callback_T *cb, int copyID)
5087{
5088 typval_T tv;
5089
5090 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5091 return FALSE;
5092
5093 tv.v_type = VAR_PARTIAL;
5094 tv.vval.v_partial = cb->cb_partial;
5095 return set_ref_in_item(&tv, copyID, NULL, NULL);
5096}
5097
5098/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005099 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005100 * "list_stack" is used to add lists to be marked. Can be NULL.
5101 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5102 *
5103 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005104 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005105 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005106set_ref_in_item(
5107 typval_T *tv,
5108 int copyID,
5109 ht_stack_T **ht_stack,
5110 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005111{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005112 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005113
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005114 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005115 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005116 dict_T *dd = tv->vval.v_dict;
5117
Bram Moolenaara03f2332016-02-06 18:09:59 +01005118 if (dd != NULL && dd->dv_copyID != copyID)
5119 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005120 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005121 dd->dv_copyID = copyID;
5122 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005123 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005124 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5125 }
5126 else
5127 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005128 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5129
Bram Moolenaara03f2332016-02-06 18:09:59 +01005130 if (newitem == NULL)
5131 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005132 else
5133 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005134 newitem->ht = &dd->dv_hashtab;
5135 newitem->prev = *ht_stack;
5136 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005137 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005138 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005139 }
5140 }
5141 else if (tv->v_type == VAR_LIST)
5142 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005143 list_T *ll = tv->vval.v_list;
5144
Bram Moolenaara03f2332016-02-06 18:09:59 +01005145 if (ll != NULL && ll->lv_copyID != copyID)
5146 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005147 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005148 ll->lv_copyID = copyID;
5149 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005150 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005151 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005152 }
5153 else
5154 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005155 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5156
Bram Moolenaara03f2332016-02-06 18:09:59 +01005157 if (newitem == NULL)
5158 abort = TRUE;
5159 else
5160 {
5161 newitem->list = ll;
5162 newitem->prev = *list_stack;
5163 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005164 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005165 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005166 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005167 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005168 else if (tv->v_type == VAR_FUNC)
5169 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005170 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005171 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005172 else if (tv->v_type == VAR_PARTIAL)
5173 {
5174 partial_T *pt = tv->vval.v_partial;
5175 int i;
5176
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005177 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005178 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005179 // Didn't see this partial yet.
5180 pt->pt_copyID = copyID;
5181
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005182 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005183
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005184 if (pt->pt_dict != NULL)
5185 {
5186 typval_T dtv;
5187
5188 dtv.v_type = VAR_DICT;
5189 dtv.vval.v_dict = pt->pt_dict;
5190 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5191 }
5192
5193 for (i = 0; i < pt->pt_argc; ++i)
5194 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5195 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005196 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005197 }
5198 }
5199#ifdef FEAT_JOB_CHANNEL
5200 else if (tv->v_type == VAR_JOB)
5201 {
5202 job_T *job = tv->vval.v_job;
5203 typval_T dtv;
5204
5205 if (job != NULL && job->jv_copyID != copyID)
5206 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005207 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005208 if (job->jv_channel != NULL)
5209 {
5210 dtv.v_type = VAR_CHANNEL;
5211 dtv.vval.v_channel = job->jv_channel;
5212 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5213 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005214 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005215 {
5216 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005217 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005218 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5219 }
5220 }
5221 }
5222 else if (tv->v_type == VAR_CHANNEL)
5223 {
5224 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005225 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005226 typval_T dtv;
5227 jsonq_T *jq;
5228 cbq_T *cq;
5229
5230 if (ch != NULL && ch->ch_copyID != copyID)
5231 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005232 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005233 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005234 {
5235 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5236 jq = jq->jq_next)
5237 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5238 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5239 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005240 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005241 {
5242 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005243 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005244 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5245 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005246 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005247 {
5248 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005249 dtv.vval.v_partial =
5250 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005251 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5252 }
5253 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005254 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005255 {
5256 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005257 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005258 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5259 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005260 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005261 {
5262 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005263 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005264 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5265 }
5266 }
5267 }
5268#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005269 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005270}
5271
Bram Moolenaar8c711452005-01-14 21:53:12 +00005272/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273 * Return a string with the string representation of a variable.
5274 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005275 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005276 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005277 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005278 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005279 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005280 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5281 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005282 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005284 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005285echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005286 typval_T *tv,
5287 char_u **tofree,
5288 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005289 int copyID,
5290 int echo_style,
5291 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005292 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005294 static int recurse = 0;
5295 char_u *r = NULL;
5296
Bram Moolenaar33570922005-01-25 22:26:29 +00005297 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005298 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005299 if (!did_echo_string_emsg)
5300 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005301 // Only give this message once for a recursive call to avoid
5302 // flooding the user with errors. And stop iterating over lists
5303 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005304 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005305 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005306 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005307 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005308 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005309 }
5310 ++recurse;
5311
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005312 switch (tv->v_type)
5313 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005314 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005315 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005316 {
5317 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005318 r = tv->vval.v_string;
5319 if (r == NULL)
5320 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005321 }
5322 else
5323 {
5324 *tofree = string_quote(tv->vval.v_string, FALSE);
5325 r = *tofree;
5326 }
5327 break;
5328
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005329 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005330 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005331 char_u buf[MAX_FUNC_NAME_LEN];
5332
5333 if (echo_style)
5334 {
LemonBoya5d35902022-04-29 21:15:02 +01005335 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5336 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005337 buf, MAX_FUNC_NAME_LEN);
5338 if (r == buf)
5339 {
5340 r = vim_strsave(buf);
5341 *tofree = r;
5342 }
5343 else
5344 *tofree = NULL;
5345 }
5346 else
5347 {
5348 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5349 : make_ufunc_name_readable(
5350 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5351 TRUE);
5352 r = *tofree;
5353 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005354 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005355 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005356
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005357 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005358 {
5359 partial_T *pt = tv->vval.v_partial;
5360 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005361 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005362 garray_T ga;
5363 int i;
5364 char_u *tf;
5365
5366 ga_init2(&ga, 1, 100);
5367 ga_concat(&ga, (char_u *)"function(");
5368 if (fname != NULL)
5369 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005370 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005371 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005372 && vim_isupper(fname[1]))
5373 {
5374 ga_concat(&ga, (char_u *)"'g:");
5375 ga_concat(&ga, fname + 1);
5376 }
5377 else
5378 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005379 vim_free(fname);
5380 }
5381 if (pt != NULL && pt->pt_argc > 0)
5382 {
5383 ga_concat(&ga, (char_u *)", [");
5384 for (i = 0; i < pt->pt_argc; ++i)
5385 {
5386 if (i > 0)
5387 ga_concat(&ga, (char_u *)", ");
5388 ga_concat(&ga,
5389 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5390 vim_free(tf);
5391 }
5392 ga_concat(&ga, (char_u *)"]");
5393 }
5394 if (pt != NULL && pt->pt_dict != NULL)
5395 {
5396 typval_T dtv;
5397
5398 ga_concat(&ga, (char_u *)", ");
5399 dtv.v_type = VAR_DICT;
5400 dtv.vval.v_dict = pt->pt_dict;
5401 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5402 vim_free(tf);
5403 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005404 // terminate with ')' and a NUL
5405 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005406
5407 *tofree = ga.ga_data;
5408 r = *tofree;
5409 break;
5410 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005411
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005412 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005413 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005414 break;
5415
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005416 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005417 if (tv->vval.v_list == NULL)
5418 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005419 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005420 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005421 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005422 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005423 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5424 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005425 {
5426 *tofree = NULL;
5427 r = (char_u *)"[...]";
5428 }
5429 else
5430 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005431 int old_copyID = tv->vval.v_list->lv_copyID;
5432
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005433 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005434 *tofree = list2string(tv, copyID, restore_copyID);
5435 if (restore_copyID)
5436 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005437 r = *tofree;
5438 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005439 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005440
Bram Moolenaar8c711452005-01-14 21:53:12 +00005441 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005442 if (tv->vval.v_dict == NULL)
5443 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005444 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005445 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005446 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005447 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005448 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5449 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005450 {
5451 *tofree = NULL;
5452 r = (char_u *)"{...}";
5453 }
5454 else
5455 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005456 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005457
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005458 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005459 *tofree = dict2string(tv, copyID, restore_copyID);
5460 if (restore_copyID)
5461 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005462 r = *tofree;
5463 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005464 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005465
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005466 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005467 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005468 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005469 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005470 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005471 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005472 break;
5473
Bram Moolenaar835dc632016-02-07 14:27:38 +01005474 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005475 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005476#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005477 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005478 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5479 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005480 if (composite_val)
5481 {
5482 *tofree = string_quote(r, FALSE);
5483 r = *tofree;
5484 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005485#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005486 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005487
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005488 case VAR_INSTR:
5489 *tofree = NULL;
5490 r = (char_u *)"instructions";
5491 break;
5492
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005493 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005494#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005495 *tofree = NULL;
5496 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5497 r = numbuf;
5498 break;
5499#endif
5500
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005501 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005502 case VAR_SPECIAL:
5503 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005504 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005505 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005506 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005507
Bram Moolenaar8502c702014-06-17 12:51:16 +02005508 if (--recurse == 0)
5509 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005510 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005511}
5512
5513/*
5514 * Return a string with the string representation of a variable.
5515 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5516 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005517 * Does not put quotes around strings, as ":echo" displays values.
5518 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5519 * May return NULL.
5520 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005521 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005522echo_string(
5523 typval_T *tv,
5524 char_u **tofree,
5525 char_u *numbuf,
5526 int copyID)
5527{
5528 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5529}
5530
5531/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005532 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5533 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005534 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005535 */
5536 int
5537buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5538{
5539 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005540 char_u *t;
5541 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005542
5543 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5544 return -1;
5545
5546 if (lnum > buf->b_ml.ml_line_count)
5547 lnum = buf->b_ml.ml_line_count;
5548
5549 str = ml_get_buf(buf, lnum, FALSE);
5550 if (str == NULL)
5551 return -1;
5552
5553 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005554 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005555
Bram Moolenaar91458462021-01-13 20:08:38 +01005556 // count the number of characters
5557 t = str;
5558 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5559 t += mb_ptr2len(t);
5560
5561 // In insert mode, when the cursor is at the end of a non-empty line,
5562 // byteidx points to the NUL character immediately past the end of the
5563 // string. In this case, add one to the character count.
5564 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5565 count++;
5566
5567 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005568}
5569
5570/*
5571 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005572 * byte index. Works only for loaded buffers. Returns -1 on failure.
5573 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005574 */
5575 int
5576buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5577{
5578 char_u *str;
5579 char_u *t;
5580
5581 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5582 return -1;
5583
5584 if (lnum > buf->b_ml.ml_line_count)
5585 lnum = buf->b_ml.ml_line_count;
5586
5587 str = ml_get_buf(buf, lnum, FALSE);
5588 if (str == NULL)
5589 return -1;
5590
5591 // Convert the character offset to a byte offset
5592 t = str;
5593 while (*t != NUL && --charidx > 0)
5594 t += mb_ptr2len(t);
5595
Bram Moolenaar91458462021-01-13 20:08:38 +01005596 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005597}
5598
5599/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005601 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005603 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005604var2fpos(
5605 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005606 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005607 int *fnum, // set to fnum for '0, 'A, etc.
5608 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005610 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005612 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005614 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005615 if (varp->v_type == VAR_LIST)
5616 {
5617 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005618 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005619 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005620 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005621
5622 l = varp->vval.v_list;
5623 if (l == NULL)
5624 return NULL;
5625
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005626 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005627 pos.lnum = list_find_nr(l, 0L, &error);
5628 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005629 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005630 if (charcol)
5631 len = (long)mb_charlen(ml_get(pos.lnum));
5632 else
5633 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005634
Bram Moolenaarec65d772020-08-20 22:29:12 +02005635 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005636 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005637 li = list_find(l, 1L);
5638 if (li != NULL && li->li_tv.v_type == VAR_STRING
5639 && li->li_tv.vval.v_string != NULL
5640 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005641 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005642 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005643 }
5644 else
5645 {
5646 pos.col = list_find_nr(l, 1L, &error);
5647 if (error)
5648 return NULL;
5649 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005650
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005651 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005652 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005653 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005654 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005655
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005656 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005657 pos.coladd = list_find_nr(l, 2L, &error);
5658 if (error)
5659 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005660
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005661 return &pos;
5662 }
5663
Bram Moolenaarc5809432021-03-27 21:23:30 +01005664 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5665 return NULL;
5666
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005667 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005668 if (name == NULL)
5669 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005670
5671 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005672 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005673 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005674 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005675 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005676 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005677 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005678 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005679 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005680 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005681 pos = VIsual;
5682 else
5683 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005684 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005685 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005686 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005688 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005689 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5691 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005692 pos = *pp;
5693 }
5694 if (pos.lnum != 0)
5695 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005696 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005697 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5698 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005700
Bram Moolenaara5525202006-03-02 22:52:09 +00005701 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005702
Bram Moolenaar477933c2007-07-17 14:32:23 +00005703 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005704 {
5705 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005706 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005707 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005708 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005709 // In silent Ex mode topline is zero, but that's not a valid line
5710 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005711 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005712 return &pos;
5713 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005714 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005715 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005716 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005717 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005718 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005719 return &pos;
5720 }
5721 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005722 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005724 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 {
5726 pos.lnum = curbuf->b_ml.ml_line_count;
5727 pos.col = 0;
5728 }
5729 else
5730 {
5731 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005732 if (charcol)
5733 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5734 else
5735 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 }
5737 return &pos;
5738 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005739 if (in_vim9script())
5740 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 return NULL;
5742}
5743
5744/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005745 * Convert list in "arg" into a position and optional file number.
5746 * When "fnump" is NULL there is no file number, only 3 items.
5747 * Note that the column is passed on as-is, the caller may want to decrement
5748 * it to use 1 for the first column.
5749 * Return FAIL when conversion is not possible, doesn't check the position for
5750 * validity.
5751 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005752 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005753list2fpos(
5754 typval_T *arg,
5755 pos_T *posp,
5756 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005757 colnr_T *curswantp,
5758 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005759{
5760 list_T *l = arg->vval.v_list;
5761 long i = 0;
5762 long n;
5763
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005764 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5765 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005766 if (arg->v_type != VAR_LIST
5767 || l == NULL
5768 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005769 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005770 return FAIL;
5771
5772 if (fnump != NULL)
5773 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005774 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005775 if (n < 0)
5776 return FAIL;
5777 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005778 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005779 *fnump = n;
5780 }
5781
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005782 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005783 if (n < 0)
5784 return FAIL;
5785 posp->lnum = n;
5786
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005787 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005788 if (n < 0)
5789 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005790 // If character position is specified, then convert to byte position
5791 if (charcol)
5792 {
5793 buf_T *buf;
5794
5795 // Get the text for the specified line in a loaded buffer
5796 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5797 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5798 return FAIL;
5799
Bram Moolenaar91458462021-01-13 20:08:38 +01005800 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005801 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005802 posp->col = n;
5803
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005804 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005805 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005806 posp->coladd = 0;
5807 else
5808 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005809
Bram Moolenaar493c1782014-05-28 14:34:46 +02005810 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005811 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005812
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005813 return OK;
5814}
5815
5816/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 * Get the length of an environment variable name.
5818 * Advance "arg" to the first character after the name.
5819 * Return 0 for error.
5820 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005821 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005822get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823{
5824 char_u *p;
5825 int len;
5826
5827 for (p = *arg; vim_isIDc(*p); ++p)
5828 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005829 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 return 0;
5831
5832 len = (int)(p - *arg);
5833 *arg = p;
5834 return len;
5835}
5836
5837/*
5838 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005839 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 * Return 0 if something is wrong.
5841 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005842 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005843get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844{
5845 char_u *p;
5846 int len;
5847
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005848 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005850 {
5851 if (*p == ':')
5852 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005853 // "s:" is start of "s:var", but "n:" is not and can be used in
5854 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005855 len = (int)(p - *arg);
5856 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5857 || len > 1)
5858 break;
5859 }
5860 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005861 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 return 0;
5863
5864 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005865 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866
5867 return len;
5868}
5869
5870/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005871 * Get the length of the name of a variable or function.
5872 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005874 * Return -1 if curly braces expansion failed.
5875 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 * If the name contains 'magic' {}'s, expand them and return the
5877 * expanded name in an allocated string via 'alias' - caller must free.
5878 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005879 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005880get_name_len(
5881 char_u **arg,
5882 char_u **alias,
5883 int evaluate,
5884 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885{
5886 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 char_u *p;
5888 char_u *expr_start;
5889 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005891 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892
5893 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5894 && (*arg)[2] == (int)KE_SNR)
5895 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005896 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 *arg += 3;
5898 return get_id_len(arg) + 3;
5899 }
5900 len = eval_fname_script(*arg);
5901 if (len > 0)
5902 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005903 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 *arg += len;
5905 }
5906
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005908 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005910 p = find_name_end(*arg, &expr_start, &expr_end,
5911 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 if (expr_start != NULL)
5913 {
5914 char_u *temp_string;
5915
5916 if (!evaluate)
5917 {
5918 len += (int)(p - *arg);
5919 *arg = skipwhite(p);
5920 return len;
5921 }
5922
5923 /*
5924 * Include any <SID> etc in the expanded string:
5925 * Thus the -len here.
5926 */
5927 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5928 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005929 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 *alias = temp_string;
5931 *arg = skipwhite(p);
5932 return (int)STRLEN(temp_string);
5933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934
5935 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005936 // Only give an error when there is something, otherwise it will be
5937 // reported at a higher level.
5938 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005939 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940
5941 return len;
5942}
5943
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005944/*
5945 * Find the end of a variable or function name, taking care of magic braces.
5946 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5947 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005948 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005949 * Return a pointer to just after the name. Equal to "arg" if there is no
5950 * valid name.
5951 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005952 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005953find_name_end(
5954 char_u *arg,
5955 char_u **expr_start,
5956 char_u **expr_end,
5957 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005959 int mb_nest = 0;
5960 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005962 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005963 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005965 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005967 *expr_start = NULL;
5968 *expr_end = NULL;
5969 }
5970
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005971 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005972 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5973 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005974 return arg;
5975
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005976 for (p = arg; *p != NUL
5977 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005978 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005979 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005980 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005981 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005982 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005983 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005984 if (*p == '\'')
5985 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005986 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005987 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005988 ;
5989 if (*p == NUL)
5990 break;
5991 }
5992 else if (*p == '"')
5993 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005994 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005995 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005996 if (*p == '\\' && p[1] != NUL)
5997 ++p;
5998 if (*p == NUL)
5999 break;
6000 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006001 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6002 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006003 // "s:" is start of "s:var", but "n:" is not and can be used in
6004 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006005 len = (int)(p - arg);
6006 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006007 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006008 break;
6009 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006010
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006011 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006013 if (*p == '[')
6014 ++br_nest;
6015 else if (*p == ']')
6016 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006018
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006019 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006021 if (*p == '{')
6022 {
6023 mb_nest++;
6024 if (expr_start != NULL && *expr_start == NULL)
6025 *expr_start = p;
6026 }
6027 else if (*p == '}')
6028 {
6029 mb_nest--;
6030 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6031 *expr_end = p;
6032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 }
6035
6036 return p;
6037}
6038
6039/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006040 * Expands out the 'magic' {}'s in a variable/function name.
6041 * Note that this can call itself recursively, to deal with
6042 * constructs like foo{bar}{baz}{bam}
6043 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6044 * "in_start" ^
6045 * "expr_start" ^
6046 * "expr_end" ^
6047 * "in_end" ^
6048 *
6049 * Returns a new allocated string, which the caller must free.
6050 * Returns NULL for failure.
6051 */
6052 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006053make_expanded_name(
6054 char_u *in_start,
6055 char_u *expr_start,
6056 char_u *expr_end,
6057 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006058{
6059 char_u c1;
6060 char_u *retval = NULL;
6061 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006062
6063 if (expr_end == NULL || in_end == NULL)
6064 return NULL;
6065 *expr_start = NUL;
6066 *expr_end = NUL;
6067 c1 = *in_end;
6068 *in_end = NUL;
6069
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006070 temp_result = eval_to_string(expr_start + 1, FALSE);
6071 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006072 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006073 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6074 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006075 if (retval != NULL)
6076 {
6077 STRCPY(retval, in_start);
6078 STRCAT(retval, temp_result);
6079 STRCAT(retval, expr_end + 1);
6080 }
6081 }
6082 vim_free(temp_result);
6083
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006084 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006085 *expr_start = '{';
6086 *expr_end = '}';
6087
6088 if (retval != NULL)
6089 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006090 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006091 if (expr_start != NULL)
6092 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006093 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006094 temp_result = make_expanded_name(retval, expr_start,
6095 expr_end, temp_result);
6096 vim_free(retval);
6097 retval = temp_result;
6098 }
6099 }
6100
6101 return retval;
6102}
6103
6104/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006106 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006108 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006109eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006111 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006112}
6113
6114/*
6115 * Return TRUE if character "c" can be used as the first character in a
6116 * variable or function name (excluding '{' and '}').
6117 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006118 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006119eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006120{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006121 return ASCII_ISALPHA(c) || c == '_';
6122}
6123
6124/*
6125 * Return TRUE if character "c" can be used as the first character of a
6126 * dictionary key.
6127 */
6128 int
6129eval_isdictc(int c)
6130{
6131 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132}
6133
6134/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006135 * Handle:
6136 * - expr[expr], expr[expr:expr] subscript
6137 * - ".name" lookup
6138 * - function call with Funcref variable: func(expr)
6139 * - method call: var->method()
6140 *
6141 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006142 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006143 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006144 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006145handle_subscript(
6146 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006147 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006148 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006149 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006150 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006151{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006152 int evaluate = evalarg != NULL
6153 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006154 int ret = OK;
6155 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006156 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006157 int getnext;
6158 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006159
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006160 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006161 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006162 // When at the end of the line and ".name" or "->{" or "->X" follows in
6163 // the next line then consume the line break.
6164 p = eval_next_non_blank(*arg, evalarg, &getnext);
6165 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006166 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006167 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6168 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6169 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006170 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006171 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006172 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006173 check_white = FALSE;
6174 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006175
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006176 if (rettv->v_type == VAR_ANY)
6177 {
6178 char_u *exp_name;
6179 int cc;
6180 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006181 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006182 type_T *type;
6183
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006184 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006185 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006186 if (**arg != '.')
6187 {
6188 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006189 semsg(_(e_expected_dot_after_name_str),
6190 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006191 ret = FAIL;
6192 break;
6193 }
6194 ++*arg;
6195 if (IS_WHITE_OR_NUL(**arg))
6196 {
6197 if (verbose)
6198 emsg(_(e_no_white_space_allowed_after_dot));
6199 ret = FAIL;
6200 break;
6201 }
6202
6203 // isolate the name
6204 exp_name = *arg;
6205 while (eval_isnamec(**arg))
6206 ++*arg;
6207 cc = **arg;
6208 **arg = NUL;
6209
6210 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00006211 evalarg->eval_cctx, evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006212 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006213
6214 if (idx < 0 && ufunc == NULL)
6215 {
6216 ret = FAIL;
6217 break;
6218 }
6219 if (idx >= 0)
6220 {
6221 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6222 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6223
6224 copy_tv(sv->sv_tv, rettv);
6225 }
6226 else
6227 {
6228 rettv->v_type = VAR_FUNC;
6229 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6230 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006231 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006232 }
6233
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006234 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6235 || rettv->v_type == VAR_PARTIAL))
6236 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006237 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006238 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6239 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006240
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006241 // Stop the expression evaluation when immediately aborting on
6242 // error, or when an interrupt occurred or an exception was thrown
6243 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006244 if (aborting())
6245 {
6246 if (ret == OK)
6247 clear_tv(rettv);
6248 ret = FAIL;
6249 }
6250 dict_unref(selfdict);
6251 selfdict = NULL;
6252 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006253 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006254 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006255 if (in_vim9script())
6256 *arg = skipwhite(p + 2);
6257 else
6258 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006259 if (ret == OK)
6260 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006261 if (VIM_ISWHITE(**arg))
6262 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006263 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006264 ret = FAIL;
6265 }
6266 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006267 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006268 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006269 else
6270 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006271 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006272 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006273 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006274 // "." is ".name" lookup when we found a dict or when evaluating and
6275 // scriptversion is at least 2, where string concatenation is "..".
6276 else if (**arg == '['
6277 || (**arg == '.' && (rettv->v_type == VAR_DICT
6278 || (!evaluate
6279 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006280 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006281 {
6282 dict_unref(selfdict);
6283 if (rettv->v_type == VAR_DICT)
6284 {
6285 selfdict = rettv->vval.v_dict;
6286 if (selfdict != NULL)
6287 ++selfdict->dv_refcount;
6288 }
6289 else
6290 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006291 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006292 {
6293 clear_tv(rettv);
6294 ret = FAIL;
6295 }
6296 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006297 else
6298 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006299 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006300
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006301 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6302 // Don't do this when "Func" is already a partial that was bound
6303 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006304 if (selfdict != NULL
6305 && (rettv->v_type == VAR_FUNC
6306 || (rettv->v_type == VAR_PARTIAL
6307 && (rettv->vval.v_partial->pt_auto
6308 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006309 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006310
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006311 dict_unref(selfdict);
6312 return ret;
6313}
6314
6315/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006316 * Make a copy of an item.
6317 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006318 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006319 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6320 * reference to an already copied list/dict can be used.
6321 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006322 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006323 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006324item_copy(
6325 typval_T *from,
6326 typval_T *to,
6327 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006328 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006329 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006330{
6331 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006332 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006333
Bram Moolenaar33570922005-01-25 22:26:29 +00006334 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006335 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006336 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006337 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006338 }
6339 ++recurse;
6340
6341 switch (from->v_type)
6342 {
6343 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006344 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006345 case VAR_STRING:
6346 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006347 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006348 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006349 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006350 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006351 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006352 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006353 copy_tv(from, to);
6354 break;
6355 case VAR_LIST:
6356 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006357 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006358 if (from->vval.v_list == NULL)
6359 to->vval.v_list = NULL;
6360 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6361 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006362 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006363 to->vval.v_list = from->vval.v_list->lv_copylist;
6364 ++to->vval.v_list->lv_refcount;
6365 }
6366 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006367 to->vval.v_list = list_copy(from->vval.v_list,
6368 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006369 if (to->vval.v_list == NULL)
6370 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006371 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006372 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006373 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006374 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006375 case VAR_DICT:
6376 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006377 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006378 if (from->vval.v_dict == NULL)
6379 to->vval.v_dict = NULL;
6380 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6381 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006382 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006383 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6384 ++to->vval.v_dict->dv_refcount;
6385 }
6386 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006387 to->vval.v_dict = dict_copy(from->vval.v_dict,
6388 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006389 if (to->vval.v_dict == NULL)
6390 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006391 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006392 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006393 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006394 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006395 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006396 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006397 }
6398 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006399 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006400}
6401
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006402 void
6403echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6404{
6405 char_u *tofree;
6406 char_u numbuf[NUMBUFLEN];
6407 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6408
6409 if (*atstart)
6410 {
6411 *atstart = FALSE;
6412 // Call msg_start() after eval1(), evaluating the expression
6413 // may cause a message to appear.
6414 if (with_space)
6415 {
6416 // Mark the saved text as finishing the line, so that what
6417 // follows is displayed on a new line when scrolling back
6418 // at the more prompt.
6419 msg_sb_eol();
6420 msg_start();
6421 }
6422 }
6423 else if (with_space)
6424 msg_puts_attr(" ", echo_attr);
6425
6426 if (p != NULL)
6427 for ( ; *p != NUL && !got_int; ++p)
6428 {
6429 if (*p == '\n' || *p == '\r' || *p == TAB)
6430 {
6431 if (*p != TAB && *needclr)
6432 {
6433 // remove any text still there from the command
6434 msg_clr_eos();
6435 *needclr = FALSE;
6436 }
6437 msg_putchar_attr(*p, echo_attr);
6438 }
6439 else
6440 {
6441 if (has_mbyte)
6442 {
6443 int i = (*mb_ptr2len)(p);
6444
6445 (void)msg_outtrans_len_attr(p, i, echo_attr);
6446 p += i - 1;
6447 }
6448 else
6449 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6450 }
6451 }
6452 vim_free(tofree);
6453}
6454
Bram Moolenaare9a41262005-01-15 22:18:47 +00006455/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 * ":echo expr1 ..." print each argument separated with a space, add a
6457 * newline at the end.
6458 * ":echon expr1 ..." print each argument plain.
6459 */
6460 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006461ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462{
6463 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006464 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006465 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 int needclr = TRUE;
6467 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006468 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006469 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006470 evalarg_T evalarg;
6471
Bram Moolenaare707c882020-07-01 13:07:37 +02006472 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473
6474 if (eap->skip)
6475 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006476 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006478 // If eval1() causes an error message the text from the command may
6479 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006480 need_clr_eos = needclr;
6481
Bram Moolenaar68db9962021-05-09 23:19:22 +02006482 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006483 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 {
6485 /*
6486 * Report the invalid expression unless the expression evaluation
6487 * has been cancelled due to an aborting error, an interrupt, or an
6488 * exception.
6489 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006490 if (!aborting() && did_emsg == did_emsg_before
6491 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006492 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006493 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 break;
6495 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006496 need_clr_eos = FALSE;
6497
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006499 {
6500 if (rettv.v_type == VAR_VOID)
6501 {
6502 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6503 break;
6504 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006505 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006506 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006507
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006508 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 arg = skipwhite(arg);
6510 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006511 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006512 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513
6514 if (eap->skip)
6515 --emsg_skip;
6516 else
6517 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006518 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 if (needclr)
6520 msg_clr_eos();
6521 if (eap->cmdidx == CMD_echo)
6522 msg_end();
6523 }
6524}
6525
6526/*
6527 * ":echohl {name}".
6528 */
6529 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006530ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006532 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533}
6534
6535/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006536 * Returns the :echo attribute
6537 */
6538 int
6539get_echo_attr(void)
6540{
6541 return echo_attr;
6542}
6543
6544/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 * ":execute expr1 ..." execute the result of an expression.
6546 * ":echomsg expr1 ..." Print a message
6547 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006548 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 * Each gets spaces around each argument and a newline at the end for
6550 * echo commands
6551 */
6552 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006553ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554{
6555 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006556 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 int ret = OK;
6558 char_u *p;
6559 garray_T ga;
6560 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006561 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562
6563 ga_init2(&ga, 1, 80);
6564
6565 if (eap->skip)
6566 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006567 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006569 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006570 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572
6573 if (!eap->skip)
6574 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006575 char_u buf[NUMBUFLEN];
6576
6577 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006578 {
6579 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6580 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006581 semsg(_(e_using_invalid_value_as_string_str),
6582 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006583 p = NULL;
6584 }
6585 else
6586 p = tv_get_string_buf(&rettv, buf);
6587 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006588 else
6589 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006590 if (p == NULL)
6591 {
6592 clear_tv(&rettv);
6593 ret = FAIL;
6594 break;
6595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 len = (int)STRLEN(p);
6597 if (ga_grow(&ga, len + 2) == FAIL)
6598 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006599 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 ret = FAIL;
6601 break;
6602 }
6603 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 ga.ga_len += len;
6607 }
6608
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006609 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 arg = skipwhite(arg);
6611 }
6612
6613 if (ret != FAIL && ga.ga_data != NULL)
6614 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006615 // use the first line of continuation lines for messages
6616 SOURCING_LNUM = start_lnum;
6617
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006618 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6619 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006620 // Mark the already saved text as finishing the line, so that what
6621 // follows is displayed on a new line when scrolling back at the
6622 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006623 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006624 }
6625
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006627 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006628 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006629 out_flush();
6630 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006631 else if (eap->cmdidx == CMD_echoconsole)
6632 {
6633 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6634 ui_write((char_u *)"\r\n", 2, TRUE);
6635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 else if (eap->cmdidx == CMD_echoerr)
6637 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006638 int save_did_emsg = did_emsg;
6639
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006640 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006641 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 if (!force_abort)
6643 did_emsg = save_did_emsg;
6644 }
6645 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006646 {
6647 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6648
6649 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6650 sticky_cmdmod_flags = cmdmod.cmod_flags
6651 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 do_cmdline((char_u *)ga.ga_data,
6653 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006654 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 }
6657
6658 ga_clear(&ga);
6659
6660 if (eap->skip)
6661 --emsg_skip;
6662
Bram Moolenaar63b91732021-08-05 20:40:03 +02006663 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664}
6665
6666/*
6667 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6668 * "arg" points to the "&" or '+' when called, to "option" when returning.
6669 * Returns NULL when no option name found. Otherwise pointer to the char
6670 * after the option name.
6671 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006672 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006673find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674{
6675 char_u *p = *arg;
6676
6677 ++p;
6678 if (*p == 'g' && p[1] == ':')
6679 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006680 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 p += 2;
6682 }
6683 else if (*p == 'l' && p[1] == ':')
6684 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006685 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 p += 2;
6687 }
6688 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006689 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690
6691 if (!ASCII_ISALPHA(*p))
6692 return NULL;
6693 *arg = p;
6694
6695 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006696 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 else
6698 while (ASCII_ISALPHA(*p))
6699 ++p;
6700 return p;
6701}
6702
6703/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006704 * Display script name where an item was last set.
6705 * Should only be invoked when 'verbose' is non-zero.
6706 */
6707 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006708last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006709{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006710 char_u *p;
6711
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006712 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006713 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006714 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006715 if (p != NULL)
6716 {
6717 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006718 msg_puts(_("\n\tLast set from "));
6719 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006720 if (script_ctx.sc_lnum > 0)
6721 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006722 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006723 msg_outnum((long)script_ctx.sc_lnum);
6724 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006725 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006726 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006727 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006728 }
6729}
6730
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006731#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732
6733/*
6734 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006735 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 * "flags" can be "g" to do a global substitute.
6737 * Returns an allocated string, NULL for error.
6738 */
6739 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006740do_string_sub(
6741 char_u *str,
6742 char_u *pat,
6743 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006744 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006745 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746{
6747 int sublen;
6748 regmatch_T regmatch;
6749 int i;
6750 int do_all;
6751 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006752 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 garray_T ga;
6754 char_u *ret;
6755 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006756 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006758 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006760 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761
6762 ga_init2(&ga, 1, 200);
6763
6764 do_all = (flags[0] == 'g');
6765
6766 regmatch.rm_ic = p_ic;
6767 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6768 if (regmatch.regprog != NULL)
6769 {
6770 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006771 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6773 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006774 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006775 if (regmatch.startp[0] == regmatch.endp[0])
6776 {
6777 if (zero_width == regmatch.startp[0])
6778 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006779 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006780 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006781 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6782 (size_t)i);
6783 ga.ga_len += i;
6784 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006785 continue;
6786 }
6787 zero_width = regmatch.startp[0];
6788 }
6789
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 /*
6791 * Get some space for a temporary buffer to do the substitution
6792 * into. It will contain:
6793 * - The text up to where the match is.
6794 * - The substituted text.
6795 * - The text after the match.
6796 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006797 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006798 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6800 {
6801 ga_clear(&ga);
6802 break;
6803 }
6804
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006805 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 i = (int)(regmatch.startp[0] - tail);
6807 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006808 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006809 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 + ga.ga_len + i, TRUE, TRUE, FALSE);
6811 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006812 tail = regmatch.endp[0];
6813 if (*tail == NUL)
6814 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 if (!do_all)
6816 break;
6817 }
6818
6819 if (ga.ga_data != NULL)
6820 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6821
Bram Moolenaar473de612013-06-08 18:19:48 +02006822 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 }
6824
6825 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6826 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006827 if (p_cpo == empty_option)
6828 p_cpo = save_cpo;
6829 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006830 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006831 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006832 // If it's still empty it was changed and restored, need to restore in
6833 // the complicated way.
6834 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01006835 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006836 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838
6839 return ret;
6840}