blob: 9313c272077d3da6fffee847269c3d4597c7bf36 [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 Moolenaar071d4272004-06-13 20:20:40 +0000775#ifdef FEAT_FOLDING
776/*
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 Moolenaar7454a062016-01-30 15:14:10 +0100783eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784{
Bram Moolenaar33570922005-01-25 22:26:29 +0000785 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200786 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000788 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
789 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790
791 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000792 if (use_sandbox)
793 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200794 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200796 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 retval = 0;
798 else
799 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100800 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000801 if (tv.v_type == VAR_NUMBER)
802 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000803 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 retval = 0;
805 else
806 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100807 // If the result is a string, check if there is a non-digit before
808 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000809 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 if (!VIM_ISDIGIT(*s) && *s != '-')
811 *cp = *s++;
812 retval = atol((char *)s);
813 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000814 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 }
816 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000817 if (use_sandbox)
818 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200819 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200820 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200822 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823}
824#endif
825
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000827 * Get an lval: variable, Dict item or List item that can be assigned a value
828 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
829 * "name.key", "name.key[expr]" etc.
830 * Indexing only works if "name" is an existing List or Dictionary.
831 * "name" points to the start of the name.
832 * If "rettv" is not NULL it points to the value to be assigned.
833 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
834 * wrong; must end in space or cmd separator.
835 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100836 * flags:
837 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100838 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100839 * GLV_NO_AUTOLOAD: do not use script autoloading
840 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000841 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000842 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000843 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000844 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200845 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100846get_lval(
847 char_u *name,
848 typval_T *rettv,
849 lval_T *lp,
850 int unlet,
851 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100852 int flags, // GLV_ values
853 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000854{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000855 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000856 char_u *expr_start, *expr_end;
857 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000858 dictitem_T *v;
859 typval_T var1;
860 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000861 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000862 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000863 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100864 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100865 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100866 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000867
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100868 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200869 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000870
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100871 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000872 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100873 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000874 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200875 lp->ll_name_end = find_name_end(name, NULL, NULL,
876 FNE_INCL_BR | fne_flags);
877 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000878 }
879
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100880 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000881 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100882 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000883 if (expr_start != NULL)
884 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100885 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100886 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000887 && *p != '[' && *p != '.')
888 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000889 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000890 return NULL;
891 }
892
893 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
894 if (lp->ll_exp_name == NULL)
895 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100896 // Report an invalid expression in braces, unless the
897 // expression evaluation has been cancelled due to an
898 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000899 if (!aborting() && !quiet)
900 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000901 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000902 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000903 return NULL;
904 }
905 }
906 lp->ll_name = lp->ll_exp_name;
907 }
908 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100909 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000910 lp->ll_name = name;
911
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200912 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100913 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200914 // "a: type" is declaring variable "a" with a type, not "a:".
915 if (p == name + 2 && p[-1] == ':')
916 {
917 --p;
918 lp->ll_name_end = p;
919 }
920 if (*p == ':')
921 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000922 garray_T tmp_type_list;
923 garray_T *type_list;
924 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +0200925
926 if (tp == p + 1 && !quiet)
927 {
928 semsg(_(e_white_space_required_after_str_str), ":", p);
929 return NULL;
930 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100931
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000932 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
933 type_list = &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list;
934 else
935 {
936 type_list = &tmp_type_list;
937 ga_init2(type_list, sizeof(type_T), 10);
938 }
939
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200940 // parse the type after the name
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000941 lp->ll_type = parse_type(&tp, type_list, !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100942 if (lp->ll_type == NULL && !quiet)
943 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200944 lp->ll_name_end = tp;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000945
946 // drop the type when not in a script
947 if (type_list == &tmp_type_list)
948 {
949 lp->ll_type = NULL;
950 clear_type_list(type_list);
951 }
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200952 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100953 }
954 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000955 if (lp->ll_name == NULL)
956 return p;
957
958 if (*p == '.' && in_vim9script())
959 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000960 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name,
961 TRUE, NULL);
962
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000963 if (import != NULL)
964 {
965 ufunc_T *ufunc;
966 type_T *type;
967
968 lp->ll_sid = import->imp_sid;
969 lp->ll_name = skipwhite(p + 1);
970 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
971 lp->ll_name_end = p;
972
973 // check the item is exported
974 cc = *p;
975 *p = NUL;
976 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
977 NULL, TRUE) == -1)
978 {
979 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +0000980 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000981 }
982 *p = cc;
983 }
984 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100986 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000987 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000988 return p;
989
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200990 if (in_vim9script() && lval_root != NULL)
991 {
992 // using local variable
993 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +0200994 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200995 }
996 else
997 {
998 cc = *p;
999 *p = NUL;
1000 // When we would write to the variable pass &ht and prevent autoload.
1001 writing = !(flags & GLV_READ_ONLY);
1002 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001003 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001004 if (v == NULL && !quiet)
1005 semsg(_(e_undefined_variable_str), lp->ll_name);
1006 *p = cc;
1007 if (v == NULL)
1008 return NULL;
1009 lp->ll_tv = &v->di_tv;
1010 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001011
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001012 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
1013 {
1014 if (!quiet)
1015 semsg(_(e_variable_already_declared), lp->ll_name);
1016 return NULL;
1017 }
1018
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001019 /*
1020 * Loop until no more [idx] or .key is following.
1021 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001022 var1.v_type = VAR_UNKNOWN;
1023 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001024 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001025 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001026 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1027 {
1028 if (!quiet)
1029 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1030 return NULL;
1031 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001032 if (lp->ll_tv->v_type != VAR_LIST
1033 && lp->ll_tv->v_type != VAR_DICT
1034 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001035 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001036 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001037 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001038 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001039 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001040
1041 // a NULL list/blob works like an empty list/blob, allocate one now.
1042 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1043 rettv_list_alloc(lp->ll_tv);
1044 else if (lp->ll_tv->v_type == VAR_BLOB
1045 && lp->ll_tv->vval.v_blob == NULL)
1046 rettv_blob_alloc(lp->ll_tv);
1047
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001048 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001049 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001050 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001051 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001052 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001053 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001054
Bram Moolenaar10c65862020-10-08 21:16:42 +02001055 if (in_vim9script() && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001056 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001057 && lp->ll_tv == &v->di_tv
1058 && ht != NULL && ht == get_script_local_ht())
1059 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001060 svar_T *sv = find_typval_in_script(lp->ll_tv, 0);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001061
1062 // Vim9 script local variable: get the type
1063 if (sv != NULL)
1064 lp->ll_valtype = sv->sv_type;
1065 }
1066
Bram Moolenaar8c711452005-01-14 21:53:12 +00001067 len = -1;
1068 if (*p == '.')
1069 {
1070 key = p + 1;
1071 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1072 ;
1073 if (len == 0)
1074 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001075 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001076 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001077 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001078 }
1079 p = key + len;
1080 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001081 else
1082 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001083 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001084 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001085 if (*p == ':')
1086 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001087 else
1088 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001089 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001090 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001091 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001092 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001093 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001094 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001095 clear_tv(&var1);
1096 return NULL;
1097 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001098 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001099 }
1100
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001101 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001102 if (*p == ':')
1103 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001104 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001105 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001106 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001107 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001108 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001109 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001110 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001111 if (rettv != NULL
1112 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001113 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001114 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001115 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001116 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001117 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001118 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001119 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001120 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001121 }
1122 p = skipwhite(p + 1);
1123 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001124 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001125 else
1126 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001127 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001128 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001129 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001130 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001131 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001132 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001133 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001134 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001135 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001136 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001137 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001138 clear_tv(&var2);
1139 return NULL;
1140 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001141 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001142 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001143 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001144 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001145 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001146
Bram Moolenaar8c711452005-01-14 21:53:12 +00001147 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001148 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001149 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001150 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001151 clear_tv(&var1);
1152 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001153 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001154 }
1155
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001156 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001157 ++p;
1158 }
1159
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001160 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001161 {
1162 if (len == -1)
1163 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001164 // "[key]": get key from "var1"
1165 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001166 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001167 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001168 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001169 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001170 }
1171 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001172 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001173
1174 // a NULL dict is equivalent with an empty dict
1175 if (lp->ll_tv->vval.v_dict == NULL)
1176 {
1177 lp->ll_tv->vval.v_dict = dict_alloc();
1178 if (lp->ll_tv->vval.v_dict == NULL)
1179 {
1180 clear_tv(&var1);
1181 return NULL;
1182 }
1183 ++lp->ll_tv->vval.v_dict->dv_refcount;
1184 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001185 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001186
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001187 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001188
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001189 // When assigning to a scope dictionary check that a function and
1190 // variable name is valid (only variable name unless it is l: or
1191 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001192 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001193 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001194 int prevval;
1195 int wrong;
1196
1197 if (len != -1)
1198 {
1199 prevval = key[len];
1200 key[len] = NUL;
1201 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001202 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001203 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001204 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1205 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001206 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001207 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001208 if (len != -1)
1209 key[len] = prevval;
1210 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001211 {
1212 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001213 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001214 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001215 }
1216
Bram Moolenaar10c65862020-10-08 21:16:42 +02001217 if (lp->ll_valtype != NULL)
1218 // use the type of the member
1219 lp->ll_valtype = lp->ll_valtype->tt_member;
1220
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001221 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001222 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001223 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001224 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001225 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001226 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001227 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001228 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001229 return NULL;
1230 }
1231
Bram Moolenaar31b81602019-02-10 22:14:27 +01001232 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001233 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001234 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001235 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001236 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001237 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001238 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001239 }
1240 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001241 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001242 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001243 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001244 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001245 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001246 p = NULL;
1247 break;
1248 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001249 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001250 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001251 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1252 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001253 {
1254 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001255 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001256 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001257
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001258 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001259 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001260 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001261 else if (lp->ll_tv->v_type == VAR_BLOB)
1262 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001263 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1264
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001265 /*
1266 * Get the number and item for the only or first index of the List.
1267 */
1268 if (empty1)
1269 lp->ll_n1 = 0;
1270 else
1271 // is number or string
1272 lp->ll_n1 = (long)tv_get_number(&var1);
1273 clear_tv(&var1);
1274
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001275 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001276 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001277 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001278 return NULL;
1279 }
1280 if (lp->ll_range && !lp->ll_empty2)
1281 {
1282 lp->ll_n2 = (long)tv_get_number(&var2);
1283 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001284 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1285 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001286 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001287 }
1288 lp->ll_blob = lp->ll_tv->vval.v_blob;
1289 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001290 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001291 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001292 else
1293 {
1294 /*
1295 * Get the number and item for the only or first index of the List.
1296 */
1297 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001298 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001299 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001300 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001301 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001302 clear_tv(&var1);
1303
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001304 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001305 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001306 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001307 if (lp->ll_li == NULL)
1308 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001309 clear_tv(&var2);
1310 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001311 }
1312
Bram Moolenaar10c65862020-10-08 21:16:42 +02001313 if (lp->ll_valtype != NULL)
1314 // use the type of the member
1315 lp->ll_valtype = lp->ll_valtype->tt_member;
1316
Bram Moolenaar8c711452005-01-14 21:53:12 +00001317 /*
1318 * May need to find the item or absolute index for the second
1319 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001320 * When no index given: "lp->ll_empty2" is TRUE.
1321 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001322 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001323 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001324 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001325 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001326 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001327 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001328 if (check_range_index_two(lp->ll_list,
1329 &lp->ll_n1, lp->ll_li,
1330 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001331 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001332 }
1333
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001334 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001335 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001336 }
1337
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001338 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001340 return p;
1341}
1342
1343/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001344 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001345 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001346 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001347clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001348{
1349 vim_free(lp->ll_exp_name);
1350 vim_free(lp->ll_newkey);
1351}
1352
1353/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001354 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001355 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001356 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1357 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001358 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001359 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001360set_var_lval(
1361 lval_T *lp,
1362 char_u *endp,
1363 typval_T *rettv,
1364 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001365 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1366 char_u *op,
1367 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001368{
1369 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001370 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001371
1372 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001373 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001374 cc = *endp;
1375 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001376 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1377 return;
1378
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001379 if (lp->ll_blob != NULL)
1380 {
1381 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001382
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001383 if (op != NULL && *op != '=')
1384 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001385 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001386 return;
1387 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001388 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001389 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001390
1391 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1392 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001393 if (lp->ll_empty2)
1394 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1395
Bram Moolenaar68452172021-04-12 21:21:02 +02001396 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1397 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001398 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001399 }
1400 else
1401 {
1402 val = (int)tv_get_number_chk(rettv, &error);
1403 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001404 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001405 }
1406 }
1407 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001408 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001409 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001410
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001411 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1412 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001413 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001414 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001415 *endp = cc;
1416 return;
1417 }
1418
Bram Moolenaarff697e62019-02-12 22:28:33 +01001419 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001420 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001421 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001422 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001423 {
1424 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001425 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1426 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001427 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001428 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001429 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001430 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001431 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001432 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001433 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001434 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001435 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1436 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001437 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001438 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001439 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001440 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001441 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001442 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001443 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001444 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001445 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001446 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001447 else if (lp->ll_range)
1448 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001449 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1450 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001451 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001452 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001453 return;
1454 }
1455
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001456 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1457 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001458 }
1459 else
1460 {
1461 /*
1462 * Assign to a List or Dictionary item.
1463 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001464 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1465 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001466 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001467 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001468 return;
1469 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001470
1471 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001472 && check_typval_arg_type(lp->ll_valtype, rettv,
1473 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001474 return;
1475
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001476 if (lp->ll_newkey != NULL)
1477 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001478 if (op != NULL && *op != '=')
1479 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001480 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001481 return;
1482 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001483 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1484 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001485 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001486
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001487 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001488 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001489 if (di == NULL)
1490 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001491 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1492 {
1493 vim_free(di);
1494 return;
1495 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001496 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001497 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001498 else if (op != NULL && *op != '=')
1499 {
1500 tv_op(lp->ll_tv, rettv, op);
1501 return;
1502 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001503 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001504 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001505
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001506 /*
1507 * Assign the value to the variable or list item.
1508 */
1509 if (copy)
1510 copy_tv(rettv, lp->ll_tv);
1511 else
1512 {
1513 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001514 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001515 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001516 }
1517 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001518}
1519
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001520/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001521 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1522 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001523 * Returns OK or FAIL.
1524 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001525 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001526tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001527{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001528 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001529 char_u numbuf[NUMBUFLEN];
1530 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001531 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001532
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001533 // Can't do anything with a Funcref or Dict on the right.
1534 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001535 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001536 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1537 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001538 {
1539 switch (tv1->v_type)
1540 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001541 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001542 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001543 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001544 case VAR_DICT:
1545 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001546 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001547 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001548 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001549 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001550 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001551 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001552 break;
1553
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001554 case VAR_BLOB:
1555 if (*op != '+' || tv2->v_type != VAR_BLOB)
1556 break;
1557 // BLOB += BLOB
1558 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1559 {
1560 blob_T *b1 = tv1->vval.v_blob;
1561 blob_T *b2 = tv2->vval.v_blob;
1562 int i, len = blob_len(b2);
1563 for (i = 0; i < len; i++)
1564 ga_append(&b1->bv_ga, blob_get(b2, i));
1565 }
1566 return OK;
1567
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001568 case VAR_LIST:
1569 if (*op != '+' || tv2->v_type != VAR_LIST)
1570 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001571 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001572 if (tv2->vval.v_list != NULL)
1573 {
1574 if (tv1->vval.v_list == NULL)
1575 {
1576 tv1->vval.v_list = tv2->vval.v_list;
1577 ++tv1->vval.v_list->lv_refcount;
1578 }
1579 else
1580 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1581 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001582 return OK;
1583
1584 case VAR_NUMBER:
1585 case VAR_STRING:
1586 if (tv2->v_type == VAR_LIST)
1587 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001588 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001589 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001590 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001591 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001592#ifdef FEAT_FLOAT
1593 if (tv2->v_type == VAR_FLOAT)
1594 {
1595 float_T f = n;
1596
Bram Moolenaarff697e62019-02-12 22:28:33 +01001597 if (*op == '%')
1598 break;
1599 switch (*op)
1600 {
1601 case '+': f += tv2->vval.v_float; break;
1602 case '-': f -= tv2->vval.v_float; break;
1603 case '*': f *= tv2->vval.v_float; break;
1604 case '/': f /= tv2->vval.v_float; break;
1605 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001606 clear_tv(tv1);
1607 tv1->v_type = VAR_FLOAT;
1608 tv1->vval.v_float = f;
1609 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001610 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001611#endif
1612 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001613 switch (*op)
1614 {
1615 case '+': n += tv_get_number(tv2); break;
1616 case '-': n -= tv_get_number(tv2); break;
1617 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001618 case '/': n = num_divide(n, tv_get_number(tv2),
1619 &failed); break;
1620 case '%': n = num_modulus(n, tv_get_number(tv2),
1621 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001622 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001623 clear_tv(tv1);
1624 tv1->v_type = VAR_NUMBER;
1625 tv1->vval.v_number = n;
1626 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001627 }
1628 else
1629 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001630 if (tv2->v_type == VAR_FLOAT)
1631 break;
1632
Bram Moolenaarff697e62019-02-12 22:28:33 +01001633 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001634 s = tv_get_string(tv1);
1635 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001636 clear_tv(tv1);
1637 tv1->v_type = VAR_STRING;
1638 tv1->vval.v_string = s;
1639 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001640 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001641
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001642 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001643#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001644 {
1645 float_T f;
1646
Bram Moolenaarff697e62019-02-12 22:28:33 +01001647 if (*op == '%' || *op == '.'
1648 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001649 && tv2->v_type != VAR_NUMBER
1650 && tv2->v_type != VAR_STRING))
1651 break;
1652 if (tv2->v_type == VAR_FLOAT)
1653 f = tv2->vval.v_float;
1654 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001655 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001656 switch (*op)
1657 {
1658 case '+': tv1->vval.v_float += f; break;
1659 case '-': tv1->vval.v_float -= f; break;
1660 case '*': tv1->vval.v_float *= f; break;
1661 case '/': tv1->vval.v_float /= f; break;
1662 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001663 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001664#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001665 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001666 }
1667 }
1668
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001669 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001670 return FAIL;
1671}
1672
1673/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001674 * Evaluate the expression used in a ":for var in expr" command.
1675 * "arg" points to "var".
1676 * Set "*errp" to TRUE for an error, FALSE otherwise;
1677 * Return a pointer that holds the info. Null when there is an error.
1678 */
1679 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001680eval_for_line(
1681 char_u *arg,
1682 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001683 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001684 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001685{
Bram Moolenaar33570922005-01-25 22:26:29 +00001686 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001687 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001689 typval_T tv;
1690 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001691 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001692
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001693 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001694
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001695 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001696 if (fi == NULL)
1697 return NULL;
1698
Bram Moolenaar404557e2021-07-05 21:41:48 +02001699 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1700 &fi->fi_semicolon, FALSE);
1701 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001702 return fi;
1703
Bram Moolenaar404557e2021-07-05 21:41:48 +02001704 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001705 if (expr[0] != 'i' || expr[1] != 'n'
1706 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001707 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001708 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1709 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1710 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001711 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001712 return fi;
1713 }
1714
1715 if (skip)
1716 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001717 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1718 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001719 {
1720 *errp = FALSE;
1721 if (!skip)
1722 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001723 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001724 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001725 l = tv.vval.v_list;
1726 if (l == NULL)
1727 {
1728 // a null list is like an empty list: do nothing
1729 clear_tv(&tv);
1730 }
1731 else
1732 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001733 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001734 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001735
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001736 // No need to increment the refcount, it's already set for
1737 // the list being used in "tv".
1738 fi->fi_list = l;
1739 list_add_watch(l, &fi->fi_lw);
1740 fi->fi_lw.lw_item = l->lv_first;
1741 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001742 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001743 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001744 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001745 fi->fi_bi = 0;
1746 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001747 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001748 typval_T btv;
1749
1750 // Make a copy, so that the iteration still works when the
1751 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001753 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001754 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001755 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001756 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001757 else if (tv.v_type == VAR_STRING)
1758 {
1759 fi->fi_byte_idx = 0;
1760 fi->fi_string = tv.vval.v_string;
1761 tv.vval.v_string = NULL;
1762 if (fi->fi_string == NULL)
1763 fi->fi_string = vim_strsave((char_u *)"");
1764 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001765 else
1766 {
Sean Dewar80d73952021-08-04 19:25:54 +02001767 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001768 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001769 }
1770 }
1771 }
1772 if (skip)
1773 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001774 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001775
1776 return fi;
1777}
1778
1779/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001780 * Used when looping over a :for line, skip the "in expr" part.
1781 */
1782 void
1783skip_for_lines(void *fi_void, evalarg_T *evalarg)
1784{
1785 forinfo_T *fi = (forinfo_T *)fi_void;
1786 int i;
1787
1788 for (i = 0; i < fi->fi_break_count; ++i)
1789 eval_next_line(evalarg);
1790}
1791
1792/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001793 * Use the first item in a ":for" list. Advance to the next.
1794 * Assign the values to the variable (list). "arg" points to the first one.
1795 * Return TRUE when a valid item was found, FALSE when at end of list or
1796 * something wrong.
1797 */
1798 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001799next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001800{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001801 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001802 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001803 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001804 ? (ASSIGN_FINAL
1805 // first round: error if variable exists
1806 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1807 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001808 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001809 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001810 int skip_assign = in_vim9script() && arg[0] == '_'
1811 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001812
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001813 if (fi->fi_blob != NULL)
1814 {
1815 typval_T tv;
1816
1817 if (fi->fi_bi >= blob_len(fi->fi_blob))
1818 return FALSE;
1819 tv.v_type = VAR_NUMBER;
1820 tv.v_lock = VAR_FIXED;
1821 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1822 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001823 if (skip_assign)
1824 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001825 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001826 fi->fi_varcount, flag, NULL) == OK;
1827 }
1828
1829 if (fi->fi_string != NULL)
1830 {
1831 typval_T tv;
1832 int len;
1833
1834 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1835 if (len == 0)
1836 return FALSE;
1837 tv.v_type = VAR_STRING;
1838 tv.v_lock = VAR_FIXED;
1839 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1840 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001841 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001842 if (skip_assign)
1843 result = TRUE;
1844 else
1845 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001846 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001847 vim_free(tv.vval.v_string);
1848 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001849 }
1850
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851 item = fi->fi_lw.lw_item;
1852 if (item == NULL)
1853 result = FALSE;
1854 else
1855 {
1856 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001857 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001858 if (skip_assign)
1859 result = TRUE;
1860 else
1861 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001862 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001863 }
1864 return result;
1865}
1866
1867/*
1868 * Free the structure used to store info used by ":for".
1869 */
1870 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001871free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001872{
Bram Moolenaar33570922005-01-25 22:26:29 +00001873 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001874
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001875 if (fi == NULL)
1876 return;
1877 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001878 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001879 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001880 list_unref(fi->fi_list);
1881 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001882 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001883 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001884 else
1885 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001886 vim_free(fi);
1887}
1888
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001890set_context_for_expression(
1891 expand_T *xp,
1892 char_u *arg,
1893 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001895 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001897 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001899 if (cmdidx == CMD_let || cmdidx == CMD_var
1900 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001901 {
1902 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001903 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001904 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001905 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001906 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001907 {
1908 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001909 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001910 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001911 break;
1912 }
1913 return;
1914 }
1915 }
1916 else
1917 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1918 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 while ((xp->xp_pattern = vim_strpbrk(arg,
1920 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1921 {
1922 c = *xp->xp_pattern;
1923 if (c == '&')
1924 {
1925 c = xp->xp_pattern[1];
1926 if (c == '&')
1927 {
1928 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001929 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 }
1931 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001932 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001934 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1935 xp->xp_pattern += 2;
1936
1937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 }
1939 else if (c == '$')
1940 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001941 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 xp->xp_context = EXPAND_ENV_VARS;
1943 }
1944 else if (c == '=')
1945 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001946 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 xp->xp_context = EXPAND_EXPRESSION;
1948 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001949 else if (c == '#'
1950 && xp->xp_context == EXPAND_EXPRESSION)
1951 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001952 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001953 break;
1954 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001955 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 && xp->xp_context == EXPAND_FUNCTIONS
1957 && vim_strchr(xp->xp_pattern, '(') == NULL)
1958 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001959 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 break;
1961 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001962 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001964 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 {
1966 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1967 if (c == '\\' && xp->xp_pattern[1] != NUL)
1968 ++xp->xp_pattern;
1969 xp->xp_context = EXPAND_NOTHING;
1970 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001971 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001973 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1975 /* skip */ ;
1976 xp->xp_context = EXPAND_NOTHING;
1977 }
1978 else if (c == '|')
1979 {
1980 if (xp->xp_pattern[1] == '|')
1981 {
1982 ++xp->xp_pattern;
1983 xp->xp_context = EXPAND_EXPRESSION;
1984 }
1985 else
1986 xp->xp_context = EXPAND_COMMANDS;
1987 }
1988 else
1989 xp->xp_context = EXPAND_EXPRESSION;
1990 }
1991 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001992 // Doesn't look like something valid, expand as an expression
1993 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001994 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 arg = xp->xp_pattern;
1996 if (*arg != NUL)
1997 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1998 /* skip */ ;
1999 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002000
2001 // ":exe one two" completes "two"
2002 if ((cmdidx == CMD_execute
2003 || cmdidx == CMD_echo
2004 || cmdidx == CMD_echon
2005 || cmdidx == CMD_echomsg)
2006 && xp->xp_context == EXPAND_EXPRESSION)
2007 {
2008 for (;;)
2009 {
2010 char_u *n = skiptowhite(arg);
2011
2012 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2013 break;
2014 arg = skipwhite(n);
2015 }
2016 }
2017
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 xp->xp_pattern = arg;
2019}
2020
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002022 * Return TRUE if "pat" matches "text".
2023 * Does not use 'cpo' and always uses 'magic'.
2024 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002025 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002026pattern_match(char_u *pat, char_u *text, int ic)
2027{
2028 int matches = FALSE;
2029 char_u *save_cpo;
2030 regmatch_T regmatch;
2031
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002032 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002033 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002034 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002035 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2036 if (regmatch.regprog != NULL)
2037 {
2038 regmatch.rm_ic = ic;
2039 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2040 vim_regfree(regmatch.regprog);
2041 }
2042 p_cpo = save_cpo;
2043 return matches;
2044}
2045
2046/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002047 * Handle a name followed by "(". Both for just "name(arg)" and for
2048 * "expr->name(arg)".
2049 * Returns OK or FAIL.
2050 */
2051 static int
2052eval_func(
2053 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002054 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002055 char_u *name,
2056 int name_len,
2057 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002058 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002059 typval_T *basetv) // "expr" for "expr->name(arg)"
2060{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002061 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002062 char_u *s = name;
2063 int len = name_len;
2064 partial_T *partial;
2065 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002066 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002067 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002068
2069 if (!evaluate)
2070 check_vars(s, len);
2071
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002072 // If "s" is the name of a variable of type VAR_FUNC
2073 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002074 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002075 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002076
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002077 // Need to make a copy, in case evaluating the arguments makes
2078 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002079 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002080 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002081 ret = FAIL;
2082 else
2083 {
2084 funcexe_T funcexe;
2085
2086 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002087 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002088 funcexe.fe_firstline = curwin->w_cursor.lnum;
2089 funcexe.fe_lastline = curwin->w_cursor.lnum;
2090 funcexe.fe_evaluate = evaluate;
2091 funcexe.fe_partial = partial;
2092 funcexe.fe_basetv = basetv;
2093 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002094 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002095 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002096 }
2097 vim_free(s);
2098
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002099 // If evaluate is FALSE rettv->v_type was not set in
2100 // get_func_tv, but it's needed in handle_subscript() to parse
2101 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002102 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2103 {
2104 rettv->vval.v_string = NULL;
2105 rettv->v_type = VAR_FUNC;
2106 }
2107
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002108 // Stop the expression evaluation when immediately
2109 // aborting on error, or when an interrupt occurred or
2110 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002111 if (evaluate && aborting())
2112 {
2113 if (ret == OK)
2114 clear_tv(rettv);
2115 ret = FAIL;
2116 }
2117 return ret;
2118}
2119
2120/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002121 * Get the next line source line without advancing. But do skip over comment
2122 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002123 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002124 */
2125 static char_u *
2126getline_peek_skip_comments(evalarg_T *evalarg)
2127{
2128 for (;;)
2129 {
2130 char_u *next = getline_peek(evalarg->eval_getline,
2131 evalarg->eval_cookie);
2132 char_u *p;
2133
2134 if (next == NULL)
2135 break;
2136 p = skipwhite(next);
2137 if (*p != NUL && !vim9_comment_start(p))
2138 return next;
2139 (void)eval_next_line(evalarg);
2140 }
2141 return NULL;
2142}
2143
2144/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002145 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2146 * comment) and there is a next line, return the next line (skipping blanks)
2147 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002148 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2149 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002150 * "arg" must point somewhere inside a line, not at the start.
2151 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002152 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002153eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2154{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002155 char_u *p = skipwhite(arg);
2156
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002157 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002158 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002159 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002160 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002161 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002162 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002163 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002164
2165 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002166 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002167 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002168 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002169
Bram Moolenaar9d489562020-07-30 20:08:50 +02002170 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002171 {
2172 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002173 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002174 }
2175 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002176 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002177}
2178
2179/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002180 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002181 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002182 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002183 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002184eval_next_line(evalarg_T *evalarg)
2185{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002186 garray_T *gap = &evalarg->eval_ga;
2187 char_u *line;
2188
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002189 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002190 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2191 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002192 else
2193 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002194 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002195 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2196 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002197 char_u *p = skipwhite(line);
2198
2199 // Going to concatenate the lines after parsing. For an empty or
2200 // comment line use an empty string.
2201 if (*p == NUL || vim9_comment_start(p))
2202 {
2203 vim_free(line);
2204 line = vim_strsave((char_u *)"");
2205 }
2206
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002207 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2208 ++gap->ga_len;
2209 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002210 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002211 {
2212 vim_free(evalarg->eval_tofree);
2213 evalarg->eval_tofree = line;
2214 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002215
2216 // Advanced to the next line, "arg" no longer points into the previous
2217 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002218 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002219 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002220}
2221
Bram Moolenaare6b53242020-07-01 17:28:33 +02002222/*
2223 * Call eval_next_non_blank() and get the next line if needed.
2224 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002225 char_u *
2226skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2227{
2228 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002229 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002230
Bram Moolenaar962d7212020-07-04 14:15:00 +02002231 if (evalarg == NULL)
2232 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002233 eval_next_non_blank(p, evalarg, &getnext);
2234 if (getnext)
2235 return eval_next_line(evalarg);
2236 return p;
2237}
2238
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002239/*
Bram Moolenaar844fb642021-10-23 13:32:30 +01002240 * Initialize "evalarg" for use.
2241 */
2242 void
2243init_evalarg(evalarg_T *evalarg)
2244{
2245 CLEAR_POINTER(evalarg);
2246 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
2247}
2248
2249/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002250 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002251 */
2252 void
2253clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2254{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002255 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002256 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002257 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002258 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002259 if (eap != NULL)
2260 {
2261 // We may need to keep the original command line, e.g. for
2262 // ":let" it has the variable names. But we may also need the
2263 // new one, "nextcmd" points into it. Keep both.
2264 vim_free(eap->cmdline_tofree);
2265 eap->cmdline_tofree = *eap->cmdlinep;
2266 *eap->cmdlinep = evalarg->eval_tofree;
2267 }
2268 else
2269 vim_free(evalarg->eval_tofree);
2270 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002271 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002272
Bram Moolenaar844fb642021-10-23 13:32:30 +01002273 ga_clear_strings(&evalarg->eval_tofree_ga);
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002274 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002275 }
2276}
2277
2278/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2282 */
2283
2284/*
2285 * Handle zero level expression.
2286 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002287 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002288 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002289 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 * Return OK or FAIL.
2291 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002292 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002293eval0(
2294 char_u *arg,
2295 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002296 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002297 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002299 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2300}
2301
2302/*
2303 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2304 * expression and don't check what comes after the expression.
2305 */
2306 int
2307eval0_retarg(
2308 char_u *arg,
2309 typval_T *rettv,
2310 exarg_T *eap,
2311 evalarg_T *evalarg,
2312 char_u **retarg)
2313{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 int ret;
2315 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002316 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002317 int did_emsg_before = did_emsg;
2318 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002319 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002320 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002321 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322
2323 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002324 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002325 expr_end = p;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002326 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002327
Bram Moolenaar02929a32021-12-17 14:46:12 +00002328 // In Vim9 script a command block is not split at NL characters for
2329 // commands using an expression argument. Skip over a '#' comment to check
2330 // for a following NL. Require white space before the '#'.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002331 if (in_vim9script() && p > expr_end && retarg == NULL)
Bram Moolenaar02929a32021-12-17 14:46:12 +00002332 while (*p == '#')
2333 {
2334 char_u *nl = vim_strchr(p, NL);
2335
2336 if (nl == NULL)
2337 break;
2338 p = skipwhite(nl + 1);
2339 if (eap != NULL && *p != NUL)
2340 eap->nextcmd = p;
2341 check_for_end = FALSE;
2342 }
2343
2344 if (ret != FAIL && check_for_end)
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002345 end_error = !ends_excmd2(arg, p);
2346 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 {
2348 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002349 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 /*
2351 * Report the invalid expression unless the expression evaluation has
2352 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002353 * exception, or we already gave a more specific error.
2354 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002356 if (!aborting()
2357 && did_emsg == did_emsg_before
2358 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002359 && (flags & EVAL_CONSTANT) == 0
2360 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002361 {
2362 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002363 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002364 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002365 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002366 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002367
2368 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002369 // a next command to avoid more errors, unless "|" is following, which
2370 // could only be a command separator.
2371 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2372 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002373 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002375
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002376 if (retarg != NULL)
2377 *retarg = p;
2378 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002379 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002380
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 return ret;
2382}
2383
2384/*
2385 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002386 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002387 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 *
2389 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002390 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002392 * Note: "rettv.v_lock" is not set.
2393 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 * Return OK or FAIL.
2395 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002396 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002397eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002399 char_u *p;
2400 int getnext;
2401
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002402 CLEAR_POINTER(rettv);
2403
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 /*
2405 * Get the first variable.
2406 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002407 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 return FAIL;
2409
Bram Moolenaar793648f2020-06-26 21:28:25 +02002410 p = eval_next_non_blank(*arg, evalarg, &getnext);
2411 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002413 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002414 int result;
2415 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002416 evalarg_T *evalarg_used = evalarg;
2417 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002418 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002419 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002420 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002421
2422 if (evalarg == NULL)
2423 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002424 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002425 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002426 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002427 orig_flags = evalarg_used->eval_flags;
2428 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002429
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002430 if (getnext)
2431 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002432 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002433 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002434 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002435 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002436 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002437 clear_tv(rettv);
2438 return FAIL;
2439 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002440 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002441 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002442
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002444 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002446 int error = FALSE;
2447
Bram Moolenaar13106602020-10-04 16:06:05 +02002448 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002449 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002450 else if (vim9script)
2451 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002452 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002454 if (error || !op_falsy || !result)
2455 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002456 if (error)
2457 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 }
2459
2460 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002461 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002463 if (op_falsy)
2464 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002465 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002466 {
mityu4ac198c2021-05-28 17:52:40 +02002467 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002468 clear_tv(rettv);
2469 return FAIL;
2470 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002471 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002472 evalarg_used->eval_flags = (op_falsy ? !result : result)
2473 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2474 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002475 {
2476 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002478 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002479 if (!op_falsy || !result)
2480 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002482 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002484 /*
2485 * Check for the ":".
2486 */
2487 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2488 if (*p != ':')
2489 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002490 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002491 if (evaluate && result)
2492 clear_tv(rettv);
2493 evalarg_used->eval_flags = orig_flags;
2494 return FAIL;
2495 }
2496 if (getnext)
2497 *arg = eval_next_line(evalarg_used);
2498 else
2499 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002500 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002501 {
2502 error_white_both(p, 1);
2503 clear_tv(rettv);
2504 evalarg_used->eval_flags = orig_flags;
2505 return FAIL;
2506 }
2507 *arg = p;
2508 }
2509
2510 /*
2511 * Get the third variable. Recursive!
2512 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002513 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002514 {
mityu4ac198c2021-05-28 17:52:40 +02002515 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002516 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002517 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002518 return FAIL;
2519 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002520 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2521 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002522 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002523 if (eval1(arg, &var2, evalarg_used) == FAIL)
2524 {
2525 if (evaluate && result)
2526 clear_tv(rettv);
2527 evalarg_used->eval_flags = orig_flags;
2528 return FAIL;
2529 }
2530 if (evaluate && !result)
2531 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002533
2534 if (evalarg == NULL)
2535 clear_evalarg(&local_evalarg, NULL);
2536 else
2537 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 }
2539
2540 return OK;
2541}
2542
2543/*
2544 * Handle first level expression:
2545 * expr2 || expr2 || expr2 logical OR
2546 *
2547 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002548 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 *
2550 * Return OK or FAIL.
2551 */
2552 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002553eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002555 char_u *p;
2556 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
2558 /*
2559 * Get the first variable.
2560 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002561 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 return FAIL;
2563
2564 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002565 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002567 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002568 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002570 evalarg_T *evalarg_used = evalarg;
2571 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002572 int evaluate;
2573 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002574 long result = FALSE;
2575 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002576 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002577 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002578
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002579 if (evalarg == NULL)
2580 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002581 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002582 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002583 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002584 orig_flags = evalarg_used->eval_flags;
2585 evaluate = orig_flags & EVAL_EVALUATE;
2586 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002587 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002588 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002589 result = tv_get_bool_chk(rettv, &error);
2590 else if (tv_get_number_chk(rettv, &error) != 0)
2591 result = TRUE;
2592 clear_tv(rettv);
2593 if (error)
2594 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 }
2596
2597 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002598 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002600 while (p[0] == '|' && p[1] == '|')
2601 {
2602 if (getnext)
2603 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002604 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002605 {
2606 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2607 {
2608 error_white_both(p, 2);
2609 clear_tv(rettv);
2610 return FAIL;
2611 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002612 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002613 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002614
2615 /*
2616 * Get the second variable.
2617 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002618 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2619 {
mityu4ac198c2021-05-28 17:52:40 +02002620 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002621 clear_tv(rettv);
2622 return FAIL;
2623 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002624 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2625 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002626 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002627 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002628 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002629
2630 /*
2631 * Compute the result.
2632 */
2633 if (evaluate && !result)
2634 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002635 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002636 result = tv_get_bool_chk(&var2, &error);
2637 else if (tv_get_number_chk(&var2, &error) != 0)
2638 result = TRUE;
2639 clear_tv(&var2);
2640 if (error)
2641 return FAIL;
2642 }
2643 if (evaluate)
2644 {
2645 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002646 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002647 rettv->v_type = VAR_BOOL;
2648 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002649 }
2650 else
2651 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002652 rettv->v_type = VAR_NUMBER;
2653 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002654 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002655 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002656
2657 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002659
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002660 if (evalarg == NULL)
2661 clear_evalarg(&local_evalarg, NULL);
2662 else
2663 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 }
2665
2666 return OK;
2667}
2668
2669/*
2670 * Handle second level expression:
2671 * expr3 && expr3 && expr3 logical AND
2672 *
2673 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002674 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 *
2676 * Return OK or FAIL.
2677 */
2678 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002679eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002681 char_u *p;
2682 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683
2684 /*
2685 * Get the first variable.
2686 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002687 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 return FAIL;
2689
2690 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002691 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002693 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002694 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002696 evalarg_T *evalarg_used = evalarg;
2697 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002698 int orig_flags;
2699 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002700 long result = TRUE;
2701 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002702 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002703 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002704
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002705 if (evalarg == NULL)
2706 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002707 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002708 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002709 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002710 orig_flags = evalarg_used->eval_flags;
2711 evaluate = orig_flags & EVAL_EVALUATE;
2712 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002713 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002714 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002715 result = tv_get_bool_chk(rettv, &error);
2716 else if (tv_get_number_chk(rettv, &error) == 0)
2717 result = FALSE;
2718 clear_tv(rettv);
2719 if (error)
2720 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 }
2722
2723 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002724 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002726 while (p[0] == '&' && p[1] == '&')
2727 {
2728 if (getnext)
2729 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002730 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002731 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002732 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002733 {
2734 error_white_both(p, 2);
2735 clear_tv(rettv);
2736 return FAIL;
2737 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002738 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002739 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002740
2741 /*
2742 * Get the second variable.
2743 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002744 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2745 {
mityu4ac198c2021-05-28 17:52:40 +02002746 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002747 clear_tv(rettv);
2748 return FAIL;
2749 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002750 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2751 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002752 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002753 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002754 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002755 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002756
2757 /*
2758 * Compute the result.
2759 */
2760 if (evaluate && result)
2761 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002762 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002763 result = tv_get_bool_chk(&var2, &error);
2764 else if (tv_get_number_chk(&var2, &error) == 0)
2765 result = FALSE;
2766 clear_tv(&var2);
2767 if (error)
2768 return FAIL;
2769 }
2770 if (evaluate)
2771 {
2772 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002773 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002774 rettv->v_type = VAR_BOOL;
2775 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002776 }
2777 else
2778 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002779 rettv->v_type = VAR_NUMBER;
2780 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002781 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002782 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002783
2784 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002786
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002787 if (evalarg == NULL)
2788 clear_evalarg(&local_evalarg, NULL);
2789 else
2790 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 }
2792
2793 return OK;
2794}
2795
2796/*
2797 * Handle third level expression:
2798 * var1 == var2
2799 * var1 =~ var2
2800 * var1 != var2
2801 * var1 !~ var2
2802 * var1 > var2
2803 * var1 >= var2
2804 * var1 < var2
2805 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002806 * var1 is var2
2807 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 *
2809 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002810 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 *
2812 * Return OK or FAIL.
2813 */
2814 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002815eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002818 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002819 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002821 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822
2823 /*
2824 * Get the first variable.
2825 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002826 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 return FAIL;
2828
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002829 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002830 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831
2832 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002833 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002835 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002837 typval_T var2;
2838 int ic;
2839 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002840 int evaluate = evalarg == NULL
2841 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002842
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002843 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002844 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002845 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002846 p = *arg;
2847 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002848 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2849 {
mityu4ac198c2021-05-28 17:52:40 +02002850 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002851 clear_tv(rettv);
2852 return FAIL;
2853 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002854
Bram Moolenaar696ba232020-07-29 21:20:41 +02002855 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2856 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002857 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002858 clear_tv(rettv);
2859 return FAIL;
2860 }
2861
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002862 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 if (p[len] == '?')
2864 {
2865 ic = TRUE;
2866 ++len;
2867 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002868 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 else if (p[len] == '#')
2870 {
2871 ic = FALSE;
2872 ++len;
2873 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002874 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002876 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877
2878 /*
2879 * Get the second variable.
2880 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002881 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2882 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002883 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002884 clear_tv(rettv);
2885 return FAIL;
2886 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002887 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002888 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002890 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 return FAIL;
2892 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002893 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002894 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002895 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002896
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002897 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002898 {
2899 ret = FAIL;
2900 clear_tv(rettv);
2901 }
2902 else
2903 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002904 clear_tv(&var2);
2905 return ret;
2906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 }
2908
2909 return OK;
2910}
2911
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002912/*
2913 * Make a copy of blob "tv1" and append blob "tv2".
2914 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915 void
2916eval_addblob(typval_T *tv1, typval_T *tv2)
2917{
2918 blob_T *b1 = tv1->vval.v_blob;
2919 blob_T *b2 = tv2->vval.v_blob;
2920 blob_T *b = blob_alloc();
2921 int i;
2922
2923 if (b != NULL)
2924 {
2925 for (i = 0; i < blob_len(b1); i++)
2926 ga_append(&b->bv_ga, blob_get(b1, i));
2927 for (i = 0; i < blob_len(b2); i++)
2928 ga_append(&b->bv_ga, blob_get(b2, i));
2929
2930 clear_tv(tv1);
2931 rettv_blob_set(tv1, b);
2932 }
2933}
2934
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002935/*
2936 * Make a copy of list "tv1" and append list "tv2".
2937 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002938 int
2939eval_addlist(typval_T *tv1, typval_T *tv2)
2940{
2941 typval_T var3;
2942
2943 // concatenate Lists
2944 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2945 {
2946 clear_tv(tv1);
2947 clear_tv(tv2);
2948 return FAIL;
2949 }
2950 clear_tv(tv1);
2951 *tv1 = var3;
2952 return OK;
2953}
2954
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955/*
2956 * Handle fourth level expression:
2957 * + number addition
2958 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002959 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002960 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 *
2962 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002963 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 *
2965 * Return OK or FAIL.
2966 */
2967 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002968eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 /*
2971 * Get the first variable.
2972 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002973 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 return FAIL;
2975
2976 /*
2977 * Repeat computing, until no '+', '-' or '.' is following.
2978 */
2979 for (;;)
2980 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002981 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002982 int getnext;
2983 char_u *p;
2984 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002985 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002986 int concat;
2987 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002988 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002989
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002990 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002991 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002992 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002993 p = eval_next_non_blank(*arg, evalarg, &getnext);
2994 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02002995 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002996 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2997 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002999 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3000 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003001
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003002 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003003 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003004 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003005 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003006 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003007 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003008 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003009 {
mityu4ac198c2021-05-28 17:52:40 +02003010 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003011 clear_tv(rettv);
3012 return FAIL;
3013 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003014 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003015 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003016 if ((op != '+' || (rettv->v_type != VAR_LIST
3017 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003018#ifdef FEAT_FLOAT
3019 && (op == '.' || rettv->v_type != VAR_FLOAT)
3020#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003021 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003022 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003023 int error = FALSE;
3024
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003025 // For "list + ...", an illegal use of the first operand as
3026 // a number cannot be determined before evaluating the 2nd
3027 // operand: if this is also a list, all is ok.
3028 // For "something . ...", "something - ..." or "non-list + ...",
3029 // we know that the first operand needs to be a string or number
3030 // without evaluating the 2nd operand. So check before to avoid
3031 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003032 if (op != '.')
3033 tv_get_number_chk(rettv, &error);
3034 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003035 {
3036 clear_tv(rettv);
3037 return FAIL;
3038 }
3039 }
3040
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 /*
3042 * Get the second variable.
3043 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003044 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003045 {
mityu89dcb4d2021-05-28 13:50:17 +02003046 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003047 clear_tv(rettv);
3048 return FAIL;
3049 }
3050 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02003051 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003053 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 return FAIL;
3055 }
3056
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003057 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 {
3059 /*
3060 * Compute the result.
3061 */
3062 if (op == '.')
3063 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003064 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3065 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003066 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003067
Bram Moolenaar2e086612020-08-25 22:37:48 +02003068 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003069 || var2.v_type == VAR_CHANNEL
3070 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003071 semsg(_(e_using_invalid_value_as_string_str),
3072 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003073#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003074 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003075 {
3076 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3077 var2.vval.v_float);
3078 s2 = buf2;
3079 }
3080#endif
3081 else
3082 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003083 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003084 {
3085 clear_tv(rettv);
3086 clear_tv(&var2);
3087 return FAIL;
3088 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003089 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003090 clear_tv(rettv);
3091 rettv->v_type = VAR_STRING;
3092 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003094 else if (op == '+' && rettv->v_type == VAR_BLOB
3095 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003097 else if (op == '+' && rettv->v_type == VAR_LIST
3098 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003099 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003101 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 else
3104 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003105 int error = FALSE;
3106 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003107#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003108 float_T f1 = 0, f2 = 0;
3109
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003110 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003111 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003112 f1 = rettv->vval.v_float;
3113 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003114 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003115 else
3116#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003117 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003118 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003119 if (error)
3120 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003121 // This can only happen for "list + non-list" or
3122 // "blob + non-blob". For "non-list + ..." or
3123 // "something - ...", we returned before evaluating the
3124 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003125 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003126 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003127 return FAIL;
3128 }
3129#ifdef FEAT_FLOAT
3130 if (var2.v_type == VAR_FLOAT)
3131 f1 = n1;
3132#endif
3133 }
3134#ifdef FEAT_FLOAT
3135 if (var2.v_type == VAR_FLOAT)
3136 {
3137 f2 = var2.vval.v_float;
3138 n2 = 0;
3139 }
3140 else
3141#endif
3142 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003143 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003144 if (error)
3145 {
3146 clear_tv(rettv);
3147 clear_tv(&var2);
3148 return FAIL;
3149 }
3150#ifdef FEAT_FLOAT
3151 if (rettv->v_type == VAR_FLOAT)
3152 f2 = n2;
3153#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003154 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003155 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003156
3157#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003158 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003159 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3160 {
3161 if (op == '+')
3162 f1 = f1 + f2;
3163 else
3164 f1 = f1 - f2;
3165 rettv->v_type = VAR_FLOAT;
3166 rettv->vval.v_float = f1;
3167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003169#endif
3170 {
3171 if (op == '+')
3172 n1 = n1 + n2;
3173 else
3174 n1 = n1 - n2;
3175 rettv->v_type = VAR_NUMBER;
3176 rettv->vval.v_number = n1;
3177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003179 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 }
3181 }
3182 return OK;
3183}
3184
3185/*
3186 * Handle fifth level expression:
3187 * * number multiplication
3188 * / number division
3189 * % number modulo
3190 *
3191 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003192 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 *
3194 * Return OK or FAIL.
3195 */
3196 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003197eval6(
3198 char_u **arg,
3199 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003200 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003201 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003203#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003204 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206
3207 /*
3208 * Get the first variable.
3209 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003210 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 return FAIL;
3212
3213 /*
3214 * Repeat computing, until no '*', '/' or '%' is following.
3215 */
3216 for (;;)
3217 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003218 int evaluate;
3219 int getnext;
3220 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003221 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003222 int op;
3223 varnumber_T n1, n2;
3224#ifdef FEAT_FLOAT
3225 float_T f1, f2;
3226#endif
3227 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003228
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003229 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003230 p = eval_next_non_blank(*arg, evalarg, &getnext);
3231 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003232 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003234
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003235 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003236 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003237 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003238 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003239 {
3240 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3241 {
mityu4ac198c2021-05-28 17:52:40 +02003242 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003243 clear_tv(rettv);
3244 return FAIL;
3245 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003246 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003249#ifdef FEAT_FLOAT
3250 f1 = 0;
3251 f2 = 0;
3252#endif
3253 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003254 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003256#ifdef FEAT_FLOAT
3257 if (rettv->v_type == VAR_FLOAT)
3258 {
3259 f1 = rettv->vval.v_float;
3260 use_float = TRUE;
3261 n1 = 0;
3262 }
3263 else
3264#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003265 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003266 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003267 if (error)
3268 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 }
3270 else
3271 n1 = 0;
3272
3273 /*
3274 * Get the second variable.
3275 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003276 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3277 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003278 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003279 clear_tv(rettv);
3280 return FAIL;
3281 }
3282 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003283 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 return FAIL;
3285
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003286 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003288#ifdef FEAT_FLOAT
3289 if (var2.v_type == VAR_FLOAT)
3290 {
3291 if (!use_float)
3292 {
3293 f1 = n1;
3294 use_float = TRUE;
3295 }
3296 f2 = var2.vval.v_float;
3297 n2 = 0;
3298 }
3299 else
3300#endif
3301 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003302 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003303 clear_tv(&var2);
3304 if (error)
3305 return FAIL;
3306#ifdef FEAT_FLOAT
3307 if (use_float)
3308 f2 = n2;
3309#endif
3310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311
3312 /*
3313 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003314 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003316#ifdef FEAT_FLOAT
3317 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003319 if (op == '*')
3320 f1 = f1 * f2;
3321 else if (op == '/')
3322 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003323# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003324 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003325 if (f2 == 0.0)
3326 {
3327 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003328 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003329 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003330 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003331 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003332 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003333 }
3334 else
3335 f1 = f1 / f2;
3336# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003337 // We rely on the floating point library to handle divide
3338 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003339 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003340# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003343 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003344 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003345 return FAIL;
3346 }
3347 rettv->v_type = VAR_FLOAT;
3348 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 }
3350 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003353 int failed = FALSE;
3354
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003355 if (op == '*')
3356 n1 = n1 * n2;
3357 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003358 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003360 n1 = num_modulus(n1, n2, &failed);
3361 if (failed)
3362 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003363
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003364 rettv->v_type = VAR_NUMBER;
3365 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 }
3368 }
3369
3370 return OK;
3371}
3372
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003373/*
3374 * Handle a type cast before a base level expression.
3375 * "arg" must point to the first non-white of the expression.
3376 * "arg" is advanced to just after the recognized expression.
3377 * Return OK or FAIL.
3378 */
3379 static int
3380eval7t(
3381 char_u **arg,
3382 typval_T *rettv,
3383 evalarg_T *evalarg,
3384 int want_string) // after "." operator
3385{
3386 type_T *want_type = NULL;
3387 garray_T type_list; // list of pointers to allocated types
3388 int res;
3389 int evaluate = evalarg == NULL ? 0
3390 : (evalarg->eval_flags & EVAL_EVALUATE);
3391
3392 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003393 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3394 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003395 {
3396 ++*arg;
3397 ga_init2(&type_list, sizeof(type_T *), 10);
3398 want_type = parse_type(arg, &type_list, TRUE);
3399 if (want_type == NULL && (evaluate || **arg != '>'))
3400 {
3401 clear_type_list(&type_list);
3402 return FAIL;
3403 }
3404
3405 if (**arg != '>')
3406 {
3407 if (*skipwhite(*arg) == '>')
3408 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3409 else
3410 emsg(_(e_missing_gt));
3411 clear_type_list(&type_list);
3412 return FAIL;
3413 }
3414 ++*arg;
3415 *arg = skipwhite_and_linebreak(*arg, evalarg);
3416 }
3417
3418 res = eval7(arg, rettv, evalarg, want_string);
3419
3420 if (want_type != NULL && evaluate)
3421 {
3422 if (res == OK)
3423 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003424 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3425 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003426
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003427 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003428 {
3429 if (want_type == &t_bool && actual != &t_bool
3430 && (actual->tt_flags & TTFLAG_BOOL_OK))
3431 {
3432 int n = tv2bool(rettv);
3433
3434 // can use "0" and "1" for boolean in some places
3435 clear_tv(rettv);
3436 rettv->v_type = VAR_BOOL;
3437 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3438 }
3439 else
3440 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003441 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003442
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003443 where.wt_variable = TRUE;
3444 res = check_type(want_type, actual, TRUE, where);
3445 }
3446 }
3447 }
3448 clear_type_list(&type_list);
3449 }
3450
3451 return res;
3452}
3453
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003454 int
3455eval_leader(char_u **arg, int vim9)
3456{
3457 char_u *s = *arg;
3458 char_u *p = *arg;
3459
3460 while (*p == '!' || *p == '-' || *p == '+')
3461 {
3462 char_u *n = skipwhite(p + 1);
3463
3464 // ++, --, -+ and +- are not accepted in Vim9 script
3465 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3466 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003467 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003468 return FAIL;
3469 }
3470 p = n;
3471 }
3472 *arg = p;
3473 return OK;
3474}
3475
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476/*
3477 * Handle sixth level expression:
3478 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003479 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003480 * "string" string constant
3481 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 * &option-name option value
3483 * @r register contents
3484 * identifier variable value
3485 * function() function call
3486 * $VAR environment variable
3487 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003488 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003489 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003490 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003491 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 *
3493 * Also handle:
3494 * ! in front logical NOT
3495 * - in front unary minus
3496 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003497 * trailing [] subscript in String or List
3498 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003499 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 *
3501 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003502 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 *
3504 * Return OK or FAIL.
3505 */
3506 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003507eval7(
3508 char_u **arg,
3509 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003510 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003513 int evaluate = evalarg != NULL
3514 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 int len;
3516 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003517 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 char_u *start_leader, *end_leader;
3519 int ret = OK;
3520 char_u *alias;
3521
3522 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003523 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003524 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003526 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527
3528 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003529 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 */
3531 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003532 if (eval_leader(arg, in_vim9script()) == FAIL)
3533 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 end_leader = *arg;
3535
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003536 if (**arg == '.' && (!isdigit(*(*arg + 1))
3537#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003538 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003539#endif
3540 ))
3541 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003542 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003543 ++*arg;
3544 return FAIL;
3545 }
3546
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 switch (**arg)
3548 {
3549 /*
3550 * Number constant.
3551 */
3552 case '0':
3553 case '1':
3554 case '2':
3555 case '3':
3556 case '4':
3557 case '5':
3558 case '6':
3559 case '7':
3560 case '8':
3561 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003562 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003563
3564 // Apply prefixed "-" and "+" now. Matters especially when
3565 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003566 if (ret == OK && evaluate && end_leader > start_leader
3567 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003568 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 break;
3570
3571 /*
3572 * String constant: "string".
3573 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003574 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 break;
3576
3577 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003578 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003580 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003581 break;
3582
3583 /*
3584 * List: [expr, expr]
3585 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003586 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 break;
3588
3589 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003590 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003591 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003592 case '#': if (in_vim9script())
3593 {
3594 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3595 }
3596 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003597 {
3598 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003599 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003600 }
3601 else
3602 ret = NOTDONE;
3603 break;
3604
3605 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003606 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003607 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003608 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003609 case '{': if (in_vim9script())
3610 ret = NOTDONE;
3611 else
3612 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003613 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003614 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003615 break;
3616
3617 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003618 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003620 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 break;
3622
3623 /*
3624 * Environment variable: $VAR.
3625 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003626 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 break;
3628
3629 /*
3630 * Register contents: @r.
3631 */
3632 case '@': ++*arg;
3633 if (evaluate)
3634 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003635 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3636 semsg(_(e_syntax_error_at_str), *arg);
3637 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3638 emsg_invreg(**arg);
3639 else
3640 {
3641 rettv->v_type = VAR_STRING;
3642 rettv->vval.v_string = get_reg_contents(**arg,
3643 GREG_EXPR_SRC);
3644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 }
3646 if (**arg != NUL)
3647 ++*arg;
3648 break;
3649
3650 /*
3651 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003652 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003654 case '(': ret = NOTDONE;
3655 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003656 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003657 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003658 if (ret == OK && evaluate)
3659 {
3660 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3661
Bram Moolenaara9931532021-06-12 15:58:16 +02003662 // Compile it here to get the return type. The return
3663 // type is optional, when it's missing use t_unknown.
3664 // This is recognized in compile_return().
3665 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3666 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003667 if (compile_def_function(ufunc,
Bram Moolenaare99d4222021-06-13 14:01:26 +02003668 FALSE, COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003669 {
3670 clear_tv(rettv);
3671 ret = FAIL;
3672 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003673 }
3674 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003675 if (ret == NOTDONE)
3676 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003677 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003678 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003679
Bram Moolenaar9215f012020-06-27 21:18:00 +02003680 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003681 if (**arg == ')')
3682 ++*arg;
3683 else if (ret == OK)
3684 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003685 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003686 clear_tv(rettv);
3687 ret = FAIL;
3688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 }
3690 break;
3691
Bram Moolenaar8c711452005-01-14 21:53:12 +00003692 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 break;
3694 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003695
3696 if (ret == NOTDONE)
3697 {
3698 /*
3699 * Must be a variable or function name.
3700 * Can also be a curly-braces kind of name: {expr}.
3701 */
3702 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003703 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003704 if (alias != NULL)
3705 s = alias;
3706
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003707 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003708 ret = FAIL;
3709 else
3710 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003711 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3712
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003713 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003714 {
3715 emsg(_(e_cannot_use_underscore_here));
3716 ret = FAIL;
3717 }
3718 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003719 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003720 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003721 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003722 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003723 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003724 else if (flags & EVAL_CONSTANT)
3725 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003726 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003727 {
3728 // get the value of "true", "false" or a variable
3729 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3730 {
3731 rettv->v_type = VAR_BOOL;
3732 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003733 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003734 }
3735 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003736 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003737 {
3738 rettv->v_type = VAR_BOOL;
3739 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003740 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003741 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003742 else if (len == 4 && in_vim9script()
3743 && STRNCMP(s, "null", 4) == 0)
3744 {
3745 rettv->v_type = VAR_SPECIAL;
3746 rettv->vval.v_number = VVAL_NULL;
3747 ret = OK;
3748 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003749 else
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003750 {
3751 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003752 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003753 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003754 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003755 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003756 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003757 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003758 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003759 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003760 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003761 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003762 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003763 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003764 }
3765
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003766 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3767 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003768 if (ret == OK)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003769 ret = handle_subscript(arg, name_start, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770
3771 /*
3772 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3773 */
3774 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003775 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003776 return ret;
3777}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003778
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003779/*
3780 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003781 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003782 * Adjusts "end_leaderp" until it is at "start_leader".
3783 */
3784 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003785eval7_leader(
3786 typval_T *rettv,
3787 int numeric_only,
3788 char_u *start_leader,
3789 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003790{
3791 char_u *end_leader = *end_leaderp;
3792 int ret = OK;
3793 int error = FALSE;
3794 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003795 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003796#ifdef FEAT_FLOAT
3797 float_T f = 0.0;
3798
3799 if (rettv->v_type == VAR_FLOAT)
3800 f = rettv->vval.v_float;
3801 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003802#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003803 {
3804 while (VIM_ISWHITE(end_leader[-1]))
3805 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003806 if (in_vim9script() && end_leader[-1] == '!')
3807 val = tv2bool(rettv);
3808 else
3809 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003810 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003811 if (error)
3812 {
3813 clear_tv(rettv);
3814 ret = FAIL;
3815 }
3816 else
3817 {
3818 while (end_leader > start_leader)
3819 {
3820 --end_leader;
3821 if (*end_leader == '!')
3822 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003823 if (numeric_only)
3824 {
3825 ++end_leader;
3826 break;
3827 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003828#ifdef FEAT_FLOAT
3829 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003830 {
3831 if (in_vim9script())
3832 {
3833 rettv->v_type = VAR_BOOL;
3834 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3835 }
3836 else
3837 f = !f;
3838 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003839 else
3840#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003841 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003842 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003843 type = VAR_BOOL;
3844 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003845 }
3846 else if (*end_leader == '-')
3847 {
3848#ifdef FEAT_FLOAT
3849 if (rettv->v_type == VAR_FLOAT)
3850 f = -f;
3851 else
3852#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003853 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003854 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003855 type = VAR_NUMBER;
3856 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003857 }
3858 }
3859#ifdef FEAT_FLOAT
3860 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003862 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003863 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003865 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003866#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003867 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003868 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003869 if (in_vim9script())
3870 rettv->v_type = type;
3871 else
3872 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003873 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003876 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 return ret;
3878}
3879
3880/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003881 * Call the function referred to in "rettv".
3882 */
3883 static int
3884call_func_rettv(
3885 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003886 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003887 typval_T *rettv,
3888 int evaluate,
3889 dict_T *selfdict,
3890 typval_T *basetv)
3891{
3892 partial_T *pt = NULL;
3893 funcexe_T funcexe;
3894 typval_T functv;
3895 char_u *s;
3896 int ret;
3897
3898 // need to copy the funcref so that we can clear rettv
3899 if (evaluate)
3900 {
3901 functv = *rettv;
3902 rettv->v_type = VAR_UNKNOWN;
3903
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003904 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003905 if (functv.v_type == VAR_PARTIAL)
3906 {
3907 pt = functv.vval.v_partial;
3908 s = partial_name(pt);
3909 }
3910 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003911 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003912 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003913 if (s == NULL || *s == NUL)
3914 {
3915 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02003916 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003917 goto theend;
3918 }
3919 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003920 }
3921 else
3922 s = (char_u *)"";
3923
Bram Moolenaara80faa82020-04-12 19:37:17 +02003924 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003925 funcexe.fe_firstline = curwin->w_cursor.lnum;
3926 funcexe.fe_lastline = curwin->w_cursor.lnum;
3927 funcexe.fe_evaluate = evaluate;
3928 funcexe.fe_partial = pt;
3929 funcexe.fe_selfdict = selfdict;
3930 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003931 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003932
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003933theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003934 // Clear the funcref afterwards, so that deleting it while
3935 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003936 if (evaluate)
3937 clear_tv(&functv);
3938
3939 return ret;
3940}
3941
3942/*
3943 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003944 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003945 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3946 */
3947 static int
3948eval_lambda(
3949 char_u **arg,
3950 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003951 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003952 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003953{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003954 int evaluate = evalarg != NULL
3955 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003956 typval_T base = *rettv;
3957 int ret;
3958
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003959 rettv->v_type = VAR_UNKNOWN;
3960
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003961 if (**arg == '{')
3962 {
3963 // ->{lambda}()
3964 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3965 }
3966 else
3967 {
3968 // ->(lambda)()
3969 ++*arg;
3970 ret = eval1(arg, rettv, evalarg);
3971 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar605ec912021-12-18 16:54:31 +00003972 if (**arg == ')')
3973 {
3974 ++*arg;
3975 }
3976 else
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003977 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003978 emsg(_(e_missing_closing_paren));
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003979 ret = FAIL;
3980 }
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003981 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003982 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003983 return FAIL;
3984 else if (**arg != '(')
3985 {
3986 if (verbose)
3987 {
3988 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00003989 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003990 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003991 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003992 }
3993 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003994 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003995 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003996 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003997 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003998
3999 // Clear the funcref afterwards, so that deleting it while
4000 // evaluating the arguments is possible (see test55).
4001 if (evaluate)
4002 clear_tv(&base);
4003
4004 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004005}
4006
4007/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004008 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004009 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004010 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4011 */
4012 static int
4013eval_method(
4014 char_u **arg,
4015 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004016 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004017 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004018{
4019 char_u *name;
4020 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004021 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004022 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004023 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004024 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004025 int evaluate = evalarg != NULL
4026 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004027
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004028 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004029
Bram Moolenaarac92e252019-08-03 21:58:38 +02004030 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004031 len = get_name_len(arg, &alias, evaluate, TRUE);
4032 if (alias != NULL)
4033 name = alias;
4034
4035 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004036 {
4037 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004038 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004039 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004040 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004041 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004042 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004043 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004044
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004045 // If there is no "(" immediately following, but there is further on,
4046 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4047 // Does not handle anything where "(" is part of the expression.
4048 *arg = skipwhite(*arg);
4049
4050 if (**arg != '(' && alias == NULL
4051 && (paren = vim_strchr(*arg, '(')) != NULL)
4052 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004053 char_u *deref;
4054
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004055 *arg = name;
4056 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004057 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4058 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004059 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004060 *arg = name + len;
4061 ret = FAIL;
4062 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004063 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004064 {
4065 name = deref;
Bram Moolenaar15d16352022-01-17 20:09:08 +00004066 len = STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004067 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004068 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004069 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004070
4071 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004072 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004073 *arg = skipwhite(*arg);
4074
4075 if (**arg != '(')
4076 {
4077 if (verbose)
4078 semsg(_(e_missing_parenthesis_str), name);
4079 ret = FAIL;
4080 }
4081 else if (VIM_ISWHITE((*arg)[-1]))
4082 {
4083 if (verbose)
4084 emsg(_(e_no_white_space_allowed_before_parenthesis));
4085 ret = FAIL;
4086 }
4087 else
4088 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004089 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004090 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004091 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004092
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004093 // Clear the funcref afterwards, so that deleting it while
4094 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004095 if (evaluate)
4096 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004097 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004098
Bram Moolenaarac92e252019-08-03 21:58:38 +02004099 return ret;
4100}
4101
4102/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004103 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4104 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004105 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4106 */
4107 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004108eval_index(
4109 char_u **arg,
4110 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004111 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004112 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004113{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004114 int evaluate = evalarg != NULL
4115 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004116 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004117 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004118 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004119 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004120 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004121 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004122
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004123 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4124 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004125
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004126 init_tv(&var1);
4127 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004128 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004129 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004130 /*
4131 * dict.name
4132 */
4133 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004134 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004135 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004136 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004137 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004138 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004139 }
4140 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004141 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004142 /*
4143 * something[idx]
4144 *
4145 * Get the (first) variable from inside the [].
4146 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004147 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004148 if (**arg == ':')
4149 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004150 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004151 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004152 else if (vim9 && **arg == ':')
4153 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004154 semsg(_(e_white_space_required_before_and_after_str_at_str),
4155 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004156 clear_tv(&var1);
4157 return FAIL;
4158 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004159 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004160 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004161 int error = FALSE;
4162
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004163#ifdef FEAT_FLOAT
4164 // allow for indexing with float
4165 if (vim9 && rettv->v_type == VAR_DICT
4166 && var1.v_type == VAR_FLOAT)
4167 {
4168 var1.vval.v_string = typval_tostring(&var1, TRUE);
4169 var1.v_type = VAR_STRING;
4170 }
4171#endif
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004172 if (vim9 && rettv->v_type == VAR_LIST)
4173 tv_get_number_chk(&var1, &error);
4174 else
4175 error = tv_get_string_chk(&var1) == NULL;
4176 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004177 {
4178 // not a number or string
4179 clear_tv(&var1);
4180 return FAIL;
4181 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004182 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004183
4184 /*
4185 * Get the second variable from inside the [:].
4186 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004187 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004188 if (**arg == ':')
4189 {
4190 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004191 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004192 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004193 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004194 semsg(_(e_white_space_required_before_and_after_str_at_str),
4195 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004196 if (!empty1)
4197 clear_tv(&var1);
4198 return FAIL;
4199 }
4200 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004201 if (**arg == ']')
4202 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004203 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004204 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004205 if (!empty1)
4206 clear_tv(&var1);
4207 return FAIL;
4208 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004209 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004210 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004211 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004212 if (!empty1)
4213 clear_tv(&var1);
4214 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004215 return FAIL;
4216 }
4217 }
4218
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004219 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004220 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004221 if (**arg != ']')
4222 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004223 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004224 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004225 clear_tv(&var1);
4226 if (range)
4227 clear_tv(&var2);
4228 return FAIL;
4229 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004230 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004231 }
4232
4233 if (evaluate)
4234 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004235 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004236 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004237 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004238
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004239 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004240 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004241 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004242 clear_tv(&var2);
4243 return res;
4244 }
4245 return OK;
4246}
4247
4248/*
4249 * Check if "rettv" can have an [index] or [sli:ce]
4250 */
4251 int
4252check_can_index(typval_T *rettv, int evaluate, int verbose)
4253{
4254 switch (rettv->v_type)
4255 {
4256 case VAR_FUNC:
4257 case VAR_PARTIAL:
4258 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004259 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004260 return FAIL;
4261 case VAR_FLOAT:
4262#ifdef FEAT_FLOAT
4263 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004264 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004265 return FAIL;
4266#endif
4267 case VAR_BOOL:
4268 case VAR_SPECIAL:
4269 case VAR_JOB:
4270 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004271 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004272 if (verbose)
4273 emsg(_(e_cannot_index_special_variable));
4274 return FAIL;
4275 case VAR_UNKNOWN:
4276 case VAR_ANY:
4277 case VAR_VOID:
4278 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004279 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004280 emsg(_(e_cannot_index_special_variable));
4281 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004282 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004283 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004284
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004285 case VAR_STRING:
4286 case VAR_LIST:
4287 case VAR_DICT:
4288 case VAR_BLOB:
4289 break;
4290 case VAR_NUMBER:
4291 if (in_vim9script())
4292 emsg(_(e_cannot_index_number));
4293 break;
4294 }
4295 return OK;
4296}
4297
4298/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004299 * slice() function
4300 */
4301 void
4302f_slice(typval_T *argvars, typval_T *rettv)
4303{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004304 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004305 && ((argvars[0].v_type != VAR_STRING
4306 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004307 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004308 && check_for_list_arg(argvars, 0) == FAIL)
4309 || check_for_number_arg(argvars, 1) == FAIL
4310 || check_for_opt_number_arg(argvars, 2) == FAIL))
4311 return;
4312
Bram Moolenaar6601b622021-01-13 21:47:15 +01004313 if (check_can_index(argvars, TRUE, FALSE) == OK)
4314 {
4315 copy_tv(argvars, rettv);
4316 eval_index_inner(rettv, TRUE, argvars + 1,
4317 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4318 TRUE, NULL, 0, FALSE);
4319 }
4320}
4321
4322/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004323 * Apply index or range to "rettv".
4324 * "var1" is the first index, NULL for [:expr].
4325 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004326 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4327 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004328 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4329 */
4330 int
4331eval_index_inner(
4332 typval_T *rettv,
4333 int is_range,
4334 typval_T *var1,
4335 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004336 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004337 char_u *key,
4338 int keylen,
4339 int verbose)
4340{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004341 varnumber_T n1, n2 = 0;
4342 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004343
4344 n1 = 0;
4345 if (var1 != NULL && rettv->v_type != VAR_DICT)
4346 n1 = tv_get_number(var1);
4347
4348 if (is_range)
4349 {
4350 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004351 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004352 if (verbose)
4353 emsg(_(e_cannot_slice_dictionary));
4354 return FAIL;
4355 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004356 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004357 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004358 else
4359 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004360 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004361
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004362 switch (rettv->v_type)
4363 {
4364 case VAR_UNKNOWN:
4365 case VAR_ANY:
4366 case VAR_VOID:
4367 case VAR_FUNC:
4368 case VAR_PARTIAL:
4369 case VAR_FLOAT:
4370 case VAR_BOOL:
4371 case VAR_SPECIAL:
4372 case VAR_JOB:
4373 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004374 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004375 break; // not evaluating, skipping over subscript
4376
4377 case VAR_NUMBER:
4378 case VAR_STRING:
4379 {
4380 char_u *s = tv_get_string(rettv);
4381
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004382 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004383 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004384 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004385 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004386 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004387 else
4388 s = char_from_string(s, n1);
4389 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004390 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004391 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004392 // The resulting variable is a substring. If the indexes
4393 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004394 if (n1 < 0)
4395 {
4396 n1 = len + n1;
4397 if (n1 < 0)
4398 n1 = 0;
4399 }
4400 if (n2 < 0)
4401 n2 = len + n2;
4402 else if (n2 >= len)
4403 n2 = len;
4404 if (n1 >= len || n2 < 0 || n1 > n2)
4405 s = NULL;
4406 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004407 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004408 }
4409 else
4410 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004411 // The resulting variable is a string of a single
4412 // character. If the index is too big or negative the
4413 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004414 if (n1 >= len || n1 < 0)
4415 s = NULL;
4416 else
4417 s = vim_strnsave(s + n1, 1);
4418 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004419 clear_tv(rettv);
4420 rettv->v_type = VAR_STRING;
4421 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004422 }
4423 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004424
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004425 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004426 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4427 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004428 break;
4429
4430 case VAR_LIST:
4431 if (var1 == NULL)
4432 n1 = 0;
4433 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004434 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004435 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004436 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004437 return FAIL;
4438 break;
4439
4440 case VAR_DICT:
4441 {
4442 dictitem_T *item;
4443 typval_T tmp;
4444
4445 if (key == NULL)
4446 {
4447 key = tv_get_string_chk(var1);
4448 if (key == NULL)
4449 return FAIL;
4450 }
4451
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004452 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004453
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004454 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004455 {
4456 if (verbose)
4457 {
4458 if (keylen > 0)
4459 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004460 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004461 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004462 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004463 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004464
4465 copy_tv(&item->di_tv, &tmp);
4466 clear_tv(rettv);
4467 *rettv = tmp;
4468 }
4469 break;
4470 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004471 return OK;
4472}
4473
4474/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004475 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004476 */
4477 char_u *
4478partial_name(partial_T *pt)
4479{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004480 if (pt != NULL)
4481 {
4482 if (pt->pt_name != NULL)
4483 return pt->pt_name;
4484 if (pt->pt_func != NULL)
4485 return pt->pt_func->uf_name;
4486 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004487 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004488}
4489
Bram Moolenaarddecc252016-04-06 22:59:37 +02004490 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004491partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004492{
4493 int i;
4494
4495 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004496 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004497 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004498 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004499 if (pt->pt_name != NULL)
4500 {
4501 func_unref(pt->pt_name);
4502 vim_free(pt->pt_name);
4503 }
4504 else
4505 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004506
Bram Moolenaar54656012021-06-09 20:50:46 +02004507 // "out_up" is no longer used, decrement refcount on partial that owns it.
4508 partial_unref(pt->pt_outer.out_up_partial);
4509
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004510 // Decrease the reference count for the context of a closure. If down
4511 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004512 if (pt->pt_funcstack != NULL)
4513 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004514 --pt->pt_funcstack->fs_refcount;
4515 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004516 }
4517
Bram Moolenaarddecc252016-04-06 22:59:37 +02004518 vim_free(pt);
4519}
4520
4521/*
4522 * Unreference a closure: decrement the reference count and free it when it
4523 * becomes zero.
4524 */
4525 void
4526partial_unref(partial_T *pt)
4527{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004528 if (pt != NULL)
4529 {
4530 if (--pt->pt_refcount <= 0)
4531 partial_free(pt);
4532
4533 // If the reference count goes down to one, the funcstack may be the
4534 // only reference and can be freed if no other partials reference it.
4535 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4536 funcstack_check_refcount(pt->pt_funcstack);
4537 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004538}
4539
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004540/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004541 * Return the next (unique) copy ID.
4542 * Used for serializing nested structures.
4543 */
4544 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004545get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004546{
4547 current_copyID += COPYID_INC;
4548 return current_copyID;
4549}
4550
4551/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004552 * Garbage collection for lists and dictionaries.
4553 *
4554 * We use reference counts to be able to free most items right away when they
4555 * are no longer used. But for composite items it's possible that it becomes
4556 * unused while the reference count is > 0: When there is a recursive
4557 * reference. Example:
4558 * :let l = [1, 2, 3]
4559 * :let d = {9: l}
4560 * :let l[1] = d
4561 *
4562 * Since this is quite unusual we handle this with garbage collection: every
4563 * once in a while find out which lists and dicts are not referenced from any
4564 * variable.
4565 *
4566 * Here is a good reference text about garbage collection (refers to Python
4567 * but it applies to all reference-counting mechanisms):
4568 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004569 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004570
4571/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004572 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004573 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004574 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004575 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004576 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004577garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004578{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004579 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004580 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004581 buf_T *buf;
4582 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004583 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004584 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004585
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004586 if (!testing)
4587 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004588 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004589 want_garbage_collect = FALSE;
4590 may_garbage_collect = FALSE;
4591 garbage_collect_at_exit = FALSE;
4592 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004593
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004594 // The execution stack can grow big, limit the size.
4595 if (exestack.ga_maxlen - exestack.ga_len > 500)
4596 {
4597 size_t new_len;
4598 char_u *pp;
4599 int n;
4600
4601 // Keep 150% of the current size, with a minimum of the growth size.
4602 n = exestack.ga_len / 2;
4603 if (n < exestack.ga_growsize)
4604 n = exestack.ga_growsize;
4605
4606 // Don't make it bigger though.
4607 if (exestack.ga_len + n < exestack.ga_maxlen)
4608 {
4609 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4610 pp = vim_realloc(exestack.ga_data, new_len);
4611 if (pp == NULL)
4612 return FAIL;
4613 exestack.ga_maxlen = exestack.ga_len + n;
4614 exestack.ga_data = pp;
4615 }
4616 }
4617
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004618 // We advance by two because we add one for items referenced through
4619 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004620 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004621
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004622 /*
4623 * 1. Go through all accessible variables and mark all lists and dicts
4624 * with copyID.
4625 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004626
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004627 // Don't free variables in the previous_funccal list unless they are only
4628 // referenced through previous_funccal. This must be first, because if
4629 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004630 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004631
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004632 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004633 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004634
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004635 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004636 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004637 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4638 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004639
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004640 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004641 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004642 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4643 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004644 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004645 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4646 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004647#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004648 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004649 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4650 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004651 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004652 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004653 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4654 NULL, NULL);
4655#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004656
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004657 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004658 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004659 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4660 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004661 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004662 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004663
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004664 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004665 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004666
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004667 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004668 abort = abort || set_ref_in_functions(copyID);
4669
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004670 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004671 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004672
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004673 // funcstacks keep variables for closures
4674 abort = abort || set_ref_in_funcstacks(copyID);
4675
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004676 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004677 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004678
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004679 // callbacks in buffers
4680 abort = abort || set_ref_in_buffers(copyID);
4681
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004682 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
4683 abort = abort || set_ref_in_insexpand_funcs(copyID);
4684
4685 // 'operatorfunc' callback
4686 abort = abort || set_ref_in_opfunc(copyID);
4687
4688 // 'tagfunc' callback
4689 abort = abort || set_ref_in_tagfunc(copyID);
4690
4691 // 'imactivatefunc' and 'imstatusfunc' callbacks
4692 abort = abort || set_ref_in_im_funcs(copyID);
4693
Bram Moolenaar1dced572012-04-05 16:54:08 +02004694#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004695 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004696#endif
4697
Bram Moolenaardb913952012-06-29 12:54:53 +02004698#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004699 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004700#endif
4701
4702#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004703 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004704#endif
4705
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004706#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004707 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004708 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004709#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004710#ifdef FEAT_NETBEANS_INTG
4711 abort = abort || set_ref_in_nb_channel(copyID);
4712#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004713
Bram Moolenaare3188e22016-05-31 21:13:04 +02004714#ifdef FEAT_TIMERS
4715 abort = abort || set_ref_in_timer(copyID);
4716#endif
4717
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004718#ifdef FEAT_QUICKFIX
4719 abort = abort || set_ref_in_quickfix(copyID);
4720#endif
4721
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004722#ifdef FEAT_TERMINAL
4723 abort = abort || set_ref_in_term(copyID);
4724#endif
4725
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004726#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004727 abort = abort || set_ref_in_popups(copyID);
4728#endif
4729
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004730 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004731 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004732 /*
4733 * 2. Free lists and dictionaries that are not referenced.
4734 */
4735 did_free = free_unref_items(copyID);
4736
4737 /*
4738 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004739 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004740 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004741 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004742 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004743 else if (p_verbose > 0)
4744 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004745 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004746 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004747
4748 return did_free;
4749}
4750
4751/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004752 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004753 */
4754 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004755free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004756{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004757 int did_free = FALSE;
4758
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004759 // Let all "free" functions know that we are here. This means no
4760 // dictionaries, lists, channels or jobs are to be freed, because we will
4761 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004762 in_free_unref_items = TRUE;
4763
4764 /*
4765 * PASS 1: free the contents of the items. We don't free the items
4766 * themselves yet, so that it is possible to decrement refcount counters
4767 */
4768
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004769 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004770 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004771
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004772 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004773 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004774
4775#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004776 // Go through the list of jobs and free items without the copyID. This
4777 // must happen before doing channels, because jobs refer to channels, but
4778 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004779 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4780
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004781 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004782 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4783#endif
4784
4785 /*
4786 * PASS 2: free the items themselves.
4787 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004788 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004789 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004790
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004791#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004792 // Go through the list of jobs and free items without the copyID. This
4793 // must happen before doing channels, because jobs refer to channels, but
4794 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004795 free_unused_jobs(copyID, COPYID_MASK);
4796
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004797 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004798 free_unused_channels(copyID, COPYID_MASK);
4799#endif
4800
4801 in_free_unref_items = FALSE;
4802
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004803 return did_free;
4804}
4805
4806/*
4807 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004808 * "list_stack" is used to add lists to be marked. Can be NULL.
4809 *
4810 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004811 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004812 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004813set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004814{
4815 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004816 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004817 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004818 hashtab_T *cur_ht;
4819 ht_stack_T *ht_stack = NULL;
4820 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004821
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004822 cur_ht = ht;
4823 for (;;)
4824 {
4825 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004826 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004827 // Mark each item in the hashtab. If the item contains a hashtab
4828 // it is added to ht_stack, if it contains a list it is added to
4829 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004830 todo = (int)cur_ht->ht_used;
4831 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4832 if (!HASHITEM_EMPTY(hi))
4833 {
4834 --todo;
4835 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4836 &ht_stack, list_stack);
4837 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004838 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004839
4840 if (ht_stack == NULL)
4841 break;
4842
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004843 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004844 cur_ht = ht_stack->ht;
4845 tempitem = ht_stack;
4846 ht_stack = ht_stack->prev;
4847 free(tempitem);
4848 }
4849
4850 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004851}
4852
Dominique Pelle748b3082022-01-08 12:41:16 +00004853#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
4854 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004855/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004856 * Mark a dict and its items with "copyID".
4857 * Returns TRUE if setting references failed somehow.
4858 */
4859 int
4860set_ref_in_dict(dict_T *d, int copyID)
4861{
4862 if (d != NULL && d->dv_copyID != copyID)
4863 {
4864 d->dv_copyID = copyID;
4865 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4866 }
4867 return FALSE;
4868}
Dominique Pelle748b3082022-01-08 12:41:16 +00004869#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004870
4871/*
4872 * Mark a list and its items with "copyID".
4873 * Returns TRUE if setting references failed somehow.
4874 */
4875 int
4876set_ref_in_list(list_T *ll, int copyID)
4877{
4878 if (ll != NULL && ll->lv_copyID != copyID)
4879 {
4880 ll->lv_copyID = copyID;
4881 return set_ref_in_list_items(ll, copyID, NULL);
4882 }
4883 return FALSE;
4884}
4885
4886/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004887 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004888 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4889 *
4890 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004891 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004892 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004893set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004894{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004895 listitem_T *li;
4896 int abort = FALSE;
4897 list_T *cur_l;
4898 list_stack_T *list_stack = NULL;
4899 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004900
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004901 cur_l = l;
4902 for (;;)
4903 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004904 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004905 // Mark each item in the list. If the item contains a hashtab
4906 // it is added to ht_stack, if it contains a list it is added to
4907 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004908 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4909 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4910 ht_stack, &list_stack);
4911 if (list_stack == NULL)
4912 break;
4913
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004914 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004915 cur_l = list_stack->list;
4916 tempitem = list_stack;
4917 list_stack = list_stack->prev;
4918 free(tempitem);
4919 }
4920
4921 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004922}
4923
4924/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004925 * Mark the partial in callback 'cb' with "copyID".
4926 */
4927 int
4928set_ref_in_callback(callback_T *cb, int copyID)
4929{
4930 typval_T tv;
4931
4932 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
4933 return FALSE;
4934
4935 tv.v_type = VAR_PARTIAL;
4936 tv.vval.v_partial = cb->cb_partial;
4937 return set_ref_in_item(&tv, copyID, NULL, NULL);
4938}
4939
4940/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004941 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004942 * "list_stack" is used to add lists to be marked. Can be NULL.
4943 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4944 *
4945 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004946 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004947 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004948set_ref_in_item(
4949 typval_T *tv,
4950 int copyID,
4951 ht_stack_T **ht_stack,
4952 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004953{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004954 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004955
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004956 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004957 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004958 dict_T *dd = tv->vval.v_dict;
4959
Bram Moolenaara03f2332016-02-06 18:09:59 +01004960 if (dd != NULL && dd->dv_copyID != copyID)
4961 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004962 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004963 dd->dv_copyID = copyID;
4964 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004965 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004966 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4967 }
4968 else
4969 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004970 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4971
Bram Moolenaara03f2332016-02-06 18:09:59 +01004972 if (newitem == NULL)
4973 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004974 else
4975 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004976 newitem->ht = &dd->dv_hashtab;
4977 newitem->prev = *ht_stack;
4978 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004979 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004980 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004981 }
4982 }
4983 else if (tv->v_type == VAR_LIST)
4984 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004985 list_T *ll = tv->vval.v_list;
4986
Bram Moolenaara03f2332016-02-06 18:09:59 +01004987 if (ll != NULL && ll->lv_copyID != copyID)
4988 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004989 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004990 ll->lv_copyID = copyID;
4991 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004992 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004993 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004994 }
4995 else
4996 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004997 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4998
Bram Moolenaara03f2332016-02-06 18:09:59 +01004999 if (newitem == NULL)
5000 abort = TRUE;
5001 else
5002 {
5003 newitem->list = ll;
5004 newitem->prev = *list_stack;
5005 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005006 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005007 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005008 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005009 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005010 else if (tv->v_type == VAR_FUNC)
5011 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005012 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005013 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005014 else if (tv->v_type == VAR_PARTIAL)
5015 {
5016 partial_T *pt = tv->vval.v_partial;
5017 int i;
5018
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005019 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005020 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005021 // Didn't see this partial yet.
5022 pt->pt_copyID = copyID;
5023
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005024 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005025
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005026 if (pt->pt_dict != NULL)
5027 {
5028 typval_T dtv;
5029
5030 dtv.v_type = VAR_DICT;
5031 dtv.vval.v_dict = pt->pt_dict;
5032 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5033 }
5034
5035 for (i = 0; i < pt->pt_argc; ++i)
5036 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5037 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005038 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005039 }
5040 }
5041#ifdef FEAT_JOB_CHANNEL
5042 else if (tv->v_type == VAR_JOB)
5043 {
5044 job_T *job = tv->vval.v_job;
5045 typval_T dtv;
5046
5047 if (job != NULL && job->jv_copyID != copyID)
5048 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005049 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005050 if (job->jv_channel != NULL)
5051 {
5052 dtv.v_type = VAR_CHANNEL;
5053 dtv.vval.v_channel = job->jv_channel;
5054 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5055 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005056 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005057 {
5058 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005059 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005060 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5061 }
5062 }
5063 }
5064 else if (tv->v_type == VAR_CHANNEL)
5065 {
5066 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005067 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005068 typval_T dtv;
5069 jsonq_T *jq;
5070 cbq_T *cq;
5071
5072 if (ch != NULL && ch->ch_copyID != copyID)
5073 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005074 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005075 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005076 {
5077 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5078 jq = jq->jq_next)
5079 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5080 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5081 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005082 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005083 {
5084 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005085 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005086 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5087 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005088 if (ch->ch_part[part].ch_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 =
5092 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005093 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5094 }
5095 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005096 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005097 {
5098 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005099 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005100 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5101 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005102 if (ch->ch_close_cb.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_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005106 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5107 }
5108 }
5109 }
5110#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005111 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005112}
5113
Bram Moolenaar8c711452005-01-14 21:53:12 +00005114/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005115 * Return a string with the string representation of a variable.
5116 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005117 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005118 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005119 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005120 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005121 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005122 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5123 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005124 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005125 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005126 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005127echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005128 typval_T *tv,
5129 char_u **tofree,
5130 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005131 int copyID,
5132 int echo_style,
5133 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005134 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005135{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005136 static int recurse = 0;
5137 char_u *r = NULL;
5138
Bram Moolenaar33570922005-01-25 22:26:29 +00005139 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005140 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005141 if (!did_echo_string_emsg)
5142 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005143 // Only give this message once for a recursive call to avoid
5144 // flooding the user with errors. And stop iterating over lists
5145 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005146 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005147 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005148 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005149 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005150 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005151 }
5152 ++recurse;
5153
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005154 switch (tv->v_type)
5155 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005156 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005157 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005158 {
5159 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005160 r = tv->vval.v_string;
5161 if (r == NULL)
5162 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005163 }
5164 else
5165 {
5166 *tofree = string_quote(tv->vval.v_string, FALSE);
5167 r = *tofree;
5168 }
5169 break;
5170
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005171 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005172 if (echo_style)
5173 {
5174 *tofree = NULL;
5175 r = tv->vval.v_string;
5176 }
5177 else
5178 {
5179 *tofree = string_quote(tv->vval.v_string, TRUE);
5180 r = *tofree;
5181 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005182 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005183
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005184 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005185 {
5186 partial_T *pt = tv->vval.v_partial;
5187 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005188 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005189 garray_T ga;
5190 int i;
5191 char_u *tf;
5192
5193 ga_init2(&ga, 1, 100);
5194 ga_concat(&ga, (char_u *)"function(");
5195 if (fname != NULL)
5196 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005197 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005198 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005199 && vim_isupper(fname[1]))
5200 {
5201 ga_concat(&ga, (char_u *)"'g:");
5202 ga_concat(&ga, fname + 1);
5203 }
5204 else
5205 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005206 vim_free(fname);
5207 }
5208 if (pt != NULL && pt->pt_argc > 0)
5209 {
5210 ga_concat(&ga, (char_u *)", [");
5211 for (i = 0; i < pt->pt_argc; ++i)
5212 {
5213 if (i > 0)
5214 ga_concat(&ga, (char_u *)", ");
5215 ga_concat(&ga,
5216 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5217 vim_free(tf);
5218 }
5219 ga_concat(&ga, (char_u *)"]");
5220 }
5221 if (pt != NULL && pt->pt_dict != NULL)
5222 {
5223 typval_T dtv;
5224
5225 ga_concat(&ga, (char_u *)", ");
5226 dtv.v_type = VAR_DICT;
5227 dtv.vval.v_dict = pt->pt_dict;
5228 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5229 vim_free(tf);
5230 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005231 // terminate with ')' and a NUL
5232 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005233
5234 *tofree = ga.ga_data;
5235 r = *tofree;
5236 break;
5237 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005238
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005239 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005240 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005241 break;
5242
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005243 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005244 if (tv->vval.v_list == NULL)
5245 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005246 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005247 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005248 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005249 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005250 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5251 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005252 {
5253 *tofree = NULL;
5254 r = (char_u *)"[...]";
5255 }
5256 else
5257 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005258 int old_copyID = tv->vval.v_list->lv_copyID;
5259
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005260 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005261 *tofree = list2string(tv, copyID, restore_copyID);
5262 if (restore_copyID)
5263 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005264 r = *tofree;
5265 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005266 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005267
Bram Moolenaar8c711452005-01-14 21:53:12 +00005268 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005269 if (tv->vval.v_dict == NULL)
5270 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005271 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005272 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005273 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005274 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005275 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5276 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005277 {
5278 *tofree = NULL;
5279 r = (char_u *)"{...}";
5280 }
5281 else
5282 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005283 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005284
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005285 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005286 *tofree = dict2string(tv, copyID, restore_copyID);
5287 if (restore_copyID)
5288 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005289 r = *tofree;
5290 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005291 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005292
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005293 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005294 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005295 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005296 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005297 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005298 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005299 break;
5300
Bram Moolenaar835dc632016-02-07 14:27:38 +01005301 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005302 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005303#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005304 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005305 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5306 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005307 if (composite_val)
5308 {
5309 *tofree = string_quote(r, FALSE);
5310 r = *tofree;
5311 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005312#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005313 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005314
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005315 case VAR_INSTR:
5316 *tofree = NULL;
5317 r = (char_u *)"instructions";
5318 break;
5319
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005320 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005321#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005322 *tofree = NULL;
5323 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5324 r = numbuf;
5325 break;
5326#endif
5327
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005328 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005329 case VAR_SPECIAL:
5330 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005331 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005332 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005333 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005334
Bram Moolenaar8502c702014-06-17 12:51:16 +02005335 if (--recurse == 0)
5336 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005337 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005338}
5339
5340/*
5341 * Return a string with the string representation of a variable.
5342 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5343 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005344 * Does not put quotes around strings, as ":echo" displays values.
5345 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5346 * May return NULL.
5347 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005348 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005349echo_string(
5350 typval_T *tv,
5351 char_u **tofree,
5352 char_u *numbuf,
5353 int copyID)
5354{
5355 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5356}
5357
5358/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005359 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5360 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005361 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005362 */
5363 int
5364buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5365{
5366 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005367 char_u *t;
5368 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005369
5370 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5371 return -1;
5372
5373 if (lnum > buf->b_ml.ml_line_count)
5374 lnum = buf->b_ml.ml_line_count;
5375
5376 str = ml_get_buf(buf, lnum, FALSE);
5377 if (str == NULL)
5378 return -1;
5379
5380 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005381 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005382
Bram Moolenaar91458462021-01-13 20:08:38 +01005383 // count the number of characters
5384 t = str;
5385 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5386 t += mb_ptr2len(t);
5387
5388 // In insert mode, when the cursor is at the end of a non-empty line,
5389 // byteidx points to the NUL character immediately past the end of the
5390 // string. In this case, add one to the character count.
5391 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5392 count++;
5393
5394 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005395}
5396
5397/*
5398 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005399 * byte index. Works only for loaded buffers. Returns -1 on failure.
5400 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005401 */
5402 int
5403buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5404{
5405 char_u *str;
5406 char_u *t;
5407
5408 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5409 return -1;
5410
5411 if (lnum > buf->b_ml.ml_line_count)
5412 lnum = buf->b_ml.ml_line_count;
5413
5414 str = ml_get_buf(buf, lnum, FALSE);
5415 if (str == NULL)
5416 return -1;
5417
5418 // Convert the character offset to a byte offset
5419 t = str;
5420 while (*t != NUL && --charidx > 0)
5421 t += mb_ptr2len(t);
5422
Bram Moolenaar91458462021-01-13 20:08:38 +01005423 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005424}
5425
5426/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005428 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005430 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005431var2fpos(
5432 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005433 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005434 int *fnum, // set to fnum for '0, 'A, etc.
5435 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005437 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005439 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005441 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005442 if (varp->v_type == VAR_LIST)
5443 {
5444 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005445 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005446 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005447 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005448
5449 l = varp->vval.v_list;
5450 if (l == NULL)
5451 return NULL;
5452
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005453 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005454 pos.lnum = list_find_nr(l, 0L, &error);
5455 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005456 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005457 if (charcol)
5458 len = (long)mb_charlen(ml_get(pos.lnum));
5459 else
5460 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005461
Bram Moolenaarec65d772020-08-20 22:29:12 +02005462 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005463 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005464 li = list_find(l, 1L);
5465 if (li != NULL && li->li_tv.v_type == VAR_STRING
5466 && li->li_tv.vval.v_string != NULL
5467 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005468 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005469 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005470 }
5471 else
5472 {
5473 pos.col = list_find_nr(l, 1L, &error);
5474 if (error)
5475 return NULL;
5476 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005477
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005478 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005479 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005480 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005481 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005482
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005483 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005484 pos.coladd = list_find_nr(l, 2L, &error);
5485 if (error)
5486 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005487
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005488 return &pos;
5489 }
5490
Bram Moolenaarc5809432021-03-27 21:23:30 +01005491 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5492 return NULL;
5493
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005494 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005495 if (name == NULL)
5496 return NULL;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005497 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005498 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005499 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005500 pos = curwin->w_cursor;
5501 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005502 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005503 return &pos;
5504 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005505 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005506 {
5507 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005508 pos = VIsual;
5509 else
5510 pos = curwin->w_cursor;
5511 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005512 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005513 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005514 }
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005515 if (name[0] == '\'' && (!in_vim9script()
5516 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005518 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005519 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5521 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005522 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005523 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 return pp;
5525 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005526
Bram Moolenaara5525202006-03-02 22:52:09 +00005527 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005528
Bram Moolenaar477933c2007-07-17 14:32:23 +00005529 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005530 {
5531 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005532 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005533 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005534 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005535 // In silent Ex mode topline is zero, but that's not a valid line
5536 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005537 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005538 return &pos;
5539 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005540 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005541 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005542 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005543 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005544 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005545 return &pos;
5546 }
5547 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005548 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005550 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 {
5552 pos.lnum = curbuf->b_ml.ml_line_count;
5553 pos.col = 0;
5554 }
5555 else
5556 {
5557 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005558 if (charcol)
5559 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5560 else
5561 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 }
5563 return &pos;
5564 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005565 if (in_vim9script())
5566 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 return NULL;
5568}
5569
5570/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005571 * Convert list in "arg" into a position and optional file number.
5572 * When "fnump" is NULL there is no file number, only 3 items.
5573 * Note that the column is passed on as-is, the caller may want to decrement
5574 * it to use 1 for the first column.
5575 * Return FAIL when conversion is not possible, doesn't check the position for
5576 * validity.
5577 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005578 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005579list2fpos(
5580 typval_T *arg,
5581 pos_T *posp,
5582 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005583 colnr_T *curswantp,
5584 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005585{
5586 list_T *l = arg->vval.v_list;
5587 long i = 0;
5588 long n;
5589
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005590 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5591 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005592 if (arg->v_type != VAR_LIST
5593 || l == NULL
5594 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005595 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005596 return FAIL;
5597
5598 if (fnump != NULL)
5599 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005600 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005601 if (n < 0)
5602 return FAIL;
5603 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005604 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005605 *fnump = n;
5606 }
5607
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005608 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005609 if (n < 0)
5610 return FAIL;
5611 posp->lnum = n;
5612
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005613 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005614 if (n < 0)
5615 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005616 // If character position is specified, then convert to byte position
5617 if (charcol)
5618 {
5619 buf_T *buf;
5620
5621 // Get the text for the specified line in a loaded buffer
5622 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5623 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5624 return FAIL;
5625
Bram Moolenaar91458462021-01-13 20:08:38 +01005626 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005627 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005628 posp->col = n;
5629
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005630 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005631 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005632 posp->coladd = 0;
5633 else
5634 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005635
Bram Moolenaar493c1782014-05-28 14:34:46 +02005636 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005637 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005638
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005639 return OK;
5640}
5641
5642/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 * Get the length of an environment variable name.
5644 * Advance "arg" to the first character after the name.
5645 * Return 0 for error.
5646 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005647 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005648get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649{
5650 char_u *p;
5651 int len;
5652
5653 for (p = *arg; vim_isIDc(*p); ++p)
5654 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005655 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 return 0;
5657
5658 len = (int)(p - *arg);
5659 *arg = p;
5660 return len;
5661}
5662
5663/*
5664 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005665 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 * Return 0 if something is wrong.
5667 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005668 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005669get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670{
5671 char_u *p;
5672 int len;
5673
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005674 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005676 {
5677 if (*p == ':')
5678 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005679 // "s:" is start of "s:var", but "n:" is not and can be used in
5680 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005681 len = (int)(p - *arg);
5682 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5683 || len > 1)
5684 break;
5685 }
5686 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005687 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 return 0;
5689
5690 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005691 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692
5693 return len;
5694}
5695
5696/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005697 * Get the length of the name of a variable or function.
5698 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005700 * Return -1 if curly braces expansion failed.
5701 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 * If the name contains 'magic' {}'s, expand them and return the
5703 * expanded name in an allocated string via 'alias' - caller must free.
5704 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005705 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005706get_name_len(
5707 char_u **arg,
5708 char_u **alias,
5709 int evaluate,
5710 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711{
5712 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 char_u *p;
5714 char_u *expr_start;
5715 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005717 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718
5719 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5720 && (*arg)[2] == (int)KE_SNR)
5721 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005722 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 *arg += 3;
5724 return get_id_len(arg) + 3;
5725 }
5726 len = eval_fname_script(*arg);
5727 if (len > 0)
5728 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005729 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 *arg += len;
5731 }
5732
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005734 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005736 p = find_name_end(*arg, &expr_start, &expr_end,
5737 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 if (expr_start != NULL)
5739 {
5740 char_u *temp_string;
5741
5742 if (!evaluate)
5743 {
5744 len += (int)(p - *arg);
5745 *arg = skipwhite(p);
5746 return len;
5747 }
5748
5749 /*
5750 * Include any <SID> etc in the expanded string:
5751 * Thus the -len here.
5752 */
5753 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5754 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005755 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 *alias = temp_string;
5757 *arg = skipwhite(p);
5758 return (int)STRLEN(temp_string);
5759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760
5761 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005762 // Only give an error when there is something, otherwise it will be
5763 // reported at a higher level.
5764 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005765 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766
5767 return len;
5768}
5769
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005770/*
5771 * Find the end of a variable or function name, taking care of magic braces.
5772 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5773 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005774 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005775 * Return a pointer to just after the name. Equal to "arg" if there is no
5776 * valid name.
5777 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005778 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005779find_name_end(
5780 char_u *arg,
5781 char_u **expr_start,
5782 char_u **expr_end,
5783 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005785 int mb_nest = 0;
5786 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005788 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005789 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005791 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005793 *expr_start = NULL;
5794 *expr_end = NULL;
5795 }
5796
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005797 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005798 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5799 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005800 return arg;
5801
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005802 for (p = arg; *p != NUL
5803 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005804 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005805 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005806 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005807 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005808 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005809 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005810 if (*p == '\'')
5811 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005812 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005813 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005814 ;
5815 if (*p == NUL)
5816 break;
5817 }
5818 else if (*p == '"')
5819 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005820 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005821 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005822 if (*p == '\\' && p[1] != NUL)
5823 ++p;
5824 if (*p == NUL)
5825 break;
5826 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005827 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5828 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005829 // "s:" is start of "s:var", but "n:" is not and can be used in
5830 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005831 len = (int)(p - arg);
5832 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005833 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005834 break;
5835 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005836
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005837 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005839 if (*p == '[')
5840 ++br_nest;
5841 else if (*p == ']')
5842 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005844
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005845 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005847 if (*p == '{')
5848 {
5849 mb_nest++;
5850 if (expr_start != NULL && *expr_start == NULL)
5851 *expr_start = p;
5852 }
5853 else if (*p == '}')
5854 {
5855 mb_nest--;
5856 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5857 *expr_end = p;
5858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 }
5861
5862 return p;
5863}
5864
5865/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005866 * Expands out the 'magic' {}'s in a variable/function name.
5867 * Note that this can call itself recursively, to deal with
5868 * constructs like foo{bar}{baz}{bam}
5869 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5870 * "in_start" ^
5871 * "expr_start" ^
5872 * "expr_end" ^
5873 * "in_end" ^
5874 *
5875 * Returns a new allocated string, which the caller must free.
5876 * Returns NULL for failure.
5877 */
5878 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005879make_expanded_name(
5880 char_u *in_start,
5881 char_u *expr_start,
5882 char_u *expr_end,
5883 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005884{
5885 char_u c1;
5886 char_u *retval = NULL;
5887 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005888
5889 if (expr_end == NULL || in_end == NULL)
5890 return NULL;
5891 *expr_start = NUL;
5892 *expr_end = NUL;
5893 c1 = *in_end;
5894 *in_end = NUL;
5895
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005896 temp_result = eval_to_string(expr_start + 1, FALSE);
5897 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005898 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005899 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5900 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005901 if (retval != NULL)
5902 {
5903 STRCPY(retval, in_start);
5904 STRCAT(retval, temp_result);
5905 STRCAT(retval, expr_end + 1);
5906 }
5907 }
5908 vim_free(temp_result);
5909
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005910 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005911 *expr_start = '{';
5912 *expr_end = '}';
5913
5914 if (retval != NULL)
5915 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005916 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005917 if (expr_start != NULL)
5918 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005919 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005920 temp_result = make_expanded_name(retval, expr_start,
5921 expr_end, temp_result);
5922 vim_free(retval);
5923 retval = temp_result;
5924 }
5925 }
5926
5927 return retval;
5928}
5929
5930/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005932 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005934 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005935eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005937 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005938}
5939
5940/*
5941 * Return TRUE if character "c" can be used as the first character in a
5942 * variable or function name (excluding '{' and '}').
5943 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005944 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005945eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005946{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005947 return ASCII_ISALPHA(c) || c == '_';
5948}
5949
5950/*
5951 * Return TRUE if character "c" can be used as the first character of a
5952 * dictionary key.
5953 */
5954 int
5955eval_isdictc(int c)
5956{
5957 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958}
5959
5960/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005961 * Handle:
5962 * - expr[expr], expr[expr:expr] subscript
5963 * - ".name" lookup
5964 * - function call with Funcref variable: func(expr)
5965 * - method call: var->method()
5966 *
5967 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00005968 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005969 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005970 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005971handle_subscript(
5972 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00005973 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005974 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005975 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005976 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005977{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005978 int evaluate = evalarg != NULL
5979 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005980 int ret = OK;
5981 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005982 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005983 int getnext;
5984 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005985
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005986 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005987 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005988 // When at the end of the line and ".name" or "->{" or "->X" follows in
5989 // the next line then consume the line break.
5990 p = eval_next_non_blank(*arg, evalarg, &getnext);
5991 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005992 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02005993 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
5994 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
5995 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005996 {
5997 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005998 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005999 check_white = FALSE;
6000 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006001
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006002 if (rettv->v_type == VAR_ANY)
6003 {
6004 char_u *exp_name;
6005 int cc;
6006 int idx;
6007 ufunc_T *ufunc;
6008 type_T *type;
6009
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006010 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006011 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006012 if (**arg != '.')
6013 {
6014 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006015 semsg(_(e_expected_dot_after_name_str),
6016 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006017 ret = FAIL;
6018 break;
6019 }
6020 ++*arg;
6021 if (IS_WHITE_OR_NUL(**arg))
6022 {
6023 if (verbose)
6024 emsg(_(e_no_white_space_allowed_after_dot));
6025 ret = FAIL;
6026 break;
6027 }
6028
6029 // isolate the name
6030 exp_name = *arg;
6031 while (eval_isnamec(**arg))
6032 ++*arg;
6033 cc = **arg;
6034 **arg = NUL;
6035
6036 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
6037 evalarg->eval_cctx, verbose);
6038 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006039
6040 if (idx < 0 && ufunc == NULL)
6041 {
6042 ret = FAIL;
6043 break;
6044 }
6045 if (idx >= 0)
6046 {
6047 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6048 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6049
6050 copy_tv(sv->sv_tv, rettv);
6051 }
6052 else
6053 {
6054 rettv->v_type = VAR_FUNC;
6055 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6056 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006057 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006058 }
6059
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006060 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6061 || rettv->v_type == VAR_PARTIAL))
6062 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006063 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006064 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6065 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006066
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006067 // Stop the expression evaluation when immediately aborting on
6068 // error, or when an interrupt occurred or an exception was thrown
6069 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006070 if (aborting())
6071 {
6072 if (ret == OK)
6073 clear_tv(rettv);
6074 ret = FAIL;
6075 }
6076 dict_unref(selfdict);
6077 selfdict = NULL;
6078 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006079 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006080 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006081 if (in_vim9script())
6082 *arg = skipwhite(p + 2);
6083 else
6084 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006085 if (ret == OK)
6086 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006087 if (VIM_ISWHITE(**arg))
6088 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006089 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006090 ret = FAIL;
6091 }
6092 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006093 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006094 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006095 else
6096 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006097 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006098 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006099 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006100 // "." is ".name" lookup when we found a dict or when evaluating and
6101 // scriptversion is at least 2, where string concatenation is "..".
6102 else if (**arg == '['
6103 || (**arg == '.' && (rettv->v_type == VAR_DICT
6104 || (!evaluate
6105 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006106 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006107 {
6108 dict_unref(selfdict);
6109 if (rettv->v_type == VAR_DICT)
6110 {
6111 selfdict = rettv->vval.v_dict;
6112 if (selfdict != NULL)
6113 ++selfdict->dv_refcount;
6114 }
6115 else
6116 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006117 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006118 {
6119 clear_tv(rettv);
6120 ret = FAIL;
6121 }
6122 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006123 else
6124 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006125 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006126
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006127 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6128 // Don't do this when "Func" is already a partial that was bound
6129 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006130 if (selfdict != NULL
6131 && (rettv->v_type == VAR_FUNC
6132 || (rettv->v_type == VAR_PARTIAL
6133 && (rettv->vval.v_partial->pt_auto
6134 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006135 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006136
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006137 dict_unref(selfdict);
6138 return ret;
6139}
6140
6141/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006142 * Make a copy of an item.
6143 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006144 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6145 * reference to an already copied list/dict can be used.
6146 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006147 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006148 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006149item_copy(
6150 typval_T *from,
6151 typval_T *to,
6152 int deep,
6153 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006154{
6155 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006156 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006157
Bram Moolenaar33570922005-01-25 22:26:29 +00006158 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006159 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006160 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006161 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006162 }
6163 ++recurse;
6164
6165 switch (from->v_type)
6166 {
6167 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006168 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006169 case VAR_STRING:
6170 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006171 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006172 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006173 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006174 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006175 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006176 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006177 copy_tv(from, to);
6178 break;
6179 case VAR_LIST:
6180 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006181 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006182 if (from->vval.v_list == NULL)
6183 to->vval.v_list = NULL;
6184 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6185 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006186 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006187 to->vval.v_list = from->vval.v_list->lv_copylist;
6188 ++to->vval.v_list->lv_refcount;
6189 }
6190 else
6191 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6192 if (to->vval.v_list == NULL)
6193 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006194 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006195 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006196 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006197 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006198 case VAR_DICT:
6199 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006200 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006201 if (from->vval.v_dict == NULL)
6202 to->vval.v_dict = NULL;
6203 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6204 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006205 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006206 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6207 ++to->vval.v_dict->dv_refcount;
6208 }
6209 else
6210 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6211 if (to->vval.v_dict == NULL)
6212 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006213 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006214 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006215 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006216 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006217 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006218 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006219 }
6220 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006221 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006222}
6223
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006224 void
6225echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6226{
6227 char_u *tofree;
6228 char_u numbuf[NUMBUFLEN];
6229 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6230
6231 if (*atstart)
6232 {
6233 *atstart = FALSE;
6234 // Call msg_start() after eval1(), evaluating the expression
6235 // may cause a message to appear.
6236 if (with_space)
6237 {
6238 // Mark the saved text as finishing the line, so that what
6239 // follows is displayed on a new line when scrolling back
6240 // at the more prompt.
6241 msg_sb_eol();
6242 msg_start();
6243 }
6244 }
6245 else if (with_space)
6246 msg_puts_attr(" ", echo_attr);
6247
6248 if (p != NULL)
6249 for ( ; *p != NUL && !got_int; ++p)
6250 {
6251 if (*p == '\n' || *p == '\r' || *p == TAB)
6252 {
6253 if (*p != TAB && *needclr)
6254 {
6255 // remove any text still there from the command
6256 msg_clr_eos();
6257 *needclr = FALSE;
6258 }
6259 msg_putchar_attr(*p, echo_attr);
6260 }
6261 else
6262 {
6263 if (has_mbyte)
6264 {
6265 int i = (*mb_ptr2len)(p);
6266
6267 (void)msg_outtrans_len_attr(p, i, echo_attr);
6268 p += i - 1;
6269 }
6270 else
6271 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6272 }
6273 }
6274 vim_free(tofree);
6275}
6276
Bram Moolenaare9a41262005-01-15 22:18:47 +00006277/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 * ":echo expr1 ..." print each argument separated with a space, add a
6279 * newline at the end.
6280 * ":echon expr1 ..." print each argument plain.
6281 */
6282 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006283ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284{
6285 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006286 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006287 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 int needclr = TRUE;
6289 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006290 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006291 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006292 evalarg_T evalarg;
6293
Bram Moolenaare707c882020-07-01 13:07:37 +02006294 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295
6296 if (eap->skip)
6297 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006298 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006300 // If eval1() causes an error message the text from the command may
6301 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006302 need_clr_eos = needclr;
6303
Bram Moolenaar68db9962021-05-09 23:19:22 +02006304 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006305 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 {
6307 /*
6308 * Report the invalid expression unless the expression evaluation
6309 * has been cancelled due to an aborting error, an interrupt, or an
6310 * exception.
6311 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006312 if (!aborting() && did_emsg == did_emsg_before
6313 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006314 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006315 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 break;
6317 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006318 need_clr_eos = FALSE;
6319
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006321 {
6322 if (rettv.v_type == VAR_VOID)
6323 {
6324 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6325 break;
6326 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006327 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006328 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006329
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006330 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 arg = skipwhite(arg);
6332 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006333 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006334 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335
6336 if (eap->skip)
6337 --emsg_skip;
6338 else
6339 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006340 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 if (needclr)
6342 msg_clr_eos();
6343 if (eap->cmdidx == CMD_echo)
6344 msg_end();
6345 }
6346}
6347
6348/*
6349 * ":echohl {name}".
6350 */
6351 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006352ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006354 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355}
6356
6357/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006358 * Returns the :echo attribute
6359 */
6360 int
6361get_echo_attr(void)
6362{
6363 return echo_attr;
6364}
6365
6366/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 * ":execute expr1 ..." execute the result of an expression.
6368 * ":echomsg expr1 ..." Print a message
6369 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006370 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 * Each gets spaces around each argument and a newline at the end for
6372 * echo commands
6373 */
6374 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006375ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376{
6377 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006378 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 int ret = OK;
6380 char_u *p;
6381 garray_T ga;
6382 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006383 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384
6385 ga_init2(&ga, 1, 80);
6386
6387 if (eap->skip)
6388 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006389 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006391 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006392 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394
6395 if (!eap->skip)
6396 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006397 char_u buf[NUMBUFLEN];
6398
6399 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006400 {
6401 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6402 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006403 semsg(_(e_using_invalid_value_as_string_str),
6404 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006405 p = NULL;
6406 }
6407 else
6408 p = tv_get_string_buf(&rettv, buf);
6409 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006410 else
6411 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006412 if (p == NULL)
6413 {
6414 clear_tv(&rettv);
6415 ret = FAIL;
6416 break;
6417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 len = (int)STRLEN(p);
6419 if (ga_grow(&ga, len + 2) == FAIL)
6420 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006421 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 ret = FAIL;
6423 break;
6424 }
6425 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 ga.ga_len += len;
6429 }
6430
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006431 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 arg = skipwhite(arg);
6433 }
6434
6435 if (ret != FAIL && ga.ga_data != NULL)
6436 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006437 // use the first line of continuation lines for messages
6438 SOURCING_LNUM = start_lnum;
6439
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006440 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6441 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006442 // Mark the already saved text as finishing the line, so that what
6443 // follows is displayed on a new line when scrolling back at the
6444 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006445 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006446 }
6447
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006449 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006450 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006451 out_flush();
6452 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006453 else if (eap->cmdidx == CMD_echoconsole)
6454 {
6455 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6456 ui_write((char_u *)"\r\n", 2, TRUE);
6457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 else if (eap->cmdidx == CMD_echoerr)
6459 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006460 int save_did_emsg = did_emsg;
6461
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006462 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006463 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 if (!force_abort)
6465 did_emsg = save_did_emsg;
6466 }
6467 else if (eap->cmdidx == CMD_execute)
6468 do_cmdline((char_u *)ga.ga_data,
6469 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6470 }
6471
6472 ga_clear(&ga);
6473
6474 if (eap->skip)
6475 --emsg_skip;
6476
Bram Moolenaar63b91732021-08-05 20:40:03 +02006477 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478}
6479
6480/*
6481 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6482 * "arg" points to the "&" or '+' when called, to "option" when returning.
6483 * Returns NULL when no option name found. Otherwise pointer to the char
6484 * after the option name.
6485 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006486 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006487find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488{
6489 char_u *p = *arg;
6490
6491 ++p;
6492 if (*p == 'g' && p[1] == ':')
6493 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006494 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 p += 2;
6496 }
6497 else if (*p == 'l' && p[1] == ':')
6498 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006499 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 p += 2;
6501 }
6502 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006503 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504
6505 if (!ASCII_ISALPHA(*p))
6506 return NULL;
6507 *arg = p;
6508
6509 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006510 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 else
6512 while (ASCII_ISALPHA(*p))
6513 ++p;
6514 return p;
6515}
6516
6517/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006518 * Display script name where an item was last set.
6519 * Should only be invoked when 'verbose' is non-zero.
6520 */
6521 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006522last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006523{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006524 char_u *p;
6525
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006526 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006527 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006528 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006529 if (p != NULL)
6530 {
6531 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006532 msg_puts(_("\n\tLast set from "));
6533 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006534 if (script_ctx.sc_lnum > 0)
6535 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006536 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006537 msg_outnum((long)script_ctx.sc_lnum);
6538 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006539 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006540 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006541 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006542 }
6543}
6544
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006545#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546
6547/*
6548 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006549 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 * "flags" can be "g" to do a global substitute.
6551 * Returns an allocated string, NULL for error.
6552 */
6553 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006554do_string_sub(
6555 char_u *str,
6556 char_u *pat,
6557 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006558 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006559 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560{
6561 int sublen;
6562 regmatch_T regmatch;
6563 int i;
6564 int do_all;
6565 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006566 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 garray_T ga;
6568 char_u *ret;
6569 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006570 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006572 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006574 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575
6576 ga_init2(&ga, 1, 200);
6577
6578 do_all = (flags[0] == 'g');
6579
6580 regmatch.rm_ic = p_ic;
6581 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6582 if (regmatch.regprog != NULL)
6583 {
6584 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006585 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6587 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006588 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006589 if (regmatch.startp[0] == regmatch.endp[0])
6590 {
6591 if (zero_width == regmatch.startp[0])
6592 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006593 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006594 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006595 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6596 (size_t)i);
6597 ga.ga_len += i;
6598 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006599 continue;
6600 }
6601 zero_width = regmatch.startp[0];
6602 }
6603
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 /*
6605 * Get some space for a temporary buffer to do the substitution
6606 * into. It will contain:
6607 * - The text up to where the match is.
6608 * - The substituted text.
6609 * - The text after the match.
6610 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006611 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006612 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6614 {
6615 ga_clear(&ga);
6616 break;
6617 }
6618
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006619 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 i = (int)(regmatch.startp[0] - tail);
6621 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006622 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006623 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 + ga.ga_len + i, TRUE, TRUE, FALSE);
6625 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006626 tail = regmatch.endp[0];
6627 if (*tail == NUL)
6628 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 if (!do_all)
6630 break;
6631 }
6632
6633 if (ga.ga_data != NULL)
6634 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6635
Bram Moolenaar473de612013-06-08 18:19:48 +02006636 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 }
6638
6639 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6640 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006641 if (p_cpo == empty_option)
6642 p_cpo = save_cpo;
6643 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006644 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006645 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006646 // If it's still empty it was changed and restored, need to restore in
6647 // the complicated way.
6648 if (*p_cpo == NUL)
6649 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006650 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652
6653 return ret;
6654}