blob: ec34ef13e2b532a29552c8bc53d066dad38338be [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 Moolenaar7454a062016-01-30 15:14:10 +0100558 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559{
560 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200561 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200562 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200563 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200565 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200566 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000567 if (use_sandbox)
568 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200569 ++textwinlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200570 may_garbage_collect = FALSE;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200571 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000572 if (use_sandbox)
573 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200574 --textwinlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200575 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200576 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200577 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 return retval;
579}
580
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581/*
582 * Top level evaluation function, returning a number.
583 * Evaluates "expr" silently.
584 * Returns -1 for an error.
585 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200586 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100587eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588{
Bram Moolenaar33570922005-01-25 22:26:29 +0000589 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200590 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000591 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
593 ++emsg_off;
594
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200595 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 retval = -1;
597 else
598 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100599 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000600 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 }
602 --emsg_off;
603
604 return retval;
605}
606
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000607/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000608 * Top level evaluation function.
609 * Returns an allocated typval_T with the result.
610 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000611 */
612 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200613eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000614{
615 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200616 evalarg_T evalarg;
617
618 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000619
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200620 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200621 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100622 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000623
Bram Moolenaar37c83712020-06-30 21:18:36 +0200624 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000625 return tv;
626}
627
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000629 * "*arg" points to what can be a function name in the form of "import.Name" or
630 * "Funcref". Return the name of the function. Set "tofree" to something that
631 * was allocated.
632 * If "verbose" is FALSE no errors are given.
633 * Return NULL for any failure.
634 */
635 static char_u *
636deref_function_name(
637 char_u **arg,
638 char_u **tofree,
639 evalarg_T *evalarg,
640 int verbose)
641{
642 typval_T ref;
643 char_u *name = *arg;
644
645 ref.v_type = VAR_UNKNOWN;
646 if (eval7(arg, &ref, evalarg, FALSE) == FAIL)
647 return NULL;
648 if (*skipwhite(*arg) != NUL)
649 {
650 if (verbose)
651 semsg(_(e_trailing_characters_str), *arg);
652 name = NULL;
653 }
654 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
655 {
656 name = ref.vval.v_string;
657 ref.vval.v_string = NULL;
658 *tofree = name;
659 }
660 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
661 {
662 if (ref.vval.v_partial->pt_argc > 0
663 || ref.vval.v_partial->pt_dict != NULL)
664 {
665 if (verbose)
666 emsg(_(e_cannot_use_partial_here));
667 name = NULL;
668 }
669 else
670 {
671 name = vim_strsave(partial_name(ref.vval.v_partial));
672 *tofree = name;
673 }
674 }
675 else
676 {
677 if (verbose)
678 semsg(_(e_not_callable_type_str), name);
679 name = NULL;
680 }
681 clear_tv(&ref);
682 return name;
683}
684
685/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100686 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200687 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
688 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000689 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200691 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100692call_vim_function(
693 char_u *func,
694 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200695 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200696 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000698 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200699 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000700 char_u *arg;
701 char_u *name;
702 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100704 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200705 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000706 funcexe.fe_firstline = curwin->w_cursor.lnum;
707 funcexe.fe_lastline = curwin->w_cursor.lnum;
708 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000709
710 // The name might be "import.Func" or "Funcref".
711 arg = func;
Bram Moolenaarb3d9cee2022-01-17 21:13:28 +0000712 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000713 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarb3d9cee2022-01-17 21:13:28 +0000714 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000715 if (name == NULL)
716 name = func;
717
718 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
719
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000720 if (ret == FAIL)
721 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000722 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000723
724 return ret;
725}
726
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100727/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100728 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000729 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
730 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000731 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000732 */
733 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100734call_func_retstr(
735 char_u *func,
736 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200737 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000738{
739 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000740 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000741
Bram Moolenaarded27a12018-08-01 19:06:03 +0200742 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000743 return NULL;
744
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100745 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000746 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 return retval;
748}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000749
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000750/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100751 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000752 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000753 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000754 */
755 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100756call_func_retlist(
757 char_u *func,
758 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200759 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000760{
761 typval_T rettv;
762
Bram Moolenaarded27a12018-08-01 19:06:03 +0200763 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000764 return NULL;
765
766 if (rettv.v_type != VAR_LIST)
767 {
768 clear_tv(&rettv);
769 return NULL;
770 }
771
772 return rettv.vval.v_list;
773}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774
Bram Moolenaare70dd112022-01-21 16:31:11 +0000775#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100777 * Evaluate "arg", which is 'foldexpr'.
778 * Note: caller must set "curwin" to match "arg".
779 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
780 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 */
782 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000783eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000785 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000786 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200787 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000789 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000790 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
791 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792
Bram Moolenaare70dd112022-01-21 16:31:11 +0000793 arg = wp->w_p_fde;
794 current_sctx = wp->w_p_script_ctx[WV_FDE];
795
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000797 if (use_sandbox)
798 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200799 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200801 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 retval = 0;
803 else
804 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100805 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000806 if (tv.v_type == VAR_NUMBER)
807 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000808 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 retval = 0;
810 else
811 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100812 // If the result is a string, check if there is a non-digit before
813 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000814 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 if (!VIM_ISDIGIT(*s) && *s != '-')
816 *cp = *s++;
817 retval = atol((char *)s);
818 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000819 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 }
821 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000822 if (use_sandbox)
823 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200824 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200825 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000826 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200828 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829}
830#endif
831
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000833 * Get an lval: variable, Dict item or List item that can be assigned a value
834 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
835 * "name.key", "name.key[expr]" etc.
836 * Indexing only works if "name" is an existing List or Dictionary.
837 * "name" points to the start of the name.
838 * If "rettv" is not NULL it points to the value to be assigned.
839 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
840 * wrong; must end in space or cmd separator.
841 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100842 * flags:
843 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100844 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100845 * GLV_NO_AUTOLOAD: do not use script autoloading
846 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000847 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000848 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000849 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000850 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200851 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100852get_lval(
853 char_u *name,
854 typval_T *rettv,
855 lval_T *lp,
856 int unlet,
857 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100858 int flags, // GLV_ values
859 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000860{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000861 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000862 char_u *expr_start, *expr_end;
863 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000864 dictitem_T *v;
865 typval_T var1;
866 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000867 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000868 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000869 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100870 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100871 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100872 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000873
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100874 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200875 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000876
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100877 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000878 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100879 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000880 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200881 lp->ll_name_end = find_name_end(name, NULL, NULL,
882 FNE_INCL_BR | fne_flags);
883 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000884 }
885
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100886 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000887 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100888 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000889 if (expr_start != NULL)
890 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100891 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100892 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000893 && *p != '[' && *p != '.')
894 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000895 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000896 return NULL;
897 }
898
899 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
900 if (lp->ll_exp_name == NULL)
901 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100902 // Report an invalid expression in braces, unless the
903 // expression evaluation has been cancelled due to an
904 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000905 if (!aborting() && !quiet)
906 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000907 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000908 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000909 return NULL;
910 }
911 }
912 lp->ll_name = lp->ll_exp_name;
913 }
914 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100915 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000916 lp->ll_name = name;
917
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200918 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100919 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200920 // "a: type" is declaring variable "a" with a type, not "a:".
921 if (p == name + 2 && p[-1] == ':')
922 {
923 --p;
924 lp->ll_name_end = p;
925 }
926 if (*p == ':')
927 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000928 garray_T tmp_type_list;
929 garray_T *type_list;
930 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +0200931
932 if (tp == p + 1 && !quiet)
933 {
934 semsg(_(e_white_space_required_after_str_str), ":", p);
935 return NULL;
936 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100937
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000938 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
939 type_list = &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list;
940 else
941 {
942 type_list = &tmp_type_list;
943 ga_init2(type_list, sizeof(type_T), 10);
944 }
945
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200946 // parse the type after the name
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000947 lp->ll_type = parse_type(&tp, type_list, !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100948 if (lp->ll_type == NULL && !quiet)
949 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200950 lp->ll_name_end = tp;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000951
952 // drop the type when not in a script
953 if (type_list == &tmp_type_list)
954 {
955 lp->ll_type = NULL;
956 clear_type_list(type_list);
957 }
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200958 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959 }
960 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000961 if (lp->ll_name == NULL)
962 return p;
963
964 if (*p == '.' && in_vim9script())
965 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000966 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name,
967 TRUE, NULL);
968
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000969 if (import != NULL)
970 {
971 ufunc_T *ufunc;
972 type_T *type;
973
974 lp->ll_sid = import->imp_sid;
975 lp->ll_name = skipwhite(p + 1);
976 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
977 lp->ll_name_end = p;
978
979 // check the item is exported
980 cc = *p;
981 *p = NUL;
982 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
983 NULL, TRUE) == -1)
984 {
985 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +0000986 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000987 }
988 *p = cc;
989 }
990 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100991
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100992 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000993 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000994 return p;
995
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200996 if (in_vim9script() && lval_root != NULL)
997 {
998 // using local variable
999 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001000 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001001 }
1002 else
1003 {
1004 cc = *p;
1005 *p = NUL;
1006 // When we would write to the variable pass &ht and prevent autoload.
1007 writing = !(flags & GLV_READ_ONLY);
1008 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001009 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001010 if (v == NULL && !quiet)
1011 semsg(_(e_undefined_variable_str), lp->ll_name);
1012 *p = cc;
1013 if (v == NULL)
1014 return NULL;
1015 lp->ll_tv = &v->di_tv;
1016 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001017
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001018 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
1019 {
1020 if (!quiet)
1021 semsg(_(e_variable_already_declared), lp->ll_name);
1022 return NULL;
1023 }
1024
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001025 /*
1026 * Loop until no more [idx] or .key is following.
1027 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001028 var1.v_type = VAR_UNKNOWN;
1029 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001030 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001031 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001032 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1033 {
1034 if (!quiet)
1035 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1036 return NULL;
1037 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001038 if (lp->ll_tv->v_type != VAR_LIST
1039 && lp->ll_tv->v_type != VAR_DICT
1040 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001041 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001042 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001043 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001044 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001045 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001046
1047 // a NULL list/blob works like an empty list/blob, allocate one now.
1048 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1049 rettv_list_alloc(lp->ll_tv);
1050 else if (lp->ll_tv->v_type == VAR_BLOB
1051 && lp->ll_tv->vval.v_blob == NULL)
1052 rettv_blob_alloc(lp->ll_tv);
1053
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001054 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001055 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001056 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001057 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001058 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001059 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001060
Bram Moolenaar10c65862020-10-08 21:16:42 +02001061 if (in_vim9script() && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001062 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001063 && lp->ll_tv == &v->di_tv
1064 && ht != NULL && ht == get_script_local_ht())
1065 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001066 svar_T *sv = find_typval_in_script(lp->ll_tv, 0);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001067
1068 // Vim9 script local variable: get the type
1069 if (sv != NULL)
1070 lp->ll_valtype = sv->sv_type;
1071 }
1072
Bram Moolenaar8c711452005-01-14 21:53:12 +00001073 len = -1;
1074 if (*p == '.')
1075 {
1076 key = p + 1;
1077 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1078 ;
1079 if (len == 0)
1080 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001081 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001082 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001083 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001084 }
1085 p = key + len;
1086 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001087 else
1088 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001089 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001090 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001091 if (*p == ':')
1092 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001093 else
1094 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001095 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001096 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001097 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001098 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001099 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001100 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001101 clear_tv(&var1);
1102 return NULL;
1103 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001104 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001105 }
1106
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001107 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001108 if (*p == ':')
1109 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001110 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001111 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001112 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001113 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001114 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001115 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001116 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001117 if (rettv != NULL
1118 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001119 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001120 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001121 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001122 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001123 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001124 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001125 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001126 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001127 }
1128 p = skipwhite(p + 1);
1129 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001130 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001131 else
1132 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001133 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001134 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001135 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001136 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001137 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001138 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001139 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001140 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001141 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001142 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001143 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001144 clear_tv(&var2);
1145 return NULL;
1146 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001147 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001148 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001149 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001150 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001151 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001152
Bram Moolenaar8c711452005-01-14 21:53:12 +00001153 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001154 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001155 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001156 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001157 clear_tv(&var1);
1158 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001159 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001160 }
1161
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001162 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001163 ++p;
1164 }
1165
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001166 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001167 {
1168 if (len == -1)
1169 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001170 // "[key]": get key from "var1"
1171 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001172 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001173 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001174 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001175 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001176 }
1177 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001178 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001179
1180 // a NULL dict is equivalent with an empty dict
1181 if (lp->ll_tv->vval.v_dict == NULL)
1182 {
1183 lp->ll_tv->vval.v_dict = dict_alloc();
1184 if (lp->ll_tv->vval.v_dict == NULL)
1185 {
1186 clear_tv(&var1);
1187 return NULL;
1188 }
1189 ++lp->ll_tv->vval.v_dict->dv_refcount;
1190 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001191 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001192
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001193 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001194
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001195 // When assigning to a scope dictionary check that a function and
1196 // variable name is valid (only variable name unless it is l: or
1197 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001198 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001199 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001200 int prevval;
1201 int wrong;
1202
1203 if (len != -1)
1204 {
1205 prevval = key[len];
1206 key[len] = NUL;
1207 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001208 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001209 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001210 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1211 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001212 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001213 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001214 if (len != -1)
1215 key[len] = prevval;
1216 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001217 {
1218 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001219 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001220 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001221 }
1222
Bram Moolenaar10c65862020-10-08 21:16:42 +02001223 if (lp->ll_valtype != NULL)
1224 // use the type of the member
1225 lp->ll_valtype = lp->ll_valtype->tt_member;
1226
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001227 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001228 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001229 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001230 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001231 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001232 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001233 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001234 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001235 return NULL;
1236 }
1237
Bram Moolenaar31b81602019-02-10 22:14:27 +01001238 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001239 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001240 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001241 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001242 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001243 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001244 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001245 }
1246 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001247 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001248 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001249 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001250 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001251 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001252 p = NULL;
1253 break;
1254 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001255 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001256 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001257 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1258 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001259 {
1260 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001261 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001262 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001263
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001264 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001265 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001266 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001267 else if (lp->ll_tv->v_type == VAR_BLOB)
1268 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001269 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1270
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001271 /*
1272 * Get the number and item for the only or first index of the List.
1273 */
1274 if (empty1)
1275 lp->ll_n1 = 0;
1276 else
1277 // is number or string
1278 lp->ll_n1 = (long)tv_get_number(&var1);
1279 clear_tv(&var1);
1280
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001281 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001282 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001283 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001284 return NULL;
1285 }
1286 if (lp->ll_range && !lp->ll_empty2)
1287 {
1288 lp->ll_n2 = (long)tv_get_number(&var2);
1289 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001290 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1291 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001292 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001293 }
1294 lp->ll_blob = lp->ll_tv->vval.v_blob;
1295 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001296 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001297 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001298 else
1299 {
1300 /*
1301 * Get the number and item for the only or first index of the List.
1302 */
1303 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001304 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001305 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001306 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001307 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001308 clear_tv(&var1);
1309
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001310 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001311 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001312 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001313 if (lp->ll_li == NULL)
1314 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001315 clear_tv(&var2);
1316 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001317 }
1318
Bram Moolenaar10c65862020-10-08 21:16:42 +02001319 if (lp->ll_valtype != NULL)
1320 // use the type of the member
1321 lp->ll_valtype = lp->ll_valtype->tt_member;
1322
Bram Moolenaar8c711452005-01-14 21:53:12 +00001323 /*
1324 * May need to find the item or absolute index for the second
1325 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001326 * When no index given: "lp->ll_empty2" is TRUE.
1327 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001328 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001329 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001330 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001331 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001332 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001333 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001334 if (check_range_index_two(lp->ll_list,
1335 &lp->ll_n1, lp->ll_li,
1336 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001337 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001338 }
1339
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001340 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001341 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001342 }
1343
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001344 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001346 return p;
1347}
1348
1349/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001350 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001351 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001352 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001353clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001354{
1355 vim_free(lp->ll_exp_name);
1356 vim_free(lp->ll_newkey);
1357}
1358
1359/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001360 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001361 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001362 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1363 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001364 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001365 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001366set_var_lval(
1367 lval_T *lp,
1368 char_u *endp,
1369 typval_T *rettv,
1370 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001371 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1372 char_u *op,
1373 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001374{
1375 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001376 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001377
1378 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001379 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001380 cc = *endp;
1381 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001382 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1383 return;
1384
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001385 if (lp->ll_blob != NULL)
1386 {
1387 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001388
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001389 if (op != NULL && *op != '=')
1390 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001391 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001392 return;
1393 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001394 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001395 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001396
1397 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1398 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001399 if (lp->ll_empty2)
1400 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1401
Bram Moolenaar68452172021-04-12 21:21:02 +02001402 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1403 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001404 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001405 }
1406 else
1407 {
1408 val = (int)tv_get_number_chk(rettv, &error);
1409 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001410 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001411 }
1412 }
1413 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001414 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001415 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001416
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001417 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1418 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001419 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001420 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001421 *endp = cc;
1422 return;
1423 }
1424
Bram Moolenaarff697e62019-02-12 22:28:33 +01001425 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001426 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001427 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001428 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001429 {
1430 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001431 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1432 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001433 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001434 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001435 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001436 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001437 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001438 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001439 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001440 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001441 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1442 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001443 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001444 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001445 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001446 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001447 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001448 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001449 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001450 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001451 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001452 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001453 else if (lp->ll_range)
1454 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001455 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1456 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001457 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001458 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001459 return;
1460 }
1461
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001462 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1463 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001464 }
1465 else
1466 {
1467 /*
1468 * Assign to a List or Dictionary item.
1469 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001470 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1471 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001472 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001473 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001474 return;
1475 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001476
1477 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001478 && check_typval_arg_type(lp->ll_valtype, rettv,
1479 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001480 return;
1481
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001482 if (lp->ll_newkey != NULL)
1483 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001484 if (op != NULL && *op != '=')
1485 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001486 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001487 return;
1488 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001489 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1490 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001491 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001492
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001493 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001494 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001495 if (di == NULL)
1496 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001497 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1498 {
1499 vim_free(di);
1500 return;
1501 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001502 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001503 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001504 else if (op != NULL && *op != '=')
1505 {
1506 tv_op(lp->ll_tv, rettv, op);
1507 return;
1508 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001509 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001510 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001511
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001512 /*
1513 * Assign the value to the variable or list item.
1514 */
1515 if (copy)
1516 copy_tv(rettv, lp->ll_tv);
1517 else
1518 {
1519 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001520 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001521 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001522 }
1523 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001524}
1525
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001526/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001527 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1528 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001529 * Returns OK or FAIL.
1530 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001531 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001532tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001533{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001534 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001535 char_u numbuf[NUMBUFLEN];
1536 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001537 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001538
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001539 // Can't do anything with a Funcref or Dict on the right.
1540 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001541 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001542 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1543 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001544 {
1545 switch (tv1->v_type)
1546 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001547 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001548 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001549 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001550 case VAR_DICT:
1551 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001552 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001553 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001554 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001555 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001556 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001557 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001558 break;
1559
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001560 case VAR_BLOB:
1561 if (*op != '+' || tv2->v_type != VAR_BLOB)
1562 break;
1563 // BLOB += BLOB
1564 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1565 {
1566 blob_T *b1 = tv1->vval.v_blob;
1567 blob_T *b2 = tv2->vval.v_blob;
1568 int i, len = blob_len(b2);
1569 for (i = 0; i < len; i++)
1570 ga_append(&b1->bv_ga, blob_get(b2, i));
1571 }
1572 return OK;
1573
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001574 case VAR_LIST:
1575 if (*op != '+' || tv2->v_type != VAR_LIST)
1576 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001577 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001578 if (tv2->vval.v_list != NULL)
1579 {
1580 if (tv1->vval.v_list == NULL)
1581 {
1582 tv1->vval.v_list = tv2->vval.v_list;
1583 ++tv1->vval.v_list->lv_refcount;
1584 }
1585 else
1586 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1587 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001588 return OK;
1589
1590 case VAR_NUMBER:
1591 case VAR_STRING:
1592 if (tv2->v_type == VAR_LIST)
1593 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001594 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001595 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001596 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001597 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001598#ifdef FEAT_FLOAT
1599 if (tv2->v_type == VAR_FLOAT)
1600 {
1601 float_T f = n;
1602
Bram Moolenaarff697e62019-02-12 22:28:33 +01001603 if (*op == '%')
1604 break;
1605 switch (*op)
1606 {
1607 case '+': f += tv2->vval.v_float; break;
1608 case '-': f -= tv2->vval.v_float; break;
1609 case '*': f *= tv2->vval.v_float; break;
1610 case '/': f /= tv2->vval.v_float; break;
1611 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001612 clear_tv(tv1);
1613 tv1->v_type = VAR_FLOAT;
1614 tv1->vval.v_float = f;
1615 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001616 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001617#endif
1618 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001619 switch (*op)
1620 {
1621 case '+': n += tv_get_number(tv2); break;
1622 case '-': n -= tv_get_number(tv2); break;
1623 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001624 case '/': n = num_divide(n, tv_get_number(tv2),
1625 &failed); break;
1626 case '%': n = num_modulus(n, tv_get_number(tv2),
1627 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001628 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001629 clear_tv(tv1);
1630 tv1->v_type = VAR_NUMBER;
1631 tv1->vval.v_number = n;
1632 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001633 }
1634 else
1635 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001636 if (tv2->v_type == VAR_FLOAT)
1637 break;
1638
Bram Moolenaarff697e62019-02-12 22:28:33 +01001639 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001640 s = tv_get_string(tv1);
1641 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001642 clear_tv(tv1);
1643 tv1->v_type = VAR_STRING;
1644 tv1->vval.v_string = s;
1645 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001646 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001647
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001648 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001649#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001650 {
1651 float_T f;
1652
Bram Moolenaarff697e62019-02-12 22:28:33 +01001653 if (*op == '%' || *op == '.'
1654 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001655 && tv2->v_type != VAR_NUMBER
1656 && tv2->v_type != VAR_STRING))
1657 break;
1658 if (tv2->v_type == VAR_FLOAT)
1659 f = tv2->vval.v_float;
1660 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001661 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001662 switch (*op)
1663 {
1664 case '+': tv1->vval.v_float += f; break;
1665 case '-': tv1->vval.v_float -= f; break;
1666 case '*': tv1->vval.v_float *= f; break;
1667 case '/': tv1->vval.v_float /= f; break;
1668 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001669 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001670#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001671 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001672 }
1673 }
1674
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001675 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001676 return FAIL;
1677}
1678
1679/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001680 * Evaluate the expression used in a ":for var in expr" command.
1681 * "arg" points to "var".
1682 * Set "*errp" to TRUE for an error, FALSE otherwise;
1683 * Return a pointer that holds the info. Null when there is an error.
1684 */
1685 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001686eval_for_line(
1687 char_u *arg,
1688 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001689 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001690 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001691{
Bram Moolenaar33570922005-01-25 22:26:29 +00001692 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001693 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001694 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001695 typval_T tv;
1696 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001697 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001698
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001699 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001700
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001701 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001702 if (fi == NULL)
1703 return NULL;
1704
Bram Moolenaar404557e2021-07-05 21:41:48 +02001705 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1706 &fi->fi_semicolon, FALSE);
1707 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001708 return fi;
1709
Bram Moolenaar404557e2021-07-05 21:41:48 +02001710 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001711 if (expr[0] != 'i' || expr[1] != 'n'
1712 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001713 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001714 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1715 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1716 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001717 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001718 return fi;
1719 }
1720
1721 if (skip)
1722 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001723 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1724 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001725 {
1726 *errp = FALSE;
1727 if (!skip)
1728 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001729 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001730 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001731 l = tv.vval.v_list;
1732 if (l == NULL)
1733 {
1734 // a null list is like an empty list: do nothing
1735 clear_tv(&tv);
1736 }
1737 else
1738 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001739 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001740 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001742 // No need to increment the refcount, it's already set for
1743 // the list being used in "tv".
1744 fi->fi_list = l;
1745 list_add_watch(l, &fi->fi_lw);
1746 fi->fi_lw.lw_item = l->lv_first;
1747 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001748 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001749 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001750 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001751 fi->fi_bi = 0;
1752 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001753 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001754 typval_T btv;
1755
1756 // Make a copy, so that the iteration still works when the
1757 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001758 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001759 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001760 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001761 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001762 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001763 else if (tv.v_type == VAR_STRING)
1764 {
1765 fi->fi_byte_idx = 0;
1766 fi->fi_string = tv.vval.v_string;
1767 tv.vval.v_string = NULL;
1768 if (fi->fi_string == NULL)
1769 fi->fi_string = vim_strsave((char_u *)"");
1770 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001771 else
1772 {
Sean Dewar80d73952021-08-04 19:25:54 +02001773 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001774 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001775 }
1776 }
1777 }
1778 if (skip)
1779 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001780 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001781
1782 return fi;
1783}
1784
1785/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001786 * Used when looping over a :for line, skip the "in expr" part.
1787 */
1788 void
1789skip_for_lines(void *fi_void, evalarg_T *evalarg)
1790{
1791 forinfo_T *fi = (forinfo_T *)fi_void;
1792 int i;
1793
1794 for (i = 0; i < fi->fi_break_count; ++i)
1795 eval_next_line(evalarg);
1796}
1797
1798/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001799 * Use the first item in a ":for" list. Advance to the next.
1800 * Assign the values to the variable (list). "arg" points to the first one.
1801 * Return TRUE when a valid item was found, FALSE when at end of list or
1802 * something wrong.
1803 */
1804 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001805next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001806{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001807 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001808 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001809 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001810 ? (ASSIGN_FINAL
1811 // first round: error if variable exists
1812 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1813 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001814 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001815 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001816 int skip_assign = in_vim9script() && arg[0] == '_'
1817 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001818
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001819 if (fi->fi_blob != NULL)
1820 {
1821 typval_T tv;
1822
1823 if (fi->fi_bi >= blob_len(fi->fi_blob))
1824 return FALSE;
1825 tv.v_type = VAR_NUMBER;
1826 tv.v_lock = VAR_FIXED;
1827 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1828 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001829 if (skip_assign)
1830 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001831 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001832 fi->fi_varcount, flag, NULL) == OK;
1833 }
1834
1835 if (fi->fi_string != NULL)
1836 {
1837 typval_T tv;
1838 int len;
1839
1840 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1841 if (len == 0)
1842 return FALSE;
1843 tv.v_type = VAR_STRING;
1844 tv.v_lock = VAR_FIXED;
1845 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1846 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001847 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001848 if (skip_assign)
1849 result = TRUE;
1850 else
1851 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001852 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001853 vim_free(tv.vval.v_string);
1854 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001855 }
1856
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001857 item = fi->fi_lw.lw_item;
1858 if (item == NULL)
1859 result = FALSE;
1860 else
1861 {
1862 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001863 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001864 if (skip_assign)
1865 result = TRUE;
1866 else
1867 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001868 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001869 }
1870 return result;
1871}
1872
1873/*
1874 * Free the structure used to store info used by ":for".
1875 */
1876 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001877free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001878{
Bram Moolenaar33570922005-01-25 22:26:29 +00001879 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001880
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001881 if (fi == NULL)
1882 return;
1883 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001884 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001885 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001886 list_unref(fi->fi_list);
1887 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001888 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001889 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001890 else
1891 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001892 vim_free(fi);
1893}
1894
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001896set_context_for_expression(
1897 expand_T *xp,
1898 char_u *arg,
1899 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001901 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001903 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001905 if (cmdidx == CMD_let || cmdidx == CMD_var
1906 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001907 {
1908 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001909 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001910 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001911 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001912 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913 {
1914 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001915 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001916 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001917 break;
1918 }
1919 return;
1920 }
1921 }
1922 else
1923 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1924 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 while ((xp->xp_pattern = vim_strpbrk(arg,
1926 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1927 {
1928 c = *xp->xp_pattern;
1929 if (c == '&')
1930 {
1931 c = xp->xp_pattern[1];
1932 if (c == '&')
1933 {
1934 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001935 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 }
1937 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001938 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001940 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1941 xp->xp_pattern += 2;
1942
1943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 }
1945 else if (c == '$')
1946 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001947 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 xp->xp_context = EXPAND_ENV_VARS;
1949 }
1950 else if (c == '=')
1951 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001952 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 xp->xp_context = EXPAND_EXPRESSION;
1954 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001955 else if (c == '#'
1956 && xp->xp_context == EXPAND_EXPRESSION)
1957 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001958 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001959 break;
1960 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001961 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 && xp->xp_context == EXPAND_FUNCTIONS
1963 && vim_strchr(xp->xp_pattern, '(') == NULL)
1964 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001965 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 break;
1967 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001968 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001970 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 {
1972 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1973 if (c == '\\' && xp->xp_pattern[1] != NUL)
1974 ++xp->xp_pattern;
1975 xp->xp_context = EXPAND_NOTHING;
1976 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001977 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001979 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1981 /* skip */ ;
1982 xp->xp_context = EXPAND_NOTHING;
1983 }
1984 else if (c == '|')
1985 {
1986 if (xp->xp_pattern[1] == '|')
1987 {
1988 ++xp->xp_pattern;
1989 xp->xp_context = EXPAND_EXPRESSION;
1990 }
1991 else
1992 xp->xp_context = EXPAND_COMMANDS;
1993 }
1994 else
1995 xp->xp_context = EXPAND_EXPRESSION;
1996 }
1997 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001998 // Doesn't look like something valid, expand as an expression
1999 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002000 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 arg = xp->xp_pattern;
2002 if (*arg != NUL)
2003 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2004 /* skip */ ;
2005 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002006
2007 // ":exe one two" completes "two"
2008 if ((cmdidx == CMD_execute
2009 || cmdidx == CMD_echo
2010 || cmdidx == CMD_echon
2011 || cmdidx == CMD_echomsg)
2012 && xp->xp_context == EXPAND_EXPRESSION)
2013 {
2014 for (;;)
2015 {
2016 char_u *n = skiptowhite(arg);
2017
2018 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2019 break;
2020 arg = skipwhite(n);
2021 }
2022 }
2023
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 xp->xp_pattern = arg;
2025}
2026
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002028 * Return TRUE if "pat" matches "text".
2029 * Does not use 'cpo' and always uses 'magic'.
2030 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002031 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002032pattern_match(char_u *pat, char_u *text, int ic)
2033{
2034 int matches = FALSE;
2035 char_u *save_cpo;
2036 regmatch_T regmatch;
2037
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002038 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002039 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002040 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002041 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2042 if (regmatch.regprog != NULL)
2043 {
2044 regmatch.rm_ic = ic;
2045 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2046 vim_regfree(regmatch.regprog);
2047 }
2048 p_cpo = save_cpo;
2049 return matches;
2050}
2051
2052/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002053 * Handle a name followed by "(". Both for just "name(arg)" and for
2054 * "expr->name(arg)".
2055 * Returns OK or FAIL.
2056 */
2057 static int
2058eval_func(
2059 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002060 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002061 char_u *name,
2062 int name_len,
2063 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002064 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002065 typval_T *basetv) // "expr" for "expr->name(arg)"
2066{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002067 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002068 char_u *s = name;
2069 int len = name_len;
2070 partial_T *partial;
2071 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002072 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002073 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002074
2075 if (!evaluate)
2076 check_vars(s, len);
2077
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002078 // If "s" is the name of a variable of type VAR_FUNC
2079 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002080 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002081 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002082
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002083 // Need to make a copy, in case evaluating the arguments makes
2084 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002085 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002086 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002087 ret = FAIL;
2088 else
2089 {
2090 funcexe_T funcexe;
2091
2092 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002093 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002094 funcexe.fe_firstline = curwin->w_cursor.lnum;
2095 funcexe.fe_lastline = curwin->w_cursor.lnum;
2096 funcexe.fe_evaluate = evaluate;
2097 funcexe.fe_partial = partial;
2098 funcexe.fe_basetv = basetv;
2099 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002100 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002101 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002102 }
2103 vim_free(s);
2104
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002105 // If evaluate is FALSE rettv->v_type was not set in
2106 // get_func_tv, but it's needed in handle_subscript() to parse
2107 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002108 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2109 {
2110 rettv->vval.v_string = NULL;
2111 rettv->v_type = VAR_FUNC;
2112 }
2113
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002114 // Stop the expression evaluation when immediately
2115 // aborting on error, or when an interrupt occurred or
2116 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002117 if (evaluate && aborting())
2118 {
2119 if (ret == OK)
2120 clear_tv(rettv);
2121 ret = FAIL;
2122 }
2123 return ret;
2124}
2125
2126/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002127 * Get the next line source line without advancing. But do skip over comment
2128 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002129 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002130 */
2131 static char_u *
2132getline_peek_skip_comments(evalarg_T *evalarg)
2133{
2134 for (;;)
2135 {
2136 char_u *next = getline_peek(evalarg->eval_getline,
2137 evalarg->eval_cookie);
2138 char_u *p;
2139
2140 if (next == NULL)
2141 break;
2142 p = skipwhite(next);
2143 if (*p != NUL && !vim9_comment_start(p))
2144 return next;
2145 (void)eval_next_line(evalarg);
2146 }
2147 return NULL;
2148}
2149
2150/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002151 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2152 * comment) and there is a next line, return the next line (skipping blanks)
2153 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002154 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2155 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002156 * "arg" must point somewhere inside a line, not at the start.
2157 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002158 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002159eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2160{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002161 char_u *p = skipwhite(arg);
2162
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002163 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002164 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002165 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002166 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002167 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002168 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002169 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002170
2171 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002172 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002173 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002174 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002175
Bram Moolenaar9d489562020-07-30 20:08:50 +02002176 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002177 {
2178 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002179 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002180 }
2181 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002182 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002183}
2184
2185/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002186 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002187 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002188 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002189 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002190eval_next_line(evalarg_T *evalarg)
2191{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002192 garray_T *gap = &evalarg->eval_ga;
2193 char_u *line;
2194
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002195 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002196 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2197 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002198 else
2199 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002200 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002201 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2202 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002203 char_u *p = skipwhite(line);
2204
2205 // Going to concatenate the lines after parsing. For an empty or
2206 // comment line use an empty string.
2207 if (*p == NUL || vim9_comment_start(p))
2208 {
2209 vim_free(line);
2210 line = vim_strsave((char_u *)"");
2211 }
2212
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002213 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2214 ++gap->ga_len;
2215 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002216 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002217 {
2218 vim_free(evalarg->eval_tofree);
2219 evalarg->eval_tofree = line;
2220 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002221
2222 // Advanced to the next line, "arg" no longer points into the previous
2223 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002224 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002225 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002226}
2227
Bram Moolenaare6b53242020-07-01 17:28:33 +02002228/*
2229 * Call eval_next_non_blank() and get the next line if needed.
2230 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002231 char_u *
2232skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2233{
2234 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002235 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002236
Bram Moolenaar962d7212020-07-04 14:15:00 +02002237 if (evalarg == NULL)
2238 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002239 eval_next_non_blank(p, evalarg, &getnext);
2240 if (getnext)
2241 return eval_next_line(evalarg);
2242 return p;
2243}
2244
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002245/*
Bram Moolenaar844fb642021-10-23 13:32:30 +01002246 * Initialize "evalarg" for use.
2247 */
2248 void
2249init_evalarg(evalarg_T *evalarg)
2250{
2251 CLEAR_POINTER(evalarg);
2252 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
2253}
2254
2255/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002256 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002257 */
2258 void
2259clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2260{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002261 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002262 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002263 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002264 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002265 if (eap != NULL)
2266 {
2267 // We may need to keep the original command line, e.g. for
2268 // ":let" it has the variable names. But we may also need the
2269 // new one, "nextcmd" points into it. Keep both.
2270 vim_free(eap->cmdline_tofree);
2271 eap->cmdline_tofree = *eap->cmdlinep;
2272 *eap->cmdlinep = evalarg->eval_tofree;
2273 }
2274 else
2275 vim_free(evalarg->eval_tofree);
2276 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002277 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002278
Bram Moolenaar844fb642021-10-23 13:32:30 +01002279 ga_clear_strings(&evalarg->eval_tofree_ga);
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002280 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002281 }
2282}
2283
2284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002286 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2288 */
2289
2290/*
2291 * Handle zero level expression.
2292 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002294 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002295 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 * Return OK or FAIL.
2297 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002298 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002299eval0(
2300 char_u *arg,
2301 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002302 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002303 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002305 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2306}
2307
2308/*
2309 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2310 * expression and don't check what comes after the expression.
2311 */
2312 int
2313eval0_retarg(
2314 char_u *arg,
2315 typval_T *rettv,
2316 exarg_T *eap,
2317 evalarg_T *evalarg,
2318 char_u **retarg)
2319{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 int ret;
2321 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002322 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002323 int did_emsg_before = did_emsg;
2324 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002325 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002326 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002327 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328
2329 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002330 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002331 expr_end = p;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002332 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002333
Bram Moolenaar02929a32021-12-17 14:46:12 +00002334 // In Vim9 script a command block is not split at NL characters for
2335 // commands using an expression argument. Skip over a '#' comment to check
2336 // for a following NL. Require white space before the '#'.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002337 if (in_vim9script() && p > expr_end && retarg == NULL)
Bram Moolenaar02929a32021-12-17 14:46:12 +00002338 while (*p == '#')
2339 {
2340 char_u *nl = vim_strchr(p, NL);
2341
2342 if (nl == NULL)
2343 break;
2344 p = skipwhite(nl + 1);
2345 if (eap != NULL && *p != NUL)
2346 eap->nextcmd = p;
2347 check_for_end = FALSE;
2348 }
2349
2350 if (ret != FAIL && check_for_end)
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002351 end_error = !ends_excmd2(arg, p);
2352 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 {
2354 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002355 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 /*
2357 * Report the invalid expression unless the expression evaluation has
2358 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002359 * exception, or we already gave a more specific error.
2360 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002362 if (!aborting()
2363 && did_emsg == did_emsg_before
2364 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002365 && (flags & EVAL_CONSTANT) == 0
2366 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002367 {
2368 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002369 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002370 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002371 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002372 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002373
2374 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002375 // a next command to avoid more errors, unless "|" is following, which
2376 // could only be a command separator.
2377 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2378 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002379 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002381
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002382 if (retarg != NULL)
2383 *retarg = p;
2384 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002385 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002386
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 return ret;
2388}
2389
2390/*
2391 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002392 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002393 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 *
2395 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002396 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002398 * Note: "rettv.v_lock" is not set.
2399 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 * Return OK or FAIL.
2401 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002402 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002403eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002405 char_u *p;
2406 int getnext;
2407
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002408 CLEAR_POINTER(rettv);
2409
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 /*
2411 * Get the first variable.
2412 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002413 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 return FAIL;
2415
Bram Moolenaar793648f2020-06-26 21:28:25 +02002416 p = eval_next_non_blank(*arg, evalarg, &getnext);
2417 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002419 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002420 int result;
2421 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002422 evalarg_T *evalarg_used = evalarg;
2423 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002424 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002425 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002426 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002427
2428 if (evalarg == NULL)
2429 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002430 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002431 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002432 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002433 orig_flags = evalarg_used->eval_flags;
2434 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002435
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002436 if (getnext)
2437 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002438 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002439 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002440 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002441 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002442 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002443 clear_tv(rettv);
2444 return FAIL;
2445 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002446 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002447 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002448
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002450 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002452 int error = FALSE;
2453
Bram Moolenaar13106602020-10-04 16:06:05 +02002454 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002455 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002456 else if (vim9script)
2457 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002458 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002460 if (error || !op_falsy || !result)
2461 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002462 if (error)
2463 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 }
2465
2466 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002467 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002469 if (op_falsy)
2470 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002471 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002472 {
mityu4ac198c2021-05-28 17:52:40 +02002473 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002474 clear_tv(rettv);
2475 return FAIL;
2476 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002477 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002478 evalarg_used->eval_flags = (op_falsy ? !result : result)
2479 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2480 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002481 {
2482 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002484 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002485 if (!op_falsy || !result)
2486 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002488 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002490 /*
2491 * Check for the ":".
2492 */
2493 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2494 if (*p != ':')
2495 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002496 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002497 if (evaluate && result)
2498 clear_tv(rettv);
2499 evalarg_used->eval_flags = orig_flags;
2500 return FAIL;
2501 }
2502 if (getnext)
2503 *arg = eval_next_line(evalarg_used);
2504 else
2505 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002506 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002507 {
2508 error_white_both(p, 1);
2509 clear_tv(rettv);
2510 evalarg_used->eval_flags = orig_flags;
2511 return FAIL;
2512 }
2513 *arg = p;
2514 }
2515
2516 /*
2517 * Get the third variable. Recursive!
2518 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002519 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002520 {
mityu4ac198c2021-05-28 17:52:40 +02002521 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002522 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002523 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002524 return FAIL;
2525 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002526 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2527 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002528 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002529 if (eval1(arg, &var2, evalarg_used) == FAIL)
2530 {
2531 if (evaluate && result)
2532 clear_tv(rettv);
2533 evalarg_used->eval_flags = orig_flags;
2534 return FAIL;
2535 }
2536 if (evaluate && !result)
2537 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002539
2540 if (evalarg == NULL)
2541 clear_evalarg(&local_evalarg, NULL);
2542 else
2543 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 }
2545
2546 return OK;
2547}
2548
2549/*
2550 * Handle first level expression:
2551 * expr2 || expr2 || expr2 logical OR
2552 *
2553 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002554 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 *
2556 * Return OK or FAIL.
2557 */
2558 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002559eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002561 char_u *p;
2562 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563
2564 /*
2565 * Get the first variable.
2566 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002567 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 return FAIL;
2569
2570 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002571 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002573 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002574 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002576 evalarg_T *evalarg_used = evalarg;
2577 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002578 int evaluate;
2579 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002580 long result = FALSE;
2581 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002582 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002583 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002584
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002585 if (evalarg == NULL)
2586 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002587 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002588 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002589 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002590 orig_flags = evalarg_used->eval_flags;
2591 evaluate = orig_flags & EVAL_EVALUATE;
2592 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002593 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002594 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002595 result = tv_get_bool_chk(rettv, &error);
2596 else if (tv_get_number_chk(rettv, &error) != 0)
2597 result = TRUE;
2598 clear_tv(rettv);
2599 if (error)
2600 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 }
2602
2603 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002604 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002606 while (p[0] == '|' && p[1] == '|')
2607 {
2608 if (getnext)
2609 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002610 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002611 {
2612 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2613 {
2614 error_white_both(p, 2);
2615 clear_tv(rettv);
2616 return FAIL;
2617 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002618 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002619 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002620
2621 /*
2622 * Get the second variable.
2623 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002624 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2625 {
mityu4ac198c2021-05-28 17:52:40 +02002626 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002627 clear_tv(rettv);
2628 return FAIL;
2629 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002630 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2631 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002632 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002633 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002634 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002635
2636 /*
2637 * Compute the result.
2638 */
2639 if (evaluate && !result)
2640 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002641 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002642 result = tv_get_bool_chk(&var2, &error);
2643 else if (tv_get_number_chk(&var2, &error) != 0)
2644 result = TRUE;
2645 clear_tv(&var2);
2646 if (error)
2647 return FAIL;
2648 }
2649 if (evaluate)
2650 {
2651 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002652 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002653 rettv->v_type = VAR_BOOL;
2654 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002655 }
2656 else
2657 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002658 rettv->v_type = VAR_NUMBER;
2659 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002660 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002661 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002662
2663 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002665
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002666 if (evalarg == NULL)
2667 clear_evalarg(&local_evalarg, NULL);
2668 else
2669 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 }
2671
2672 return OK;
2673}
2674
2675/*
2676 * Handle second level expression:
2677 * expr3 && expr3 && expr3 logical AND
2678 *
2679 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002680 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 *
2682 * Return OK or FAIL.
2683 */
2684 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002685eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002687 char_u *p;
2688 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689
2690 /*
2691 * Get the first variable.
2692 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002693 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 return FAIL;
2695
2696 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002697 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002699 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002700 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002702 evalarg_T *evalarg_used = evalarg;
2703 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002704 int orig_flags;
2705 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002706 long result = TRUE;
2707 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002708 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002709 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002710
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002711 if (evalarg == NULL)
2712 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002713 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002714 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002715 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002716 orig_flags = evalarg_used->eval_flags;
2717 evaluate = orig_flags & EVAL_EVALUATE;
2718 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002719 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002720 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002721 result = tv_get_bool_chk(rettv, &error);
2722 else if (tv_get_number_chk(rettv, &error) == 0)
2723 result = FALSE;
2724 clear_tv(rettv);
2725 if (error)
2726 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 }
2728
2729 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002730 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002732 while (p[0] == '&' && p[1] == '&')
2733 {
2734 if (getnext)
2735 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002736 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002737 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002738 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002739 {
2740 error_white_both(p, 2);
2741 clear_tv(rettv);
2742 return FAIL;
2743 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002744 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002745 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002746
2747 /*
2748 * Get the second variable.
2749 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002750 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2751 {
mityu4ac198c2021-05-28 17:52:40 +02002752 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002753 clear_tv(rettv);
2754 return FAIL;
2755 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002756 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2757 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002758 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002759 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002760 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002761 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002762
2763 /*
2764 * Compute the result.
2765 */
2766 if (evaluate && result)
2767 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002768 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002769 result = tv_get_bool_chk(&var2, &error);
2770 else if (tv_get_number_chk(&var2, &error) == 0)
2771 result = FALSE;
2772 clear_tv(&var2);
2773 if (error)
2774 return FAIL;
2775 }
2776 if (evaluate)
2777 {
2778 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002779 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002780 rettv->v_type = VAR_BOOL;
2781 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002782 }
2783 else
2784 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002785 rettv->v_type = VAR_NUMBER;
2786 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002787 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002788 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002789
2790 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002792
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002793 if (evalarg == NULL)
2794 clear_evalarg(&local_evalarg, NULL);
2795 else
2796 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 }
2798
2799 return OK;
2800}
2801
2802/*
2803 * Handle third level expression:
2804 * var1 == var2
2805 * var1 =~ var2
2806 * var1 != var2
2807 * var1 !~ var2
2808 * var1 > var2
2809 * var1 >= var2
2810 * var1 < var2
2811 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002812 * var1 is var2
2813 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 *
2815 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002816 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 *
2818 * Return OK or FAIL.
2819 */
2820 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002821eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002824 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002825 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002827 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828
2829 /*
2830 * Get the first variable.
2831 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002832 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 return FAIL;
2834
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002835 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002836 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837
2838 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002839 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002841 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002843 typval_T var2;
2844 int ic;
2845 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002846 int evaluate = evalarg == NULL
2847 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002848
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002849 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002850 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002851 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002852 p = *arg;
2853 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002854 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2855 {
mityu4ac198c2021-05-28 17:52:40 +02002856 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002857 clear_tv(rettv);
2858 return FAIL;
2859 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002860
Bram Moolenaar696ba232020-07-29 21:20:41 +02002861 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2862 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002863 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002864 clear_tv(rettv);
2865 return FAIL;
2866 }
2867
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002868 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 if (p[len] == '?')
2870 {
2871 ic = TRUE;
2872 ++len;
2873 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002874 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 else if (p[len] == '#')
2876 {
2877 ic = FALSE;
2878 ++len;
2879 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002880 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002882 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883
2884 /*
2885 * Get the second variable.
2886 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002887 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2888 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002889 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002890 clear_tv(rettv);
2891 return FAIL;
2892 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002893 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002894 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002896 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 return FAIL;
2898 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002899 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002900 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002901 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002902
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002903 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002904 {
2905 ret = FAIL;
2906 clear_tv(rettv);
2907 }
2908 else
2909 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002910 clear_tv(&var2);
2911 return ret;
2912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 }
2914
2915 return OK;
2916}
2917
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002918/*
2919 * Make a copy of blob "tv1" and append blob "tv2".
2920 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921 void
2922eval_addblob(typval_T *tv1, typval_T *tv2)
2923{
2924 blob_T *b1 = tv1->vval.v_blob;
2925 blob_T *b2 = tv2->vval.v_blob;
2926 blob_T *b = blob_alloc();
2927 int i;
2928
2929 if (b != NULL)
2930 {
2931 for (i = 0; i < blob_len(b1); i++)
2932 ga_append(&b->bv_ga, blob_get(b1, i));
2933 for (i = 0; i < blob_len(b2); i++)
2934 ga_append(&b->bv_ga, blob_get(b2, i));
2935
2936 clear_tv(tv1);
2937 rettv_blob_set(tv1, b);
2938 }
2939}
2940
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002941/*
2942 * Make a copy of list "tv1" and append list "tv2".
2943 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002944 int
2945eval_addlist(typval_T *tv1, typval_T *tv2)
2946{
2947 typval_T var3;
2948
2949 // concatenate Lists
2950 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2951 {
2952 clear_tv(tv1);
2953 clear_tv(tv2);
2954 return FAIL;
2955 }
2956 clear_tv(tv1);
2957 *tv1 = var3;
2958 return OK;
2959}
2960
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961/*
2962 * Handle fourth level expression:
2963 * + number addition
2964 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002965 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002966 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 *
2968 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002969 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 *
2971 * Return OK or FAIL.
2972 */
2973 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002974eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 /*
2977 * Get the first variable.
2978 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002979 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 return FAIL;
2981
2982 /*
2983 * Repeat computing, until no '+', '-' or '.' is following.
2984 */
2985 for (;;)
2986 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002987 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002988 int getnext;
2989 char_u *p;
2990 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002991 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002992 int concat;
2993 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002994 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002995
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002996 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002997 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002998 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002999 p = eval_next_non_blank(*arg, evalarg, &getnext);
3000 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003001 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003002 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3003 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003005 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3006 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003007
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003008 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003009 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003010 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003011 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003012 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003013 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003014 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003015 {
mityu4ac198c2021-05-28 17:52:40 +02003016 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003017 clear_tv(rettv);
3018 return FAIL;
3019 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003020 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003021 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003022 if ((op != '+' || (rettv->v_type != VAR_LIST
3023 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003024#ifdef FEAT_FLOAT
3025 && (op == '.' || rettv->v_type != VAR_FLOAT)
3026#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003027 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003028 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003029 int error = FALSE;
3030
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003031 // For "list + ...", an illegal use of the first operand as
3032 // a number cannot be determined before evaluating the 2nd
3033 // operand: if this is also a list, all is ok.
3034 // For "something . ...", "something - ..." or "non-list + ...",
3035 // we know that the first operand needs to be a string or number
3036 // without evaluating the 2nd operand. So check before to avoid
3037 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003038 if (op != '.')
3039 tv_get_number_chk(rettv, &error);
3040 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003041 {
3042 clear_tv(rettv);
3043 return FAIL;
3044 }
3045 }
3046
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 /*
3048 * Get the second variable.
3049 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003050 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003051 {
mityu89dcb4d2021-05-28 13:50:17 +02003052 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003053 clear_tv(rettv);
3054 return FAIL;
3055 }
3056 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02003057 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003059 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 return FAIL;
3061 }
3062
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003063 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 {
3065 /*
3066 * Compute the result.
3067 */
3068 if (op == '.')
3069 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003070 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3071 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003072 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003073
Bram Moolenaar2e086612020-08-25 22:37:48 +02003074 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003075 || var2.v_type == VAR_CHANNEL
3076 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003077 semsg(_(e_using_invalid_value_as_string_str),
3078 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003079#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003080 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003081 {
3082 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3083 var2.vval.v_float);
3084 s2 = buf2;
3085 }
3086#endif
3087 else
3088 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003089 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003090 {
3091 clear_tv(rettv);
3092 clear_tv(&var2);
3093 return FAIL;
3094 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003095 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003096 clear_tv(rettv);
3097 rettv->v_type = VAR_STRING;
3098 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003100 else if (op == '+' && rettv->v_type == VAR_BLOB
3101 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003103 else if (op == '+' && rettv->v_type == VAR_LIST
3104 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003105 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003107 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 else
3110 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003111 int error = FALSE;
3112 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003113#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003114 float_T f1 = 0, f2 = 0;
3115
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003116 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003117 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003118 f1 = rettv->vval.v_float;
3119 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003120 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003121 else
3122#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003123 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003124 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003125 if (error)
3126 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003127 // This can only happen for "list + non-list" or
3128 // "blob + non-blob". For "non-list + ..." or
3129 // "something - ...", we returned before evaluating the
3130 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003131 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003132 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003133 return FAIL;
3134 }
3135#ifdef FEAT_FLOAT
3136 if (var2.v_type == VAR_FLOAT)
3137 f1 = n1;
3138#endif
3139 }
3140#ifdef FEAT_FLOAT
3141 if (var2.v_type == VAR_FLOAT)
3142 {
3143 f2 = var2.vval.v_float;
3144 n2 = 0;
3145 }
3146 else
3147#endif
3148 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003149 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003150 if (error)
3151 {
3152 clear_tv(rettv);
3153 clear_tv(&var2);
3154 return FAIL;
3155 }
3156#ifdef FEAT_FLOAT
3157 if (rettv->v_type == VAR_FLOAT)
3158 f2 = n2;
3159#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003160 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003161 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003162
3163#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003164 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003165 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3166 {
3167 if (op == '+')
3168 f1 = f1 + f2;
3169 else
3170 f1 = f1 - f2;
3171 rettv->v_type = VAR_FLOAT;
3172 rettv->vval.v_float = f1;
3173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003175#endif
3176 {
3177 if (op == '+')
3178 n1 = n1 + n2;
3179 else
3180 n1 = n1 - n2;
3181 rettv->v_type = VAR_NUMBER;
3182 rettv->vval.v_number = n1;
3183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003185 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 }
3187 }
3188 return OK;
3189}
3190
3191/*
3192 * Handle fifth level expression:
3193 * * number multiplication
3194 * / number division
3195 * % number modulo
3196 *
3197 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003198 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 *
3200 * Return OK or FAIL.
3201 */
3202 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003203eval6(
3204 char_u **arg,
3205 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003206 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003207 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003209#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003210 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212
3213 /*
3214 * Get the first variable.
3215 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003216 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 return FAIL;
3218
3219 /*
3220 * Repeat computing, until no '*', '/' or '%' is following.
3221 */
3222 for (;;)
3223 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003224 int evaluate;
3225 int getnext;
3226 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003227 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003228 int op;
3229 varnumber_T n1, n2;
3230#ifdef FEAT_FLOAT
3231 float_T f1, f2;
3232#endif
3233 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003234
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003235 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003236 p = eval_next_non_blank(*arg, evalarg, &getnext);
3237 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003238 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003240
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003241 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003242 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003243 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003244 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003245 {
3246 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3247 {
mityu4ac198c2021-05-28 17:52:40 +02003248 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003249 clear_tv(rettv);
3250 return FAIL;
3251 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003252 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003255#ifdef FEAT_FLOAT
3256 f1 = 0;
3257 f2 = 0;
3258#endif
3259 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003260 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003262#ifdef FEAT_FLOAT
3263 if (rettv->v_type == VAR_FLOAT)
3264 {
3265 f1 = rettv->vval.v_float;
3266 use_float = TRUE;
3267 n1 = 0;
3268 }
3269 else
3270#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003271 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003272 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003273 if (error)
3274 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 }
3276 else
3277 n1 = 0;
3278
3279 /*
3280 * Get the second variable.
3281 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003282 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3283 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003284 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003285 clear_tv(rettv);
3286 return FAIL;
3287 }
3288 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003289 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 return FAIL;
3291
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003292 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003294#ifdef FEAT_FLOAT
3295 if (var2.v_type == VAR_FLOAT)
3296 {
3297 if (!use_float)
3298 {
3299 f1 = n1;
3300 use_float = TRUE;
3301 }
3302 f2 = var2.vval.v_float;
3303 n2 = 0;
3304 }
3305 else
3306#endif
3307 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003308 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003309 clear_tv(&var2);
3310 if (error)
3311 return FAIL;
3312#ifdef FEAT_FLOAT
3313 if (use_float)
3314 f2 = n2;
3315#endif
3316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317
3318 /*
3319 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003320 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003322#ifdef FEAT_FLOAT
3323 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003325 if (op == '*')
3326 f1 = f1 * f2;
3327 else if (op == '/')
3328 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003329# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003330 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003331 if (f2 == 0.0)
3332 {
3333 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003334 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003335 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003336 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003337 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003338 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003339 }
3340 else
3341 f1 = f1 / f2;
3342# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003343 // We rely on the floating point library to handle divide
3344 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003345 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003346# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003349 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003350 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003351 return FAIL;
3352 }
3353 rettv->v_type = VAR_FLOAT;
3354 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 }
3356 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003359 int failed = FALSE;
3360
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003361 if (op == '*')
3362 n1 = n1 * n2;
3363 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003364 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003366 n1 = num_modulus(n1, n2, &failed);
3367 if (failed)
3368 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003369
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003370 rettv->v_type = VAR_NUMBER;
3371 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 }
3374 }
3375
3376 return OK;
3377}
3378
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003379/*
3380 * Handle a type cast before a base level expression.
3381 * "arg" must point to the first non-white of the expression.
3382 * "arg" is advanced to just after the recognized expression.
3383 * Return OK or FAIL.
3384 */
3385 static int
3386eval7t(
3387 char_u **arg,
3388 typval_T *rettv,
3389 evalarg_T *evalarg,
3390 int want_string) // after "." operator
3391{
3392 type_T *want_type = NULL;
3393 garray_T type_list; // list of pointers to allocated types
3394 int res;
3395 int evaluate = evalarg == NULL ? 0
3396 : (evalarg->eval_flags & EVAL_EVALUATE);
3397
3398 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003399 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3400 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003401 {
3402 ++*arg;
3403 ga_init2(&type_list, sizeof(type_T *), 10);
3404 want_type = parse_type(arg, &type_list, TRUE);
3405 if (want_type == NULL && (evaluate || **arg != '>'))
3406 {
3407 clear_type_list(&type_list);
3408 return FAIL;
3409 }
3410
3411 if (**arg != '>')
3412 {
3413 if (*skipwhite(*arg) == '>')
3414 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3415 else
3416 emsg(_(e_missing_gt));
3417 clear_type_list(&type_list);
3418 return FAIL;
3419 }
3420 ++*arg;
3421 *arg = skipwhite_and_linebreak(*arg, evalarg);
3422 }
3423
3424 res = eval7(arg, rettv, evalarg, want_string);
3425
3426 if (want_type != NULL && evaluate)
3427 {
3428 if (res == OK)
3429 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003430 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3431 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003432
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003433 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003434 {
3435 if (want_type == &t_bool && actual != &t_bool
3436 && (actual->tt_flags & TTFLAG_BOOL_OK))
3437 {
3438 int n = tv2bool(rettv);
3439
3440 // can use "0" and "1" for boolean in some places
3441 clear_tv(rettv);
3442 rettv->v_type = VAR_BOOL;
3443 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3444 }
3445 else
3446 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003447 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003448
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003449 where.wt_variable = TRUE;
3450 res = check_type(want_type, actual, TRUE, where);
3451 }
3452 }
3453 }
3454 clear_type_list(&type_list);
3455 }
3456
3457 return res;
3458}
3459
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003460 int
3461eval_leader(char_u **arg, int vim9)
3462{
3463 char_u *s = *arg;
3464 char_u *p = *arg;
3465
3466 while (*p == '!' || *p == '-' || *p == '+')
3467 {
3468 char_u *n = skipwhite(p + 1);
3469
3470 // ++, --, -+ and +- are not accepted in Vim9 script
3471 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3472 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003473 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003474 return FAIL;
3475 }
3476 p = n;
3477 }
3478 *arg = p;
3479 return OK;
3480}
3481
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482/*
3483 * Handle sixth level expression:
3484 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003485 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003486 * "string" string constant
3487 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 * &option-name option value
3489 * @r register contents
3490 * identifier variable value
3491 * function() function call
3492 * $VAR environment variable
3493 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003494 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003495 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003496 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003497 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 *
3499 * Also handle:
3500 * ! in front logical NOT
3501 * - in front unary minus
3502 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003503 * trailing [] subscript in String or List
3504 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003505 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 *
3507 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003508 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 *
3510 * Return OK or FAIL.
3511 */
3512 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003513eval7(
3514 char_u **arg,
3515 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003516 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003517 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003519 int evaluate = evalarg != NULL
3520 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 int len;
3522 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003523 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 char_u *start_leader, *end_leader;
3525 int ret = OK;
3526 char_u *alias;
3527
3528 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003529 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003530 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003532 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533
3534 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003535 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 */
3537 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003538 if (eval_leader(arg, in_vim9script()) == FAIL)
3539 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 end_leader = *arg;
3541
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003542 if (**arg == '.' && (!isdigit(*(*arg + 1))
3543#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003544 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003545#endif
3546 ))
3547 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003548 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003549 ++*arg;
3550 return FAIL;
3551 }
3552
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 switch (**arg)
3554 {
3555 /*
3556 * Number constant.
3557 */
3558 case '0':
3559 case '1':
3560 case '2':
3561 case '3':
3562 case '4':
3563 case '5':
3564 case '6':
3565 case '7':
3566 case '8':
3567 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003568 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003569
3570 // Apply prefixed "-" and "+" now. Matters especially when
3571 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003572 if (ret == OK && evaluate && end_leader > start_leader
3573 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003574 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 break;
3576
3577 /*
3578 * String constant: "string".
3579 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003580 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 break;
3582
3583 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003584 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003586 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003587 break;
3588
3589 /*
3590 * List: [expr, expr]
3591 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003592 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 break;
3594
3595 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003596 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003597 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003598 case '#': if (in_vim9script())
3599 {
3600 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3601 }
3602 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003603 {
3604 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003605 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003606 }
3607 else
3608 ret = NOTDONE;
3609 break;
3610
3611 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003612 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003613 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003614 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003615 case '{': if (in_vim9script())
3616 ret = NOTDONE;
3617 else
3618 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003619 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003620 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003621 break;
3622
3623 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003624 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003626 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 break;
3628
3629 /*
3630 * Environment variable: $VAR.
3631 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003632 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 break;
3634
3635 /*
3636 * Register contents: @r.
3637 */
3638 case '@': ++*arg;
3639 if (evaluate)
3640 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003641 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3642 semsg(_(e_syntax_error_at_str), *arg);
3643 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3644 emsg_invreg(**arg);
3645 else
3646 {
3647 rettv->v_type = VAR_STRING;
3648 rettv->vval.v_string = get_reg_contents(**arg,
3649 GREG_EXPR_SRC);
3650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 }
3652 if (**arg != NUL)
3653 ++*arg;
3654 break;
3655
3656 /*
3657 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003658 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003660 case '(': ret = NOTDONE;
3661 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003662 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003663 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003664 if (ret == OK && evaluate)
3665 {
3666 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3667
Bram Moolenaara9931532021-06-12 15:58:16 +02003668 // Compile it here to get the return type. The return
3669 // type is optional, when it's missing use t_unknown.
3670 // This is recognized in compile_return().
3671 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3672 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003673 if (compile_def_function(ufunc,
Bram Moolenaare99d4222021-06-13 14:01:26 +02003674 FALSE, COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003675 {
3676 clear_tv(rettv);
3677 ret = FAIL;
3678 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003679 }
3680 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003681 if (ret == NOTDONE)
3682 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003683 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003684 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003685
Bram Moolenaar9215f012020-06-27 21:18:00 +02003686 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003687 if (**arg == ')')
3688 ++*arg;
3689 else if (ret == OK)
3690 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003691 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003692 clear_tv(rettv);
3693 ret = FAIL;
3694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 }
3696 break;
3697
Bram Moolenaar8c711452005-01-14 21:53:12 +00003698 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 break;
3700 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003701
3702 if (ret == NOTDONE)
3703 {
3704 /*
3705 * Must be a variable or function name.
3706 * Can also be a curly-braces kind of name: {expr}.
3707 */
3708 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003709 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003710 if (alias != NULL)
3711 s = alias;
3712
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003713 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003714 ret = FAIL;
3715 else
3716 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003717 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3718
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003719 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003720 {
3721 emsg(_(e_cannot_use_underscore_here));
3722 ret = FAIL;
3723 }
3724 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003725 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003726 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003727 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003728 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003729 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003730 else if (flags & EVAL_CONSTANT)
3731 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003732 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003733 {
3734 // get the value of "true", "false" or a variable
3735 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3736 {
3737 rettv->v_type = VAR_BOOL;
3738 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003739 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003740 }
3741 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003742 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003743 {
3744 rettv->v_type = VAR_BOOL;
3745 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003746 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003747 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003748 else if (len == 4 && in_vim9script()
3749 && STRNCMP(s, "null", 4) == 0)
3750 {
3751 rettv->v_type = VAR_SPECIAL;
3752 rettv->vval.v_number = VVAL_NULL;
3753 ret = OK;
3754 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003755 else
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003756 {
3757 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003758 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003759 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003760 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003761 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003762 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003763 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003764 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003765 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003766 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003767 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003768 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003769 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003770 }
3771
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003772 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3773 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003774 if (ret == OK)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003775 ret = handle_subscript(arg, name_start, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776
3777 /*
3778 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3779 */
3780 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003781 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003782 return ret;
3783}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003784
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003785/*
3786 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003787 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003788 * Adjusts "end_leaderp" until it is at "start_leader".
3789 */
3790 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003791eval7_leader(
3792 typval_T *rettv,
3793 int numeric_only,
3794 char_u *start_leader,
3795 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003796{
3797 char_u *end_leader = *end_leaderp;
3798 int ret = OK;
3799 int error = FALSE;
3800 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003801 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003802#ifdef FEAT_FLOAT
3803 float_T f = 0.0;
3804
3805 if (rettv->v_type == VAR_FLOAT)
3806 f = rettv->vval.v_float;
3807 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003808#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003809 {
3810 while (VIM_ISWHITE(end_leader[-1]))
3811 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003812 if (in_vim9script() && end_leader[-1] == '!')
3813 val = tv2bool(rettv);
3814 else
3815 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003816 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003817 if (error)
3818 {
3819 clear_tv(rettv);
3820 ret = FAIL;
3821 }
3822 else
3823 {
3824 while (end_leader > start_leader)
3825 {
3826 --end_leader;
3827 if (*end_leader == '!')
3828 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003829 if (numeric_only)
3830 {
3831 ++end_leader;
3832 break;
3833 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003834#ifdef FEAT_FLOAT
3835 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003836 {
3837 if (in_vim9script())
3838 {
3839 rettv->v_type = VAR_BOOL;
3840 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3841 }
3842 else
3843 f = !f;
3844 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003845 else
3846#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003847 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003848 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003849 type = VAR_BOOL;
3850 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003851 }
3852 else if (*end_leader == '-')
3853 {
3854#ifdef FEAT_FLOAT
3855 if (rettv->v_type == VAR_FLOAT)
3856 f = -f;
3857 else
3858#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003859 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003860 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003861 type = VAR_NUMBER;
3862 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003863 }
3864 }
3865#ifdef FEAT_FLOAT
3866 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003868 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003869 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003871 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003872#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003873 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003874 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003875 if (in_vim9script())
3876 rettv->v_type = type;
3877 else
3878 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003879 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003882 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 return ret;
3884}
3885
3886/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003887 * Call the function referred to in "rettv".
3888 */
3889 static int
3890call_func_rettv(
3891 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003892 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003893 typval_T *rettv,
3894 int evaluate,
3895 dict_T *selfdict,
3896 typval_T *basetv)
3897{
3898 partial_T *pt = NULL;
3899 funcexe_T funcexe;
3900 typval_T functv;
3901 char_u *s;
3902 int ret;
3903
3904 // need to copy the funcref so that we can clear rettv
3905 if (evaluate)
3906 {
3907 functv = *rettv;
3908 rettv->v_type = VAR_UNKNOWN;
3909
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003910 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003911 if (functv.v_type == VAR_PARTIAL)
3912 {
3913 pt = functv.vval.v_partial;
3914 s = partial_name(pt);
3915 }
3916 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003917 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003918 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003919 if (s == NULL || *s == NUL)
3920 {
3921 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02003922 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003923 goto theend;
3924 }
3925 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003926 }
3927 else
3928 s = (char_u *)"";
3929
Bram Moolenaara80faa82020-04-12 19:37:17 +02003930 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003931 funcexe.fe_firstline = curwin->w_cursor.lnum;
3932 funcexe.fe_lastline = curwin->w_cursor.lnum;
3933 funcexe.fe_evaluate = evaluate;
3934 funcexe.fe_partial = pt;
3935 funcexe.fe_selfdict = selfdict;
3936 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003937 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003938
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003939theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003940 // Clear the funcref afterwards, so that deleting it while
3941 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003942 if (evaluate)
3943 clear_tv(&functv);
3944
3945 return ret;
3946}
3947
3948/*
3949 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003950 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003951 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3952 */
3953 static int
3954eval_lambda(
3955 char_u **arg,
3956 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003957 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003958 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003959{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003960 int evaluate = evalarg != NULL
3961 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003962 typval_T base = *rettv;
3963 int ret;
3964
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003965 rettv->v_type = VAR_UNKNOWN;
3966
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003967 if (**arg == '{')
3968 {
3969 // ->{lambda}()
3970 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3971 }
3972 else
3973 {
3974 // ->(lambda)()
3975 ++*arg;
3976 ret = eval1(arg, rettv, evalarg);
3977 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar605ec912021-12-18 16:54:31 +00003978 if (**arg == ')')
3979 {
3980 ++*arg;
3981 }
3982 else
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003983 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003984 emsg(_(e_missing_closing_paren));
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003985 ret = FAIL;
3986 }
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003987 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003988 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003989 return FAIL;
3990 else if (**arg != '(')
3991 {
3992 if (verbose)
3993 {
3994 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00003995 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003996 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003997 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003998 }
3999 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004000 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004001 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004002 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004003 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004004
4005 // Clear the funcref afterwards, so that deleting it while
4006 // evaluating the arguments is possible (see test55).
4007 if (evaluate)
4008 clear_tv(&base);
4009
4010 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004011}
4012
4013/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004014 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004015 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004016 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4017 */
4018 static int
4019eval_method(
4020 char_u **arg,
4021 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004022 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004023 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004024{
4025 char_u *name;
4026 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004027 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004028 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004029 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004030 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004031 int evaluate = evalarg != NULL
4032 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004033
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004034 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004035
Bram Moolenaarac92e252019-08-03 21:58:38 +02004036 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004037 len = get_name_len(arg, &alias, evaluate, TRUE);
4038 if (alias != NULL)
4039 name = alias;
4040
4041 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004042 {
4043 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004044 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004045 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004046 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004047 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004048 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004049 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004050
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004051 // If there is no "(" immediately following, but there is further on,
4052 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4053 // Does not handle anything where "(" is part of the expression.
4054 *arg = skipwhite(*arg);
4055
4056 if (**arg != '(' && alias == NULL
4057 && (paren = vim_strchr(*arg, '(')) != NULL)
4058 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004059 char_u *deref;
4060
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004061 *arg = name;
4062 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004063 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4064 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004065 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004066 *arg = name + len;
4067 ret = FAIL;
4068 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004069 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004070 {
4071 name = deref;
Bram Moolenaar15d16352022-01-17 20:09:08 +00004072 len = STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004073 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004074 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004075 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004076
4077 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004078 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004079 *arg = skipwhite(*arg);
4080
4081 if (**arg != '(')
4082 {
4083 if (verbose)
4084 semsg(_(e_missing_parenthesis_str), name);
4085 ret = FAIL;
4086 }
4087 else if (VIM_ISWHITE((*arg)[-1]))
4088 {
4089 if (verbose)
4090 emsg(_(e_no_white_space_allowed_before_parenthesis));
4091 ret = FAIL;
4092 }
4093 else
4094 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004095 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004096 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004097 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004098
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004099 // Clear the funcref afterwards, so that deleting it while
4100 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004101 if (evaluate)
4102 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004103 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004104
Bram Moolenaarac92e252019-08-03 21:58:38 +02004105 return ret;
4106}
4107
4108/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004109 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4110 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004111 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4112 */
4113 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004114eval_index(
4115 char_u **arg,
4116 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004117 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004118 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004119{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004120 int evaluate = evalarg != NULL
4121 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004122 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004123 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004124 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004125 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004126 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004127 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004128
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004129 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4130 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004131
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004132 init_tv(&var1);
4133 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004134 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004135 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004136 /*
4137 * dict.name
4138 */
4139 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004140 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004141 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004142 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004143 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004144 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004145 }
4146 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004147 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004148 /*
4149 * something[idx]
4150 *
4151 * Get the (first) variable from inside the [].
4152 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004153 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004154 if (**arg == ':')
4155 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004156 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004157 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004158 else if (vim9 && **arg == ':')
4159 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004160 semsg(_(e_white_space_required_before_and_after_str_at_str),
4161 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004162 clear_tv(&var1);
4163 return FAIL;
4164 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004165 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004166 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004167 int error = FALSE;
4168
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004169#ifdef FEAT_FLOAT
4170 // allow for indexing with float
4171 if (vim9 && rettv->v_type == VAR_DICT
4172 && var1.v_type == VAR_FLOAT)
4173 {
4174 var1.vval.v_string = typval_tostring(&var1, TRUE);
4175 var1.v_type = VAR_STRING;
4176 }
4177#endif
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004178 if (vim9 && rettv->v_type == VAR_LIST)
4179 tv_get_number_chk(&var1, &error);
4180 else
4181 error = tv_get_string_chk(&var1) == NULL;
4182 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004183 {
4184 // not a number or string
4185 clear_tv(&var1);
4186 return FAIL;
4187 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004188 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004189
4190 /*
4191 * Get the second variable from inside the [:].
4192 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004193 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004194 if (**arg == ':')
4195 {
4196 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004197 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004198 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004199 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004200 semsg(_(e_white_space_required_before_and_after_str_at_str),
4201 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004202 if (!empty1)
4203 clear_tv(&var1);
4204 return FAIL;
4205 }
4206 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004207 if (**arg == ']')
4208 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004209 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004210 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004211 if (!empty1)
4212 clear_tv(&var1);
4213 return FAIL;
4214 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004215 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004216 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004217 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004218 if (!empty1)
4219 clear_tv(&var1);
4220 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004221 return FAIL;
4222 }
4223 }
4224
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004225 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004226 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004227 if (**arg != ']')
4228 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004229 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004230 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004231 clear_tv(&var1);
4232 if (range)
4233 clear_tv(&var2);
4234 return FAIL;
4235 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004236 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004237 }
4238
4239 if (evaluate)
4240 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004241 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004242 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004243 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004244
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004245 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004246 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004247 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004248 clear_tv(&var2);
4249 return res;
4250 }
4251 return OK;
4252}
4253
4254/*
4255 * Check if "rettv" can have an [index] or [sli:ce]
4256 */
4257 int
4258check_can_index(typval_T *rettv, int evaluate, int verbose)
4259{
4260 switch (rettv->v_type)
4261 {
4262 case VAR_FUNC:
4263 case VAR_PARTIAL:
4264 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004265 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004266 return FAIL;
4267 case VAR_FLOAT:
4268#ifdef FEAT_FLOAT
4269 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004270 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004271 return FAIL;
4272#endif
4273 case VAR_BOOL:
4274 case VAR_SPECIAL:
4275 case VAR_JOB:
4276 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004277 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004278 if (verbose)
4279 emsg(_(e_cannot_index_special_variable));
4280 return FAIL;
4281 case VAR_UNKNOWN:
4282 case VAR_ANY:
4283 case VAR_VOID:
4284 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004285 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004286 emsg(_(e_cannot_index_special_variable));
4287 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004288 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004289 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004290
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004291 case VAR_STRING:
4292 case VAR_LIST:
4293 case VAR_DICT:
4294 case VAR_BLOB:
4295 break;
4296 case VAR_NUMBER:
4297 if (in_vim9script())
4298 emsg(_(e_cannot_index_number));
4299 break;
4300 }
4301 return OK;
4302}
4303
4304/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004305 * slice() function
4306 */
4307 void
4308f_slice(typval_T *argvars, typval_T *rettv)
4309{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004310 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004311 && ((argvars[0].v_type != VAR_STRING
4312 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004313 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004314 && check_for_list_arg(argvars, 0) == FAIL)
4315 || check_for_number_arg(argvars, 1) == FAIL
4316 || check_for_opt_number_arg(argvars, 2) == FAIL))
4317 return;
4318
Bram Moolenaar6601b622021-01-13 21:47:15 +01004319 if (check_can_index(argvars, TRUE, FALSE) == OK)
4320 {
4321 copy_tv(argvars, rettv);
4322 eval_index_inner(rettv, TRUE, argvars + 1,
4323 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4324 TRUE, NULL, 0, FALSE);
4325 }
4326}
4327
4328/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004329 * Apply index or range to "rettv".
4330 * "var1" is the first index, NULL for [:expr].
4331 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004332 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4333 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004334 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4335 */
4336 int
4337eval_index_inner(
4338 typval_T *rettv,
4339 int is_range,
4340 typval_T *var1,
4341 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004342 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004343 char_u *key,
4344 int keylen,
4345 int verbose)
4346{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004347 varnumber_T n1, n2 = 0;
4348 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004349
4350 n1 = 0;
4351 if (var1 != NULL && rettv->v_type != VAR_DICT)
4352 n1 = tv_get_number(var1);
4353
4354 if (is_range)
4355 {
4356 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004357 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004358 if (verbose)
4359 emsg(_(e_cannot_slice_dictionary));
4360 return FAIL;
4361 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004362 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004363 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004364 else
4365 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004366 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004367
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004368 switch (rettv->v_type)
4369 {
4370 case VAR_UNKNOWN:
4371 case VAR_ANY:
4372 case VAR_VOID:
4373 case VAR_FUNC:
4374 case VAR_PARTIAL:
4375 case VAR_FLOAT:
4376 case VAR_BOOL:
4377 case VAR_SPECIAL:
4378 case VAR_JOB:
4379 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004380 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004381 break; // not evaluating, skipping over subscript
4382
4383 case VAR_NUMBER:
4384 case VAR_STRING:
4385 {
4386 char_u *s = tv_get_string(rettv);
4387
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004388 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004389 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004390 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004391 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004392 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004393 else
4394 s = char_from_string(s, n1);
4395 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004396 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004397 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004398 // The resulting variable is a substring. If the indexes
4399 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004400 if (n1 < 0)
4401 {
4402 n1 = len + n1;
4403 if (n1 < 0)
4404 n1 = 0;
4405 }
4406 if (n2 < 0)
4407 n2 = len + n2;
4408 else if (n2 >= len)
4409 n2 = len;
4410 if (n1 >= len || n2 < 0 || n1 > n2)
4411 s = NULL;
4412 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004413 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004414 }
4415 else
4416 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004417 // The resulting variable is a string of a single
4418 // character. If the index is too big or negative the
4419 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004420 if (n1 >= len || n1 < 0)
4421 s = NULL;
4422 else
4423 s = vim_strnsave(s + n1, 1);
4424 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004425 clear_tv(rettv);
4426 rettv->v_type = VAR_STRING;
4427 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004428 }
4429 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004430
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004431 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004432 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4433 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004434 break;
4435
4436 case VAR_LIST:
4437 if (var1 == NULL)
4438 n1 = 0;
4439 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004440 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004441 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004442 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004443 return FAIL;
4444 break;
4445
4446 case VAR_DICT:
4447 {
4448 dictitem_T *item;
4449 typval_T tmp;
4450
4451 if (key == NULL)
4452 {
4453 key = tv_get_string_chk(var1);
4454 if (key == NULL)
4455 return FAIL;
4456 }
4457
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004458 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004459
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004460 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004461 {
4462 if (verbose)
4463 {
4464 if (keylen > 0)
4465 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004466 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004467 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004468 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004469 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004470
4471 copy_tv(&item->di_tv, &tmp);
4472 clear_tv(rettv);
4473 *rettv = tmp;
4474 }
4475 break;
4476 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004477 return OK;
4478}
4479
4480/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004481 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004482 */
4483 char_u *
4484partial_name(partial_T *pt)
4485{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004486 if (pt != NULL)
4487 {
4488 if (pt->pt_name != NULL)
4489 return pt->pt_name;
4490 if (pt->pt_func != NULL)
4491 return pt->pt_func->uf_name;
4492 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004493 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004494}
4495
Bram Moolenaarddecc252016-04-06 22:59:37 +02004496 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004497partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004498{
4499 int i;
4500
4501 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004502 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004503 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004504 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004505 if (pt->pt_name != NULL)
4506 {
4507 func_unref(pt->pt_name);
4508 vim_free(pt->pt_name);
4509 }
4510 else
4511 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004512
Bram Moolenaar54656012021-06-09 20:50:46 +02004513 // "out_up" is no longer used, decrement refcount on partial that owns it.
4514 partial_unref(pt->pt_outer.out_up_partial);
4515
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004516 // Decrease the reference count for the context of a closure. If down
4517 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004518 if (pt->pt_funcstack != NULL)
4519 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004520 --pt->pt_funcstack->fs_refcount;
4521 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004522 }
4523
Bram Moolenaarddecc252016-04-06 22:59:37 +02004524 vim_free(pt);
4525}
4526
4527/*
4528 * Unreference a closure: decrement the reference count and free it when it
4529 * becomes zero.
4530 */
4531 void
4532partial_unref(partial_T *pt)
4533{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004534 if (pt != NULL)
4535 {
4536 if (--pt->pt_refcount <= 0)
4537 partial_free(pt);
4538
4539 // If the reference count goes down to one, the funcstack may be the
4540 // only reference and can be freed if no other partials reference it.
4541 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4542 funcstack_check_refcount(pt->pt_funcstack);
4543 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004544}
4545
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004546/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004547 * Return the next (unique) copy ID.
4548 * Used for serializing nested structures.
4549 */
4550 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004551get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004552{
4553 current_copyID += COPYID_INC;
4554 return current_copyID;
4555}
4556
4557/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004558 * Garbage collection for lists and dictionaries.
4559 *
4560 * We use reference counts to be able to free most items right away when they
4561 * are no longer used. But for composite items it's possible that it becomes
4562 * unused while the reference count is > 0: When there is a recursive
4563 * reference. Example:
4564 * :let l = [1, 2, 3]
4565 * :let d = {9: l}
4566 * :let l[1] = d
4567 *
4568 * Since this is quite unusual we handle this with garbage collection: every
4569 * once in a while find out which lists and dicts are not referenced from any
4570 * variable.
4571 *
4572 * Here is a good reference text about garbage collection (refers to Python
4573 * but it applies to all reference-counting mechanisms):
4574 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004575 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004576
4577/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004578 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004579 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004580 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004581 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004582 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004583garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004584{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004585 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004586 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004587 buf_T *buf;
4588 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004589 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004590 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004591
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004592 if (!testing)
4593 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004594 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004595 want_garbage_collect = FALSE;
4596 may_garbage_collect = FALSE;
4597 garbage_collect_at_exit = FALSE;
4598 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004599
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004600 // The execution stack can grow big, limit the size.
4601 if (exestack.ga_maxlen - exestack.ga_len > 500)
4602 {
4603 size_t new_len;
4604 char_u *pp;
4605 int n;
4606
4607 // Keep 150% of the current size, with a minimum of the growth size.
4608 n = exestack.ga_len / 2;
4609 if (n < exestack.ga_growsize)
4610 n = exestack.ga_growsize;
4611
4612 // Don't make it bigger though.
4613 if (exestack.ga_len + n < exestack.ga_maxlen)
4614 {
4615 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4616 pp = vim_realloc(exestack.ga_data, new_len);
4617 if (pp == NULL)
4618 return FAIL;
4619 exestack.ga_maxlen = exestack.ga_len + n;
4620 exestack.ga_data = pp;
4621 }
4622 }
4623
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004624 // We advance by two because we add one for items referenced through
4625 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004626 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004627
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004628 /*
4629 * 1. Go through all accessible variables and mark all lists and dicts
4630 * with copyID.
4631 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004632
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004633 // Don't free variables in the previous_funccal list unless they are only
4634 // referenced through previous_funccal. This must be first, because if
4635 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004636 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004637
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004638 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004639 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004640
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004641 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004642 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004643 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4644 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004645
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004646 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004647 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004648 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4649 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004650 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004651 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4652 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004653#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004654 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004655 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4656 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004657 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004658 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004659 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4660 NULL, NULL);
4661#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004662
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004663 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004664 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004665 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4666 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004667 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004668 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004669
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004670 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004671 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004672
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004673 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004674 abort = abort || set_ref_in_functions(copyID);
4675
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004676 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004677 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004678
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004679 // funcstacks keep variables for closures
4680 abort = abort || set_ref_in_funcstacks(copyID);
4681
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004682 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004683 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004684
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004685 // callbacks in buffers
4686 abort = abort || set_ref_in_buffers(copyID);
4687
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004688 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
4689 abort = abort || set_ref_in_insexpand_funcs(copyID);
4690
4691 // 'operatorfunc' callback
4692 abort = abort || set_ref_in_opfunc(copyID);
4693
4694 // 'tagfunc' callback
4695 abort = abort || set_ref_in_tagfunc(copyID);
4696
4697 // 'imactivatefunc' and 'imstatusfunc' callbacks
4698 abort = abort || set_ref_in_im_funcs(copyID);
4699
Bram Moolenaar1dced572012-04-05 16:54:08 +02004700#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004701 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004702#endif
4703
Bram Moolenaardb913952012-06-29 12:54:53 +02004704#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004705 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004706#endif
4707
4708#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004709 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004710#endif
4711
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004712#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004713 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004714 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004715#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004716#ifdef FEAT_NETBEANS_INTG
4717 abort = abort || set_ref_in_nb_channel(copyID);
4718#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004719
Bram Moolenaare3188e22016-05-31 21:13:04 +02004720#ifdef FEAT_TIMERS
4721 abort = abort || set_ref_in_timer(copyID);
4722#endif
4723
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004724#ifdef FEAT_QUICKFIX
4725 abort = abort || set_ref_in_quickfix(copyID);
4726#endif
4727
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004728#ifdef FEAT_TERMINAL
4729 abort = abort || set_ref_in_term(copyID);
4730#endif
4731
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004732#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004733 abort = abort || set_ref_in_popups(copyID);
4734#endif
4735
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004736 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004737 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004738 /*
4739 * 2. Free lists and dictionaries that are not referenced.
4740 */
4741 did_free = free_unref_items(copyID);
4742
4743 /*
4744 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004745 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004746 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004747 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004748 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004749 else if (p_verbose > 0)
4750 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004751 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004752 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004753
4754 return did_free;
4755}
4756
4757/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004758 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004759 */
4760 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004761free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004762{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004763 int did_free = FALSE;
4764
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004765 // Let all "free" functions know that we are here. This means no
4766 // dictionaries, lists, channels or jobs are to be freed, because we will
4767 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004768 in_free_unref_items = TRUE;
4769
4770 /*
4771 * PASS 1: free the contents of the items. We don't free the items
4772 * themselves yet, so that it is possible to decrement refcount counters
4773 */
4774
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004775 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004776 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004777
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004778 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004779 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004780
4781#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004782 // Go through the list of jobs and free items without the copyID. This
4783 // must happen before doing channels, because jobs refer to channels, but
4784 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004785 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4786
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004787 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004788 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4789#endif
4790
4791 /*
4792 * PASS 2: free the items themselves.
4793 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004794 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004795 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004796
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004797#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004798 // Go through the list of jobs and free items without the copyID. This
4799 // must happen before doing channels, because jobs refer to channels, but
4800 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004801 free_unused_jobs(copyID, COPYID_MASK);
4802
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004803 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004804 free_unused_channels(copyID, COPYID_MASK);
4805#endif
4806
4807 in_free_unref_items = FALSE;
4808
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004809 return did_free;
4810}
4811
4812/*
4813 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004814 * "list_stack" is used to add lists to be marked. Can be NULL.
4815 *
4816 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004817 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004818 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004819set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004820{
4821 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004822 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004823 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004824 hashtab_T *cur_ht;
4825 ht_stack_T *ht_stack = NULL;
4826 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004827
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004828 cur_ht = ht;
4829 for (;;)
4830 {
4831 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004832 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004833 // Mark each item in the hashtab. If the item contains a hashtab
4834 // it is added to ht_stack, if it contains a list it is added to
4835 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004836 todo = (int)cur_ht->ht_used;
4837 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4838 if (!HASHITEM_EMPTY(hi))
4839 {
4840 --todo;
4841 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4842 &ht_stack, list_stack);
4843 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004844 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004845
4846 if (ht_stack == NULL)
4847 break;
4848
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004849 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004850 cur_ht = ht_stack->ht;
4851 tempitem = ht_stack;
4852 ht_stack = ht_stack->prev;
4853 free(tempitem);
4854 }
4855
4856 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004857}
4858
Dominique Pelle748b3082022-01-08 12:41:16 +00004859#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
4860 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004861/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004862 * Mark a dict and its items with "copyID".
4863 * Returns TRUE if setting references failed somehow.
4864 */
4865 int
4866set_ref_in_dict(dict_T *d, int copyID)
4867{
4868 if (d != NULL && d->dv_copyID != copyID)
4869 {
4870 d->dv_copyID = copyID;
4871 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4872 }
4873 return FALSE;
4874}
Dominique Pelle748b3082022-01-08 12:41:16 +00004875#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004876
4877/*
4878 * Mark a list and its items with "copyID".
4879 * Returns TRUE if setting references failed somehow.
4880 */
4881 int
4882set_ref_in_list(list_T *ll, int copyID)
4883{
4884 if (ll != NULL && ll->lv_copyID != copyID)
4885 {
4886 ll->lv_copyID = copyID;
4887 return set_ref_in_list_items(ll, copyID, NULL);
4888 }
4889 return FALSE;
4890}
4891
4892/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004893 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004894 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4895 *
4896 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004897 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004898 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004899set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004900{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004901 listitem_T *li;
4902 int abort = FALSE;
4903 list_T *cur_l;
4904 list_stack_T *list_stack = NULL;
4905 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004906
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004907 cur_l = l;
4908 for (;;)
4909 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004910 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004911 // Mark each item in the list. If the item contains a hashtab
4912 // it is added to ht_stack, if it contains a list it is added to
4913 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004914 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4915 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4916 ht_stack, &list_stack);
4917 if (list_stack == NULL)
4918 break;
4919
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004920 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004921 cur_l = list_stack->list;
4922 tempitem = list_stack;
4923 list_stack = list_stack->prev;
4924 free(tempitem);
4925 }
4926
4927 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004928}
4929
4930/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004931 * Mark the partial in callback 'cb' with "copyID".
4932 */
4933 int
4934set_ref_in_callback(callback_T *cb, int copyID)
4935{
4936 typval_T tv;
4937
4938 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
4939 return FALSE;
4940
4941 tv.v_type = VAR_PARTIAL;
4942 tv.vval.v_partial = cb->cb_partial;
4943 return set_ref_in_item(&tv, copyID, NULL, NULL);
4944}
4945
4946/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004947 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004948 * "list_stack" is used to add lists to be marked. Can be NULL.
4949 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4950 *
4951 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004952 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004953 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004954set_ref_in_item(
4955 typval_T *tv,
4956 int copyID,
4957 ht_stack_T **ht_stack,
4958 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004959{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004960 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004961
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004962 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004963 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004964 dict_T *dd = tv->vval.v_dict;
4965
Bram Moolenaara03f2332016-02-06 18:09:59 +01004966 if (dd != NULL && dd->dv_copyID != copyID)
4967 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004968 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004969 dd->dv_copyID = copyID;
4970 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004971 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004972 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4973 }
4974 else
4975 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004976 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4977
Bram Moolenaara03f2332016-02-06 18:09:59 +01004978 if (newitem == NULL)
4979 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004980 else
4981 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004982 newitem->ht = &dd->dv_hashtab;
4983 newitem->prev = *ht_stack;
4984 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004985 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004986 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004987 }
4988 }
4989 else if (tv->v_type == VAR_LIST)
4990 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004991 list_T *ll = tv->vval.v_list;
4992
Bram Moolenaara03f2332016-02-06 18:09:59 +01004993 if (ll != NULL && ll->lv_copyID != copyID)
4994 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004995 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004996 ll->lv_copyID = copyID;
4997 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004998 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004999 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005000 }
5001 else
5002 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005003 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5004
Bram Moolenaara03f2332016-02-06 18:09:59 +01005005 if (newitem == NULL)
5006 abort = TRUE;
5007 else
5008 {
5009 newitem->list = ll;
5010 newitem->prev = *list_stack;
5011 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005012 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005013 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005014 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005015 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005016 else if (tv->v_type == VAR_FUNC)
5017 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005018 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005019 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005020 else if (tv->v_type == VAR_PARTIAL)
5021 {
5022 partial_T *pt = tv->vval.v_partial;
5023 int i;
5024
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005025 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005026 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005027 // Didn't see this partial yet.
5028 pt->pt_copyID = copyID;
5029
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005030 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005031
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005032 if (pt->pt_dict != NULL)
5033 {
5034 typval_T dtv;
5035
5036 dtv.v_type = VAR_DICT;
5037 dtv.vval.v_dict = pt->pt_dict;
5038 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5039 }
5040
5041 for (i = 0; i < pt->pt_argc; ++i)
5042 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5043 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005044 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005045 }
5046 }
5047#ifdef FEAT_JOB_CHANNEL
5048 else if (tv->v_type == VAR_JOB)
5049 {
5050 job_T *job = tv->vval.v_job;
5051 typval_T dtv;
5052
5053 if (job != NULL && job->jv_copyID != copyID)
5054 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005055 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005056 if (job->jv_channel != NULL)
5057 {
5058 dtv.v_type = VAR_CHANNEL;
5059 dtv.vval.v_channel = job->jv_channel;
5060 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5061 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005062 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005063 {
5064 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005065 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005066 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5067 }
5068 }
5069 }
5070 else if (tv->v_type == VAR_CHANNEL)
5071 {
5072 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005073 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005074 typval_T dtv;
5075 jsonq_T *jq;
5076 cbq_T *cq;
5077
5078 if (ch != NULL && ch->ch_copyID != copyID)
5079 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005080 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005081 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005082 {
5083 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5084 jq = jq->jq_next)
5085 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5086 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5087 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005088 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005089 {
5090 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005091 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005092 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5093 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005094 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005095 {
5096 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005097 dtv.vval.v_partial =
5098 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005099 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5100 }
5101 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005102 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005103 {
5104 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005105 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005106 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5107 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005108 if (ch->ch_close_cb.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 = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005112 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5113 }
5114 }
5115 }
5116#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005117 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005118}
5119
Bram Moolenaar8c711452005-01-14 21:53:12 +00005120/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005121 * Return a string with the string representation of a variable.
5122 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005123 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005124 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005125 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005126 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005127 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005128 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5129 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005130 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005131 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005132 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005133echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005134 typval_T *tv,
5135 char_u **tofree,
5136 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005137 int copyID,
5138 int echo_style,
5139 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005140 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005141{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005142 static int recurse = 0;
5143 char_u *r = NULL;
5144
Bram Moolenaar33570922005-01-25 22:26:29 +00005145 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005146 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005147 if (!did_echo_string_emsg)
5148 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005149 // Only give this message once for a recursive call to avoid
5150 // flooding the user with errors. And stop iterating over lists
5151 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005152 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005153 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005154 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005155 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005156 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005157 }
5158 ++recurse;
5159
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005160 switch (tv->v_type)
5161 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005162 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005163 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005164 {
5165 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005166 r = tv->vval.v_string;
5167 if (r == NULL)
5168 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005169 }
5170 else
5171 {
5172 *tofree = string_quote(tv->vval.v_string, FALSE);
5173 r = *tofree;
5174 }
5175 break;
5176
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005177 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005178 if (echo_style)
5179 {
5180 *tofree = NULL;
5181 r = tv->vval.v_string;
5182 }
5183 else
5184 {
5185 *tofree = string_quote(tv->vval.v_string, TRUE);
5186 r = *tofree;
5187 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005188 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005189
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005190 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005191 {
5192 partial_T *pt = tv->vval.v_partial;
5193 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005194 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005195 garray_T ga;
5196 int i;
5197 char_u *tf;
5198
5199 ga_init2(&ga, 1, 100);
5200 ga_concat(&ga, (char_u *)"function(");
5201 if (fname != NULL)
5202 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005203 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005204 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005205 && vim_isupper(fname[1]))
5206 {
5207 ga_concat(&ga, (char_u *)"'g:");
5208 ga_concat(&ga, fname + 1);
5209 }
5210 else
5211 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005212 vim_free(fname);
5213 }
5214 if (pt != NULL && pt->pt_argc > 0)
5215 {
5216 ga_concat(&ga, (char_u *)", [");
5217 for (i = 0; i < pt->pt_argc; ++i)
5218 {
5219 if (i > 0)
5220 ga_concat(&ga, (char_u *)", ");
5221 ga_concat(&ga,
5222 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5223 vim_free(tf);
5224 }
5225 ga_concat(&ga, (char_u *)"]");
5226 }
5227 if (pt != NULL && pt->pt_dict != NULL)
5228 {
5229 typval_T dtv;
5230
5231 ga_concat(&ga, (char_u *)", ");
5232 dtv.v_type = VAR_DICT;
5233 dtv.vval.v_dict = pt->pt_dict;
5234 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5235 vim_free(tf);
5236 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005237 // terminate with ')' and a NUL
5238 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005239
5240 *tofree = ga.ga_data;
5241 r = *tofree;
5242 break;
5243 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005244
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005245 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005246 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005247 break;
5248
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005249 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005250 if (tv->vval.v_list == NULL)
5251 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005252 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005253 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005254 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005255 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005256 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5257 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005258 {
5259 *tofree = NULL;
5260 r = (char_u *)"[...]";
5261 }
5262 else
5263 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005264 int old_copyID = tv->vval.v_list->lv_copyID;
5265
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005266 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005267 *tofree = list2string(tv, copyID, restore_copyID);
5268 if (restore_copyID)
5269 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005270 r = *tofree;
5271 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005272 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005273
Bram Moolenaar8c711452005-01-14 21:53:12 +00005274 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005275 if (tv->vval.v_dict == NULL)
5276 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005277 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005278 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005279 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005280 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005281 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5282 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005283 {
5284 *tofree = NULL;
5285 r = (char_u *)"{...}";
5286 }
5287 else
5288 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005289 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005290
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005291 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005292 *tofree = dict2string(tv, copyID, restore_copyID);
5293 if (restore_copyID)
5294 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005295 r = *tofree;
5296 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005297 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005298
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005299 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005300 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005301 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005302 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005303 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005304 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005305 break;
5306
Bram Moolenaar835dc632016-02-07 14:27:38 +01005307 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005308 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005309#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005310 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005311 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5312 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005313 if (composite_val)
5314 {
5315 *tofree = string_quote(r, FALSE);
5316 r = *tofree;
5317 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005318#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005319 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005320
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005321 case VAR_INSTR:
5322 *tofree = NULL;
5323 r = (char_u *)"instructions";
5324 break;
5325
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005326 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005327#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005328 *tofree = NULL;
5329 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5330 r = numbuf;
5331 break;
5332#endif
5333
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005334 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005335 case VAR_SPECIAL:
5336 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005337 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005338 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005339 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005340
Bram Moolenaar8502c702014-06-17 12:51:16 +02005341 if (--recurse == 0)
5342 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005343 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005344}
5345
5346/*
5347 * Return a string with the string representation of a variable.
5348 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5349 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005350 * Does not put quotes around strings, as ":echo" displays values.
5351 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5352 * May return NULL.
5353 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005354 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005355echo_string(
5356 typval_T *tv,
5357 char_u **tofree,
5358 char_u *numbuf,
5359 int copyID)
5360{
5361 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5362}
5363
5364/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005365 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5366 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005367 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005368 */
5369 int
5370buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5371{
5372 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005373 char_u *t;
5374 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005375
5376 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5377 return -1;
5378
5379 if (lnum > buf->b_ml.ml_line_count)
5380 lnum = buf->b_ml.ml_line_count;
5381
5382 str = ml_get_buf(buf, lnum, FALSE);
5383 if (str == NULL)
5384 return -1;
5385
5386 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005387 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005388
Bram Moolenaar91458462021-01-13 20:08:38 +01005389 // count the number of characters
5390 t = str;
5391 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5392 t += mb_ptr2len(t);
5393
5394 // In insert mode, when the cursor is at the end of a non-empty line,
5395 // byteidx points to the NUL character immediately past the end of the
5396 // string. In this case, add one to the character count.
5397 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5398 count++;
5399
5400 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005401}
5402
5403/*
5404 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005405 * byte index. Works only for loaded buffers. Returns -1 on failure.
5406 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005407 */
5408 int
5409buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5410{
5411 char_u *str;
5412 char_u *t;
5413
5414 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5415 return -1;
5416
5417 if (lnum > buf->b_ml.ml_line_count)
5418 lnum = buf->b_ml.ml_line_count;
5419
5420 str = ml_get_buf(buf, lnum, FALSE);
5421 if (str == NULL)
5422 return -1;
5423
5424 // Convert the character offset to a byte offset
5425 t = str;
5426 while (*t != NUL && --charidx > 0)
5427 t += mb_ptr2len(t);
5428
Bram Moolenaar91458462021-01-13 20:08:38 +01005429 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005430}
5431
5432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005434 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005436 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005437var2fpos(
5438 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005439 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005440 int *fnum, // set to fnum for '0, 'A, etc.
5441 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005443 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005445 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005447 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005448 if (varp->v_type == VAR_LIST)
5449 {
5450 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005451 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005452 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005453 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005454
5455 l = varp->vval.v_list;
5456 if (l == NULL)
5457 return NULL;
5458
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005459 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005460 pos.lnum = list_find_nr(l, 0L, &error);
5461 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005462 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005463 if (charcol)
5464 len = (long)mb_charlen(ml_get(pos.lnum));
5465 else
5466 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005467
Bram Moolenaarec65d772020-08-20 22:29:12 +02005468 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005469 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005470 li = list_find(l, 1L);
5471 if (li != NULL && li->li_tv.v_type == VAR_STRING
5472 && li->li_tv.vval.v_string != NULL
5473 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005474 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005475 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005476 }
5477 else
5478 {
5479 pos.col = list_find_nr(l, 1L, &error);
5480 if (error)
5481 return NULL;
5482 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005483
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005484 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005485 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005486 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005487 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005488
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005489 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005490 pos.coladd = list_find_nr(l, 2L, &error);
5491 if (error)
5492 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005493
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005494 return &pos;
5495 }
5496
Bram Moolenaarc5809432021-03-27 21:23:30 +01005497 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5498 return NULL;
5499
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005500 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005501 if (name == NULL)
5502 return NULL;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005503 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005504 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005505 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005506 pos = curwin->w_cursor;
5507 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005508 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005509 return &pos;
5510 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005511 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005512 {
5513 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005514 pos = VIsual;
5515 else
5516 pos = curwin->w_cursor;
5517 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005518 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005519 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005520 }
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005521 if (name[0] == '\'' && (!in_vim9script()
5522 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005524 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005525 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5527 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005528 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005529 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 return pp;
5531 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005532
Bram Moolenaara5525202006-03-02 22:52:09 +00005533 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005534
Bram Moolenaar477933c2007-07-17 14:32:23 +00005535 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005536 {
5537 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005538 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005539 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005540 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005541 // In silent Ex mode topline is zero, but that's not a valid line
5542 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005543 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005544 return &pos;
5545 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005546 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005547 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005548 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005549 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005550 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005551 return &pos;
5552 }
5553 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005554 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005556 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 {
5558 pos.lnum = curbuf->b_ml.ml_line_count;
5559 pos.col = 0;
5560 }
5561 else
5562 {
5563 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005564 if (charcol)
5565 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5566 else
5567 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 }
5569 return &pos;
5570 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005571 if (in_vim9script())
5572 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 return NULL;
5574}
5575
5576/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005577 * Convert list in "arg" into a position and optional file number.
5578 * When "fnump" is NULL there is no file number, only 3 items.
5579 * Note that the column is passed on as-is, the caller may want to decrement
5580 * it to use 1 for the first column.
5581 * Return FAIL when conversion is not possible, doesn't check the position for
5582 * validity.
5583 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005584 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005585list2fpos(
5586 typval_T *arg,
5587 pos_T *posp,
5588 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005589 colnr_T *curswantp,
5590 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005591{
5592 list_T *l = arg->vval.v_list;
5593 long i = 0;
5594 long n;
5595
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005596 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5597 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005598 if (arg->v_type != VAR_LIST
5599 || l == NULL
5600 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005601 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005602 return FAIL;
5603
5604 if (fnump != NULL)
5605 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005606 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005607 if (n < 0)
5608 return FAIL;
5609 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005610 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005611 *fnump = n;
5612 }
5613
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005614 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005615 if (n < 0)
5616 return FAIL;
5617 posp->lnum = n;
5618
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005619 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005620 if (n < 0)
5621 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005622 // If character position is specified, then convert to byte position
5623 if (charcol)
5624 {
5625 buf_T *buf;
5626
5627 // Get the text for the specified line in a loaded buffer
5628 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5629 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5630 return FAIL;
5631
Bram Moolenaar91458462021-01-13 20:08:38 +01005632 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005633 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005634 posp->col = n;
5635
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005636 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005637 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005638 posp->coladd = 0;
5639 else
5640 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005641
Bram Moolenaar493c1782014-05-28 14:34:46 +02005642 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005643 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005644
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005645 return OK;
5646}
5647
5648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 * Get the length of an environment variable name.
5650 * Advance "arg" to the first character after the name.
5651 * Return 0 for error.
5652 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005653 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005654get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655{
5656 char_u *p;
5657 int len;
5658
5659 for (p = *arg; vim_isIDc(*p); ++p)
5660 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005661 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 return 0;
5663
5664 len = (int)(p - *arg);
5665 *arg = p;
5666 return len;
5667}
5668
5669/*
5670 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005671 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 * Return 0 if something is wrong.
5673 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005674 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005675get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676{
5677 char_u *p;
5678 int len;
5679
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005680 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005682 {
5683 if (*p == ':')
5684 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005685 // "s:" is start of "s:var", but "n:" is not and can be used in
5686 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005687 len = (int)(p - *arg);
5688 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5689 || len > 1)
5690 break;
5691 }
5692 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005693 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 return 0;
5695
5696 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005697 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698
5699 return len;
5700}
5701
5702/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005703 * Get the length of the name of a variable or function.
5704 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005706 * Return -1 if curly braces expansion failed.
5707 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 * If the name contains 'magic' {}'s, expand them and return the
5709 * expanded name in an allocated string via 'alias' - caller must free.
5710 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005711 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005712get_name_len(
5713 char_u **arg,
5714 char_u **alias,
5715 int evaluate,
5716 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717{
5718 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 char_u *p;
5720 char_u *expr_start;
5721 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005723 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724
5725 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5726 && (*arg)[2] == (int)KE_SNR)
5727 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005728 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 *arg += 3;
5730 return get_id_len(arg) + 3;
5731 }
5732 len = eval_fname_script(*arg);
5733 if (len > 0)
5734 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005735 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 *arg += len;
5737 }
5738
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005740 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005742 p = find_name_end(*arg, &expr_start, &expr_end,
5743 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 if (expr_start != NULL)
5745 {
5746 char_u *temp_string;
5747
5748 if (!evaluate)
5749 {
5750 len += (int)(p - *arg);
5751 *arg = skipwhite(p);
5752 return len;
5753 }
5754
5755 /*
5756 * Include any <SID> etc in the expanded string:
5757 * Thus the -len here.
5758 */
5759 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5760 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005761 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 *alias = temp_string;
5763 *arg = skipwhite(p);
5764 return (int)STRLEN(temp_string);
5765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766
5767 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005768 // Only give an error when there is something, otherwise it will be
5769 // reported at a higher level.
5770 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005771 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772
5773 return len;
5774}
5775
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005776/*
5777 * Find the end of a variable or function name, taking care of magic braces.
5778 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5779 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005780 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005781 * Return a pointer to just after the name. Equal to "arg" if there is no
5782 * valid name.
5783 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005784 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005785find_name_end(
5786 char_u *arg,
5787 char_u **expr_start,
5788 char_u **expr_end,
5789 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005791 int mb_nest = 0;
5792 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005794 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005795 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005797 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005799 *expr_start = NULL;
5800 *expr_end = NULL;
5801 }
5802
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005803 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005804 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5805 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005806 return arg;
5807
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005808 for (p = arg; *p != NUL
5809 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005810 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005811 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005812 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005813 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005814 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005815 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005816 if (*p == '\'')
5817 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005818 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005819 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005820 ;
5821 if (*p == NUL)
5822 break;
5823 }
5824 else if (*p == '"')
5825 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005826 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005827 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005828 if (*p == '\\' && p[1] != NUL)
5829 ++p;
5830 if (*p == NUL)
5831 break;
5832 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005833 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5834 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005835 // "s:" is start of "s:var", but "n:" is not and can be used in
5836 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005837 len = (int)(p - arg);
5838 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005839 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005840 break;
5841 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005842
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005843 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005845 if (*p == '[')
5846 ++br_nest;
5847 else if (*p == ']')
5848 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005850
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005851 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005853 if (*p == '{')
5854 {
5855 mb_nest++;
5856 if (expr_start != NULL && *expr_start == NULL)
5857 *expr_start = p;
5858 }
5859 else if (*p == '}')
5860 {
5861 mb_nest--;
5862 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5863 *expr_end = p;
5864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 }
5867
5868 return p;
5869}
5870
5871/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005872 * Expands out the 'magic' {}'s in a variable/function name.
5873 * Note that this can call itself recursively, to deal with
5874 * constructs like foo{bar}{baz}{bam}
5875 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5876 * "in_start" ^
5877 * "expr_start" ^
5878 * "expr_end" ^
5879 * "in_end" ^
5880 *
5881 * Returns a new allocated string, which the caller must free.
5882 * Returns NULL for failure.
5883 */
5884 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005885make_expanded_name(
5886 char_u *in_start,
5887 char_u *expr_start,
5888 char_u *expr_end,
5889 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005890{
5891 char_u c1;
5892 char_u *retval = NULL;
5893 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005894
5895 if (expr_end == NULL || in_end == NULL)
5896 return NULL;
5897 *expr_start = NUL;
5898 *expr_end = NUL;
5899 c1 = *in_end;
5900 *in_end = NUL;
5901
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005902 temp_result = eval_to_string(expr_start + 1, FALSE);
5903 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005904 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005905 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5906 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005907 if (retval != NULL)
5908 {
5909 STRCPY(retval, in_start);
5910 STRCAT(retval, temp_result);
5911 STRCAT(retval, expr_end + 1);
5912 }
5913 }
5914 vim_free(temp_result);
5915
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005916 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005917 *expr_start = '{';
5918 *expr_end = '}';
5919
5920 if (retval != NULL)
5921 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005922 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005923 if (expr_start != NULL)
5924 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005925 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005926 temp_result = make_expanded_name(retval, expr_start,
5927 expr_end, temp_result);
5928 vim_free(retval);
5929 retval = temp_result;
5930 }
5931 }
5932
5933 return retval;
5934}
5935
5936/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005938 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005940 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005941eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005943 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005944}
5945
5946/*
5947 * Return TRUE if character "c" can be used as the first character in a
5948 * variable or function name (excluding '{' and '}').
5949 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005950 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005951eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005952{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005953 return ASCII_ISALPHA(c) || c == '_';
5954}
5955
5956/*
5957 * Return TRUE if character "c" can be used as the first character of a
5958 * dictionary key.
5959 */
5960 int
5961eval_isdictc(int c)
5962{
5963 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964}
5965
5966/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005967 * Handle:
5968 * - expr[expr], expr[expr:expr] subscript
5969 * - ".name" lookup
5970 * - function call with Funcref variable: func(expr)
5971 * - method call: var->method()
5972 *
5973 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00005974 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005975 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005976 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005977handle_subscript(
5978 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00005979 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005980 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005981 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005982 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005983{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005984 int evaluate = evalarg != NULL
5985 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005986 int ret = OK;
5987 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005988 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005989 int getnext;
5990 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005991
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005992 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005993 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005994 // When at the end of the line and ".name" or "->{" or "->X" follows in
5995 // the next line then consume the line break.
5996 p = eval_next_non_blank(*arg, evalarg, &getnext);
5997 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005998 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02005999 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6000 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6001 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006002 {
6003 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006004 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006005 check_white = FALSE;
6006 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006007
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006008 if (rettv->v_type == VAR_ANY)
6009 {
6010 char_u *exp_name;
6011 int cc;
6012 int idx;
6013 ufunc_T *ufunc;
6014 type_T *type;
6015
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006016 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006017 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006018 if (**arg != '.')
6019 {
6020 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006021 semsg(_(e_expected_dot_after_name_str),
6022 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006023 ret = FAIL;
6024 break;
6025 }
6026 ++*arg;
6027 if (IS_WHITE_OR_NUL(**arg))
6028 {
6029 if (verbose)
6030 emsg(_(e_no_white_space_allowed_after_dot));
6031 ret = FAIL;
6032 break;
6033 }
6034
6035 // isolate the name
6036 exp_name = *arg;
6037 while (eval_isnamec(**arg))
6038 ++*arg;
6039 cc = **arg;
6040 **arg = NUL;
6041
6042 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
6043 evalarg->eval_cctx, verbose);
6044 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006045
6046 if (idx < 0 && ufunc == NULL)
6047 {
6048 ret = FAIL;
6049 break;
6050 }
6051 if (idx >= 0)
6052 {
6053 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6054 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6055
6056 copy_tv(sv->sv_tv, rettv);
6057 }
6058 else
6059 {
6060 rettv->v_type = VAR_FUNC;
6061 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6062 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006063 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006064 }
6065
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006066 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6067 || rettv->v_type == VAR_PARTIAL))
6068 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006069 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006070 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6071 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006072
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006073 // Stop the expression evaluation when immediately aborting on
6074 // error, or when an interrupt occurred or an exception was thrown
6075 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006076 if (aborting())
6077 {
6078 if (ret == OK)
6079 clear_tv(rettv);
6080 ret = FAIL;
6081 }
6082 dict_unref(selfdict);
6083 selfdict = NULL;
6084 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006085 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006086 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006087 if (in_vim9script())
6088 *arg = skipwhite(p + 2);
6089 else
6090 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006091 if (ret == OK)
6092 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006093 if (VIM_ISWHITE(**arg))
6094 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006095 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006096 ret = FAIL;
6097 }
6098 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006099 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006100 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006101 else
6102 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006103 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006104 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006105 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006106 // "." is ".name" lookup when we found a dict or when evaluating and
6107 // scriptversion is at least 2, where string concatenation is "..".
6108 else if (**arg == '['
6109 || (**arg == '.' && (rettv->v_type == VAR_DICT
6110 || (!evaluate
6111 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006112 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006113 {
6114 dict_unref(selfdict);
6115 if (rettv->v_type == VAR_DICT)
6116 {
6117 selfdict = rettv->vval.v_dict;
6118 if (selfdict != NULL)
6119 ++selfdict->dv_refcount;
6120 }
6121 else
6122 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006123 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006124 {
6125 clear_tv(rettv);
6126 ret = FAIL;
6127 }
6128 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006129 else
6130 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006131 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006132
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006133 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6134 // Don't do this when "Func" is already a partial that was bound
6135 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006136 if (selfdict != NULL
6137 && (rettv->v_type == VAR_FUNC
6138 || (rettv->v_type == VAR_PARTIAL
6139 && (rettv->vval.v_partial->pt_auto
6140 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006141 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006142
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006143 dict_unref(selfdict);
6144 return ret;
6145}
6146
6147/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006148 * Make a copy of an item.
6149 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006150 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6151 * reference to an already copied list/dict can be used.
6152 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006153 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006154 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006155item_copy(
6156 typval_T *from,
6157 typval_T *to,
6158 int deep,
6159 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006160{
6161 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006162 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006163
Bram Moolenaar33570922005-01-25 22:26:29 +00006164 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006165 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006166 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006167 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006168 }
6169 ++recurse;
6170
6171 switch (from->v_type)
6172 {
6173 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006174 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006175 case VAR_STRING:
6176 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006177 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006178 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006179 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006180 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006181 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006182 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006183 copy_tv(from, to);
6184 break;
6185 case VAR_LIST:
6186 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006187 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006188 if (from->vval.v_list == NULL)
6189 to->vval.v_list = NULL;
6190 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6191 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006192 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006193 to->vval.v_list = from->vval.v_list->lv_copylist;
6194 ++to->vval.v_list->lv_refcount;
6195 }
6196 else
6197 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6198 if (to->vval.v_list == NULL)
6199 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006200 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006201 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006202 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006203 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006204 case VAR_DICT:
6205 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006206 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006207 if (from->vval.v_dict == NULL)
6208 to->vval.v_dict = NULL;
6209 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6210 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006211 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006212 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6213 ++to->vval.v_dict->dv_refcount;
6214 }
6215 else
6216 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6217 if (to->vval.v_dict == NULL)
6218 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006219 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006220 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006221 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006222 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006223 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006224 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006225 }
6226 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006227 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006228}
6229
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006230 void
6231echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6232{
6233 char_u *tofree;
6234 char_u numbuf[NUMBUFLEN];
6235 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6236
6237 if (*atstart)
6238 {
6239 *atstart = FALSE;
6240 // Call msg_start() after eval1(), evaluating the expression
6241 // may cause a message to appear.
6242 if (with_space)
6243 {
6244 // Mark the saved text as finishing the line, so that what
6245 // follows is displayed on a new line when scrolling back
6246 // at the more prompt.
6247 msg_sb_eol();
6248 msg_start();
6249 }
6250 }
6251 else if (with_space)
6252 msg_puts_attr(" ", echo_attr);
6253
6254 if (p != NULL)
6255 for ( ; *p != NUL && !got_int; ++p)
6256 {
6257 if (*p == '\n' || *p == '\r' || *p == TAB)
6258 {
6259 if (*p != TAB && *needclr)
6260 {
6261 // remove any text still there from the command
6262 msg_clr_eos();
6263 *needclr = FALSE;
6264 }
6265 msg_putchar_attr(*p, echo_attr);
6266 }
6267 else
6268 {
6269 if (has_mbyte)
6270 {
6271 int i = (*mb_ptr2len)(p);
6272
6273 (void)msg_outtrans_len_attr(p, i, echo_attr);
6274 p += i - 1;
6275 }
6276 else
6277 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6278 }
6279 }
6280 vim_free(tofree);
6281}
6282
Bram Moolenaare9a41262005-01-15 22:18:47 +00006283/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 * ":echo expr1 ..." print each argument separated with a space, add a
6285 * newline at the end.
6286 * ":echon expr1 ..." print each argument plain.
6287 */
6288 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006289ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290{
6291 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006292 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006293 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 int needclr = TRUE;
6295 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006296 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006297 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006298 evalarg_T evalarg;
6299
Bram Moolenaare707c882020-07-01 13:07:37 +02006300 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301
6302 if (eap->skip)
6303 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006304 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006306 // If eval1() causes an error message the text from the command may
6307 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006308 need_clr_eos = needclr;
6309
Bram Moolenaar68db9962021-05-09 23:19:22 +02006310 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006311 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 {
6313 /*
6314 * Report the invalid expression unless the expression evaluation
6315 * has been cancelled due to an aborting error, an interrupt, or an
6316 * exception.
6317 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006318 if (!aborting() && did_emsg == did_emsg_before
6319 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006320 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006321 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 break;
6323 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006324 need_clr_eos = FALSE;
6325
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006327 {
6328 if (rettv.v_type == VAR_VOID)
6329 {
6330 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6331 break;
6332 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006333 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006334 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006335
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006336 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 arg = skipwhite(arg);
6338 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006339 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006340 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341
6342 if (eap->skip)
6343 --emsg_skip;
6344 else
6345 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006346 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 if (needclr)
6348 msg_clr_eos();
6349 if (eap->cmdidx == CMD_echo)
6350 msg_end();
6351 }
6352}
6353
6354/*
6355 * ":echohl {name}".
6356 */
6357 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006358ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006360 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361}
6362
6363/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006364 * Returns the :echo attribute
6365 */
6366 int
6367get_echo_attr(void)
6368{
6369 return echo_attr;
6370}
6371
6372/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 * ":execute expr1 ..." execute the result of an expression.
6374 * ":echomsg expr1 ..." Print a message
6375 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006376 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 * Each gets spaces around each argument and a newline at the end for
6378 * echo commands
6379 */
6380 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006381ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382{
6383 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006384 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 int ret = OK;
6386 char_u *p;
6387 garray_T ga;
6388 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006389 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390
6391 ga_init2(&ga, 1, 80);
6392
6393 if (eap->skip)
6394 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006395 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006397 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006398 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400
6401 if (!eap->skip)
6402 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006403 char_u buf[NUMBUFLEN];
6404
6405 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006406 {
6407 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6408 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006409 semsg(_(e_using_invalid_value_as_string_str),
6410 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006411 p = NULL;
6412 }
6413 else
6414 p = tv_get_string_buf(&rettv, buf);
6415 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006416 else
6417 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006418 if (p == NULL)
6419 {
6420 clear_tv(&rettv);
6421 ret = FAIL;
6422 break;
6423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 len = (int)STRLEN(p);
6425 if (ga_grow(&ga, len + 2) == FAIL)
6426 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006427 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 ret = FAIL;
6429 break;
6430 }
6431 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 ga.ga_len += len;
6435 }
6436
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006437 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 arg = skipwhite(arg);
6439 }
6440
6441 if (ret != FAIL && ga.ga_data != NULL)
6442 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006443 // use the first line of continuation lines for messages
6444 SOURCING_LNUM = start_lnum;
6445
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006446 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6447 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006448 // Mark the already saved text as finishing the line, so that what
6449 // follows is displayed on a new line when scrolling back at the
6450 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006451 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006452 }
6453
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006455 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006456 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006457 out_flush();
6458 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006459 else if (eap->cmdidx == CMD_echoconsole)
6460 {
6461 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6462 ui_write((char_u *)"\r\n", 2, TRUE);
6463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 else if (eap->cmdidx == CMD_echoerr)
6465 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006466 int save_did_emsg = did_emsg;
6467
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006468 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006469 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 if (!force_abort)
6471 did_emsg = save_did_emsg;
6472 }
6473 else if (eap->cmdidx == CMD_execute)
6474 do_cmdline((char_u *)ga.ga_data,
6475 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6476 }
6477
6478 ga_clear(&ga);
6479
6480 if (eap->skip)
6481 --emsg_skip;
6482
Bram Moolenaar63b91732021-08-05 20:40:03 +02006483 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484}
6485
6486/*
6487 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6488 * "arg" points to the "&" or '+' when called, to "option" when returning.
6489 * Returns NULL when no option name found. Otherwise pointer to the char
6490 * after the option name.
6491 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006492 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006493find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494{
6495 char_u *p = *arg;
6496
6497 ++p;
6498 if (*p == 'g' && p[1] == ':')
6499 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006500 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 p += 2;
6502 }
6503 else if (*p == 'l' && p[1] == ':')
6504 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006505 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 p += 2;
6507 }
6508 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006509 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510
6511 if (!ASCII_ISALPHA(*p))
6512 return NULL;
6513 *arg = p;
6514
6515 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006516 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 else
6518 while (ASCII_ISALPHA(*p))
6519 ++p;
6520 return p;
6521}
6522
6523/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006524 * Display script name where an item was last set.
6525 * Should only be invoked when 'verbose' is non-zero.
6526 */
6527 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006528last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006529{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006530 char_u *p;
6531
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006532 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006533 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006534 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006535 if (p != NULL)
6536 {
6537 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006538 msg_puts(_("\n\tLast set from "));
6539 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006540 if (script_ctx.sc_lnum > 0)
6541 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006542 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006543 msg_outnum((long)script_ctx.sc_lnum);
6544 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006545 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006546 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006547 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006548 }
6549}
6550
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006551#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552
6553/*
6554 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006555 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 * "flags" can be "g" to do a global substitute.
6557 * Returns an allocated string, NULL for error.
6558 */
6559 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006560do_string_sub(
6561 char_u *str,
6562 char_u *pat,
6563 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006564 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006565 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566{
6567 int sublen;
6568 regmatch_T regmatch;
6569 int i;
6570 int do_all;
6571 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006572 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 garray_T ga;
6574 char_u *ret;
6575 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006576 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006578 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006580 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581
6582 ga_init2(&ga, 1, 200);
6583
6584 do_all = (flags[0] == 'g');
6585
6586 regmatch.rm_ic = p_ic;
6587 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6588 if (regmatch.regprog != NULL)
6589 {
6590 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006591 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6593 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006594 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006595 if (regmatch.startp[0] == regmatch.endp[0])
6596 {
6597 if (zero_width == regmatch.startp[0])
6598 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006599 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006600 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006601 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6602 (size_t)i);
6603 ga.ga_len += i;
6604 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006605 continue;
6606 }
6607 zero_width = regmatch.startp[0];
6608 }
6609
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 /*
6611 * Get some space for a temporary buffer to do the substitution
6612 * into. It will contain:
6613 * - The text up to where the match is.
6614 * - The substituted text.
6615 * - The text after the match.
6616 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006617 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006618 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6620 {
6621 ga_clear(&ga);
6622 break;
6623 }
6624
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006625 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 i = (int)(regmatch.startp[0] - tail);
6627 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006628 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006629 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 + ga.ga_len + i, TRUE, TRUE, FALSE);
6631 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006632 tail = regmatch.endp[0];
6633 if (*tail == NUL)
6634 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 if (!do_all)
6636 break;
6637 }
6638
6639 if (ga.ga_data != NULL)
6640 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6641
Bram Moolenaar473de612013-06-08 18:19:48 +02006642 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 }
6644
6645 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6646 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006647 if (p_cpo == empty_option)
6648 p_cpo = save_cpo;
6649 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006650 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006651 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006652 // If it's still empty it was changed and restored, need to restore in
6653 // the complicated way.
6654 if (*p_cpo == NUL)
6655 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006656 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658
6659 return ret;
6660}