blob: 9d4079d4558b7687f34e0c3531195ab38c3a7079 [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 Moolenaar33570922005-01-25 22:26:29 +0000114
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200115#ifdef EBCDIC
116 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100117 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200118 */
119 sortFunctions();
120#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000121}
122
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000123#if defined(EXITFREE) || defined(PROTO)
124 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100125eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000126{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200127 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100128 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200129 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000130
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200131 // autoloaded script names
132 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000133
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200134 // unreferenced lists and dicts
135 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200136
137 // functions not garbage collected
138 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000139}
140#endif
141
Bram Moolenaare6b53242020-07-01 17:28:33 +0200142 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200143fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
144{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100145 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200146 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200147 if (eap != NULL)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200148 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200149 evalarg->eval_cstack = eap->cstack;
150 if (getline_equal(eap->getline, eap->cookie, getsourceline))
151 {
152 evalarg->eval_getline = eap->getline;
153 evalarg->eval_cookie = eap->cookie;
154 }
Bram Moolenaar37c83712020-06-30 21:18:36 +0200155 }
156}
157
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158/*
159 * Top level evaluation function, returning a boolean.
160 * Sets "error" to TRUE if there was an error.
161 * Return TRUE or FALSE.
162 */
163 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100164eval_to_bool(
165 char_u *arg,
166 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200167 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100168 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169{
Bram Moolenaar33570922005-01-25 22:26:29 +0000170 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200171 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200172 evalarg_T evalarg;
173
Bram Moolenaar37c83712020-06-30 21:18:36 +0200174 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175
176 if (skip)
177 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200178 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 else
181 {
182 *error = FALSE;
183 if (!skip)
184 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200185 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200186 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200187 else
188 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000189 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 }
191 }
192 if (skip)
193 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200194 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200196 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197}
198
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100199/*
200 * Call eval1() and give an error message if not done at a lower level.
201 */
202 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200203eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100204{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100205 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100206 int ret;
207 int did_emsg_before = did_emsg;
208 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200209 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100210
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200211 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
212
213 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100214 if (ret == FAIL)
215 {
216 // Report the invalid expression unless the expression evaluation has
217 // been cancelled due to an aborting error, an interrupt, or an
218 // exception, or we already gave a more specific error.
219 // Also check called_emsg for when using assert_fails().
220 if (!aborting() && did_emsg == did_emsg_before
221 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200222 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100223 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200224 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100225 return ret;
226}
227
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100228/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200229 * Return whether a typval is a valid expression to pass to eval_expr_typval()
230 * or eval_expr_to_bool(). An empty string returns FALSE;
231 */
232 int
233eval_expr_valid_arg(typval_T *tv)
234{
235 return tv->v_type != VAR_UNKNOWN
236 && (tv->v_type != VAR_STRING
237 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
238}
239
240/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100241 * Evaluate an expression, which can be a function, partial or string.
242 * Pass arguments "argv[argc]".
243 * Return the result in "rettv" and OK or FAIL.
244 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200245 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100246eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
247{
248 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100249 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200250 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100251
252 if (expr->v_type == VAR_FUNC)
253 {
254 s = expr->vval.v_string;
255 if (s == NULL || *s == NUL)
256 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200257 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000258 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200259 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100260 return FAIL;
261 }
262 else if (expr->v_type == VAR_PARTIAL)
263 {
264 partial_T *partial = expr->vval.v_partial;
265
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200266 if (partial == NULL)
267 return FAIL;
268
Bram Moolenaar822ba242020-05-24 23:00:18 +0200269 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200270 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200272 if (call_def_function(partial->pt_func, argc, argv,
273 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274 return FAIL;
275 }
276 else
277 {
278 s = partial_name(partial);
279 if (s == NULL || *s == NUL)
280 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200281 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000282 funcexe.fe_evaluate = TRUE;
283 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100284 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
285 return FAIL;
286 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100287 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200288 else if (expr->v_type == VAR_INSTR)
289 {
290 return exe_typval_instr(expr, rettv);
291 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100292 else
293 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100294 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100295 if (s == NULL)
296 return FAIL;
297 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200298 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100299 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200300 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100301 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200302 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200303 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100304 return FAIL;
305 }
306 }
307 return OK;
308}
309
310/*
311 * Like eval_to_bool() but using a typval_T instead of a string.
312 * Works for string, funcref and partial.
313 */
314 int
315eval_expr_to_bool(typval_T *expr, int *error)
316{
317 typval_T rettv;
318 int res;
319
320 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
321 {
322 *error = TRUE;
323 return FALSE;
324 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200325 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100326 clear_tv(&rettv);
327 return res;
328}
329
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330/*
331 * Top level evaluation function, returning a string. If "skip" is TRUE,
332 * only parsing to "nextcmd" is done, without reporting errors. Return
333 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
334 */
335 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100336eval_to_string_skip(
337 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200338 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100339 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340{
Bram Moolenaar33570922005-01-25 22:26:29 +0000341 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200343 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344
Bram Moolenaar37c83712020-06-30 21:18:36 +0200345 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 if (skip)
347 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200348 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 retval = NULL;
350 else
351 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100352 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000353 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 }
355 if (skip)
356 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200357 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358
359 return retval;
360}
361
362/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000363 * Skip over an expression at "*pp".
364 * Return FAIL for an error, OK otherwise.
365 */
366 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200367skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000368{
Bram Moolenaar33570922005-01-25 22:26:29 +0000369 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000370
371 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200372 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000373}
374
375/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200376 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200377 * If in Vim9 script and line breaks are encountered, the lines are
378 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200379 * "arg" is advanced to just after the expression.
380 * "start" is set to the start of the expression, "end" to just after the end.
381 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200382 * Return FAIL for an error, OK otherwise.
383 */
384 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200385skip_expr_concatenate(
386 char_u **arg,
387 char_u **start,
388 char_u **end,
389 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200390{
391 typval_T rettv;
392 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200393 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200394 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200395 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200396 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200397 int evaluate = evalarg == NULL
398 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200399
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200400 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200401 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200402 {
403 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200404 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200405 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200406 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200407 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200408 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200409 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200410
411 // Don't evaluate the expression.
412 if (evalarg != NULL)
413 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200414 *arg = skipwhite(*arg);
415 res = eval1(arg, &rettv, evalarg);
416 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200417 if (evalarg != NULL)
418 evalarg->eval_flags = save_flags;
419
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200420 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200421 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200422 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200423 if (evalarg->eval_ga.ga_len == 1)
424 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200425 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200426 ga_clear(gap);
427 gap->ga_itemsize = 0;
428 }
429 else
430 {
431 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200432 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200433
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200434 // Line breaks encountered, concatenate all the lines.
435 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200436 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200437
438 // free the lines only when using getsourceline()
439 if (evalarg->eval_cookie != NULL)
440 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200441 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200442 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200443 // Do not free the last line, "arg" points into it, free it
444 // later.
445 vim_free(evalarg->eval_tofree);
446 evalarg->eval_tofree =
447 ((char_u **)gap->ga_data)[gap->ga_len - 1];
448 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200449 ga_clear_strings(gap);
450 }
451 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200452 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200453 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200454
455 // free lines that were explicitly marked for freeing
456 ga_clear_strings(freegap);
457 }
458
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200459 gap->ga_itemsize = 0;
460 if (p == NULL)
461 return FAIL;
462 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200463 vim_free(evalarg->eval_tofree_lambda);
464 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200465 // Compute "end" relative to the end.
466 *end = *start + STRLEN(*start) - endoff;
467 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200468 }
469
470 return res;
471}
472
473/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100474 * Convert "tv" to a string.
475 * When "convert" is TRUE convert a List into a sequence of lines and convert
476 * a Float to a String.
477 * Returns an allocated string (NULL when out of memory).
478 */
479 char_u *
480typval2string(typval_T *tv, int convert)
481{
482 garray_T ga;
483 char_u *retval;
484#ifdef FEAT_FLOAT
485 char_u numbuf[NUMBUFLEN];
486#endif
487
488 if (convert && tv->v_type == VAR_LIST)
489 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000490 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100491 if (tv->vval.v_list != NULL)
492 {
493 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
494 if (tv->vval.v_list->lv_len > 0)
495 ga_append(&ga, NL);
496 }
497 ga_append(&ga, NUL);
498 retval = (char_u *)ga.ga_data;
499 }
500#ifdef FEAT_FLOAT
501 else if (convert && tv->v_type == VAR_FLOAT)
502 {
503 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
504 retval = vim_strsave(numbuf);
505 }
506#endif
507 else
508 retval = vim_strsave(tv_get_string(tv));
509 return retval;
510}
511
512/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200513 * Top level evaluation function, returning a string. Does not handle line
514 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000515 * When "convert" is TRUE convert a List into a sequence of lines and convert
516 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 * Return pointer to allocated memory, or NULL for failure.
518 */
519 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100520eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100521 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100522 int convert,
523 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524{
Bram Moolenaar33570922005-01-25 22:26:29 +0000525 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100527 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100529 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
530 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 retval = NULL;
532 else
533 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100534 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000535 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100537 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538
539 return retval;
540}
541
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100542 char_u *
543eval_to_string(
544 char_u *arg,
545 int convert)
546{
547 return eval_to_string_eap(arg, convert, NULL);
548}
549
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000551 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200552 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200553 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 */
555 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100556eval_to_string_safe(
557 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000558 int use_sandbox,
559 int keep_script_version)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560{
561 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200562 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200563 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200564 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565
Bram Moolenaar9530b582022-01-22 13:39:08 +0000566 if (!keep_script_version)
567 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200568 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000569 if (use_sandbox)
570 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200571 ++textwinlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200572 may_garbage_collect = FALSE;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200573 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000574 if (use_sandbox)
575 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200576 --textwinlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200577 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200578 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200579 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 return retval;
581}
582
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583/*
584 * Top level evaluation function, returning a number.
585 * Evaluates "expr" silently.
586 * Returns -1 for an error.
587 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200588 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100589eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590{
Bram Moolenaar33570922005-01-25 22:26:29 +0000591 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200592 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000593 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594
595 ++emsg_off;
596
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200597 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 retval = -1;
599 else
600 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100601 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000602 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 }
604 --emsg_off;
605
606 return retval;
607}
608
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000609/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000610 * Top level evaluation function.
611 * Returns an allocated typval_T with the result.
612 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000613 */
614 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200615eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000616{
617 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200618 evalarg_T evalarg;
619
620 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000621
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200622 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200623 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100624 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000625
Bram Moolenaar37c83712020-06-30 21:18:36 +0200626 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000627 return tv;
628}
629
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000631 * "*arg" points to what can be a function name in the form of "import.Name" or
632 * "Funcref". Return the name of the function. Set "tofree" to something that
633 * was allocated.
634 * If "verbose" is FALSE no errors are given.
635 * Return NULL for any failure.
636 */
637 static char_u *
638deref_function_name(
639 char_u **arg,
640 char_u **tofree,
641 evalarg_T *evalarg,
642 int verbose)
643{
644 typval_T ref;
645 char_u *name = *arg;
646
647 ref.v_type = VAR_UNKNOWN;
648 if (eval7(arg, &ref, evalarg, FALSE) == FAIL)
649 return NULL;
650 if (*skipwhite(*arg) != NUL)
651 {
652 if (verbose)
653 semsg(_(e_trailing_characters_str), *arg);
654 name = NULL;
655 }
656 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
657 {
658 name = ref.vval.v_string;
659 ref.vval.v_string = NULL;
660 *tofree = name;
661 }
662 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
663 {
664 if (ref.vval.v_partial->pt_argc > 0
665 || ref.vval.v_partial->pt_dict != NULL)
666 {
667 if (verbose)
668 emsg(_(e_cannot_use_partial_here));
669 name = NULL;
670 }
671 else
672 {
673 name = vim_strsave(partial_name(ref.vval.v_partial));
674 *tofree = name;
675 }
676 }
677 else
678 {
679 if (verbose)
680 semsg(_(e_not_callable_type_str), name);
681 name = NULL;
682 }
683 clear_tv(&ref);
684 return name;
685}
686
687/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100688 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200689 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
690 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000691 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200693 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100694call_vim_function(
695 char_u *func,
696 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200697 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200698 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000700 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200701 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000702 char_u *arg;
703 char_u *name;
704 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100706 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200707 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000708 funcexe.fe_firstline = curwin->w_cursor.lnum;
709 funcexe.fe_lastline = curwin->w_cursor.lnum;
710 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000711
712 // The name might be "import.Func" or "Funcref".
713 arg = func;
Bram Moolenaarb3d9cee2022-01-17 21:13:28 +0000714 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000715 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarb3d9cee2022-01-17 21:13:28 +0000716 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000717 if (name == NULL)
718 name = func;
719
720 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
721
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000722 if (ret == FAIL)
723 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000724 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000725
726 return ret;
727}
728
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100729/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100730 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000731 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
732 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000733 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000734 */
735 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100736call_func_retstr(
737 char_u *func,
738 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200739 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000740{
741 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000742 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000743
Bram Moolenaarded27a12018-08-01 19:06:03 +0200744 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000745 return NULL;
746
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100747 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000748 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 return retval;
750}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000751
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000752/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100753 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000754 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000755 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000756 */
757 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100758call_func_retlist(
759 char_u *func,
760 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200761 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000762{
763 typval_T rettv;
764
Bram Moolenaarded27a12018-08-01 19:06:03 +0200765 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000766 return NULL;
767
768 if (rettv.v_type != VAR_LIST)
769 {
770 clear_tv(&rettv);
771 return NULL;
772 }
773
774 return rettv.vval.v_list;
775}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776
Bram Moolenaare70dd112022-01-21 16:31:11 +0000777#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100779 * Evaluate "arg", which is 'foldexpr'.
780 * Note: caller must set "curwin" to match "arg".
781 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
782 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 */
784 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000785eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000787 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000788 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200789 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000791 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000792 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
793 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794
Bram Moolenaare70dd112022-01-21 16:31:11 +0000795 arg = wp->w_p_fde;
796 current_sctx = wp->w_p_script_ctx[WV_FDE];
797
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000799 if (use_sandbox)
800 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200801 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200803 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 retval = 0;
805 else
806 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100807 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000808 if (tv.v_type == VAR_NUMBER)
809 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000810 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 retval = 0;
812 else
813 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100814 // If the result is a string, check if there is a non-digit before
815 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000816 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 if (!VIM_ISDIGIT(*s) && *s != '-')
818 *cp = *s++;
819 retval = atol((char *)s);
820 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000821 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 }
823 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000824 if (use_sandbox)
825 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200826 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200827 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000828 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200830 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831}
832#endif
833
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000835 * Get an lval: variable, Dict item or List item that can be assigned a value
836 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
837 * "name.key", "name.key[expr]" etc.
838 * Indexing only works if "name" is an existing List or Dictionary.
839 * "name" points to the start of the name.
840 * If "rettv" is not NULL it points to the value to be assigned.
841 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
842 * wrong; must end in space or cmd separator.
843 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100844 * flags:
845 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100846 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100847 * GLV_NO_AUTOLOAD: do not use script autoloading
848 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000849 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000850 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000851 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000852 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200853 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100854get_lval(
855 char_u *name,
856 typval_T *rettv,
857 lval_T *lp,
858 int unlet,
859 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100860 int flags, // GLV_ values
861 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000862{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000863 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000864 char_u *expr_start, *expr_end;
865 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000866 dictitem_T *v;
867 typval_T var1;
868 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000869 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000870 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000871 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100872 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100873 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100874 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000875
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100876 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200877 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000878
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100879 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000880 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100881 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000882 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200883 lp->ll_name_end = find_name_end(name, NULL, NULL,
884 FNE_INCL_BR | fne_flags);
885 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000886 }
887
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100888 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000889 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100890 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000891 if (expr_start != NULL)
892 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100893 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100894 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000895 && *p != '[' && *p != '.')
896 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000897 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000898 return NULL;
899 }
900
901 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
902 if (lp->ll_exp_name == NULL)
903 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100904 // Report an invalid expression in braces, unless the
905 // expression evaluation has been cancelled due to an
906 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000907 if (!aborting() && !quiet)
908 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000909 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000910 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000911 return NULL;
912 }
913 }
914 lp->ll_name = lp->ll_exp_name;
915 }
916 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100917 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000918 lp->ll_name = name;
919
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200920 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100921 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200922 // "a: type" is declaring variable "a" with a type, not "a:".
923 if (p == name + 2 && p[-1] == ':')
924 {
925 --p;
926 lp->ll_name_end = p;
927 }
928 if (*p == ':')
929 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000930 garray_T tmp_type_list;
931 garray_T *type_list;
932 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +0200933
934 if (tp == p + 1 && !quiet)
935 {
936 semsg(_(e_white_space_required_after_str_str), ":", p);
937 return NULL;
938 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100939
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000940 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
941 type_list = &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list;
942 else
943 {
944 type_list = &tmp_type_list;
945 ga_init2(type_list, sizeof(type_T), 10);
946 }
947
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200948 // parse the type after the name
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000949 lp->ll_type = parse_type(&tp, type_list, !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100950 if (lp->ll_type == NULL && !quiet)
951 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200952 lp->ll_name_end = tp;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000953
954 // drop the type when not in a script
955 if (type_list == &tmp_type_list)
956 {
957 lp->ll_type = NULL;
958 clear_type_list(type_list);
959 }
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200960 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100961 }
962 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000963 if (lp->ll_name == NULL)
964 return p;
965
966 if (*p == '.' && in_vim9script())
967 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000968 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name,
969 TRUE, NULL);
970
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000971 if (import != NULL)
972 {
973 ufunc_T *ufunc;
974 type_T *type;
975
976 lp->ll_sid = import->imp_sid;
977 lp->ll_name = skipwhite(p + 1);
978 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
979 lp->ll_name_end = p;
980
981 // check the item is exported
982 cc = *p;
983 *p = NUL;
984 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
985 NULL, TRUE) == -1)
986 {
987 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +0000988 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000989 }
990 *p = cc;
991 }
992 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100993
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100994 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000995 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000996 return p;
997
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200998 if (in_vim9script() && lval_root != NULL)
999 {
1000 // using local variable
1001 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001002 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001003 }
1004 else
1005 {
1006 cc = *p;
1007 *p = NUL;
1008 // When we would write to the variable pass &ht and prevent autoload.
1009 writing = !(flags & GLV_READ_ONLY);
1010 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001011 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001012 if (v == NULL && !quiet)
1013 semsg(_(e_undefined_variable_str), lp->ll_name);
1014 *p = cc;
1015 if (v == NULL)
1016 return NULL;
1017 lp->ll_tv = &v->di_tv;
1018 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001019
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001020 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
1021 {
1022 if (!quiet)
1023 semsg(_(e_variable_already_declared), lp->ll_name);
1024 return NULL;
1025 }
1026
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001027 /*
1028 * Loop until no more [idx] or .key is following.
1029 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001030 var1.v_type = VAR_UNKNOWN;
1031 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001032 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001033 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001034 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1035 {
1036 if (!quiet)
1037 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1038 return NULL;
1039 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001040 if (lp->ll_tv->v_type != VAR_LIST
1041 && lp->ll_tv->v_type != VAR_DICT
1042 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001043 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001044 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001045 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001046 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001047 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001048
1049 // a NULL list/blob works like an empty list/blob, allocate one now.
1050 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1051 rettv_list_alloc(lp->ll_tv);
1052 else if (lp->ll_tv->v_type == VAR_BLOB
1053 && lp->ll_tv->vval.v_blob == NULL)
1054 rettv_blob_alloc(lp->ll_tv);
1055
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001056 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001057 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001058 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001059 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001060 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001061 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001062
Bram Moolenaar10c65862020-10-08 21:16:42 +02001063 if (in_vim9script() && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001064 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001065 && lp->ll_tv == &v->di_tv
1066 && ht != NULL && ht == get_script_local_ht())
1067 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001068 svar_T *sv = find_typval_in_script(lp->ll_tv, 0);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001069
1070 // Vim9 script local variable: get the type
1071 if (sv != NULL)
1072 lp->ll_valtype = sv->sv_type;
1073 }
1074
Bram Moolenaar8c711452005-01-14 21:53:12 +00001075 len = -1;
1076 if (*p == '.')
1077 {
1078 key = p + 1;
1079 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1080 ;
1081 if (len == 0)
1082 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001083 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001084 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001085 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001086 }
1087 p = key + len;
1088 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001089 else
1090 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001091 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001092 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001093 if (*p == ':')
1094 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001095 else
1096 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001097 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001098 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001099 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001100 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001101 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001102 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001103 clear_tv(&var1);
1104 return NULL;
1105 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001106 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001107 }
1108
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001109 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001110 if (*p == ':')
1111 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001112 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001113 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001114 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001115 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001116 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001117 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001118 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001119 if (rettv != NULL
1120 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001121 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001122 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001123 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001124 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001125 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001126 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001127 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001128 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001129 }
1130 p = skipwhite(p + 1);
1131 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001132 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001133 else
1134 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001135 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001136 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001137 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001138 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001139 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001140 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001141 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001142 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001143 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001144 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001145 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001146 clear_tv(&var2);
1147 return NULL;
1148 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001149 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001150 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001151 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001152 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001153 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001154
Bram Moolenaar8c711452005-01-14 21:53:12 +00001155 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001156 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001157 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001158 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001159 clear_tv(&var1);
1160 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001161 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001162 }
1163
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001164 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001165 ++p;
1166 }
1167
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001168 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001169 {
1170 if (len == -1)
1171 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001172 // "[key]": get key from "var1"
1173 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001174 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001175 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001176 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001177 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001178 }
1179 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001180 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001181
1182 // a NULL dict is equivalent with an empty dict
1183 if (lp->ll_tv->vval.v_dict == NULL)
1184 {
1185 lp->ll_tv->vval.v_dict = dict_alloc();
1186 if (lp->ll_tv->vval.v_dict == NULL)
1187 {
1188 clear_tv(&var1);
1189 return NULL;
1190 }
1191 ++lp->ll_tv->vval.v_dict->dv_refcount;
1192 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001193 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001194
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001195 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001196
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001197 // When assigning to a scope dictionary check that a function and
1198 // variable name is valid (only variable name unless it is l: or
1199 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001200 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001201 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001202 int prevval;
1203 int wrong;
1204
1205 if (len != -1)
1206 {
1207 prevval = key[len];
1208 key[len] = NUL;
1209 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001210 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001211 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001212 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1213 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001214 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001215 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001216 if (len != -1)
1217 key[len] = prevval;
1218 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001219 {
1220 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001221 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001222 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001223 }
1224
Bram Moolenaar10c65862020-10-08 21:16:42 +02001225 if (lp->ll_valtype != NULL)
1226 // use the type of the member
1227 lp->ll_valtype = lp->ll_valtype->tt_member;
1228
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001229 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001230 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001231 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001232 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001233 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001234 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001235 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001236 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001237 return NULL;
1238 }
1239
Bram Moolenaar31b81602019-02-10 22:14:27 +01001240 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001241 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001242 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001243 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001244 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001245 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001246 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001247 }
1248 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001249 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001250 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001251 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001252 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001253 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001254 p = NULL;
1255 break;
1256 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001257 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001258 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001259 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1260 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001261 {
1262 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001263 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001264 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001265
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001266 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001267 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001268 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001269 else if (lp->ll_tv->v_type == VAR_BLOB)
1270 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001271 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1272
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001273 /*
1274 * Get the number and item for the only or first index of the List.
1275 */
1276 if (empty1)
1277 lp->ll_n1 = 0;
1278 else
1279 // is number or string
1280 lp->ll_n1 = (long)tv_get_number(&var1);
1281 clear_tv(&var1);
1282
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001283 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001284 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001285 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001286 return NULL;
1287 }
1288 if (lp->ll_range && !lp->ll_empty2)
1289 {
1290 lp->ll_n2 = (long)tv_get_number(&var2);
1291 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001292 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1293 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001294 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001295 }
1296 lp->ll_blob = lp->ll_tv->vval.v_blob;
1297 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001298 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001299 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001300 else
1301 {
1302 /*
1303 * Get the number and item for the only or first index of the List.
1304 */
1305 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001306 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001307 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001308 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001309 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001310 clear_tv(&var1);
1311
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001312 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001313 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001314 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001315 if (lp->ll_li == NULL)
1316 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001317 clear_tv(&var2);
1318 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001319 }
1320
Bram Moolenaar10c65862020-10-08 21:16:42 +02001321 if (lp->ll_valtype != NULL)
1322 // use the type of the member
1323 lp->ll_valtype = lp->ll_valtype->tt_member;
1324
Bram Moolenaar8c711452005-01-14 21:53:12 +00001325 /*
1326 * May need to find the item or absolute index for the second
1327 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001328 * When no index given: "lp->ll_empty2" is TRUE.
1329 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001330 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001331 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001332 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001333 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001334 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001335 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001336 if (check_range_index_two(lp->ll_list,
1337 &lp->ll_n1, lp->ll_li,
1338 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001339 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001340 }
1341
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001342 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001343 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001344 }
1345
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001346 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001347 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001348 return p;
1349}
1350
1351/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001352 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001353 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001354 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001355clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001356{
1357 vim_free(lp->ll_exp_name);
1358 vim_free(lp->ll_newkey);
1359}
1360
1361/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001362 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001363 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001364 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1365 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001366 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001367 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001368set_var_lval(
1369 lval_T *lp,
1370 char_u *endp,
1371 typval_T *rettv,
1372 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001373 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1374 char_u *op,
1375 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001376{
1377 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001378 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001379
1380 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001381 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001382 cc = *endp;
1383 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001384 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1385 return;
1386
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001387 if (lp->ll_blob != NULL)
1388 {
1389 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001390
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001391 if (op != NULL && *op != '=')
1392 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001393 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001394 return;
1395 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001396 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001397 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001398
1399 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1400 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001401 if (lp->ll_empty2)
1402 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1403
Bram Moolenaar68452172021-04-12 21:21:02 +02001404 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1405 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001406 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001407 }
1408 else
1409 {
1410 val = (int)tv_get_number_chk(rettv, &error);
1411 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001412 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001413 }
1414 }
1415 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001416 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001417 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001418
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001419 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1420 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001421 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001422 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001423 *endp = cc;
1424 return;
1425 }
1426
Bram Moolenaarff697e62019-02-12 22:28:33 +01001427 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001428 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001429 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001430 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001431 {
1432 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001433 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1434 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001435 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001436 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001437 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001438 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001439 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001440 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001441 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001442 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001443 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1444 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001445 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001446 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001447 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001448 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001449 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001450 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001451 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001452 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001453 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001454 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001455 else if (lp->ll_range)
1456 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001457 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1458 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001459 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001460 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001461 return;
1462 }
1463
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001464 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1465 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001466 }
1467 else
1468 {
1469 /*
1470 * Assign to a List or Dictionary item.
1471 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001472 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1473 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001474 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001475 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001476 return;
1477 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001478
1479 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001480 && check_typval_arg_type(lp->ll_valtype, rettv,
1481 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001482 return;
1483
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001484 if (lp->ll_newkey != NULL)
1485 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001486 if (op != NULL && *op != '=')
1487 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001488 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001489 return;
1490 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001491 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1492 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001493 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001494
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001495 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001496 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001497 if (di == NULL)
1498 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001499 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1500 {
1501 vim_free(di);
1502 return;
1503 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001504 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001505 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001506 else if (op != NULL && *op != '=')
1507 {
1508 tv_op(lp->ll_tv, rettv, op);
1509 return;
1510 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001511 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001512 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001513
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001514 /*
1515 * Assign the value to the variable or list item.
1516 */
1517 if (copy)
1518 copy_tv(rettv, lp->ll_tv);
1519 else
1520 {
1521 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001522 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001523 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001524 }
1525 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001526}
1527
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001528/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001529 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1530 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001531 * Returns OK or FAIL.
1532 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001533 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001534tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001535{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001536 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001537 char_u numbuf[NUMBUFLEN];
1538 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001539 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001540
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001541 // Can't do anything with a Funcref or Dict on the right.
1542 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001543 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001544 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1545 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001546 {
1547 switch (tv1->v_type)
1548 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001549 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001550 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001551 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001552 case VAR_DICT:
1553 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001554 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001555 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001556 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001557 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001558 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001559 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001560 break;
1561
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001562 case VAR_BLOB:
1563 if (*op != '+' || tv2->v_type != VAR_BLOB)
1564 break;
1565 // BLOB += BLOB
1566 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1567 {
1568 blob_T *b1 = tv1->vval.v_blob;
1569 blob_T *b2 = tv2->vval.v_blob;
1570 int i, len = blob_len(b2);
1571 for (i = 0; i < len; i++)
1572 ga_append(&b1->bv_ga, blob_get(b2, i));
1573 }
1574 return OK;
1575
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001576 case VAR_LIST:
1577 if (*op != '+' || tv2->v_type != VAR_LIST)
1578 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001579 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001580 if (tv2->vval.v_list != NULL)
1581 {
1582 if (tv1->vval.v_list == NULL)
1583 {
1584 tv1->vval.v_list = tv2->vval.v_list;
1585 ++tv1->vval.v_list->lv_refcount;
1586 }
1587 else
1588 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1589 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001590 return OK;
1591
1592 case VAR_NUMBER:
1593 case VAR_STRING:
1594 if (tv2->v_type == VAR_LIST)
1595 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001596 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001597 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001598 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001599 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001600#ifdef FEAT_FLOAT
1601 if (tv2->v_type == VAR_FLOAT)
1602 {
1603 float_T f = n;
1604
Bram Moolenaarff697e62019-02-12 22:28:33 +01001605 if (*op == '%')
1606 break;
1607 switch (*op)
1608 {
1609 case '+': f += tv2->vval.v_float; break;
1610 case '-': f -= tv2->vval.v_float; break;
1611 case '*': f *= tv2->vval.v_float; break;
1612 case '/': f /= tv2->vval.v_float; break;
1613 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001614 clear_tv(tv1);
1615 tv1->v_type = VAR_FLOAT;
1616 tv1->vval.v_float = f;
1617 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001618 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001619#endif
1620 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001621 switch (*op)
1622 {
1623 case '+': n += tv_get_number(tv2); break;
1624 case '-': n -= tv_get_number(tv2); break;
1625 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001626 case '/': n = num_divide(n, tv_get_number(tv2),
1627 &failed); break;
1628 case '%': n = num_modulus(n, tv_get_number(tv2),
1629 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001630 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001631 clear_tv(tv1);
1632 tv1->v_type = VAR_NUMBER;
1633 tv1->vval.v_number = n;
1634 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001635 }
1636 else
1637 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001638 if (tv2->v_type == VAR_FLOAT)
1639 break;
1640
Bram Moolenaarff697e62019-02-12 22:28:33 +01001641 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001642 s = tv_get_string(tv1);
1643 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001644 clear_tv(tv1);
1645 tv1->v_type = VAR_STRING;
1646 tv1->vval.v_string = s;
1647 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001648 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001649
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001650 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001651#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001652 {
1653 float_T f;
1654
Bram Moolenaarff697e62019-02-12 22:28:33 +01001655 if (*op == '%' || *op == '.'
1656 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001657 && tv2->v_type != VAR_NUMBER
1658 && tv2->v_type != VAR_STRING))
1659 break;
1660 if (tv2->v_type == VAR_FLOAT)
1661 f = tv2->vval.v_float;
1662 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001663 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001664 switch (*op)
1665 {
1666 case '+': tv1->vval.v_float += f; break;
1667 case '-': tv1->vval.v_float -= f; break;
1668 case '*': tv1->vval.v_float *= f; break;
1669 case '/': tv1->vval.v_float /= f; break;
1670 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001671 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001672#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001673 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001674 }
1675 }
1676
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001677 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001678 return FAIL;
1679}
1680
1681/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001682 * Evaluate the expression used in a ":for var in expr" command.
1683 * "arg" points to "var".
1684 * Set "*errp" to TRUE for an error, FALSE otherwise;
1685 * Return a pointer that holds the info. Null when there is an error.
1686 */
1687 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001688eval_for_line(
1689 char_u *arg,
1690 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001691 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001692 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001693{
Bram Moolenaar33570922005-01-25 22:26:29 +00001694 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001695 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001696 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001697 typval_T tv;
1698 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001699 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001700
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001701 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001702
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001703 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001704 if (fi == NULL)
1705 return NULL;
1706
Bram Moolenaar404557e2021-07-05 21:41:48 +02001707 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1708 &fi->fi_semicolon, FALSE);
1709 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001710 return fi;
1711
Bram Moolenaar404557e2021-07-05 21:41:48 +02001712 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001713 if (expr[0] != 'i' || expr[1] != 'n'
1714 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001715 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001716 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1717 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1718 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001719 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001720 return fi;
1721 }
1722
1723 if (skip)
1724 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001725 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1726 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001727 {
1728 *errp = FALSE;
1729 if (!skip)
1730 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001731 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001732 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001733 l = tv.vval.v_list;
1734 if (l == NULL)
1735 {
1736 // a null list is like an empty list: do nothing
1737 clear_tv(&tv);
1738 }
1739 else
1740 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001742 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001743
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001744 // No need to increment the refcount, it's already set for
1745 // the list being used in "tv".
1746 fi->fi_list = l;
1747 list_add_watch(l, &fi->fi_lw);
1748 fi->fi_lw.lw_item = l->lv_first;
1749 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001750 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001751 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001752 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001753 fi->fi_bi = 0;
1754 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001755 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001756 typval_T btv;
1757
1758 // Make a copy, so that the iteration still works when the
1759 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001760 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001761 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001762 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001763 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001764 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001765 else if (tv.v_type == VAR_STRING)
1766 {
1767 fi->fi_byte_idx = 0;
1768 fi->fi_string = tv.vval.v_string;
1769 tv.vval.v_string = NULL;
1770 if (fi->fi_string == NULL)
1771 fi->fi_string = vim_strsave((char_u *)"");
1772 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001773 else
1774 {
Sean Dewar80d73952021-08-04 19:25:54 +02001775 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001776 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001777 }
1778 }
1779 }
1780 if (skip)
1781 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001782 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001783
1784 return fi;
1785}
1786
1787/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001788 * Used when looping over a :for line, skip the "in expr" part.
1789 */
1790 void
1791skip_for_lines(void *fi_void, evalarg_T *evalarg)
1792{
1793 forinfo_T *fi = (forinfo_T *)fi_void;
1794 int i;
1795
1796 for (i = 0; i < fi->fi_break_count; ++i)
1797 eval_next_line(evalarg);
1798}
1799
1800/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001801 * Use the first item in a ":for" list. Advance to the next.
1802 * Assign the values to the variable (list). "arg" points to the first one.
1803 * Return TRUE when a valid item was found, FALSE when at end of list or
1804 * something wrong.
1805 */
1806 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001807next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001808{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001809 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001810 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001811 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001812 ? (ASSIGN_FINAL
1813 // first round: error if variable exists
1814 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1815 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001816 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001817 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001818 int skip_assign = in_vim9script() && arg[0] == '_'
1819 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001820
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001821 if (fi->fi_blob != NULL)
1822 {
1823 typval_T tv;
1824
1825 if (fi->fi_bi >= blob_len(fi->fi_blob))
1826 return FALSE;
1827 tv.v_type = VAR_NUMBER;
1828 tv.v_lock = VAR_FIXED;
1829 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1830 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001831 if (skip_assign)
1832 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001833 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001834 fi->fi_varcount, flag, NULL) == OK;
1835 }
1836
1837 if (fi->fi_string != NULL)
1838 {
1839 typval_T tv;
1840 int len;
1841
1842 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1843 if (len == 0)
1844 return FALSE;
1845 tv.v_type = VAR_STRING;
1846 tv.v_lock = VAR_FIXED;
1847 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1848 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001849 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001850 if (skip_assign)
1851 result = TRUE;
1852 else
1853 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001854 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001855 vim_free(tv.vval.v_string);
1856 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001857 }
1858
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001859 item = fi->fi_lw.lw_item;
1860 if (item == NULL)
1861 result = FALSE;
1862 else
1863 {
1864 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001865 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001866 if (skip_assign)
1867 result = TRUE;
1868 else
1869 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001870 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001871 }
1872 return result;
1873}
1874
1875/*
1876 * Free the structure used to store info used by ":for".
1877 */
1878 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001879free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001880{
Bram Moolenaar33570922005-01-25 22:26:29 +00001881 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001882
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001883 if (fi == NULL)
1884 return;
1885 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001886 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001887 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001888 list_unref(fi->fi_list);
1889 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001890 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001891 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001892 else
1893 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001894 vim_free(fi);
1895}
1896
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001898set_context_for_expression(
1899 expand_T *xp,
1900 char_u *arg,
1901 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001903 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001905 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001907 if (cmdidx == CMD_let || cmdidx == CMD_var
1908 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001909 {
1910 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001911 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001913 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001914 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001915 {
1916 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001917 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001918 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 break;
1920 }
1921 return;
1922 }
1923 }
1924 else
1925 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1926 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 while ((xp->xp_pattern = vim_strpbrk(arg,
1928 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1929 {
1930 c = *xp->xp_pattern;
1931 if (c == '&')
1932 {
1933 c = xp->xp_pattern[1];
1934 if (c == '&')
1935 {
1936 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001937 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 }
1939 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001940 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001942 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1943 xp->xp_pattern += 2;
1944
1945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 }
1947 else if (c == '$')
1948 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001949 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 xp->xp_context = EXPAND_ENV_VARS;
1951 }
1952 else if (c == '=')
1953 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001954 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 xp->xp_context = EXPAND_EXPRESSION;
1956 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001957 else if (c == '#'
1958 && xp->xp_context == EXPAND_EXPRESSION)
1959 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001960 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001961 break;
1962 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001963 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 && xp->xp_context == EXPAND_FUNCTIONS
1965 && vim_strchr(xp->xp_pattern, '(') == NULL)
1966 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001967 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 break;
1969 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001970 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001972 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 {
1974 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1975 if (c == '\\' && xp->xp_pattern[1] != NUL)
1976 ++xp->xp_pattern;
1977 xp->xp_context = EXPAND_NOTHING;
1978 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001979 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001981 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1983 /* skip */ ;
1984 xp->xp_context = EXPAND_NOTHING;
1985 }
1986 else if (c == '|')
1987 {
1988 if (xp->xp_pattern[1] == '|')
1989 {
1990 ++xp->xp_pattern;
1991 xp->xp_context = EXPAND_EXPRESSION;
1992 }
1993 else
1994 xp->xp_context = EXPAND_COMMANDS;
1995 }
1996 else
1997 xp->xp_context = EXPAND_EXPRESSION;
1998 }
1999 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002000 // Doesn't look like something valid, expand as an expression
2001 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002002 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 arg = xp->xp_pattern;
2004 if (*arg != NUL)
2005 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2006 /* skip */ ;
2007 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002008
2009 // ":exe one two" completes "two"
2010 if ((cmdidx == CMD_execute
2011 || cmdidx == CMD_echo
2012 || cmdidx == CMD_echon
2013 || cmdidx == CMD_echomsg)
2014 && xp->xp_context == EXPAND_EXPRESSION)
2015 {
2016 for (;;)
2017 {
2018 char_u *n = skiptowhite(arg);
2019
2020 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2021 break;
2022 arg = skipwhite(n);
2023 }
2024 }
2025
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 xp->xp_pattern = arg;
2027}
2028
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002030 * Return TRUE if "pat" matches "text".
2031 * Does not use 'cpo' and always uses 'magic'.
2032 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002033 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002034pattern_match(char_u *pat, char_u *text, int ic)
2035{
2036 int matches = FALSE;
2037 char_u *save_cpo;
2038 regmatch_T regmatch;
2039
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002040 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002041 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002042 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002043 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2044 if (regmatch.regprog != NULL)
2045 {
2046 regmatch.rm_ic = ic;
2047 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2048 vim_regfree(regmatch.regprog);
2049 }
2050 p_cpo = save_cpo;
2051 return matches;
2052}
2053
2054/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002055 * Handle a name followed by "(". Both for just "name(arg)" and for
2056 * "expr->name(arg)".
2057 * Returns OK or FAIL.
2058 */
2059 static int
2060eval_func(
2061 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002062 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002063 char_u *name,
2064 int name_len,
2065 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002066 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002067 typval_T *basetv) // "expr" for "expr->name(arg)"
2068{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002069 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002070 char_u *s = name;
2071 int len = name_len;
2072 partial_T *partial;
2073 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002074 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002075 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002076
2077 if (!evaluate)
2078 check_vars(s, len);
2079
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002080 // If "s" is the name of a variable of type VAR_FUNC
2081 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002082 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002083 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002084
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002085 // Need to make a copy, in case evaluating the arguments makes
2086 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002087 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002088 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002089 ret = FAIL;
2090 else
2091 {
2092 funcexe_T funcexe;
2093
2094 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002095 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002096 funcexe.fe_firstline = curwin->w_cursor.lnum;
2097 funcexe.fe_lastline = curwin->w_cursor.lnum;
2098 funcexe.fe_evaluate = evaluate;
2099 funcexe.fe_partial = partial;
2100 funcexe.fe_basetv = basetv;
2101 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002102 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002103 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002104 }
2105 vim_free(s);
2106
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002107 // If evaluate is FALSE rettv->v_type was not set in
2108 // get_func_tv, but it's needed in handle_subscript() to parse
2109 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002110 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2111 {
2112 rettv->vval.v_string = NULL;
2113 rettv->v_type = VAR_FUNC;
2114 }
2115
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002116 // Stop the expression evaluation when immediately
2117 // aborting on error, or when an interrupt occurred or
2118 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002119 if (evaluate && aborting())
2120 {
2121 if (ret == OK)
2122 clear_tv(rettv);
2123 ret = FAIL;
2124 }
2125 return ret;
2126}
2127
2128/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002129 * Get the next line source line without advancing. But do skip over comment
2130 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002131 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002132 */
2133 static char_u *
2134getline_peek_skip_comments(evalarg_T *evalarg)
2135{
2136 for (;;)
2137 {
2138 char_u *next = getline_peek(evalarg->eval_getline,
2139 evalarg->eval_cookie);
2140 char_u *p;
2141
2142 if (next == NULL)
2143 break;
2144 p = skipwhite(next);
2145 if (*p != NUL && !vim9_comment_start(p))
2146 return next;
2147 (void)eval_next_line(evalarg);
2148 }
2149 return NULL;
2150}
2151
2152/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002153 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2154 * comment) and there is a next line, return the next line (skipping blanks)
2155 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002156 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2157 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002158 * "arg" must point somewhere inside a line, not at the start.
2159 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002160 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002161eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2162{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002163 char_u *p = skipwhite(arg);
2164
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002165 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002166 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002167 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002168 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002169 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002170 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002171 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002172
2173 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002174 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002175 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002176 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002177
Bram Moolenaar9d489562020-07-30 20:08:50 +02002178 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002179 {
2180 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002181 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002182 }
2183 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002184 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002185}
2186
2187/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002188 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002189 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002190 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002191 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002192eval_next_line(evalarg_T *evalarg)
2193{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002194 garray_T *gap = &evalarg->eval_ga;
2195 char_u *line;
2196
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002197 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002198 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2199 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002200 else
2201 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002202 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002203 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2204 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002205 char_u *p = skipwhite(line);
2206
2207 // Going to concatenate the lines after parsing. For an empty or
2208 // comment line use an empty string.
2209 if (*p == NUL || vim9_comment_start(p))
2210 {
2211 vim_free(line);
2212 line = vim_strsave((char_u *)"");
2213 }
2214
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002215 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2216 ++gap->ga_len;
2217 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002218 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002219 {
2220 vim_free(evalarg->eval_tofree);
2221 evalarg->eval_tofree = line;
2222 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002223
2224 // Advanced to the next line, "arg" no longer points into the previous
2225 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002226 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002227 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002228}
2229
Bram Moolenaare6b53242020-07-01 17:28:33 +02002230/*
2231 * Call eval_next_non_blank() and get the next line if needed.
2232 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002233 char_u *
2234skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2235{
2236 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002237 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002238
Bram Moolenaar962d7212020-07-04 14:15:00 +02002239 if (evalarg == NULL)
2240 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002241 eval_next_non_blank(p, evalarg, &getnext);
2242 if (getnext)
2243 return eval_next_line(evalarg);
2244 return p;
2245}
2246
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002247/*
Bram Moolenaar844fb642021-10-23 13:32:30 +01002248 * Initialize "evalarg" for use.
2249 */
2250 void
2251init_evalarg(evalarg_T *evalarg)
2252{
2253 CLEAR_POINTER(evalarg);
2254 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
2255}
2256
2257/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002258 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002259 */
2260 void
2261clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2262{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002263 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002264 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002265 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002266 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002267 if (eap != NULL)
2268 {
2269 // We may need to keep the original command line, e.g. for
2270 // ":let" it has the variable names. But we may also need the
2271 // new one, "nextcmd" points into it. Keep both.
2272 vim_free(eap->cmdline_tofree);
2273 eap->cmdline_tofree = *eap->cmdlinep;
2274 *eap->cmdlinep = evalarg->eval_tofree;
2275 }
2276 else
2277 vim_free(evalarg->eval_tofree);
2278 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002279 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002280
Bram Moolenaar844fb642021-10-23 13:32:30 +01002281 ga_clear_strings(&evalarg->eval_tofree_ga);
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002282 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002283 }
2284}
2285
2286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2290 */
2291
2292/*
2293 * Handle zero level expression.
2294 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002296 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002297 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 * Return OK or FAIL.
2299 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002300 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002301eval0(
2302 char_u *arg,
2303 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002304 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002305 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002307 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2308}
2309
2310/*
2311 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2312 * expression and don't check what comes after the expression.
2313 */
2314 int
2315eval0_retarg(
2316 char_u *arg,
2317 typval_T *rettv,
2318 exarg_T *eap,
2319 evalarg_T *evalarg,
2320 char_u **retarg)
2321{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 int ret;
2323 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002324 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002325 int did_emsg_before = did_emsg;
2326 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002327 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002328 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002329 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330
2331 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002332 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002333 expr_end = p;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002334 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002335
Bram Moolenaar02929a32021-12-17 14:46:12 +00002336 // In Vim9 script a command block is not split at NL characters for
2337 // commands using an expression argument. Skip over a '#' comment to check
2338 // for a following NL. Require white space before the '#'.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002339 if (in_vim9script() && p > expr_end && retarg == NULL)
Bram Moolenaar02929a32021-12-17 14:46:12 +00002340 while (*p == '#')
2341 {
2342 char_u *nl = vim_strchr(p, NL);
2343
2344 if (nl == NULL)
2345 break;
2346 p = skipwhite(nl + 1);
2347 if (eap != NULL && *p != NUL)
2348 eap->nextcmd = p;
2349 check_for_end = FALSE;
2350 }
2351
2352 if (ret != FAIL && check_for_end)
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002353 end_error = !ends_excmd2(arg, p);
2354 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 {
2356 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002357 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 /*
2359 * Report the invalid expression unless the expression evaluation has
2360 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002361 * exception, or we already gave a more specific error.
2362 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002364 if (!aborting()
2365 && did_emsg == did_emsg_before
2366 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002367 && (flags & EVAL_CONSTANT) == 0
2368 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002369 {
2370 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002371 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002372 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002373 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002374 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002375
2376 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002377 // a next command to avoid more errors, unless "|" is following, which
2378 // could only be a command separator.
2379 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2380 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002381 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002383
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002384 if (retarg != NULL)
2385 *retarg = p;
2386 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002387 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002388
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 return ret;
2390}
2391
2392/*
2393 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002394 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002395 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 *
2397 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002398 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002400 * Note: "rettv.v_lock" is not set.
2401 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 * Return OK or FAIL.
2403 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002404 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002405eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002407 char_u *p;
2408 int getnext;
2409
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002410 CLEAR_POINTER(rettv);
2411
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 /*
2413 * Get the first variable.
2414 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002415 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 return FAIL;
2417
Bram Moolenaar793648f2020-06-26 21:28:25 +02002418 p = eval_next_non_blank(*arg, evalarg, &getnext);
2419 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002421 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002422 int result;
2423 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002424 evalarg_T *evalarg_used = evalarg;
2425 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002426 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002427 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002428 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002429
2430 if (evalarg == NULL)
2431 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002432 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002433 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002434 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002435 orig_flags = evalarg_used->eval_flags;
2436 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002437
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002438 if (getnext)
2439 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002440 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002441 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002442 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002443 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002444 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002445 clear_tv(rettv);
2446 return FAIL;
2447 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002448 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002449 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002450
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002452 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002454 int error = FALSE;
2455
Bram Moolenaar13106602020-10-04 16:06:05 +02002456 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002457 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002458 else if (vim9script)
2459 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002460 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002462 if (error || !op_falsy || !result)
2463 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002464 if (error)
2465 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 }
2467
2468 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002469 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002471 if (op_falsy)
2472 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002473 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002474 {
mityu4ac198c2021-05-28 17:52:40 +02002475 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002476 clear_tv(rettv);
2477 return FAIL;
2478 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002479 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002480 evalarg_used->eval_flags = (op_falsy ? !result : result)
2481 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2482 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002483 {
2484 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002486 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002487 if (!op_falsy || !result)
2488 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002490 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002492 /*
2493 * Check for the ":".
2494 */
2495 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2496 if (*p != ':')
2497 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002498 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002499 if (evaluate && result)
2500 clear_tv(rettv);
2501 evalarg_used->eval_flags = orig_flags;
2502 return FAIL;
2503 }
2504 if (getnext)
2505 *arg = eval_next_line(evalarg_used);
2506 else
2507 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002508 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002509 {
2510 error_white_both(p, 1);
2511 clear_tv(rettv);
2512 evalarg_used->eval_flags = orig_flags;
2513 return FAIL;
2514 }
2515 *arg = p;
2516 }
2517
2518 /*
2519 * Get the third variable. Recursive!
2520 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002521 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002522 {
mityu4ac198c2021-05-28 17:52:40 +02002523 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002524 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002525 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002526 return FAIL;
2527 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002528 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2529 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002530 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002531 if (eval1(arg, &var2, evalarg_used) == FAIL)
2532 {
2533 if (evaluate && result)
2534 clear_tv(rettv);
2535 evalarg_used->eval_flags = orig_flags;
2536 return FAIL;
2537 }
2538 if (evaluate && !result)
2539 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002541
2542 if (evalarg == NULL)
2543 clear_evalarg(&local_evalarg, NULL);
2544 else
2545 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 }
2547
2548 return OK;
2549}
2550
2551/*
2552 * Handle first level expression:
2553 * expr2 || expr2 || expr2 logical OR
2554 *
2555 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002556 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 *
2558 * Return OK or FAIL.
2559 */
2560 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002561eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002563 char_u *p;
2564 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565
2566 /*
2567 * Get the first variable.
2568 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002569 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 return FAIL;
2571
2572 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002573 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002575 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002576 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002578 evalarg_T *evalarg_used = evalarg;
2579 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002580 int evaluate;
2581 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002582 long result = FALSE;
2583 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002584 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002585 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002586
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002587 if (evalarg == NULL)
2588 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002589 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002590 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002591 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002592 orig_flags = evalarg_used->eval_flags;
2593 evaluate = orig_flags & EVAL_EVALUATE;
2594 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002595 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002596 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002597 result = tv_get_bool_chk(rettv, &error);
2598 else if (tv_get_number_chk(rettv, &error) != 0)
2599 result = TRUE;
2600 clear_tv(rettv);
2601 if (error)
2602 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 }
2604
2605 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002606 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002608 while (p[0] == '|' && p[1] == '|')
2609 {
2610 if (getnext)
2611 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002612 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002613 {
2614 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2615 {
2616 error_white_both(p, 2);
2617 clear_tv(rettv);
2618 return FAIL;
2619 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002620 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002621 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002622
2623 /*
2624 * Get the second variable.
2625 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002626 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2627 {
mityu4ac198c2021-05-28 17:52:40 +02002628 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002629 clear_tv(rettv);
2630 return FAIL;
2631 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002632 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2633 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002634 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002635 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002636 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002637
2638 /*
2639 * Compute the result.
2640 */
2641 if (evaluate && !result)
2642 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002643 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002644 result = tv_get_bool_chk(&var2, &error);
2645 else if (tv_get_number_chk(&var2, &error) != 0)
2646 result = TRUE;
2647 clear_tv(&var2);
2648 if (error)
2649 return FAIL;
2650 }
2651 if (evaluate)
2652 {
2653 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002654 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002655 rettv->v_type = VAR_BOOL;
2656 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002657 }
2658 else
2659 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002660 rettv->v_type = VAR_NUMBER;
2661 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002662 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002663 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002664
2665 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002667
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002668 if (evalarg == NULL)
2669 clear_evalarg(&local_evalarg, NULL);
2670 else
2671 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 }
2673
2674 return OK;
2675}
2676
2677/*
2678 * Handle second level expression:
2679 * expr3 && expr3 && expr3 logical AND
2680 *
2681 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002682 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 *
2684 * Return OK or FAIL.
2685 */
2686 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002687eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002689 char_u *p;
2690 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691
2692 /*
2693 * Get the first variable.
2694 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002695 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 return FAIL;
2697
2698 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002699 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002701 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002702 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002704 evalarg_T *evalarg_used = evalarg;
2705 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002706 int orig_flags;
2707 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002708 long result = TRUE;
2709 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002710 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002711 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002712
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002713 if (evalarg == NULL)
2714 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002715 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002716 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002717 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002718 orig_flags = evalarg_used->eval_flags;
2719 evaluate = orig_flags & EVAL_EVALUATE;
2720 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002721 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002722 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002723 result = tv_get_bool_chk(rettv, &error);
2724 else if (tv_get_number_chk(rettv, &error) == 0)
2725 result = FALSE;
2726 clear_tv(rettv);
2727 if (error)
2728 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 }
2730
2731 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002732 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002734 while (p[0] == '&' && p[1] == '&')
2735 {
2736 if (getnext)
2737 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002738 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002739 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002740 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002741 {
2742 error_white_both(p, 2);
2743 clear_tv(rettv);
2744 return FAIL;
2745 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002746 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002747 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002748
2749 /*
2750 * Get the second variable.
2751 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002752 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2753 {
mityu4ac198c2021-05-28 17:52:40 +02002754 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002755 clear_tv(rettv);
2756 return FAIL;
2757 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002758 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2759 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002760 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002761 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002762 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002763 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002764
2765 /*
2766 * Compute the result.
2767 */
2768 if (evaluate && result)
2769 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002770 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002771 result = tv_get_bool_chk(&var2, &error);
2772 else if (tv_get_number_chk(&var2, &error) == 0)
2773 result = FALSE;
2774 clear_tv(&var2);
2775 if (error)
2776 return FAIL;
2777 }
2778 if (evaluate)
2779 {
2780 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002781 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002782 rettv->v_type = VAR_BOOL;
2783 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002784 }
2785 else
2786 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002787 rettv->v_type = VAR_NUMBER;
2788 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002789 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002790 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002791
2792 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002794
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002795 if (evalarg == NULL)
2796 clear_evalarg(&local_evalarg, NULL);
2797 else
2798 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 }
2800
2801 return OK;
2802}
2803
2804/*
2805 * Handle third level expression:
2806 * var1 == var2
2807 * var1 =~ var2
2808 * var1 != var2
2809 * var1 !~ var2
2810 * var1 > var2
2811 * var1 >= var2
2812 * var1 < var2
2813 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002814 * var1 is var2
2815 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 *
2817 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002818 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 *
2820 * Return OK or FAIL.
2821 */
2822 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002823eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002826 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002827 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002829 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830
2831 /*
2832 * Get the first variable.
2833 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002834 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 return FAIL;
2836
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002837 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002838 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839
2840 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002841 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002843 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002845 typval_T var2;
2846 int ic;
2847 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002848 int evaluate = evalarg == NULL
2849 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002850
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002851 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002852 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002853 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002854 p = *arg;
2855 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002856 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2857 {
mityu4ac198c2021-05-28 17:52:40 +02002858 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002859 clear_tv(rettv);
2860 return FAIL;
2861 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002862
Bram Moolenaar696ba232020-07-29 21:20:41 +02002863 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2864 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002865 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002866 clear_tv(rettv);
2867 return FAIL;
2868 }
2869
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002870 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 if (p[len] == '?')
2872 {
2873 ic = TRUE;
2874 ++len;
2875 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002876 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 else if (p[len] == '#')
2878 {
2879 ic = FALSE;
2880 ++len;
2881 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002882 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002884 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885
2886 /*
2887 * Get the second variable.
2888 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002889 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2890 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002891 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002892 clear_tv(rettv);
2893 return FAIL;
2894 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002895 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002896 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002898 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 return FAIL;
2900 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002901 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002902 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002903 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002904
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002905 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002906 {
2907 ret = FAIL;
2908 clear_tv(rettv);
2909 }
2910 else
2911 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002912 clear_tv(&var2);
2913 return ret;
2914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 }
2916
2917 return OK;
2918}
2919
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002920/*
2921 * Make a copy of blob "tv1" and append blob "tv2".
2922 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002923 void
2924eval_addblob(typval_T *tv1, typval_T *tv2)
2925{
2926 blob_T *b1 = tv1->vval.v_blob;
2927 blob_T *b2 = tv2->vval.v_blob;
2928 blob_T *b = blob_alloc();
2929 int i;
2930
2931 if (b != NULL)
2932 {
2933 for (i = 0; i < blob_len(b1); i++)
2934 ga_append(&b->bv_ga, blob_get(b1, i));
2935 for (i = 0; i < blob_len(b2); i++)
2936 ga_append(&b->bv_ga, blob_get(b2, i));
2937
2938 clear_tv(tv1);
2939 rettv_blob_set(tv1, b);
2940 }
2941}
2942
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002943/*
2944 * Make a copy of list "tv1" and append list "tv2".
2945 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 int
2947eval_addlist(typval_T *tv1, typval_T *tv2)
2948{
2949 typval_T var3;
2950
2951 // concatenate Lists
2952 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2953 {
2954 clear_tv(tv1);
2955 clear_tv(tv2);
2956 return FAIL;
2957 }
2958 clear_tv(tv1);
2959 *tv1 = var3;
2960 return OK;
2961}
2962
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963/*
2964 * Handle fourth level expression:
2965 * + number addition
2966 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002967 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002968 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 *
2970 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002971 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 *
2973 * Return OK or FAIL.
2974 */
2975 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002976eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 /*
2979 * Get the first variable.
2980 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002981 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 return FAIL;
2983
2984 /*
2985 * Repeat computing, until no '+', '-' or '.' is following.
2986 */
2987 for (;;)
2988 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002989 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002990 int getnext;
2991 char_u *p;
2992 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002993 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002994 int concat;
2995 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002996 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002997
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002998 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002999 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003000 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003001 p = eval_next_non_blank(*arg, evalarg, &getnext);
3002 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003003 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003004 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3005 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003007 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3008 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003009
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003010 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003011 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003012 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003013 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003014 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003015 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003016 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003017 {
mityu4ac198c2021-05-28 17:52:40 +02003018 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003019 clear_tv(rettv);
3020 return FAIL;
3021 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003022 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003023 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003024 if ((op != '+' || (rettv->v_type != VAR_LIST
3025 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003026#ifdef FEAT_FLOAT
3027 && (op == '.' || rettv->v_type != VAR_FLOAT)
3028#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003029 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003030 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003031 int error = FALSE;
3032
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003033 // For "list + ...", an illegal use of the first operand as
3034 // a number cannot be determined before evaluating the 2nd
3035 // operand: if this is also a list, all is ok.
3036 // For "something . ...", "something - ..." or "non-list + ...",
3037 // we know that the first operand needs to be a string or number
3038 // without evaluating the 2nd operand. So check before to avoid
3039 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003040 if (op != '.')
3041 tv_get_number_chk(rettv, &error);
3042 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003043 {
3044 clear_tv(rettv);
3045 return FAIL;
3046 }
3047 }
3048
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 /*
3050 * Get the second variable.
3051 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003052 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003053 {
mityu89dcb4d2021-05-28 13:50:17 +02003054 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003055 clear_tv(rettv);
3056 return FAIL;
3057 }
3058 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02003059 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003061 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 return FAIL;
3063 }
3064
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003065 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 {
3067 /*
3068 * Compute the result.
3069 */
3070 if (op == '.')
3071 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003072 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3073 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003074 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003075
Bram Moolenaar2e086612020-08-25 22:37:48 +02003076 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003077 || var2.v_type == VAR_CHANNEL
3078 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003079 semsg(_(e_using_invalid_value_as_string_str),
3080 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003081#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003082 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003083 {
3084 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3085 var2.vval.v_float);
3086 s2 = buf2;
3087 }
3088#endif
3089 else
3090 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003091 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003092 {
3093 clear_tv(rettv);
3094 clear_tv(&var2);
3095 return FAIL;
3096 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003097 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003098 clear_tv(rettv);
3099 rettv->v_type = VAR_STRING;
3100 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003102 else if (op == '+' && rettv->v_type == VAR_BLOB
3103 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003105 else if (op == '+' && rettv->v_type == VAR_LIST
3106 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003107 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003109 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 else
3112 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003113 int error = FALSE;
3114 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003115#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003116 float_T f1 = 0, f2 = 0;
3117
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003118 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003119 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003120 f1 = rettv->vval.v_float;
3121 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003122 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003123 else
3124#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003125 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003126 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003127 if (error)
3128 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003129 // This can only happen for "list + non-list" or
3130 // "blob + non-blob". For "non-list + ..." or
3131 // "something - ...", we returned before evaluating the
3132 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003133 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003134 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003135 return FAIL;
3136 }
3137#ifdef FEAT_FLOAT
3138 if (var2.v_type == VAR_FLOAT)
3139 f1 = n1;
3140#endif
3141 }
3142#ifdef FEAT_FLOAT
3143 if (var2.v_type == VAR_FLOAT)
3144 {
3145 f2 = var2.vval.v_float;
3146 n2 = 0;
3147 }
3148 else
3149#endif
3150 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003151 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003152 if (error)
3153 {
3154 clear_tv(rettv);
3155 clear_tv(&var2);
3156 return FAIL;
3157 }
3158#ifdef FEAT_FLOAT
3159 if (rettv->v_type == VAR_FLOAT)
3160 f2 = n2;
3161#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003162 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003163 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003164
3165#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003166 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003167 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3168 {
3169 if (op == '+')
3170 f1 = f1 + f2;
3171 else
3172 f1 = f1 - f2;
3173 rettv->v_type = VAR_FLOAT;
3174 rettv->vval.v_float = f1;
3175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003177#endif
3178 {
3179 if (op == '+')
3180 n1 = n1 + n2;
3181 else
3182 n1 = n1 - n2;
3183 rettv->v_type = VAR_NUMBER;
3184 rettv->vval.v_number = n1;
3185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003187 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 }
3189 }
3190 return OK;
3191}
3192
3193/*
3194 * Handle fifth level expression:
3195 * * number multiplication
3196 * / number division
3197 * % number modulo
3198 *
3199 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003200 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 *
3202 * Return OK or FAIL.
3203 */
3204 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003205eval6(
3206 char_u **arg,
3207 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003208 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003209 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003211#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003212 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214
3215 /*
3216 * Get the first variable.
3217 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003218 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 return FAIL;
3220
3221 /*
3222 * Repeat computing, until no '*', '/' or '%' is following.
3223 */
3224 for (;;)
3225 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003226 int evaluate;
3227 int getnext;
3228 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003229 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003230 int op;
3231 varnumber_T n1, n2;
3232#ifdef FEAT_FLOAT
3233 float_T f1, f2;
3234#endif
3235 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003236
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003237 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003238 p = eval_next_non_blank(*arg, evalarg, &getnext);
3239 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003240 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003242
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003243 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003244 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003245 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003246 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003247 {
3248 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3249 {
mityu4ac198c2021-05-28 17:52:40 +02003250 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003251 clear_tv(rettv);
3252 return FAIL;
3253 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003254 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003257#ifdef FEAT_FLOAT
3258 f1 = 0;
3259 f2 = 0;
3260#endif
3261 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003262 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003264#ifdef FEAT_FLOAT
3265 if (rettv->v_type == VAR_FLOAT)
3266 {
3267 f1 = rettv->vval.v_float;
3268 use_float = TRUE;
3269 n1 = 0;
3270 }
3271 else
3272#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003273 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003274 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003275 if (error)
3276 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 }
3278 else
3279 n1 = 0;
3280
3281 /*
3282 * Get the second variable.
3283 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003284 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3285 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003286 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003287 clear_tv(rettv);
3288 return FAIL;
3289 }
3290 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003291 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 return FAIL;
3293
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003294 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003296#ifdef FEAT_FLOAT
3297 if (var2.v_type == VAR_FLOAT)
3298 {
3299 if (!use_float)
3300 {
3301 f1 = n1;
3302 use_float = TRUE;
3303 }
3304 f2 = var2.vval.v_float;
3305 n2 = 0;
3306 }
3307 else
3308#endif
3309 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003310 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003311 clear_tv(&var2);
3312 if (error)
3313 return FAIL;
3314#ifdef FEAT_FLOAT
3315 if (use_float)
3316 f2 = n2;
3317#endif
3318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319
3320 /*
3321 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003322 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003324#ifdef FEAT_FLOAT
3325 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003327 if (op == '*')
3328 f1 = f1 * f2;
3329 else if (op == '/')
3330 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003331# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003332 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003333 if (f2 == 0.0)
3334 {
3335 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003336 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003337 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003338 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003339 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003340 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003341 }
3342 else
3343 f1 = f1 / f2;
3344# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003345 // We rely on the floating point library to handle divide
3346 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003347 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003348# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003351 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003352 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003353 return FAIL;
3354 }
3355 rettv->v_type = VAR_FLOAT;
3356 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 }
3358 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003361 int failed = FALSE;
3362
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003363 if (op == '*')
3364 n1 = n1 * n2;
3365 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003366 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003368 n1 = num_modulus(n1, n2, &failed);
3369 if (failed)
3370 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003371
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003372 rettv->v_type = VAR_NUMBER;
3373 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 }
3376 }
3377
3378 return OK;
3379}
3380
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003381/*
3382 * Handle a type cast before a base level expression.
3383 * "arg" must point to the first non-white of the expression.
3384 * "arg" is advanced to just after the recognized expression.
3385 * Return OK or FAIL.
3386 */
3387 static int
3388eval7t(
3389 char_u **arg,
3390 typval_T *rettv,
3391 evalarg_T *evalarg,
3392 int want_string) // after "." operator
3393{
3394 type_T *want_type = NULL;
3395 garray_T type_list; // list of pointers to allocated types
3396 int res;
3397 int evaluate = evalarg == NULL ? 0
3398 : (evalarg->eval_flags & EVAL_EVALUATE);
3399
3400 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003401 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3402 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003403 {
3404 ++*arg;
3405 ga_init2(&type_list, sizeof(type_T *), 10);
3406 want_type = parse_type(arg, &type_list, TRUE);
3407 if (want_type == NULL && (evaluate || **arg != '>'))
3408 {
3409 clear_type_list(&type_list);
3410 return FAIL;
3411 }
3412
3413 if (**arg != '>')
3414 {
3415 if (*skipwhite(*arg) == '>')
3416 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3417 else
3418 emsg(_(e_missing_gt));
3419 clear_type_list(&type_list);
3420 return FAIL;
3421 }
3422 ++*arg;
3423 *arg = skipwhite_and_linebreak(*arg, evalarg);
3424 }
3425
3426 res = eval7(arg, rettv, evalarg, want_string);
3427
3428 if (want_type != NULL && evaluate)
3429 {
3430 if (res == OK)
3431 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003432 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3433 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003434
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003435 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003436 {
3437 if (want_type == &t_bool && actual != &t_bool
3438 && (actual->tt_flags & TTFLAG_BOOL_OK))
3439 {
3440 int n = tv2bool(rettv);
3441
3442 // can use "0" and "1" for boolean in some places
3443 clear_tv(rettv);
3444 rettv->v_type = VAR_BOOL;
3445 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3446 }
3447 else
3448 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003449 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003450
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003451 where.wt_variable = TRUE;
3452 res = check_type(want_type, actual, TRUE, where);
3453 }
3454 }
3455 }
3456 clear_type_list(&type_list);
3457 }
3458
3459 return res;
3460}
3461
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003462 int
3463eval_leader(char_u **arg, int vim9)
3464{
3465 char_u *s = *arg;
3466 char_u *p = *arg;
3467
3468 while (*p == '!' || *p == '-' || *p == '+')
3469 {
3470 char_u *n = skipwhite(p + 1);
3471
3472 // ++, --, -+ and +- are not accepted in Vim9 script
3473 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3474 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003475 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003476 return FAIL;
3477 }
3478 p = n;
3479 }
3480 *arg = p;
3481 return OK;
3482}
3483
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484/*
3485 * Handle sixth level expression:
3486 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003487 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003488 * "string" string constant
3489 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 * &option-name option value
3491 * @r register contents
3492 * identifier variable value
3493 * function() function call
3494 * $VAR environment variable
3495 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003496 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003497 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003498 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003499 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 *
3501 * Also handle:
3502 * ! in front logical NOT
3503 * - in front unary minus
3504 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003505 * trailing [] subscript in String or List
3506 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003507 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 *
3509 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003510 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 *
3512 * Return OK or FAIL.
3513 */
3514 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003515eval7(
3516 char_u **arg,
3517 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003518 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003521 int evaluate = evalarg != NULL
3522 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 int len;
3524 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003525 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 char_u *start_leader, *end_leader;
3527 int ret = OK;
3528 char_u *alias;
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003529 static int recurse = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530
3531 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003532 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003533 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003535 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536
3537 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003538 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 */
3540 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003541 if (eval_leader(arg, in_vim9script()) == FAIL)
3542 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 end_leader = *arg;
3544
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003545 if (**arg == '.' && (!isdigit(*(*arg + 1))
3546#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003547 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003548#endif
3549 ))
3550 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003551 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003552 ++*arg;
3553 return FAIL;
3554 }
3555
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003556 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003557 // and crash. With MSVC the stack is smaller.
3558 if (recurse ==
3559#ifdef _MSC_VER
3560 300
3561#else
3562 1000
3563#endif
3564 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003565 {
3566 semsg(_(e_expression_too_recursive_str), *arg);
3567 return FAIL;
3568 }
3569 ++recurse;
3570
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 switch (**arg)
3572 {
3573 /*
3574 * Number constant.
3575 */
3576 case '0':
3577 case '1':
3578 case '2':
3579 case '3':
3580 case '4':
3581 case '5':
3582 case '6':
3583 case '7':
3584 case '8':
3585 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003586 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003587
3588 // Apply prefixed "-" and "+" now. Matters especially when
3589 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003590 if (ret == OK && evaluate && end_leader > start_leader
3591 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003592 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 break;
3594
3595 /*
3596 * String constant: "string".
3597 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003598 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 break;
3600
3601 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003602 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003604 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003605 break;
3606
3607 /*
3608 * List: [expr, expr]
3609 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003610 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 break;
3612
3613 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003614 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003615 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003616 case '#': if (in_vim9script())
3617 {
3618 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3619 }
3620 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003621 {
3622 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003623 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003624 }
3625 else
3626 ret = NOTDONE;
3627 break;
3628
3629 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003630 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003631 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003632 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003633 case '{': if (in_vim9script())
3634 ret = NOTDONE;
3635 else
3636 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003637 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003638 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003639 break;
3640
3641 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003642 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003644 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 break;
3646
3647 /*
3648 * Environment variable: $VAR.
3649 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003650 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 break;
3652
3653 /*
3654 * Register contents: @r.
3655 */
3656 case '@': ++*arg;
3657 if (evaluate)
3658 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003659 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3660 semsg(_(e_syntax_error_at_str), *arg);
3661 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3662 emsg_invreg(**arg);
3663 else
3664 {
3665 rettv->v_type = VAR_STRING;
3666 rettv->vval.v_string = get_reg_contents(**arg,
3667 GREG_EXPR_SRC);
3668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 }
3670 if (**arg != NUL)
3671 ++*arg;
3672 break;
3673
3674 /*
3675 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003676 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003678 case '(': ret = NOTDONE;
3679 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003680 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003681 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003682 if (ret == OK && evaluate)
3683 {
3684 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3685
Bram Moolenaara9931532021-06-12 15:58:16 +02003686 // Compile it here to get the return type. The return
3687 // type is optional, when it's missing use t_unknown.
3688 // This is recognized in compile_return().
3689 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3690 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003691 if (compile_def_function(ufunc,
Bram Moolenaare99d4222021-06-13 14:01:26 +02003692 FALSE, COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003693 {
3694 clear_tv(rettv);
3695 ret = FAIL;
3696 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003697 }
3698 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003699 if (ret == NOTDONE)
3700 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003701 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003702 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003703
Bram Moolenaar9215f012020-06-27 21:18:00 +02003704 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003705 if (**arg == ')')
3706 ++*arg;
3707 else if (ret == OK)
3708 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003709 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003710 clear_tv(rettv);
3711 ret = FAIL;
3712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 }
3714 break;
3715
Bram Moolenaar8c711452005-01-14 21:53:12 +00003716 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 break;
3718 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003719
3720 if (ret == NOTDONE)
3721 {
3722 /*
3723 * Must be a variable or function name.
3724 * Can also be a curly-braces kind of name: {expr}.
3725 */
3726 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003727 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003728 if (alias != NULL)
3729 s = alias;
3730
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003731 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003732 ret = FAIL;
3733 else
3734 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003735 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3736
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003737 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003738 {
3739 emsg(_(e_cannot_use_underscore_here));
3740 ret = FAIL;
3741 }
3742 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003743 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003744 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003745 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003746 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003747 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003748 else if (flags & EVAL_CONSTANT)
3749 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003750 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003751 {
3752 // get the value of "true", "false" or a variable
3753 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3754 {
3755 rettv->v_type = VAR_BOOL;
3756 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003757 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003758 }
3759 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003760 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003761 {
3762 rettv->v_type = VAR_BOOL;
3763 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003764 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003765 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003766 else if (len == 4 && in_vim9script()
3767 && STRNCMP(s, "null", 4) == 0)
3768 {
3769 rettv->v_type = VAR_SPECIAL;
3770 rettv->vval.v_number = VVAL_NULL;
3771 ret = OK;
3772 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003773 else
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003774 {
3775 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003776 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003777 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003778 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003779 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003780 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003781 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003782 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003783 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003784 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003785 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003786 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003787 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003788 }
3789
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003790 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3791 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003792 if (ret == OK)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003793 ret = handle_subscript(arg, name_start, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794
3795 /*
3796 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3797 */
3798 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003799 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003800
3801 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003802 return ret;
3803}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003804
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003805/*
3806 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003807 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003808 * Adjusts "end_leaderp" until it is at "start_leader".
3809 */
3810 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003811eval7_leader(
3812 typval_T *rettv,
3813 int numeric_only,
3814 char_u *start_leader,
3815 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003816{
3817 char_u *end_leader = *end_leaderp;
3818 int ret = OK;
3819 int error = FALSE;
3820 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003821 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003822#ifdef FEAT_FLOAT
3823 float_T f = 0.0;
3824
3825 if (rettv->v_type == VAR_FLOAT)
3826 f = rettv->vval.v_float;
3827 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003828#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003829 {
3830 while (VIM_ISWHITE(end_leader[-1]))
3831 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003832 if (in_vim9script() && end_leader[-1] == '!')
3833 val = tv2bool(rettv);
3834 else
3835 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003836 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003837 if (error)
3838 {
3839 clear_tv(rettv);
3840 ret = FAIL;
3841 }
3842 else
3843 {
3844 while (end_leader > start_leader)
3845 {
3846 --end_leader;
3847 if (*end_leader == '!')
3848 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003849 if (numeric_only)
3850 {
3851 ++end_leader;
3852 break;
3853 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003854#ifdef FEAT_FLOAT
3855 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003856 {
3857 if (in_vim9script())
3858 {
3859 rettv->v_type = VAR_BOOL;
3860 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3861 }
3862 else
3863 f = !f;
3864 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003865 else
3866#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003867 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003868 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003869 type = VAR_BOOL;
3870 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003871 }
3872 else if (*end_leader == '-')
3873 {
3874#ifdef FEAT_FLOAT
3875 if (rettv->v_type == VAR_FLOAT)
3876 f = -f;
3877 else
3878#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003879 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003880 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003881 type = VAR_NUMBER;
3882 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003883 }
3884 }
3885#ifdef FEAT_FLOAT
3886 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003888 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003889 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003891 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003892#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003893 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003894 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003895 if (in_vim9script())
3896 rettv->v_type = type;
3897 else
3898 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003899 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003902 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 return ret;
3904}
3905
3906/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003907 * Call the function referred to in "rettv".
3908 */
3909 static int
3910call_func_rettv(
3911 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003912 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003913 typval_T *rettv,
3914 int evaluate,
3915 dict_T *selfdict,
3916 typval_T *basetv)
3917{
3918 partial_T *pt = NULL;
3919 funcexe_T funcexe;
3920 typval_T functv;
3921 char_u *s;
3922 int ret;
3923
3924 // need to copy the funcref so that we can clear rettv
3925 if (evaluate)
3926 {
3927 functv = *rettv;
3928 rettv->v_type = VAR_UNKNOWN;
3929
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003930 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003931 if (functv.v_type == VAR_PARTIAL)
3932 {
3933 pt = functv.vval.v_partial;
3934 s = partial_name(pt);
3935 }
3936 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003937 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003938 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003939 if (s == NULL || *s == NUL)
3940 {
3941 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02003942 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003943 goto theend;
3944 }
3945 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003946 }
3947 else
3948 s = (char_u *)"";
3949
Bram Moolenaara80faa82020-04-12 19:37:17 +02003950 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003951 funcexe.fe_firstline = curwin->w_cursor.lnum;
3952 funcexe.fe_lastline = curwin->w_cursor.lnum;
3953 funcexe.fe_evaluate = evaluate;
3954 funcexe.fe_partial = pt;
3955 funcexe.fe_selfdict = selfdict;
3956 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003957 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003958
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003959theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003960 // Clear the funcref afterwards, so that deleting it while
3961 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003962 if (evaluate)
3963 clear_tv(&functv);
3964
3965 return ret;
3966}
3967
3968/*
3969 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003970 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003971 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3972 */
3973 static int
3974eval_lambda(
3975 char_u **arg,
3976 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003977 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003978 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003979{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003980 int evaluate = evalarg != NULL
3981 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003982 typval_T base = *rettv;
3983 int ret;
3984
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003985 rettv->v_type = VAR_UNKNOWN;
3986
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003987 if (**arg == '{')
3988 {
3989 // ->{lambda}()
3990 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3991 }
3992 else
3993 {
3994 // ->(lambda)()
3995 ++*arg;
3996 ret = eval1(arg, rettv, evalarg);
3997 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar605ec912021-12-18 16:54:31 +00003998 if (**arg == ')')
3999 {
4000 ++*arg;
4001 }
4002 else
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004003 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004004 emsg(_(e_missing_closing_paren));
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004005 ret = FAIL;
4006 }
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004007 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004008 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004009 return FAIL;
4010 else if (**arg != '(')
4011 {
4012 if (verbose)
4013 {
4014 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004015 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004016 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004017 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004018 }
4019 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004020 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004021 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004022 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004023 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004024
4025 // Clear the funcref afterwards, so that deleting it while
4026 // evaluating the arguments is possible (see test55).
4027 if (evaluate)
4028 clear_tv(&base);
4029
4030 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004031}
4032
4033/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004034 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004035 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004036 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4037 */
4038 static int
4039eval_method(
4040 char_u **arg,
4041 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004042 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004043 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004044{
4045 char_u *name;
4046 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004047 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004048 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004049 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004050 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004051 int evaluate = evalarg != NULL
4052 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004053
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004054 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004055
Bram Moolenaarac92e252019-08-03 21:58:38 +02004056 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004057 len = get_name_len(arg, &alias, evaluate, TRUE);
4058 if (alias != NULL)
4059 name = alias;
4060
4061 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004062 {
4063 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004064 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004065 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004066 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004067 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004068 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004069 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004070
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004071 // If there is no "(" immediately following, but there is further on,
4072 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4073 // Does not handle anything where "(" is part of the expression.
4074 *arg = skipwhite(*arg);
4075
4076 if (**arg != '(' && alias == NULL
4077 && (paren = vim_strchr(*arg, '(')) != NULL)
4078 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004079 char_u *deref;
4080
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004081 *arg = name;
4082 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004083 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4084 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004085 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004086 *arg = name + len;
4087 ret = FAIL;
4088 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004089 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004090 {
4091 name = deref;
Bram Moolenaar15d16352022-01-17 20:09:08 +00004092 len = STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004093 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004094 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004095 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004096
4097 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004098 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004099 *arg = skipwhite(*arg);
4100
4101 if (**arg != '(')
4102 {
4103 if (verbose)
4104 semsg(_(e_missing_parenthesis_str), name);
4105 ret = FAIL;
4106 }
4107 else if (VIM_ISWHITE((*arg)[-1]))
4108 {
4109 if (verbose)
4110 emsg(_(e_no_white_space_allowed_before_parenthesis));
4111 ret = FAIL;
4112 }
4113 else
4114 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004115 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004116 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004117 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004118
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004119 // Clear the funcref afterwards, so that deleting it while
4120 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004121 if (evaluate)
4122 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004123 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004124
Bram Moolenaarac92e252019-08-03 21:58:38 +02004125 return ret;
4126}
4127
4128/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004129 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4130 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004131 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4132 */
4133 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004134eval_index(
4135 char_u **arg,
4136 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004137 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004138 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004139{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004140 int evaluate = evalarg != NULL
4141 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004142 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004143 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004144 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004145 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004146 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004147 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004148
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004149 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4150 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004151
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004152 init_tv(&var1);
4153 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004154 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004155 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004156 /*
4157 * dict.name
4158 */
4159 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004160 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004161 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004162 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004163 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004164 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004165 }
4166 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004167 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004168 /*
4169 * something[idx]
4170 *
4171 * Get the (first) variable from inside the [].
4172 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004173 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004174 if (**arg == ':')
4175 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004176 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004177 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004178 else if (vim9 && **arg == ':')
4179 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004180 semsg(_(e_white_space_required_before_and_after_str_at_str),
4181 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004182 clear_tv(&var1);
4183 return FAIL;
4184 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004185 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004186 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004187 int error = FALSE;
4188
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004189#ifdef FEAT_FLOAT
4190 // allow for indexing with float
4191 if (vim9 && rettv->v_type == VAR_DICT
4192 && var1.v_type == VAR_FLOAT)
4193 {
4194 var1.vval.v_string = typval_tostring(&var1, TRUE);
4195 var1.v_type = VAR_STRING;
4196 }
4197#endif
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004198 if (vim9 && rettv->v_type == VAR_LIST)
4199 tv_get_number_chk(&var1, &error);
4200 else
4201 error = tv_get_string_chk(&var1) == NULL;
4202 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004203 {
4204 // not a number or string
4205 clear_tv(&var1);
4206 return FAIL;
4207 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004208 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004209
4210 /*
4211 * Get the second variable from inside the [:].
4212 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004213 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004214 if (**arg == ':')
4215 {
4216 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004217 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004218 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004219 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004220 semsg(_(e_white_space_required_before_and_after_str_at_str),
4221 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004222 if (!empty1)
4223 clear_tv(&var1);
4224 return FAIL;
4225 }
4226 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004227 if (**arg == ']')
4228 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004229 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004230 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004231 if (!empty1)
4232 clear_tv(&var1);
4233 return FAIL;
4234 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004235 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004236 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004237 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004238 if (!empty1)
4239 clear_tv(&var1);
4240 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004241 return FAIL;
4242 }
4243 }
4244
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004245 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004246 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004247 if (**arg != ']')
4248 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004249 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004250 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004251 clear_tv(&var1);
4252 if (range)
4253 clear_tv(&var2);
4254 return FAIL;
4255 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004256 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004257 }
4258
4259 if (evaluate)
4260 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004261 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004262 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004263 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004264
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004265 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004266 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004267 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004268 clear_tv(&var2);
4269 return res;
4270 }
4271 return OK;
4272}
4273
4274/*
4275 * Check if "rettv" can have an [index] or [sli:ce]
4276 */
4277 int
4278check_can_index(typval_T *rettv, int evaluate, int verbose)
4279{
4280 switch (rettv->v_type)
4281 {
4282 case VAR_FUNC:
4283 case VAR_PARTIAL:
4284 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004285 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004286 return FAIL;
4287 case VAR_FLOAT:
4288#ifdef FEAT_FLOAT
4289 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004290 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004291 return FAIL;
4292#endif
4293 case VAR_BOOL:
4294 case VAR_SPECIAL:
4295 case VAR_JOB:
4296 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004297 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004298 if (verbose)
4299 emsg(_(e_cannot_index_special_variable));
4300 return FAIL;
4301 case VAR_UNKNOWN:
4302 case VAR_ANY:
4303 case VAR_VOID:
4304 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004305 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004306 emsg(_(e_cannot_index_special_variable));
4307 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004308 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004309 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004310
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004311 case VAR_STRING:
4312 case VAR_LIST:
4313 case VAR_DICT:
4314 case VAR_BLOB:
4315 break;
4316 case VAR_NUMBER:
4317 if (in_vim9script())
4318 emsg(_(e_cannot_index_number));
4319 break;
4320 }
4321 return OK;
4322}
4323
4324/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004325 * slice() function
4326 */
4327 void
4328f_slice(typval_T *argvars, typval_T *rettv)
4329{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004330 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004331 && ((argvars[0].v_type != VAR_STRING
4332 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004333 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004334 && check_for_list_arg(argvars, 0) == FAIL)
4335 || check_for_number_arg(argvars, 1) == FAIL
4336 || check_for_opt_number_arg(argvars, 2) == FAIL))
4337 return;
4338
Bram Moolenaar6601b622021-01-13 21:47:15 +01004339 if (check_can_index(argvars, TRUE, FALSE) == OK)
4340 {
4341 copy_tv(argvars, rettv);
4342 eval_index_inner(rettv, TRUE, argvars + 1,
4343 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4344 TRUE, NULL, 0, FALSE);
4345 }
4346}
4347
4348/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004349 * Apply index or range to "rettv".
4350 * "var1" is the first index, NULL for [:expr].
4351 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004352 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4353 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004354 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4355 */
4356 int
4357eval_index_inner(
4358 typval_T *rettv,
4359 int is_range,
4360 typval_T *var1,
4361 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004362 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004363 char_u *key,
4364 int keylen,
4365 int verbose)
4366{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004367 varnumber_T n1, n2 = 0;
4368 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004369
4370 n1 = 0;
4371 if (var1 != NULL && rettv->v_type != VAR_DICT)
4372 n1 = tv_get_number(var1);
4373
4374 if (is_range)
4375 {
4376 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004377 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004378 if (verbose)
4379 emsg(_(e_cannot_slice_dictionary));
4380 return FAIL;
4381 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004382 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004383 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004384 else
4385 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004386 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004387
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004388 switch (rettv->v_type)
4389 {
4390 case VAR_UNKNOWN:
4391 case VAR_ANY:
4392 case VAR_VOID:
4393 case VAR_FUNC:
4394 case VAR_PARTIAL:
4395 case VAR_FLOAT:
4396 case VAR_BOOL:
4397 case VAR_SPECIAL:
4398 case VAR_JOB:
4399 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004400 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004401 break; // not evaluating, skipping over subscript
4402
4403 case VAR_NUMBER:
4404 case VAR_STRING:
4405 {
4406 char_u *s = tv_get_string(rettv);
4407
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004408 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004409 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004410 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004411 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004412 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004413 else
4414 s = char_from_string(s, n1);
4415 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004416 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004417 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004418 // The resulting variable is a substring. If the indexes
4419 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004420 if (n1 < 0)
4421 {
4422 n1 = len + n1;
4423 if (n1 < 0)
4424 n1 = 0;
4425 }
4426 if (n2 < 0)
4427 n2 = len + n2;
4428 else if (n2 >= len)
4429 n2 = len;
4430 if (n1 >= len || n2 < 0 || n1 > n2)
4431 s = NULL;
4432 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004433 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004434 }
4435 else
4436 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004437 // The resulting variable is a string of a single
4438 // character. If the index is too big or negative the
4439 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004440 if (n1 >= len || n1 < 0)
4441 s = NULL;
4442 else
4443 s = vim_strnsave(s + n1, 1);
4444 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004445 clear_tv(rettv);
4446 rettv->v_type = VAR_STRING;
4447 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004448 }
4449 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004450
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004451 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004452 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4453 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004454 break;
4455
4456 case VAR_LIST:
4457 if (var1 == NULL)
4458 n1 = 0;
4459 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004460 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004461 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004462 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004463 return FAIL;
4464 break;
4465
4466 case VAR_DICT:
4467 {
4468 dictitem_T *item;
4469 typval_T tmp;
4470
4471 if (key == NULL)
4472 {
4473 key = tv_get_string_chk(var1);
4474 if (key == NULL)
4475 return FAIL;
4476 }
4477
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004478 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004479
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004480 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004481 {
4482 if (verbose)
4483 {
4484 if (keylen > 0)
4485 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004486 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004487 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004488 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004489 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004490
4491 copy_tv(&item->di_tv, &tmp);
4492 clear_tv(rettv);
4493 *rettv = tmp;
4494 }
4495 break;
4496 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004497 return OK;
4498}
4499
4500/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004501 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004502 */
4503 char_u *
4504partial_name(partial_T *pt)
4505{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004506 if (pt != NULL)
4507 {
4508 if (pt->pt_name != NULL)
4509 return pt->pt_name;
4510 if (pt->pt_func != NULL)
4511 return pt->pt_func->uf_name;
4512 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004513 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004514}
4515
Bram Moolenaarddecc252016-04-06 22:59:37 +02004516 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004517partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004518{
4519 int i;
4520
4521 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004522 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004523 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004524 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004525 if (pt->pt_name != NULL)
4526 {
4527 func_unref(pt->pt_name);
4528 vim_free(pt->pt_name);
4529 }
4530 else
4531 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004532
Bram Moolenaar54656012021-06-09 20:50:46 +02004533 // "out_up" is no longer used, decrement refcount on partial that owns it.
4534 partial_unref(pt->pt_outer.out_up_partial);
4535
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004536 // Decrease the reference count for the context of a closure. If down
4537 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004538 if (pt->pt_funcstack != NULL)
4539 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004540 --pt->pt_funcstack->fs_refcount;
4541 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004542 }
4543
Bram Moolenaarddecc252016-04-06 22:59:37 +02004544 vim_free(pt);
4545}
4546
4547/*
4548 * Unreference a closure: decrement the reference count and free it when it
4549 * becomes zero.
4550 */
4551 void
4552partial_unref(partial_T *pt)
4553{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004554 if (pt != NULL)
4555 {
4556 if (--pt->pt_refcount <= 0)
4557 partial_free(pt);
4558
4559 // If the reference count goes down to one, the funcstack may be the
4560 // only reference and can be freed if no other partials reference it.
4561 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4562 funcstack_check_refcount(pt->pt_funcstack);
4563 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004564}
4565
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004566/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004567 * Return the next (unique) copy ID.
4568 * Used for serializing nested structures.
4569 */
4570 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004571get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004572{
4573 current_copyID += COPYID_INC;
4574 return current_copyID;
4575}
4576
4577/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004578 * Garbage collection for lists and dictionaries.
4579 *
4580 * We use reference counts to be able to free most items right away when they
4581 * are no longer used. But for composite items it's possible that it becomes
4582 * unused while the reference count is > 0: When there is a recursive
4583 * reference. Example:
4584 * :let l = [1, 2, 3]
4585 * :let d = {9: l}
4586 * :let l[1] = d
4587 *
4588 * Since this is quite unusual we handle this with garbage collection: every
4589 * once in a while find out which lists and dicts are not referenced from any
4590 * variable.
4591 *
4592 * Here is a good reference text about garbage collection (refers to Python
4593 * but it applies to all reference-counting mechanisms):
4594 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004595 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004596
4597/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004598 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004599 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004600 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004601 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004602 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004603garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004604{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004605 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004606 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004607 buf_T *buf;
4608 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004609 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004610 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004611
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004612 if (!testing)
4613 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004614 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004615 want_garbage_collect = FALSE;
4616 may_garbage_collect = FALSE;
4617 garbage_collect_at_exit = FALSE;
4618 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004619
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004620 // The execution stack can grow big, limit the size.
4621 if (exestack.ga_maxlen - exestack.ga_len > 500)
4622 {
4623 size_t new_len;
4624 char_u *pp;
4625 int n;
4626
4627 // Keep 150% of the current size, with a minimum of the growth size.
4628 n = exestack.ga_len / 2;
4629 if (n < exestack.ga_growsize)
4630 n = exestack.ga_growsize;
4631
4632 // Don't make it bigger though.
4633 if (exestack.ga_len + n < exestack.ga_maxlen)
4634 {
4635 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4636 pp = vim_realloc(exestack.ga_data, new_len);
4637 if (pp == NULL)
4638 return FAIL;
4639 exestack.ga_maxlen = exestack.ga_len + n;
4640 exestack.ga_data = pp;
4641 }
4642 }
4643
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004644 // We advance by two because we add one for items referenced through
4645 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004646 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004647
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004648 /*
4649 * 1. Go through all accessible variables and mark all lists and dicts
4650 * with copyID.
4651 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004652
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004653 // Don't free variables in the previous_funccal list unless they are only
4654 // referenced through previous_funccal. This must be first, because if
4655 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004656 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004657
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004658 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004659 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004660
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004661 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004662 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004663 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4664 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004665
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004666 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004667 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004668 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4669 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004670 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004671 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4672 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004673#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004674 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004675 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4676 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004677 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004678 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004679 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4680 NULL, NULL);
4681#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004682
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004683 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004684 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004685 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4686 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004687 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004688 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004689
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004690 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004691 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004692
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004693 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004694 abort = abort || set_ref_in_functions(copyID);
4695
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004696 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004697 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004698
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004699 // funcstacks keep variables for closures
4700 abort = abort || set_ref_in_funcstacks(copyID);
4701
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004702 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004703 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004704
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004705 // callbacks in buffers
4706 abort = abort || set_ref_in_buffers(copyID);
4707
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004708 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
4709 abort = abort || set_ref_in_insexpand_funcs(copyID);
4710
4711 // 'operatorfunc' callback
4712 abort = abort || set_ref_in_opfunc(copyID);
4713
4714 // 'tagfunc' callback
4715 abort = abort || set_ref_in_tagfunc(copyID);
4716
4717 // 'imactivatefunc' and 'imstatusfunc' callbacks
4718 abort = abort || set_ref_in_im_funcs(copyID);
4719
Bram Moolenaar1dced572012-04-05 16:54:08 +02004720#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004721 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004722#endif
4723
Bram Moolenaardb913952012-06-29 12:54:53 +02004724#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004725 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004726#endif
4727
4728#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004729 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004730#endif
4731
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004732#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004733 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004734 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004735#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004736#ifdef FEAT_NETBEANS_INTG
4737 abort = abort || set_ref_in_nb_channel(copyID);
4738#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004739
Bram Moolenaare3188e22016-05-31 21:13:04 +02004740#ifdef FEAT_TIMERS
4741 abort = abort || set_ref_in_timer(copyID);
4742#endif
4743
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004744#ifdef FEAT_QUICKFIX
4745 abort = abort || set_ref_in_quickfix(copyID);
4746#endif
4747
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004748#ifdef FEAT_TERMINAL
4749 abort = abort || set_ref_in_term(copyID);
4750#endif
4751
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004752#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004753 abort = abort || set_ref_in_popups(copyID);
4754#endif
4755
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004756 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004757 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004758 /*
4759 * 2. Free lists and dictionaries that are not referenced.
4760 */
4761 did_free = free_unref_items(copyID);
4762
4763 /*
4764 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004765 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004766 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004767 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004768 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004769 else if (p_verbose > 0)
4770 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004771 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004772 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004773
4774 return did_free;
4775}
4776
4777/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004778 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004779 */
4780 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004781free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004782{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004783 int did_free = FALSE;
4784
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004785 // Let all "free" functions know that we are here. This means no
4786 // dictionaries, lists, channels or jobs are to be freed, because we will
4787 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004788 in_free_unref_items = TRUE;
4789
4790 /*
4791 * PASS 1: free the contents of the items. We don't free the items
4792 * themselves yet, so that it is possible to decrement refcount counters
4793 */
4794
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004795 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004796 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004797
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004798 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004799 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004800
4801#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004802 // Go through the list of jobs and free items without the copyID. This
4803 // must happen before doing channels, because jobs refer to channels, but
4804 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004805 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4806
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004807 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004808 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4809#endif
4810
4811 /*
4812 * PASS 2: free the items themselves.
4813 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004814 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004815 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004816
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004817#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004818 // Go through the list of jobs and free items without the copyID. This
4819 // must happen before doing channels, because jobs refer to channels, but
4820 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004821 free_unused_jobs(copyID, COPYID_MASK);
4822
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004823 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004824 free_unused_channels(copyID, COPYID_MASK);
4825#endif
4826
4827 in_free_unref_items = FALSE;
4828
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004829 return did_free;
4830}
4831
4832/*
4833 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004834 * "list_stack" is used to add lists to be marked. Can be NULL.
4835 *
4836 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004837 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004838 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004839set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004840{
4841 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004842 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004843 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004844 hashtab_T *cur_ht;
4845 ht_stack_T *ht_stack = NULL;
4846 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004847
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004848 cur_ht = ht;
4849 for (;;)
4850 {
4851 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004852 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004853 // Mark each item in the hashtab. If the item contains a hashtab
4854 // it is added to ht_stack, if it contains a list it is added to
4855 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004856 todo = (int)cur_ht->ht_used;
4857 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4858 if (!HASHITEM_EMPTY(hi))
4859 {
4860 --todo;
4861 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4862 &ht_stack, list_stack);
4863 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004864 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004865
4866 if (ht_stack == NULL)
4867 break;
4868
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004869 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004870 cur_ht = ht_stack->ht;
4871 tempitem = ht_stack;
4872 ht_stack = ht_stack->prev;
4873 free(tempitem);
4874 }
4875
4876 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004877}
4878
Dominique Pelle748b3082022-01-08 12:41:16 +00004879#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
4880 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004881/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004882 * Mark a dict and its items with "copyID".
4883 * Returns TRUE if setting references failed somehow.
4884 */
4885 int
4886set_ref_in_dict(dict_T *d, int copyID)
4887{
4888 if (d != NULL && d->dv_copyID != copyID)
4889 {
4890 d->dv_copyID = copyID;
4891 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4892 }
4893 return FALSE;
4894}
Dominique Pelle748b3082022-01-08 12:41:16 +00004895#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004896
4897/*
4898 * Mark a list and its items with "copyID".
4899 * Returns TRUE if setting references failed somehow.
4900 */
4901 int
4902set_ref_in_list(list_T *ll, int copyID)
4903{
4904 if (ll != NULL && ll->lv_copyID != copyID)
4905 {
4906 ll->lv_copyID = copyID;
4907 return set_ref_in_list_items(ll, copyID, NULL);
4908 }
4909 return FALSE;
4910}
4911
4912/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004913 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004914 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4915 *
4916 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004917 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004918 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004919set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004920{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004921 listitem_T *li;
4922 int abort = FALSE;
4923 list_T *cur_l;
4924 list_stack_T *list_stack = NULL;
4925 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004926
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004927 cur_l = l;
4928 for (;;)
4929 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004930 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004931 // Mark each item in the list. If the item contains a hashtab
4932 // it is added to ht_stack, if it contains a list it is added to
4933 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004934 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4935 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4936 ht_stack, &list_stack);
4937 if (list_stack == NULL)
4938 break;
4939
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004940 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004941 cur_l = list_stack->list;
4942 tempitem = list_stack;
4943 list_stack = list_stack->prev;
4944 free(tempitem);
4945 }
4946
4947 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004948}
4949
4950/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004951 * Mark the partial in callback 'cb' with "copyID".
4952 */
4953 int
4954set_ref_in_callback(callback_T *cb, int copyID)
4955{
4956 typval_T tv;
4957
4958 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
4959 return FALSE;
4960
4961 tv.v_type = VAR_PARTIAL;
4962 tv.vval.v_partial = cb->cb_partial;
4963 return set_ref_in_item(&tv, copyID, NULL, NULL);
4964}
4965
4966/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004967 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004968 * "list_stack" is used to add lists to be marked. Can be NULL.
4969 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4970 *
4971 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004972 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004973 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004974set_ref_in_item(
4975 typval_T *tv,
4976 int copyID,
4977 ht_stack_T **ht_stack,
4978 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004979{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004980 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004981
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004982 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004983 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004984 dict_T *dd = tv->vval.v_dict;
4985
Bram Moolenaara03f2332016-02-06 18:09:59 +01004986 if (dd != NULL && dd->dv_copyID != copyID)
4987 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004988 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004989 dd->dv_copyID = copyID;
4990 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004991 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004992 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4993 }
4994 else
4995 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004996 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4997
Bram Moolenaara03f2332016-02-06 18:09:59 +01004998 if (newitem == NULL)
4999 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005000 else
5001 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005002 newitem->ht = &dd->dv_hashtab;
5003 newitem->prev = *ht_stack;
5004 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005005 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005006 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005007 }
5008 }
5009 else if (tv->v_type == VAR_LIST)
5010 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005011 list_T *ll = tv->vval.v_list;
5012
Bram Moolenaara03f2332016-02-06 18:09:59 +01005013 if (ll != NULL && ll->lv_copyID != copyID)
5014 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005015 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005016 ll->lv_copyID = copyID;
5017 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005018 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005019 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005020 }
5021 else
5022 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005023 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5024
Bram Moolenaara03f2332016-02-06 18:09:59 +01005025 if (newitem == NULL)
5026 abort = TRUE;
5027 else
5028 {
5029 newitem->list = ll;
5030 newitem->prev = *list_stack;
5031 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005032 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005033 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005034 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005035 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005036 else if (tv->v_type == VAR_FUNC)
5037 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005038 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005039 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005040 else if (tv->v_type == VAR_PARTIAL)
5041 {
5042 partial_T *pt = tv->vval.v_partial;
5043 int i;
5044
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005045 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005046 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005047 // Didn't see this partial yet.
5048 pt->pt_copyID = copyID;
5049
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005050 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005051
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005052 if (pt->pt_dict != NULL)
5053 {
5054 typval_T dtv;
5055
5056 dtv.v_type = VAR_DICT;
5057 dtv.vval.v_dict = pt->pt_dict;
5058 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5059 }
5060
5061 for (i = 0; i < pt->pt_argc; ++i)
5062 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5063 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005064 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005065 }
5066 }
5067#ifdef FEAT_JOB_CHANNEL
5068 else if (tv->v_type == VAR_JOB)
5069 {
5070 job_T *job = tv->vval.v_job;
5071 typval_T dtv;
5072
5073 if (job != NULL && job->jv_copyID != copyID)
5074 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005075 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005076 if (job->jv_channel != NULL)
5077 {
5078 dtv.v_type = VAR_CHANNEL;
5079 dtv.vval.v_channel = job->jv_channel;
5080 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5081 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005082 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005083 {
5084 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005085 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005086 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5087 }
5088 }
5089 }
5090 else if (tv->v_type == VAR_CHANNEL)
5091 {
5092 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005093 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005094 typval_T dtv;
5095 jsonq_T *jq;
5096 cbq_T *cq;
5097
5098 if (ch != NULL && ch->ch_copyID != copyID)
5099 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005100 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005101 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005102 {
5103 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5104 jq = jq->jq_next)
5105 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5106 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5107 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005108 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005109 {
5110 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005111 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005112 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5113 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005114 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005115 {
5116 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005117 dtv.vval.v_partial =
5118 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005119 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5120 }
5121 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005122 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005123 {
5124 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005125 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005126 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5127 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005128 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005129 {
5130 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005131 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005132 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5133 }
5134 }
5135 }
5136#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005137 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005138}
5139
Bram Moolenaar8c711452005-01-14 21:53:12 +00005140/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005141 * Return a string with the string representation of a variable.
5142 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005143 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005144 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005145 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005146 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005147 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005148 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5149 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005150 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005151 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005152 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005153echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005154 typval_T *tv,
5155 char_u **tofree,
5156 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005157 int copyID,
5158 int echo_style,
5159 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005160 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005161{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005162 static int recurse = 0;
5163 char_u *r = NULL;
5164
Bram Moolenaar33570922005-01-25 22:26:29 +00005165 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005166 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005167 if (!did_echo_string_emsg)
5168 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005169 // Only give this message once for a recursive call to avoid
5170 // flooding the user with errors. And stop iterating over lists
5171 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005172 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005173 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005174 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005175 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005176 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005177 }
5178 ++recurse;
5179
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005180 switch (tv->v_type)
5181 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005182 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005183 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005184 {
5185 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005186 r = tv->vval.v_string;
5187 if (r == NULL)
5188 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005189 }
5190 else
5191 {
5192 *tofree = string_quote(tv->vval.v_string, FALSE);
5193 r = *tofree;
5194 }
5195 break;
5196
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005197 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005198 if (echo_style)
5199 {
5200 *tofree = NULL;
5201 r = tv->vval.v_string;
5202 }
5203 else
5204 {
5205 *tofree = string_quote(tv->vval.v_string, TRUE);
5206 r = *tofree;
5207 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005208 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005209
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005210 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005211 {
5212 partial_T *pt = tv->vval.v_partial;
5213 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005214 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005215 garray_T ga;
5216 int i;
5217 char_u *tf;
5218
5219 ga_init2(&ga, 1, 100);
5220 ga_concat(&ga, (char_u *)"function(");
5221 if (fname != NULL)
5222 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005223 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005224 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005225 && vim_isupper(fname[1]))
5226 {
5227 ga_concat(&ga, (char_u *)"'g:");
5228 ga_concat(&ga, fname + 1);
5229 }
5230 else
5231 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005232 vim_free(fname);
5233 }
5234 if (pt != NULL && pt->pt_argc > 0)
5235 {
5236 ga_concat(&ga, (char_u *)", [");
5237 for (i = 0; i < pt->pt_argc; ++i)
5238 {
5239 if (i > 0)
5240 ga_concat(&ga, (char_u *)", ");
5241 ga_concat(&ga,
5242 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5243 vim_free(tf);
5244 }
5245 ga_concat(&ga, (char_u *)"]");
5246 }
5247 if (pt != NULL && pt->pt_dict != NULL)
5248 {
5249 typval_T dtv;
5250
5251 ga_concat(&ga, (char_u *)", ");
5252 dtv.v_type = VAR_DICT;
5253 dtv.vval.v_dict = pt->pt_dict;
5254 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5255 vim_free(tf);
5256 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005257 // terminate with ')' and a NUL
5258 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005259
5260 *tofree = ga.ga_data;
5261 r = *tofree;
5262 break;
5263 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005264
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005265 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005266 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005267 break;
5268
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005269 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005270 if (tv->vval.v_list == NULL)
5271 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005272 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005273 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005274 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005275 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005276 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5277 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005278 {
5279 *tofree = NULL;
5280 r = (char_u *)"[...]";
5281 }
5282 else
5283 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005284 int old_copyID = tv->vval.v_list->lv_copyID;
5285
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005286 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005287 *tofree = list2string(tv, copyID, restore_copyID);
5288 if (restore_copyID)
5289 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005290 r = *tofree;
5291 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005292 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005293
Bram Moolenaar8c711452005-01-14 21:53:12 +00005294 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005295 if (tv->vval.v_dict == NULL)
5296 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005297 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005298 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005299 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005300 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005301 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5302 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005303 {
5304 *tofree = NULL;
5305 r = (char_u *)"{...}";
5306 }
5307 else
5308 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005309 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005310
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005311 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005312 *tofree = dict2string(tv, copyID, restore_copyID);
5313 if (restore_copyID)
5314 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005315 r = *tofree;
5316 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005317 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005318
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005319 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005320 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005321 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005322 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005323 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005324 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005325 break;
5326
Bram Moolenaar835dc632016-02-07 14:27:38 +01005327 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005328 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005329#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005330 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005331 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5332 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005333 if (composite_val)
5334 {
5335 *tofree = string_quote(r, FALSE);
5336 r = *tofree;
5337 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005338#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005339 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005340
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005341 case VAR_INSTR:
5342 *tofree = NULL;
5343 r = (char_u *)"instructions";
5344 break;
5345
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005346 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005347#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005348 *tofree = NULL;
5349 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5350 r = numbuf;
5351 break;
5352#endif
5353
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005354 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005355 case VAR_SPECIAL:
5356 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005357 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005358 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005359 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005360
Bram Moolenaar8502c702014-06-17 12:51:16 +02005361 if (--recurse == 0)
5362 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005363 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005364}
5365
5366/*
5367 * Return a string with the string representation of a variable.
5368 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5369 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005370 * Does not put quotes around strings, as ":echo" displays values.
5371 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5372 * May return NULL.
5373 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005374 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005375echo_string(
5376 typval_T *tv,
5377 char_u **tofree,
5378 char_u *numbuf,
5379 int copyID)
5380{
5381 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5382}
5383
5384/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005385 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5386 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005387 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005388 */
5389 int
5390buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5391{
5392 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005393 char_u *t;
5394 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005395
5396 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5397 return -1;
5398
5399 if (lnum > buf->b_ml.ml_line_count)
5400 lnum = buf->b_ml.ml_line_count;
5401
5402 str = ml_get_buf(buf, lnum, FALSE);
5403 if (str == NULL)
5404 return -1;
5405
5406 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005407 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005408
Bram Moolenaar91458462021-01-13 20:08:38 +01005409 // count the number of characters
5410 t = str;
5411 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5412 t += mb_ptr2len(t);
5413
5414 // In insert mode, when the cursor is at the end of a non-empty line,
5415 // byteidx points to the NUL character immediately past the end of the
5416 // string. In this case, add one to the character count.
5417 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5418 count++;
5419
5420 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005421}
5422
5423/*
5424 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005425 * byte index. Works only for loaded buffers. Returns -1 on failure.
5426 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005427 */
5428 int
5429buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5430{
5431 char_u *str;
5432 char_u *t;
5433
5434 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5435 return -1;
5436
5437 if (lnum > buf->b_ml.ml_line_count)
5438 lnum = buf->b_ml.ml_line_count;
5439
5440 str = ml_get_buf(buf, lnum, FALSE);
5441 if (str == NULL)
5442 return -1;
5443
5444 // Convert the character offset to a byte offset
5445 t = str;
5446 while (*t != NUL && --charidx > 0)
5447 t += mb_ptr2len(t);
5448
Bram Moolenaar91458462021-01-13 20:08:38 +01005449 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005450}
5451
5452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005454 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005456 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005457var2fpos(
5458 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005459 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005460 int *fnum, // set to fnum for '0, 'A, etc.
5461 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005463 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005465 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005467 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005468 if (varp->v_type == VAR_LIST)
5469 {
5470 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005471 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005472 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005473 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005474
5475 l = varp->vval.v_list;
5476 if (l == NULL)
5477 return NULL;
5478
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005479 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005480 pos.lnum = list_find_nr(l, 0L, &error);
5481 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005482 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005483 if (charcol)
5484 len = (long)mb_charlen(ml_get(pos.lnum));
5485 else
5486 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005487
Bram Moolenaarec65d772020-08-20 22:29:12 +02005488 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005489 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005490 li = list_find(l, 1L);
5491 if (li != NULL && li->li_tv.v_type == VAR_STRING
5492 && li->li_tv.vval.v_string != NULL
5493 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005494 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005495 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005496 }
5497 else
5498 {
5499 pos.col = list_find_nr(l, 1L, &error);
5500 if (error)
5501 return NULL;
5502 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005503
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005504 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005505 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005506 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005507 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005508
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005509 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005510 pos.coladd = list_find_nr(l, 2L, &error);
5511 if (error)
5512 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005513
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005514 return &pos;
5515 }
5516
Bram Moolenaarc5809432021-03-27 21:23:30 +01005517 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5518 return NULL;
5519
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005520 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005521 if (name == NULL)
5522 return NULL;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005523 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005524 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005525 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005526 pos = curwin->w_cursor;
5527 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005528 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005529 return &pos;
5530 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005531 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005532 {
5533 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005534 pos = VIsual;
5535 else
5536 pos = curwin->w_cursor;
5537 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005538 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005539 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005540 }
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005541 if (name[0] == '\'' && (!in_vim9script()
5542 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005544 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005545 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5547 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005548 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005549 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 return pp;
5551 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005552
Bram Moolenaara5525202006-03-02 22:52:09 +00005553 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005554
Bram Moolenaar477933c2007-07-17 14:32:23 +00005555 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005556 {
5557 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005558 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005559 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005560 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005561 // In silent Ex mode topline is zero, but that's not a valid line
5562 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005563 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005564 return &pos;
5565 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005566 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005567 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005568 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005569 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005570 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005571 return &pos;
5572 }
5573 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005574 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005576 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 {
5578 pos.lnum = curbuf->b_ml.ml_line_count;
5579 pos.col = 0;
5580 }
5581 else
5582 {
5583 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005584 if (charcol)
5585 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5586 else
5587 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 }
5589 return &pos;
5590 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005591 if (in_vim9script())
5592 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 return NULL;
5594}
5595
5596/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005597 * Convert list in "arg" into a position and optional file number.
5598 * When "fnump" is NULL there is no file number, only 3 items.
5599 * Note that the column is passed on as-is, the caller may want to decrement
5600 * it to use 1 for the first column.
5601 * Return FAIL when conversion is not possible, doesn't check the position for
5602 * validity.
5603 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005604 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005605list2fpos(
5606 typval_T *arg,
5607 pos_T *posp,
5608 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005609 colnr_T *curswantp,
5610 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005611{
5612 list_T *l = arg->vval.v_list;
5613 long i = 0;
5614 long n;
5615
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005616 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5617 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005618 if (arg->v_type != VAR_LIST
5619 || l == NULL
5620 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005621 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005622 return FAIL;
5623
5624 if (fnump != NULL)
5625 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005626 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005627 if (n < 0)
5628 return FAIL;
5629 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005630 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005631 *fnump = n;
5632 }
5633
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005634 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005635 if (n < 0)
5636 return FAIL;
5637 posp->lnum = n;
5638
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005639 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005640 if (n < 0)
5641 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005642 // If character position is specified, then convert to byte position
5643 if (charcol)
5644 {
5645 buf_T *buf;
5646
5647 // Get the text for the specified line in a loaded buffer
5648 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5649 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5650 return FAIL;
5651
Bram Moolenaar91458462021-01-13 20:08:38 +01005652 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005653 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005654 posp->col = n;
5655
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005656 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005657 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005658 posp->coladd = 0;
5659 else
5660 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005661
Bram Moolenaar493c1782014-05-28 14:34:46 +02005662 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005663 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005664
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005665 return OK;
5666}
5667
5668/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 * Get the length of an environment variable name.
5670 * Advance "arg" to the first character after the name.
5671 * Return 0 for error.
5672 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005673 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005674get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675{
5676 char_u *p;
5677 int len;
5678
5679 for (p = *arg; vim_isIDc(*p); ++p)
5680 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005681 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 return 0;
5683
5684 len = (int)(p - *arg);
5685 *arg = p;
5686 return len;
5687}
5688
5689/*
5690 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005691 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 * Return 0 if something is wrong.
5693 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005694 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005695get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696{
5697 char_u *p;
5698 int len;
5699
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005700 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005702 {
5703 if (*p == ':')
5704 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005705 // "s:" is start of "s:var", but "n:" is not and can be used in
5706 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005707 len = (int)(p - *arg);
5708 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5709 || len > 1)
5710 break;
5711 }
5712 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005713 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 return 0;
5715
5716 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005717 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718
5719 return len;
5720}
5721
5722/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005723 * Get the length of the name of a variable or function.
5724 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005726 * Return -1 if curly braces expansion failed.
5727 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 * If the name contains 'magic' {}'s, expand them and return the
5729 * expanded name in an allocated string via 'alias' - caller must free.
5730 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005731 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005732get_name_len(
5733 char_u **arg,
5734 char_u **alias,
5735 int evaluate,
5736 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737{
5738 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 char_u *p;
5740 char_u *expr_start;
5741 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005743 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744
5745 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5746 && (*arg)[2] == (int)KE_SNR)
5747 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005748 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 *arg += 3;
5750 return get_id_len(arg) + 3;
5751 }
5752 len = eval_fname_script(*arg);
5753 if (len > 0)
5754 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005755 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 *arg += len;
5757 }
5758
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005760 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005762 p = find_name_end(*arg, &expr_start, &expr_end,
5763 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 if (expr_start != NULL)
5765 {
5766 char_u *temp_string;
5767
5768 if (!evaluate)
5769 {
5770 len += (int)(p - *arg);
5771 *arg = skipwhite(p);
5772 return len;
5773 }
5774
5775 /*
5776 * Include any <SID> etc in the expanded string:
5777 * Thus the -len here.
5778 */
5779 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5780 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005781 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 *alias = temp_string;
5783 *arg = skipwhite(p);
5784 return (int)STRLEN(temp_string);
5785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786
5787 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005788 // Only give an error when there is something, otherwise it will be
5789 // reported at a higher level.
5790 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005791 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792
5793 return len;
5794}
5795
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005796/*
5797 * Find the end of a variable or function name, taking care of magic braces.
5798 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5799 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005800 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005801 * Return a pointer to just after the name. Equal to "arg" if there is no
5802 * valid name.
5803 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005804 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005805find_name_end(
5806 char_u *arg,
5807 char_u **expr_start,
5808 char_u **expr_end,
5809 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005811 int mb_nest = 0;
5812 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005814 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005815 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005817 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005819 *expr_start = NULL;
5820 *expr_end = NULL;
5821 }
5822
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005823 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005824 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5825 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005826 return arg;
5827
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005828 for (p = arg; *p != NUL
5829 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005830 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005831 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005832 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005833 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005834 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005835 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005836 if (*p == '\'')
5837 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005838 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005839 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005840 ;
5841 if (*p == NUL)
5842 break;
5843 }
5844 else if (*p == '"')
5845 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005846 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005847 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005848 if (*p == '\\' && p[1] != NUL)
5849 ++p;
5850 if (*p == NUL)
5851 break;
5852 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005853 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5854 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005855 // "s:" is start of "s:var", but "n:" is not and can be used in
5856 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005857 len = (int)(p - arg);
5858 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005859 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005860 break;
5861 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005862
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005863 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005865 if (*p == '[')
5866 ++br_nest;
5867 else if (*p == ']')
5868 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005870
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005871 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005873 if (*p == '{')
5874 {
5875 mb_nest++;
5876 if (expr_start != NULL && *expr_start == NULL)
5877 *expr_start = p;
5878 }
5879 else if (*p == '}')
5880 {
5881 mb_nest--;
5882 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5883 *expr_end = p;
5884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 }
5887
5888 return p;
5889}
5890
5891/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005892 * Expands out the 'magic' {}'s in a variable/function name.
5893 * Note that this can call itself recursively, to deal with
5894 * constructs like foo{bar}{baz}{bam}
5895 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5896 * "in_start" ^
5897 * "expr_start" ^
5898 * "expr_end" ^
5899 * "in_end" ^
5900 *
5901 * Returns a new allocated string, which the caller must free.
5902 * Returns NULL for failure.
5903 */
5904 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005905make_expanded_name(
5906 char_u *in_start,
5907 char_u *expr_start,
5908 char_u *expr_end,
5909 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005910{
5911 char_u c1;
5912 char_u *retval = NULL;
5913 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005914
5915 if (expr_end == NULL || in_end == NULL)
5916 return NULL;
5917 *expr_start = NUL;
5918 *expr_end = NUL;
5919 c1 = *in_end;
5920 *in_end = NUL;
5921
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005922 temp_result = eval_to_string(expr_start + 1, FALSE);
5923 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005924 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005925 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5926 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005927 if (retval != NULL)
5928 {
5929 STRCPY(retval, in_start);
5930 STRCAT(retval, temp_result);
5931 STRCAT(retval, expr_end + 1);
5932 }
5933 }
5934 vim_free(temp_result);
5935
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005936 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005937 *expr_start = '{';
5938 *expr_end = '}';
5939
5940 if (retval != NULL)
5941 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005942 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005943 if (expr_start != NULL)
5944 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005945 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005946 temp_result = make_expanded_name(retval, expr_start,
5947 expr_end, temp_result);
5948 vim_free(retval);
5949 retval = temp_result;
5950 }
5951 }
5952
5953 return retval;
5954}
5955
5956/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005958 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005960 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005961eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005963 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005964}
5965
5966/*
5967 * Return TRUE if character "c" can be used as the first character in a
5968 * variable or function name (excluding '{' and '}').
5969 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005970 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005971eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005972{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005973 return ASCII_ISALPHA(c) || c == '_';
5974}
5975
5976/*
5977 * Return TRUE if character "c" can be used as the first character of a
5978 * dictionary key.
5979 */
5980 int
5981eval_isdictc(int c)
5982{
5983 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984}
5985
5986/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005987 * Handle:
5988 * - expr[expr], expr[expr:expr] subscript
5989 * - ".name" lookup
5990 * - function call with Funcref variable: func(expr)
5991 * - method call: var->method()
5992 *
5993 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00005994 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005995 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005996 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005997handle_subscript(
5998 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00005999 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006000 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006001 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006002 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006003{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006004 int evaluate = evalarg != NULL
6005 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006006 int ret = OK;
6007 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006008 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006009 int getnext;
6010 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006011
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006012 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006013 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006014 // When at the end of the line and ".name" or "->{" or "->X" follows in
6015 // the next line then consume the line break.
6016 p = eval_next_non_blank(*arg, evalarg, &getnext);
6017 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006018 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006019 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6020 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6021 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006022 {
6023 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006024 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006025 check_white = FALSE;
6026 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006027
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006028 if (rettv->v_type == VAR_ANY)
6029 {
6030 char_u *exp_name;
6031 int cc;
6032 int idx;
6033 ufunc_T *ufunc;
6034 type_T *type;
6035
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006036 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006037 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006038 if (**arg != '.')
6039 {
6040 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006041 semsg(_(e_expected_dot_after_name_str),
6042 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006043 ret = FAIL;
6044 break;
6045 }
6046 ++*arg;
6047 if (IS_WHITE_OR_NUL(**arg))
6048 {
6049 if (verbose)
6050 emsg(_(e_no_white_space_allowed_after_dot));
6051 ret = FAIL;
6052 break;
6053 }
6054
6055 // isolate the name
6056 exp_name = *arg;
6057 while (eval_isnamec(**arg))
6058 ++*arg;
6059 cc = **arg;
6060 **arg = NUL;
6061
6062 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
6063 evalarg->eval_cctx, verbose);
6064 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006065
6066 if (idx < 0 && ufunc == NULL)
6067 {
6068 ret = FAIL;
6069 break;
6070 }
6071 if (idx >= 0)
6072 {
6073 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6074 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6075
6076 copy_tv(sv->sv_tv, rettv);
6077 }
6078 else
6079 {
6080 rettv->v_type = VAR_FUNC;
6081 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6082 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006083 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006084 }
6085
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006086 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6087 || rettv->v_type == VAR_PARTIAL))
6088 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006089 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006090 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6091 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006092
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006093 // Stop the expression evaluation when immediately aborting on
6094 // error, or when an interrupt occurred or an exception was thrown
6095 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006096 if (aborting())
6097 {
6098 if (ret == OK)
6099 clear_tv(rettv);
6100 ret = FAIL;
6101 }
6102 dict_unref(selfdict);
6103 selfdict = NULL;
6104 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006105 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006106 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006107 if (in_vim9script())
6108 *arg = skipwhite(p + 2);
6109 else
6110 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006111 if (ret == OK)
6112 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006113 if (VIM_ISWHITE(**arg))
6114 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006115 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006116 ret = FAIL;
6117 }
6118 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006119 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006120 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006121 else
6122 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006123 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006124 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006125 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006126 // "." is ".name" lookup when we found a dict or when evaluating and
6127 // scriptversion is at least 2, where string concatenation is "..".
6128 else if (**arg == '['
6129 || (**arg == '.' && (rettv->v_type == VAR_DICT
6130 || (!evaluate
6131 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006132 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006133 {
6134 dict_unref(selfdict);
6135 if (rettv->v_type == VAR_DICT)
6136 {
6137 selfdict = rettv->vval.v_dict;
6138 if (selfdict != NULL)
6139 ++selfdict->dv_refcount;
6140 }
6141 else
6142 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006143 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006144 {
6145 clear_tv(rettv);
6146 ret = FAIL;
6147 }
6148 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006149 else
6150 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006151 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006152
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006153 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6154 // Don't do this when "Func" is already a partial that was bound
6155 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006156 if (selfdict != NULL
6157 && (rettv->v_type == VAR_FUNC
6158 || (rettv->v_type == VAR_PARTIAL
6159 && (rettv->vval.v_partial->pt_auto
6160 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006161 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006162
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006163 dict_unref(selfdict);
6164 return ret;
6165}
6166
6167/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006168 * Make a copy of an item.
6169 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006170 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6171 * reference to an already copied list/dict can be used.
6172 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006173 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006174 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006175item_copy(
6176 typval_T *from,
6177 typval_T *to,
6178 int deep,
6179 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006180{
6181 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006182 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006183
Bram Moolenaar33570922005-01-25 22:26:29 +00006184 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006185 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006186 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006187 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006188 }
6189 ++recurse;
6190
6191 switch (from->v_type)
6192 {
6193 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006194 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006195 case VAR_STRING:
6196 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006197 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006198 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006199 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006200 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006201 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006202 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006203 copy_tv(from, to);
6204 break;
6205 case VAR_LIST:
6206 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006207 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006208 if (from->vval.v_list == NULL)
6209 to->vval.v_list = NULL;
6210 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6211 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006212 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006213 to->vval.v_list = from->vval.v_list->lv_copylist;
6214 ++to->vval.v_list->lv_refcount;
6215 }
6216 else
6217 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6218 if (to->vval.v_list == NULL)
6219 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006220 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006221 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006222 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006223 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006224 case VAR_DICT:
6225 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006226 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006227 if (from->vval.v_dict == NULL)
6228 to->vval.v_dict = NULL;
6229 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6230 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006231 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006232 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6233 ++to->vval.v_dict->dv_refcount;
6234 }
6235 else
6236 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6237 if (to->vval.v_dict == NULL)
6238 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006239 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006240 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006241 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006242 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006243 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006244 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006245 }
6246 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006247 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006248}
6249
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006250 void
6251echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6252{
6253 char_u *tofree;
6254 char_u numbuf[NUMBUFLEN];
6255 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6256
6257 if (*atstart)
6258 {
6259 *atstart = FALSE;
6260 // Call msg_start() after eval1(), evaluating the expression
6261 // may cause a message to appear.
6262 if (with_space)
6263 {
6264 // Mark the saved text as finishing the line, so that what
6265 // follows is displayed on a new line when scrolling back
6266 // at the more prompt.
6267 msg_sb_eol();
6268 msg_start();
6269 }
6270 }
6271 else if (with_space)
6272 msg_puts_attr(" ", echo_attr);
6273
6274 if (p != NULL)
6275 for ( ; *p != NUL && !got_int; ++p)
6276 {
6277 if (*p == '\n' || *p == '\r' || *p == TAB)
6278 {
6279 if (*p != TAB && *needclr)
6280 {
6281 // remove any text still there from the command
6282 msg_clr_eos();
6283 *needclr = FALSE;
6284 }
6285 msg_putchar_attr(*p, echo_attr);
6286 }
6287 else
6288 {
6289 if (has_mbyte)
6290 {
6291 int i = (*mb_ptr2len)(p);
6292
6293 (void)msg_outtrans_len_attr(p, i, echo_attr);
6294 p += i - 1;
6295 }
6296 else
6297 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6298 }
6299 }
6300 vim_free(tofree);
6301}
6302
Bram Moolenaare9a41262005-01-15 22:18:47 +00006303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 * ":echo expr1 ..." print each argument separated with a space, add a
6305 * newline at the end.
6306 * ":echon expr1 ..." print each argument plain.
6307 */
6308 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006309ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310{
6311 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006312 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006313 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 int needclr = TRUE;
6315 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006316 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006317 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006318 evalarg_T evalarg;
6319
Bram Moolenaare707c882020-07-01 13:07:37 +02006320 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321
6322 if (eap->skip)
6323 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006324 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006326 // If eval1() causes an error message the text from the command may
6327 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006328 need_clr_eos = needclr;
6329
Bram Moolenaar68db9962021-05-09 23:19:22 +02006330 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006331 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 {
6333 /*
6334 * Report the invalid expression unless the expression evaluation
6335 * has been cancelled due to an aborting error, an interrupt, or an
6336 * exception.
6337 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006338 if (!aborting() && did_emsg == did_emsg_before
6339 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006340 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006341 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 break;
6343 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006344 need_clr_eos = FALSE;
6345
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006347 {
6348 if (rettv.v_type == VAR_VOID)
6349 {
6350 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6351 break;
6352 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006353 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006354 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006355
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006356 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 arg = skipwhite(arg);
6358 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006359 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006360 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361
6362 if (eap->skip)
6363 --emsg_skip;
6364 else
6365 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006366 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 if (needclr)
6368 msg_clr_eos();
6369 if (eap->cmdidx == CMD_echo)
6370 msg_end();
6371 }
6372}
6373
6374/*
6375 * ":echohl {name}".
6376 */
6377 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006378ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006380 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381}
6382
6383/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006384 * Returns the :echo attribute
6385 */
6386 int
6387get_echo_attr(void)
6388{
6389 return echo_attr;
6390}
6391
6392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 * ":execute expr1 ..." execute the result of an expression.
6394 * ":echomsg expr1 ..." Print a message
6395 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006396 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 * Each gets spaces around each argument and a newline at the end for
6398 * echo commands
6399 */
6400 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006401ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402{
6403 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006404 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 int ret = OK;
6406 char_u *p;
6407 garray_T ga;
6408 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006409 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410
6411 ga_init2(&ga, 1, 80);
6412
6413 if (eap->skip)
6414 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006415 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006417 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006418 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420
6421 if (!eap->skip)
6422 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006423 char_u buf[NUMBUFLEN];
6424
6425 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006426 {
6427 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6428 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006429 semsg(_(e_using_invalid_value_as_string_str),
6430 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006431 p = NULL;
6432 }
6433 else
6434 p = tv_get_string_buf(&rettv, buf);
6435 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006436 else
6437 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006438 if (p == NULL)
6439 {
6440 clear_tv(&rettv);
6441 ret = FAIL;
6442 break;
6443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 len = (int)STRLEN(p);
6445 if (ga_grow(&ga, len + 2) == FAIL)
6446 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006447 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 ret = FAIL;
6449 break;
6450 }
6451 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 ga.ga_len += len;
6455 }
6456
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006457 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 arg = skipwhite(arg);
6459 }
6460
6461 if (ret != FAIL && ga.ga_data != NULL)
6462 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006463 // use the first line of continuation lines for messages
6464 SOURCING_LNUM = start_lnum;
6465
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006466 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6467 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006468 // Mark the already saved text as finishing the line, so that what
6469 // follows is displayed on a new line when scrolling back at the
6470 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006471 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006472 }
6473
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006475 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006476 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006477 out_flush();
6478 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006479 else if (eap->cmdidx == CMD_echoconsole)
6480 {
6481 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6482 ui_write((char_u *)"\r\n", 2, TRUE);
6483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 else if (eap->cmdidx == CMD_echoerr)
6485 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006486 int save_did_emsg = did_emsg;
6487
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006488 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006489 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 if (!force_abort)
6491 did_emsg = save_did_emsg;
6492 }
6493 else if (eap->cmdidx == CMD_execute)
6494 do_cmdline((char_u *)ga.ga_data,
6495 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6496 }
6497
6498 ga_clear(&ga);
6499
6500 if (eap->skip)
6501 --emsg_skip;
6502
Bram Moolenaar63b91732021-08-05 20:40:03 +02006503 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504}
6505
6506/*
6507 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6508 * "arg" points to the "&" or '+' when called, to "option" when returning.
6509 * Returns NULL when no option name found. Otherwise pointer to the char
6510 * after the option name.
6511 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006512 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006513find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514{
6515 char_u *p = *arg;
6516
6517 ++p;
6518 if (*p == 'g' && p[1] == ':')
6519 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006520 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 p += 2;
6522 }
6523 else if (*p == 'l' && p[1] == ':')
6524 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006525 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 p += 2;
6527 }
6528 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006529 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530
6531 if (!ASCII_ISALPHA(*p))
6532 return NULL;
6533 *arg = p;
6534
6535 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006536 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 else
6538 while (ASCII_ISALPHA(*p))
6539 ++p;
6540 return p;
6541}
6542
6543/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006544 * Display script name where an item was last set.
6545 * Should only be invoked when 'verbose' is non-zero.
6546 */
6547 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006548last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006549{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006550 char_u *p;
6551
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006552 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006553 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006554 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006555 if (p != NULL)
6556 {
6557 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006558 msg_puts(_("\n\tLast set from "));
6559 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006560 if (script_ctx.sc_lnum > 0)
6561 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006562 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006563 msg_outnum((long)script_ctx.sc_lnum);
6564 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006565 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006566 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006567 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006568 }
6569}
6570
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006571#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572
6573/*
6574 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006575 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 * "flags" can be "g" to do a global substitute.
6577 * Returns an allocated string, NULL for error.
6578 */
6579 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006580do_string_sub(
6581 char_u *str,
6582 char_u *pat,
6583 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006584 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006585 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586{
6587 int sublen;
6588 regmatch_T regmatch;
6589 int i;
6590 int do_all;
6591 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006592 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 garray_T ga;
6594 char_u *ret;
6595 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006596 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006598 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006600 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601
6602 ga_init2(&ga, 1, 200);
6603
6604 do_all = (flags[0] == 'g');
6605
6606 regmatch.rm_ic = p_ic;
6607 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6608 if (regmatch.regprog != NULL)
6609 {
6610 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006611 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6613 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006614 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006615 if (regmatch.startp[0] == regmatch.endp[0])
6616 {
6617 if (zero_width == regmatch.startp[0])
6618 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006619 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006620 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006621 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6622 (size_t)i);
6623 ga.ga_len += i;
6624 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006625 continue;
6626 }
6627 zero_width = regmatch.startp[0];
6628 }
6629
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 /*
6631 * Get some space for a temporary buffer to do the substitution
6632 * into. It will contain:
6633 * - The text up to where the match is.
6634 * - The substituted text.
6635 * - The text after the match.
6636 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006637 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006638 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6640 {
6641 ga_clear(&ga);
6642 break;
6643 }
6644
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006645 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 i = (int)(regmatch.startp[0] - tail);
6647 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006648 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006649 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 + ga.ga_len + i, TRUE, TRUE, FALSE);
6651 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006652 tail = regmatch.endp[0];
6653 if (*tail == NUL)
6654 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 if (!do_all)
6656 break;
6657 }
6658
6659 if (ga.ga_data != NULL)
6660 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6661
Bram Moolenaar473de612013-06-08 18:19:48 +02006662 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 }
6664
6665 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6666 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006667 if (p_cpo == empty_option)
6668 p_cpo = save_cpo;
6669 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006670 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006671 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006672 // If it's still empty it was changed and restored, need to restore in
6673 // the complicated way.
6674 if (*p_cpo == NUL)
6675 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006676 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678
6679 return ret;
6680}