blob: 7fed2867d1b3b971a78d7bfd03d7412a0101d268 [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 {
490 ga_init2(&ga, (int)sizeof(char), 80);
491 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 Moolenaarb544f3c2017-02-23 19:03:28 +0100629 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200630 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
631 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000632 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200634 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100635call_vim_function(
636 char_u *func,
637 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200638 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200639 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000641 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200642 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100644 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200645 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000646 funcexe.fe_firstline = curwin->w_cursor.lnum;
647 funcexe.fe_lastline = curwin->w_cursor.lnum;
648 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200649 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000650 if (ret == FAIL)
651 clear_tv(rettv);
652
653 return ret;
654}
655
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100656/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100657 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100658 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200659 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
660 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100661 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200662 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100663call_func_retnr(
664 char_u *func,
665 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200666 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100667{
668 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200669 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100670
Bram Moolenaarded27a12018-08-01 19:06:03 +0200671 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100672 return -1;
673
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100674 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100675 clear_tv(&rettv);
676 return retval;
677}
678
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000679/*
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100680 * Call Vim script function like call_func_retnr() and drop the result.
681 * Returns FAIL when calling the function fails.
682 */
683 int
684call_func_noret(
685 char_u *func,
686 int argc,
687 typval_T *argv)
688{
689 typval_T rettv;
690
691 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
692 return FAIL;
693 clear_tv(&rettv);
694 return OK;
695}
696
697/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100698 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100699 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000700 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000701 */
702 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100703call_func_retstr(
704 char_u *func,
705 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200706 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000707{
708 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000709 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000710
Bram Moolenaarded27a12018-08-01 19:06:03 +0200711 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000712 return NULL;
713
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100714 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000715 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 return retval;
717}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000718
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000719/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100720 * Call Vim script function "func" and return the result as a List.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100721 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000722 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000723 */
724 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100725call_func_retlist(
726 char_u *func,
727 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200728 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000729{
730 typval_T rettv;
731
Bram Moolenaarded27a12018-08-01 19:06:03 +0200732 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000733 return NULL;
734
735 if (rettv.v_type != VAR_LIST)
736 {
737 clear_tv(&rettv);
738 return NULL;
739 }
740
741 return rettv.vval.v_list;
742}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744#ifdef FEAT_FOLDING
745/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100746 * Evaluate "arg", which is 'foldexpr'.
747 * Note: caller must set "curwin" to match "arg".
748 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
749 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 */
751 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100752eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753{
Bram Moolenaar33570922005-01-25 22:26:29 +0000754 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200755 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000757 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
758 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759
760 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000761 if (use_sandbox)
762 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200763 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200765 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 retval = 0;
767 else
768 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100769 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000770 if (tv.v_type == VAR_NUMBER)
771 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000772 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 retval = 0;
774 else
775 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100776 // If the result is a string, check if there is a non-digit before
777 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000778 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 if (!VIM_ISDIGIT(*s) && *s != '-')
780 *cp = *s++;
781 retval = atol((char *)s);
782 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000783 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 }
785 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000786 if (use_sandbox)
787 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200788 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200789 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200791 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792}
793#endif
794
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000796 * Get an lval: variable, Dict item or List item that can be assigned a value
797 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
798 * "name.key", "name.key[expr]" etc.
799 * Indexing only works if "name" is an existing List or Dictionary.
800 * "name" points to the start of the name.
801 * If "rettv" is not NULL it points to the value to be assigned.
802 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
803 * wrong; must end in space or cmd separator.
804 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100805 * flags:
806 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100807 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100808 * GLV_NO_AUTOLOAD: do not use script autoloading
809 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000810 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000811 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000812 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000813 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200814 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100815get_lval(
816 char_u *name,
817 typval_T *rettv,
818 lval_T *lp,
819 int unlet,
820 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100821 int flags, // GLV_ values
822 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000823{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000824 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000825 char_u *expr_start, *expr_end;
826 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000827 dictitem_T *v;
828 typval_T var1;
829 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000830 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000831 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000832 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100833 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100834 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100835 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000836
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100837 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200838 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000839
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100840 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000841 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100842 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000843 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200844 lp->ll_name_end = find_name_end(name, NULL, NULL,
845 FNE_INCL_BR | fne_flags);
846 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000847 }
848
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100849 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000850 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100851 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000852 if (expr_start != NULL)
853 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100854 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100855 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000856 && *p != '[' && *p != '.')
857 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000858 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000859 return NULL;
860 }
861
862 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
863 if (lp->ll_exp_name == NULL)
864 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100865 // Report an invalid expression in braces, unless the
866 // expression evaluation has been cancelled due to an
867 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000868 if (!aborting() && !quiet)
869 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000870 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000871 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000872 return NULL;
873 }
874 }
875 lp->ll_name = lp->ll_exp_name;
876 }
877 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100878 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000879 lp->ll_name = name;
880
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200881 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100882 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200883 // "a: type" is declaring variable "a" with a type, not "a:".
884 if (p == name + 2 && p[-1] == ':')
885 {
886 --p;
887 lp->ll_name_end = p;
888 }
889 if (*p == ':')
890 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000891 garray_T tmp_type_list;
892 garray_T *type_list;
893 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +0200894
895 if (tp == p + 1 && !quiet)
896 {
897 semsg(_(e_white_space_required_after_str_str), ":", p);
898 return NULL;
899 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100900
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000901 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
902 type_list = &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list;
903 else
904 {
905 type_list = &tmp_type_list;
906 ga_init2(type_list, sizeof(type_T), 10);
907 }
908
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200909 // parse the type after the name
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000910 lp->ll_type = parse_type(&tp, type_list, !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100911 if (lp->ll_type == NULL && !quiet)
912 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200913 lp->ll_name_end = tp;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000914
915 // drop the type when not in a script
916 if (type_list == &tmp_type_list)
917 {
918 lp->ll_type = NULL;
919 clear_type_list(type_list);
920 }
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200921 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100922 }
923 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000924 if (lp->ll_name == NULL)
925 return p;
926
927 if (*p == '.' && in_vim9script())
928 {
929 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, NULL);
930 if (import != NULL)
931 {
932 ufunc_T *ufunc;
933 type_T *type;
934
935 lp->ll_sid = import->imp_sid;
936 lp->ll_name = skipwhite(p + 1);
937 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
938 lp->ll_name_end = p;
939
940 // check the item is exported
941 cc = *p;
942 *p = NUL;
943 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
944 NULL, TRUE) == -1)
945 {
946 *p = cc;
947 return FAIL;
948 }
949 *p = cc;
950 }
951 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100952
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100953 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000954 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000955 return p;
956
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200957 if (in_vim9script() && lval_root != NULL)
958 {
959 // using local variable
960 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +0200961 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200962 }
963 else
964 {
965 cc = *p;
966 *p = NUL;
967 // When we would write to the variable pass &ht and prevent autoload.
968 writing = !(flags & GLV_READ_ONLY);
969 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100970 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200971 if (v == NULL && !quiet)
972 semsg(_(e_undefined_variable_str), lp->ll_name);
973 *p = cc;
974 if (v == NULL)
975 return NULL;
976 lp->ll_tv = &v->di_tv;
977 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000978
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100979 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
980 {
981 if (!quiet)
982 semsg(_(e_variable_already_declared), lp->ll_name);
983 return NULL;
984 }
985
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000986 /*
987 * Loop until no more [idx] or .key is following.
988 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100989 var1.v_type = VAR_UNKNOWN;
990 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200991 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000992 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200993 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
994 {
995 if (!quiet)
996 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
997 return NULL;
998 }
Bram Moolenaare65081d2021-06-27 15:04:05 +0200999 if (lp->ll_tv->v_type != VAR_LIST
1000 && lp->ll_tv->v_type != VAR_DICT
1001 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001002 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001003 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001004 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001005 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001006 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001007
1008 // a NULL list/blob works like an empty list/blob, allocate one now.
1009 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1010 rettv_list_alloc(lp->ll_tv);
1011 else if (lp->ll_tv->v_type == VAR_BLOB
1012 && lp->ll_tv->vval.v_blob == NULL)
1013 rettv_blob_alloc(lp->ll_tv);
1014
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001015 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001016 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001017 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001018 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001019 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001020 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001021
Bram Moolenaar10c65862020-10-08 21:16:42 +02001022 if (in_vim9script() && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001023 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001024 && lp->ll_tv == &v->di_tv
1025 && ht != NULL && ht == get_script_local_ht())
1026 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001027 svar_T *sv = find_typval_in_script(lp->ll_tv, 0);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001028
1029 // Vim9 script local variable: get the type
1030 if (sv != NULL)
1031 lp->ll_valtype = sv->sv_type;
1032 }
1033
Bram Moolenaar8c711452005-01-14 21:53:12 +00001034 len = -1;
1035 if (*p == '.')
1036 {
1037 key = p + 1;
1038 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1039 ;
1040 if (len == 0)
1041 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001042 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001043 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001044 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001045 }
1046 p = key + len;
1047 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001048 else
1049 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001050 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001051 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001052 if (*p == ':')
1053 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001054 else
1055 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001056 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001057 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001058 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001059 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001060 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001061 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001062 clear_tv(&var1);
1063 return NULL;
1064 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001065 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001066 }
1067
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001068 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001069 if (*p == ':')
1070 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001071 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001072 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001073 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001074 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001075 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001076 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001077 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001078 if (rettv != NULL
1079 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001080 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001081 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001082 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001083 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001084 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001085 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001086 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001087 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001088 }
1089 p = skipwhite(p + 1);
1090 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001091 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001092 else
1093 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001094 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001095 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001096 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001097 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001098 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001099 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001100 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001101 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001102 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001103 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001104 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001105 clear_tv(&var2);
1106 return NULL;
1107 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001108 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001109 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001110 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001111 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001112 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001113
Bram Moolenaar8c711452005-01-14 21:53:12 +00001114 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001115 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001116 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001117 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001118 clear_tv(&var1);
1119 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001120 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001121 }
1122
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001123 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001124 ++p;
1125 }
1126
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001127 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001128 {
1129 if (len == -1)
1130 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001131 // "[key]": get key from "var1"
1132 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001133 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001134 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001135 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001136 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001137 }
1138 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001139 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001140
1141 // a NULL dict is equivalent with an empty dict
1142 if (lp->ll_tv->vval.v_dict == NULL)
1143 {
1144 lp->ll_tv->vval.v_dict = dict_alloc();
1145 if (lp->ll_tv->vval.v_dict == NULL)
1146 {
1147 clear_tv(&var1);
1148 return NULL;
1149 }
1150 ++lp->ll_tv->vval.v_dict->dv_refcount;
1151 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001152 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001153
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001154 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001155
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001156 // When assigning to a scope dictionary check that a function and
1157 // variable name is valid (only variable name unless it is l: or
1158 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001159 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001160 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001161 int prevval;
1162 int wrong;
1163
1164 if (len != -1)
1165 {
1166 prevval = key[len];
1167 key[len] = NUL;
1168 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001169 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001170 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001171 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1172 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001173 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001174 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001175 if (len != -1)
1176 key[len] = prevval;
1177 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001178 {
1179 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001180 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001181 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001182 }
1183
Bram Moolenaar10c65862020-10-08 21:16:42 +02001184 if (lp->ll_valtype != NULL)
1185 // use the type of the member
1186 lp->ll_valtype = lp->ll_valtype->tt_member;
1187
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001188 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001189 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001190 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001191 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001192 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001193 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001194 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001195 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001196 return NULL;
1197 }
1198
Bram Moolenaar31b81602019-02-10 22:14:27 +01001199 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001200 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001201 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001202 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001203 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001204 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001205 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001206 }
1207 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001208 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001209 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001210 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001211 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001212 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001213 p = NULL;
1214 break;
1215 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001216 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001217 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001218 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1219 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001220 {
1221 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001222 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001223 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001224
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001225 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001226 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001227 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001228 else if (lp->ll_tv->v_type == VAR_BLOB)
1229 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001230 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1231
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001232 /*
1233 * Get the number and item for the only or first index of the List.
1234 */
1235 if (empty1)
1236 lp->ll_n1 = 0;
1237 else
1238 // is number or string
1239 lp->ll_n1 = (long)tv_get_number(&var1);
1240 clear_tv(&var1);
1241
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001242 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001243 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001244 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001245 return NULL;
1246 }
1247 if (lp->ll_range && !lp->ll_empty2)
1248 {
1249 lp->ll_n2 = (long)tv_get_number(&var2);
1250 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001251 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1252 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001253 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001254 }
1255 lp->ll_blob = lp->ll_tv->vval.v_blob;
1256 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001257 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001258 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001259 else
1260 {
1261 /*
1262 * Get the number and item for the only or first index of the List.
1263 */
1264 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001265 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001266 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001267 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001268 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001269 clear_tv(&var1);
1270
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001271 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001272 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001273 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001274 if (lp->ll_li == NULL)
1275 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001276 clear_tv(&var2);
1277 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001278 }
1279
Bram Moolenaar10c65862020-10-08 21:16:42 +02001280 if (lp->ll_valtype != NULL)
1281 // use the type of the member
1282 lp->ll_valtype = lp->ll_valtype->tt_member;
1283
Bram Moolenaar8c711452005-01-14 21:53:12 +00001284 /*
1285 * May need to find the item or absolute index for the second
1286 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001287 * When no index given: "lp->ll_empty2" is TRUE.
1288 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001289 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001290 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001291 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001292 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001293 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001294 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001295 if (check_range_index_two(lp->ll_list,
1296 &lp->ll_n1, lp->ll_li,
1297 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001298 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001299 }
1300
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001301 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001302 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001303 }
1304
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001305 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001307 return p;
1308}
1309
1310/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001311 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001312 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001313 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001314clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001315{
1316 vim_free(lp->ll_exp_name);
1317 vim_free(lp->ll_newkey);
1318}
1319
1320/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001321 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001322 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001323 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1324 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001325 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001326 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001327set_var_lval(
1328 lval_T *lp,
1329 char_u *endp,
1330 typval_T *rettv,
1331 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001332 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1333 char_u *op,
1334 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001335{
1336 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001337 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001338
1339 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001340 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001341 cc = *endp;
1342 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001343 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1344 return;
1345
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001346 if (lp->ll_blob != NULL)
1347 {
1348 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001349
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001350 if (op != NULL && *op != '=')
1351 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001352 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001353 return;
1354 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001355 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001356 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001357
1358 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1359 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001360 if (lp->ll_empty2)
1361 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1362
Bram Moolenaar68452172021-04-12 21:21:02 +02001363 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1364 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001365 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001366 }
1367 else
1368 {
1369 val = (int)tv_get_number_chk(rettv, &error);
1370 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001371 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001372 }
1373 }
1374 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001375 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001376 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001377
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001378 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1379 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001380 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001381 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001382 *endp = cc;
1383 return;
1384 }
1385
Bram Moolenaarff697e62019-02-12 22:28:33 +01001386 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001387 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001388 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001389 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001390 {
1391 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001392 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1393 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001394 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001395 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001396 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001397 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001398 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001399 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001400 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001401 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001402 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1403 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001404 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001405 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001406 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001407 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001408 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001409 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001410 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001411 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001412 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001413 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001414 else if (lp->ll_range)
1415 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001416 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1417 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001418 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001419 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001420 return;
1421 }
1422
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001423 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1424 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001425 }
1426 else
1427 {
1428 /*
1429 * Assign to a List or Dictionary item.
1430 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001431 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1432 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001433 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001434 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001435 return;
1436 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001437
1438 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001439 && check_typval_arg_type(lp->ll_valtype, rettv,
1440 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001441 return;
1442
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001443 if (lp->ll_newkey != NULL)
1444 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001445 if (op != NULL && *op != '=')
1446 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001447 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001448 return;
1449 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001450 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1451 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001452 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001453
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001454 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001455 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001456 if (di == NULL)
1457 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001458 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1459 {
1460 vim_free(di);
1461 return;
1462 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001463 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001464 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001465 else if (op != NULL && *op != '=')
1466 {
1467 tv_op(lp->ll_tv, rettv, op);
1468 return;
1469 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001470 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001471 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001472
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001473 /*
1474 * Assign the value to the variable or list item.
1475 */
1476 if (copy)
1477 copy_tv(rettv, lp->ll_tv);
1478 else
1479 {
1480 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001481 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001482 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001483 }
1484 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001485}
1486
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001487/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001488 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1489 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001490 * Returns OK or FAIL.
1491 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001492 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001493tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001494{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001495 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001496 char_u numbuf[NUMBUFLEN];
1497 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001498 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001499
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001500 // Can't do anything with a Funcref or Dict on the right.
1501 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001502 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001503 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1504 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001505 {
1506 switch (tv1->v_type)
1507 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001508 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001509 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001511 case VAR_DICT:
1512 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001513 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001514 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001515 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001516 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001517 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001518 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001519 break;
1520
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001521 case VAR_BLOB:
1522 if (*op != '+' || tv2->v_type != VAR_BLOB)
1523 break;
1524 // BLOB += BLOB
1525 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1526 {
1527 blob_T *b1 = tv1->vval.v_blob;
1528 blob_T *b2 = tv2->vval.v_blob;
1529 int i, len = blob_len(b2);
1530 for (i = 0; i < len; i++)
1531 ga_append(&b1->bv_ga, blob_get(b2, i));
1532 }
1533 return OK;
1534
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001535 case VAR_LIST:
1536 if (*op != '+' || tv2->v_type != VAR_LIST)
1537 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001538 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001539 if (tv2->vval.v_list != NULL)
1540 {
1541 if (tv1->vval.v_list == NULL)
1542 {
1543 tv1->vval.v_list = tv2->vval.v_list;
1544 ++tv1->vval.v_list->lv_refcount;
1545 }
1546 else
1547 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1548 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001549 return OK;
1550
1551 case VAR_NUMBER:
1552 case VAR_STRING:
1553 if (tv2->v_type == VAR_LIST)
1554 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001555 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001556 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001557 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001558 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001559#ifdef FEAT_FLOAT
1560 if (tv2->v_type == VAR_FLOAT)
1561 {
1562 float_T f = n;
1563
Bram Moolenaarff697e62019-02-12 22:28:33 +01001564 if (*op == '%')
1565 break;
1566 switch (*op)
1567 {
1568 case '+': f += tv2->vval.v_float; break;
1569 case '-': f -= tv2->vval.v_float; break;
1570 case '*': f *= tv2->vval.v_float; break;
1571 case '/': f /= tv2->vval.v_float; break;
1572 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001573 clear_tv(tv1);
1574 tv1->v_type = VAR_FLOAT;
1575 tv1->vval.v_float = f;
1576 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001577 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001578#endif
1579 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001580 switch (*op)
1581 {
1582 case '+': n += tv_get_number(tv2); break;
1583 case '-': n -= tv_get_number(tv2); break;
1584 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001585 case '/': n = num_divide(n, tv_get_number(tv2),
1586 &failed); break;
1587 case '%': n = num_modulus(n, tv_get_number(tv2),
1588 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001589 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001590 clear_tv(tv1);
1591 tv1->v_type = VAR_NUMBER;
1592 tv1->vval.v_number = n;
1593 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001594 }
1595 else
1596 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001597 if (tv2->v_type == VAR_FLOAT)
1598 break;
1599
Bram Moolenaarff697e62019-02-12 22:28:33 +01001600 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001601 s = tv_get_string(tv1);
1602 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001603 clear_tv(tv1);
1604 tv1->v_type = VAR_STRING;
1605 tv1->vval.v_string = s;
1606 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001607 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001608
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001609 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001610#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001611 {
1612 float_T f;
1613
Bram Moolenaarff697e62019-02-12 22:28:33 +01001614 if (*op == '%' || *op == '.'
1615 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001616 && tv2->v_type != VAR_NUMBER
1617 && tv2->v_type != VAR_STRING))
1618 break;
1619 if (tv2->v_type == VAR_FLOAT)
1620 f = tv2->vval.v_float;
1621 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001622 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001623 switch (*op)
1624 {
1625 case '+': tv1->vval.v_float += f; break;
1626 case '-': tv1->vval.v_float -= f; break;
1627 case '*': tv1->vval.v_float *= f; break;
1628 case '/': tv1->vval.v_float /= f; break;
1629 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001630 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001631#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001632 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001633 }
1634 }
1635
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001636 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001637 return FAIL;
1638}
1639
1640/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001641 * Evaluate the expression used in a ":for var in expr" command.
1642 * "arg" points to "var".
1643 * Set "*errp" to TRUE for an error, FALSE otherwise;
1644 * Return a pointer that holds the info. Null when there is an error.
1645 */
1646 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001647eval_for_line(
1648 char_u *arg,
1649 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001650 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001651 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001652{
Bram Moolenaar33570922005-01-25 22:26:29 +00001653 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001654 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001655 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001656 typval_T tv;
1657 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001658 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001659
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001660 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001661
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001662 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001663 if (fi == NULL)
1664 return NULL;
1665
Bram Moolenaar404557e2021-07-05 21:41:48 +02001666 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1667 &fi->fi_semicolon, FALSE);
1668 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001669 return fi;
1670
Bram Moolenaar404557e2021-07-05 21:41:48 +02001671 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001672 if (expr[0] != 'i' || expr[1] != 'n'
1673 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001674 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001675 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1676 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1677 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001678 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001679 return fi;
1680 }
1681
1682 if (skip)
1683 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001684 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1685 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001686 {
1687 *errp = FALSE;
1688 if (!skip)
1689 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001690 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001691 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001692 l = tv.vval.v_list;
1693 if (l == NULL)
1694 {
1695 // a null list is like an empty list: do nothing
1696 clear_tv(&tv);
1697 }
1698 else
1699 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001700 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001701 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001702
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001703 // No need to increment the refcount, it's already set for
1704 // the list being used in "tv".
1705 fi->fi_list = l;
1706 list_add_watch(l, &fi->fi_lw);
1707 fi->fi_lw.lw_item = l->lv_first;
1708 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001709 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001710 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001711 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001712 fi->fi_bi = 0;
1713 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001714 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001715 typval_T btv;
1716
1717 // Make a copy, so that the iteration still works when the
1718 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001720 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001721 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001722 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001723 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001724 else if (tv.v_type == VAR_STRING)
1725 {
1726 fi->fi_byte_idx = 0;
1727 fi->fi_string = tv.vval.v_string;
1728 tv.vval.v_string = NULL;
1729 if (fi->fi_string == NULL)
1730 fi->fi_string = vim_strsave((char_u *)"");
1731 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001732 else
1733 {
Sean Dewar80d73952021-08-04 19:25:54 +02001734 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001735 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001736 }
1737 }
1738 }
1739 if (skip)
1740 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001741 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001742
1743 return fi;
1744}
1745
1746/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001747 * Used when looping over a :for line, skip the "in expr" part.
1748 */
1749 void
1750skip_for_lines(void *fi_void, evalarg_T *evalarg)
1751{
1752 forinfo_T *fi = (forinfo_T *)fi_void;
1753 int i;
1754
1755 for (i = 0; i < fi->fi_break_count; ++i)
1756 eval_next_line(evalarg);
1757}
1758
1759/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001760 * Use the first item in a ":for" list. Advance to the next.
1761 * Assign the values to the variable (list). "arg" points to the first one.
1762 * Return TRUE when a valid item was found, FALSE when at end of list or
1763 * something wrong.
1764 */
1765 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001766next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001767{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001768 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001769 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001770 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001771 ? (ASSIGN_FINAL
1772 // first round: error if variable exists
1773 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1774 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001775 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001776 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001777 int skip_assign = in_vim9script() && arg[0] == '_'
1778 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001779
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001780 if (fi->fi_blob != NULL)
1781 {
1782 typval_T tv;
1783
1784 if (fi->fi_bi >= blob_len(fi->fi_blob))
1785 return FALSE;
1786 tv.v_type = VAR_NUMBER;
1787 tv.v_lock = VAR_FIXED;
1788 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1789 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001790 if (skip_assign)
1791 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001792 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001793 fi->fi_varcount, flag, NULL) == OK;
1794 }
1795
1796 if (fi->fi_string != NULL)
1797 {
1798 typval_T tv;
1799 int len;
1800
1801 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1802 if (len == 0)
1803 return FALSE;
1804 tv.v_type = VAR_STRING;
1805 tv.v_lock = VAR_FIXED;
1806 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1807 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001808 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001809 if (skip_assign)
1810 result = TRUE;
1811 else
1812 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001813 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001814 vim_free(tv.vval.v_string);
1815 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001816 }
1817
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001818 item = fi->fi_lw.lw_item;
1819 if (item == NULL)
1820 result = FALSE;
1821 else
1822 {
1823 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001824 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001825 if (skip_assign)
1826 result = TRUE;
1827 else
1828 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001829 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001830 }
1831 return result;
1832}
1833
1834/*
1835 * Free the structure used to store info used by ":for".
1836 */
1837 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001838free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001839{
Bram Moolenaar33570922005-01-25 22:26:29 +00001840 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001841
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001842 if (fi == NULL)
1843 return;
1844 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001845 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001846 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001847 list_unref(fi->fi_list);
1848 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001849 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001850 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001851 else
1852 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001853 vim_free(fi);
1854}
1855
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001857set_context_for_expression(
1858 expand_T *xp,
1859 char_u *arg,
1860 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001862 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001864 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001866 if (cmdidx == CMD_let || cmdidx == CMD_var
1867 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001868 {
1869 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001870 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001871 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001872 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001873 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001874 {
1875 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001876 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001877 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001878 break;
1879 }
1880 return;
1881 }
1882 }
1883 else
1884 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1885 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 while ((xp->xp_pattern = vim_strpbrk(arg,
1887 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1888 {
1889 c = *xp->xp_pattern;
1890 if (c == '&')
1891 {
1892 c = xp->xp_pattern[1];
1893 if (c == '&')
1894 {
1895 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001896 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 }
1898 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001899 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001901 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1902 xp->xp_pattern += 2;
1903
1904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 }
1906 else if (c == '$')
1907 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001908 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 xp->xp_context = EXPAND_ENV_VARS;
1910 }
1911 else if (c == '=')
1912 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001913 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 xp->xp_context = EXPAND_EXPRESSION;
1915 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001916 else if (c == '#'
1917 && xp->xp_context == EXPAND_EXPRESSION)
1918 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001919 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001920 break;
1921 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001922 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 && xp->xp_context == EXPAND_FUNCTIONS
1924 && vim_strchr(xp->xp_pattern, '(') == NULL)
1925 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001926 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 break;
1928 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001929 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001931 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 {
1933 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1934 if (c == '\\' && xp->xp_pattern[1] != NUL)
1935 ++xp->xp_pattern;
1936 xp->xp_context = EXPAND_NOTHING;
1937 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001938 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001940 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1942 /* skip */ ;
1943 xp->xp_context = EXPAND_NOTHING;
1944 }
1945 else if (c == '|')
1946 {
1947 if (xp->xp_pattern[1] == '|')
1948 {
1949 ++xp->xp_pattern;
1950 xp->xp_context = EXPAND_EXPRESSION;
1951 }
1952 else
1953 xp->xp_context = EXPAND_COMMANDS;
1954 }
1955 else
1956 xp->xp_context = EXPAND_EXPRESSION;
1957 }
1958 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001959 // Doesn't look like something valid, expand as an expression
1960 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001961 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 arg = xp->xp_pattern;
1963 if (*arg != NUL)
1964 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1965 /* skip */ ;
1966 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001967
1968 // ":exe one two" completes "two"
1969 if ((cmdidx == CMD_execute
1970 || cmdidx == CMD_echo
1971 || cmdidx == CMD_echon
1972 || cmdidx == CMD_echomsg)
1973 && xp->xp_context == EXPAND_EXPRESSION)
1974 {
1975 for (;;)
1976 {
1977 char_u *n = skiptowhite(arg);
1978
1979 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1980 break;
1981 arg = skipwhite(n);
1982 }
1983 }
1984
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 xp->xp_pattern = arg;
1986}
1987
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001989 * Return TRUE if "pat" matches "text".
1990 * Does not use 'cpo' and always uses 'magic'.
1991 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001992 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001993pattern_match(char_u *pat, char_u *text, int ic)
1994{
1995 int matches = FALSE;
1996 char_u *save_cpo;
1997 regmatch_T regmatch;
1998
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001999 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002000 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002001 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002002 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2003 if (regmatch.regprog != NULL)
2004 {
2005 regmatch.rm_ic = ic;
2006 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2007 vim_regfree(regmatch.regprog);
2008 }
2009 p_cpo = save_cpo;
2010 return matches;
2011}
2012
2013/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002014 * Handle a name followed by "(". Both for just "name(arg)" and for
2015 * "expr->name(arg)".
2016 * Returns OK or FAIL.
2017 */
2018 static int
2019eval_func(
2020 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002021 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002022 char_u *name,
2023 int name_len,
2024 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002025 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002026 typval_T *basetv) // "expr" for "expr->name(arg)"
2027{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002028 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002029 char_u *s = name;
2030 int len = name_len;
2031 partial_T *partial;
2032 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002033 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002034 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002035
2036 if (!evaluate)
2037 check_vars(s, len);
2038
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002039 // If "s" is the name of a variable of type VAR_FUNC
2040 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002041 s = deref_func_name(s, &len, &partial,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002042 in_vim9script() ? &type : NULL, !evaluate, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002043
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002044 // Need to make a copy, in case evaluating the arguments makes
2045 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002046 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002047 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002048 ret = FAIL;
2049 else
2050 {
2051 funcexe_T funcexe;
2052
2053 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002054 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002055 funcexe.fe_firstline = curwin->w_cursor.lnum;
2056 funcexe.fe_lastline = curwin->w_cursor.lnum;
2057 funcexe.fe_evaluate = evaluate;
2058 funcexe.fe_partial = partial;
2059 funcexe.fe_basetv = basetv;
2060 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002061 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002062 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002063 }
2064 vim_free(s);
2065
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002066 // If evaluate is FALSE rettv->v_type was not set in
2067 // get_func_tv, but it's needed in handle_subscript() to parse
2068 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002069 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2070 {
2071 rettv->vval.v_string = NULL;
2072 rettv->v_type = VAR_FUNC;
2073 }
2074
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002075 // Stop the expression evaluation when immediately
2076 // aborting on error, or when an interrupt occurred or
2077 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002078 if (evaluate && aborting())
2079 {
2080 if (ret == OK)
2081 clear_tv(rettv);
2082 ret = FAIL;
2083 }
2084 return ret;
2085}
2086
2087/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002088 * Get the next line source line without advancing. But do skip over comment
2089 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002090 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002091 */
2092 static char_u *
2093getline_peek_skip_comments(evalarg_T *evalarg)
2094{
2095 for (;;)
2096 {
2097 char_u *next = getline_peek(evalarg->eval_getline,
2098 evalarg->eval_cookie);
2099 char_u *p;
2100
2101 if (next == NULL)
2102 break;
2103 p = skipwhite(next);
2104 if (*p != NUL && !vim9_comment_start(p))
2105 return next;
2106 (void)eval_next_line(evalarg);
2107 }
2108 return NULL;
2109}
2110
2111/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002112 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2113 * comment) and there is a next line, return the next line (skipping blanks)
2114 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002115 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2116 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002117 * "arg" must point somewhere inside a line, not at the start.
2118 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002119 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002120eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2121{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002122 char_u *p = skipwhite(arg);
2123
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002124 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002125 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002126 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002127 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002128 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002129 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002130 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002131
2132 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002133 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002134 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002135 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002136
Bram Moolenaar9d489562020-07-30 20:08:50 +02002137 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002138 {
2139 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002140 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002141 }
2142 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002143 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002144}
2145
2146/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002147 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002148 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002149 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002150 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002151eval_next_line(evalarg_T *evalarg)
2152{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002153 garray_T *gap = &evalarg->eval_ga;
2154 char_u *line;
2155
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002156 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002157 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2158 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002159 else
2160 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002161 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002162 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2163 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002164 char_u *p = skipwhite(line);
2165
2166 // Going to concatenate the lines after parsing. For an empty or
2167 // comment line use an empty string.
2168 if (*p == NUL || vim9_comment_start(p))
2169 {
2170 vim_free(line);
2171 line = vim_strsave((char_u *)"");
2172 }
2173
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002174 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2175 ++gap->ga_len;
2176 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002177 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002178 {
2179 vim_free(evalarg->eval_tofree);
2180 evalarg->eval_tofree = line;
2181 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002182
2183 // Advanced to the next line, "arg" no longer points into the previous
2184 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002185 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002186 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002187}
2188
Bram Moolenaare6b53242020-07-01 17:28:33 +02002189/*
2190 * Call eval_next_non_blank() and get the next line if needed.
2191 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002192 char_u *
2193skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2194{
2195 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002196 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002197
Bram Moolenaar962d7212020-07-04 14:15:00 +02002198 if (evalarg == NULL)
2199 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002200 eval_next_non_blank(p, evalarg, &getnext);
2201 if (getnext)
2202 return eval_next_line(evalarg);
2203 return p;
2204}
2205
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002206/*
Bram Moolenaar844fb642021-10-23 13:32:30 +01002207 * Initialize "evalarg" for use.
2208 */
2209 void
2210init_evalarg(evalarg_T *evalarg)
2211{
2212 CLEAR_POINTER(evalarg);
2213 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
2214}
2215
2216/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002217 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002218 */
2219 void
2220clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2221{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002222 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002223 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002224 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002225 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002226 if (eap != NULL)
2227 {
2228 // We may need to keep the original command line, e.g. for
2229 // ":let" it has the variable names. But we may also need the
2230 // new one, "nextcmd" points into it. Keep both.
2231 vim_free(eap->cmdline_tofree);
2232 eap->cmdline_tofree = *eap->cmdlinep;
2233 *eap->cmdlinep = evalarg->eval_tofree;
2234 }
2235 else
2236 vim_free(evalarg->eval_tofree);
2237 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002238 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002239
Bram Moolenaar844fb642021-10-23 13:32:30 +01002240 ga_clear_strings(&evalarg->eval_tofree_ga);
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002241 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002242 }
2243}
2244
2245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2249 */
2250
2251/*
2252 * Handle zero level expression.
2253 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002255 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002256 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 * Return OK or FAIL.
2258 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002259 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002260eval0(
2261 char_u *arg,
2262 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002263 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002264 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002266 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2267}
2268
2269/*
2270 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2271 * expression and don't check what comes after the expression.
2272 */
2273 int
2274eval0_retarg(
2275 char_u *arg,
2276 typval_T *rettv,
2277 exarg_T *eap,
2278 evalarg_T *evalarg,
2279 char_u **retarg)
2280{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 int ret;
2282 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002283 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002284 int did_emsg_before = did_emsg;
2285 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002286 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002287 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002288 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289
2290 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002291 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002292 expr_end = p;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002293 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002294
Bram Moolenaar02929a32021-12-17 14:46:12 +00002295 // In Vim9 script a command block is not split at NL characters for
2296 // commands using an expression argument. Skip over a '#' comment to check
2297 // for a following NL. Require white space before the '#'.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002298 if (in_vim9script() && p > expr_end && retarg == NULL)
Bram Moolenaar02929a32021-12-17 14:46:12 +00002299 while (*p == '#')
2300 {
2301 char_u *nl = vim_strchr(p, NL);
2302
2303 if (nl == NULL)
2304 break;
2305 p = skipwhite(nl + 1);
2306 if (eap != NULL && *p != NUL)
2307 eap->nextcmd = p;
2308 check_for_end = FALSE;
2309 }
2310
2311 if (ret != FAIL && check_for_end)
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002312 end_error = !ends_excmd2(arg, p);
2313 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 {
2315 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 /*
2318 * Report the invalid expression unless the expression evaluation has
2319 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002320 * exception, or we already gave a more specific error.
2321 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002323 if (!aborting()
2324 && did_emsg == did_emsg_before
2325 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002326 && (flags & EVAL_CONSTANT) == 0
2327 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002328 {
2329 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002330 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002331 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002332 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002333 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002334
2335 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002336 // a next command to avoid more errors, unless "|" is following, which
2337 // could only be a command separator.
2338 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2339 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002340 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002342
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002343 if (retarg != NULL)
2344 *retarg = p;
2345 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002346 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002347
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 return ret;
2349}
2350
2351/*
2352 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002353 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002354 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 *
2356 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002357 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002359 * Note: "rettv.v_lock" is not set.
2360 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 * Return OK or FAIL.
2362 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002363 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002364eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002366 char_u *p;
2367 int getnext;
2368
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002369 CLEAR_POINTER(rettv);
2370
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 /*
2372 * Get the first variable.
2373 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002374 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 return FAIL;
2376
Bram Moolenaar793648f2020-06-26 21:28:25 +02002377 p = eval_next_non_blank(*arg, evalarg, &getnext);
2378 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002380 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002381 int result;
2382 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002383 evalarg_T *evalarg_used = evalarg;
2384 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002385 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002386 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002387 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002388
2389 if (evalarg == NULL)
2390 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002391 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002392 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002393 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002394 orig_flags = evalarg_used->eval_flags;
2395 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002396
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002397 if (getnext)
2398 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002399 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002400 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002401 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002402 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002403 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002404 clear_tv(rettv);
2405 return FAIL;
2406 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002407 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002408 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002409
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002411 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002413 int error = FALSE;
2414
Bram Moolenaar13106602020-10-04 16:06:05 +02002415 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002416 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002417 else if (vim9script)
2418 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002419 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002421 if (error || !op_falsy || !result)
2422 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002423 if (error)
2424 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 }
2426
2427 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002428 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002430 if (op_falsy)
2431 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002432 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002433 {
mityu4ac198c2021-05-28 17:52:40 +02002434 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002435 clear_tv(rettv);
2436 return FAIL;
2437 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002438 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002439 evalarg_used->eval_flags = (op_falsy ? !result : result)
2440 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2441 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002442 {
2443 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002445 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002446 if (!op_falsy || !result)
2447 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002449 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002451 /*
2452 * Check for the ":".
2453 */
2454 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2455 if (*p != ':')
2456 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002457 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002458 if (evaluate && result)
2459 clear_tv(rettv);
2460 evalarg_used->eval_flags = orig_flags;
2461 return FAIL;
2462 }
2463 if (getnext)
2464 *arg = eval_next_line(evalarg_used);
2465 else
2466 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002467 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002468 {
2469 error_white_both(p, 1);
2470 clear_tv(rettv);
2471 evalarg_used->eval_flags = orig_flags;
2472 return FAIL;
2473 }
2474 *arg = p;
2475 }
2476
2477 /*
2478 * Get the third variable. Recursive!
2479 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002480 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002481 {
mityu4ac198c2021-05-28 17:52:40 +02002482 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002483 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002484 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002485 return FAIL;
2486 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002487 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2488 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002489 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002490 if (eval1(arg, &var2, evalarg_used) == FAIL)
2491 {
2492 if (evaluate && result)
2493 clear_tv(rettv);
2494 evalarg_used->eval_flags = orig_flags;
2495 return FAIL;
2496 }
2497 if (evaluate && !result)
2498 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002500
2501 if (evalarg == NULL)
2502 clear_evalarg(&local_evalarg, NULL);
2503 else
2504 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 }
2506
2507 return OK;
2508}
2509
2510/*
2511 * Handle first level expression:
2512 * expr2 || expr2 || expr2 logical OR
2513 *
2514 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002515 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 *
2517 * Return OK or FAIL.
2518 */
2519 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002520eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002522 char_u *p;
2523 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524
2525 /*
2526 * Get the first variable.
2527 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002528 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 return FAIL;
2530
2531 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002532 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002534 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002535 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002537 evalarg_T *evalarg_used = evalarg;
2538 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002539 int evaluate;
2540 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002541 long result = FALSE;
2542 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002543 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002544 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002545
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002546 if (evalarg == NULL)
2547 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002548 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002549 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002550 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002551 orig_flags = evalarg_used->eval_flags;
2552 evaluate = orig_flags & EVAL_EVALUATE;
2553 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002554 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002555 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002556 result = tv_get_bool_chk(rettv, &error);
2557 else if (tv_get_number_chk(rettv, &error) != 0)
2558 result = TRUE;
2559 clear_tv(rettv);
2560 if (error)
2561 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 }
2563
2564 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002565 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002567 while (p[0] == '|' && p[1] == '|')
2568 {
2569 if (getnext)
2570 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002571 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002572 {
2573 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2574 {
2575 error_white_both(p, 2);
2576 clear_tv(rettv);
2577 return FAIL;
2578 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002579 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002580 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002581
2582 /*
2583 * Get the second variable.
2584 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002585 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2586 {
mityu4ac198c2021-05-28 17:52:40 +02002587 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002588 clear_tv(rettv);
2589 return FAIL;
2590 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002591 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2592 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002593 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002594 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002595 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002596
2597 /*
2598 * Compute the result.
2599 */
2600 if (evaluate && !result)
2601 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002602 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002603 result = tv_get_bool_chk(&var2, &error);
2604 else if (tv_get_number_chk(&var2, &error) != 0)
2605 result = TRUE;
2606 clear_tv(&var2);
2607 if (error)
2608 return FAIL;
2609 }
2610 if (evaluate)
2611 {
2612 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002613 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002614 rettv->v_type = VAR_BOOL;
2615 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002616 }
2617 else
2618 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002619 rettv->v_type = VAR_NUMBER;
2620 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002621 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002622 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002623
2624 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002626
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002627 if (evalarg == NULL)
2628 clear_evalarg(&local_evalarg, NULL);
2629 else
2630 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 }
2632
2633 return OK;
2634}
2635
2636/*
2637 * Handle second level expression:
2638 * expr3 && expr3 && expr3 logical AND
2639 *
2640 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002641 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 *
2643 * Return OK or FAIL.
2644 */
2645 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002646eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002648 char_u *p;
2649 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650
2651 /*
2652 * Get the first variable.
2653 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002654 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 return FAIL;
2656
2657 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002658 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002660 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002661 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002663 evalarg_T *evalarg_used = evalarg;
2664 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002665 int orig_flags;
2666 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002667 long result = TRUE;
2668 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002669 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002670 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002671
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002672 if (evalarg == NULL)
2673 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002674 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002675 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002676 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002677 orig_flags = evalarg_used->eval_flags;
2678 evaluate = orig_flags & EVAL_EVALUATE;
2679 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002680 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002681 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002682 result = tv_get_bool_chk(rettv, &error);
2683 else if (tv_get_number_chk(rettv, &error) == 0)
2684 result = FALSE;
2685 clear_tv(rettv);
2686 if (error)
2687 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 }
2689
2690 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002691 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002693 while (p[0] == '&' && p[1] == '&')
2694 {
2695 if (getnext)
2696 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002697 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002698 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002699 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002700 {
2701 error_white_both(p, 2);
2702 clear_tv(rettv);
2703 return FAIL;
2704 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002705 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002706 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002707
2708 /*
2709 * Get the second variable.
2710 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002711 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2712 {
mityu4ac198c2021-05-28 17:52:40 +02002713 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002714 clear_tv(rettv);
2715 return FAIL;
2716 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002717 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2718 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002719 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002720 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002721 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002722 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002723
2724 /*
2725 * Compute the result.
2726 */
2727 if (evaluate && result)
2728 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002729 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002730 result = tv_get_bool_chk(&var2, &error);
2731 else if (tv_get_number_chk(&var2, &error) == 0)
2732 result = FALSE;
2733 clear_tv(&var2);
2734 if (error)
2735 return FAIL;
2736 }
2737 if (evaluate)
2738 {
2739 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002740 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002741 rettv->v_type = VAR_BOOL;
2742 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002743 }
2744 else
2745 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002746 rettv->v_type = VAR_NUMBER;
2747 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002748 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002749 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002750
2751 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002753
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002754 if (evalarg == NULL)
2755 clear_evalarg(&local_evalarg, NULL);
2756 else
2757 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 }
2759
2760 return OK;
2761}
2762
2763/*
2764 * Handle third level expression:
2765 * var1 == var2
2766 * var1 =~ var2
2767 * var1 != var2
2768 * var1 !~ var2
2769 * var1 > var2
2770 * var1 >= var2
2771 * var1 < var2
2772 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002773 * var1 is var2
2774 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 *
2776 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002777 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 *
2779 * Return OK or FAIL.
2780 */
2781 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002782eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002785 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002786 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002788 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789
2790 /*
2791 * Get the first variable.
2792 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002793 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 return FAIL;
2795
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002796 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002797 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798
2799 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002800 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002802 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002804 typval_T var2;
2805 int ic;
2806 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002807 int evaluate = evalarg == NULL
2808 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002809
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002810 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002811 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002812 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002813 p = *arg;
2814 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002815 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2816 {
mityu4ac198c2021-05-28 17:52:40 +02002817 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002818 clear_tv(rettv);
2819 return FAIL;
2820 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002821
Bram Moolenaar696ba232020-07-29 21:20:41 +02002822 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2823 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002824 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002825 clear_tv(rettv);
2826 return FAIL;
2827 }
2828
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002829 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 if (p[len] == '?')
2831 {
2832 ic = TRUE;
2833 ++len;
2834 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002835 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 else if (p[len] == '#')
2837 {
2838 ic = FALSE;
2839 ++len;
2840 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002841 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002843 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844
2845 /*
2846 * Get the second variable.
2847 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002848 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2849 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002850 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002851 clear_tv(rettv);
2852 return FAIL;
2853 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002854 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002855 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002857 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 return FAIL;
2859 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002860 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002861 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002862 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002863
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002864 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002865 {
2866 ret = FAIL;
2867 clear_tv(rettv);
2868 }
2869 else
2870 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002871 clear_tv(&var2);
2872 return ret;
2873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 }
2875
2876 return OK;
2877}
2878
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002879/*
2880 * Make a copy of blob "tv1" and append blob "tv2".
2881 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 void
2883eval_addblob(typval_T *tv1, typval_T *tv2)
2884{
2885 blob_T *b1 = tv1->vval.v_blob;
2886 blob_T *b2 = tv2->vval.v_blob;
2887 blob_T *b = blob_alloc();
2888 int i;
2889
2890 if (b != NULL)
2891 {
2892 for (i = 0; i < blob_len(b1); i++)
2893 ga_append(&b->bv_ga, blob_get(b1, i));
2894 for (i = 0; i < blob_len(b2); i++)
2895 ga_append(&b->bv_ga, blob_get(b2, i));
2896
2897 clear_tv(tv1);
2898 rettv_blob_set(tv1, b);
2899 }
2900}
2901
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002902/*
2903 * Make a copy of list "tv1" and append list "tv2".
2904 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002905 int
2906eval_addlist(typval_T *tv1, typval_T *tv2)
2907{
2908 typval_T var3;
2909
2910 // concatenate Lists
2911 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2912 {
2913 clear_tv(tv1);
2914 clear_tv(tv2);
2915 return FAIL;
2916 }
2917 clear_tv(tv1);
2918 *tv1 = var3;
2919 return OK;
2920}
2921
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922/*
2923 * Handle fourth level expression:
2924 * + number addition
2925 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002926 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002927 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 *
2929 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002930 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 *
2932 * Return OK or FAIL.
2933 */
2934 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002935eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 /*
2938 * Get the first variable.
2939 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002940 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 return FAIL;
2942
2943 /*
2944 * Repeat computing, until no '+', '-' or '.' is following.
2945 */
2946 for (;;)
2947 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002948 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002949 int getnext;
2950 char_u *p;
2951 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002952 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002953 int concat;
2954 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002955 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002956
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002957 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002958 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002959 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002960 p = eval_next_non_blank(*arg, evalarg, &getnext);
2961 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02002962 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002963 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2964 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002966 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
2967 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002968
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002969 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002970 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002971 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002972 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002973 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002974 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002975 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002976 {
mityu4ac198c2021-05-28 17:52:40 +02002977 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002978 clear_tv(rettv);
2979 return FAIL;
2980 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002981 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002982 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002983 if ((op != '+' || (rettv->v_type != VAR_LIST
2984 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002985#ifdef FEAT_FLOAT
2986 && (op == '.' || rettv->v_type != VAR_FLOAT)
2987#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002988 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002989 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002990 int error = FALSE;
2991
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002992 // For "list + ...", an illegal use of the first operand as
2993 // a number cannot be determined before evaluating the 2nd
2994 // operand: if this is also a list, all is ok.
2995 // For "something . ...", "something - ..." or "non-list + ...",
2996 // we know that the first operand needs to be a string or number
2997 // without evaluating the 2nd operand. So check before to avoid
2998 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002999 if (op != '.')
3000 tv_get_number_chk(rettv, &error);
3001 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003002 {
3003 clear_tv(rettv);
3004 return FAIL;
3005 }
3006 }
3007
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 /*
3009 * Get the second variable.
3010 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003011 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003012 {
mityu89dcb4d2021-05-28 13:50:17 +02003013 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003014 clear_tv(rettv);
3015 return FAIL;
3016 }
3017 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02003018 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003020 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 return FAIL;
3022 }
3023
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003024 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 {
3026 /*
3027 * Compute the result.
3028 */
3029 if (op == '.')
3030 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003031 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3032 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003033 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003034
Bram Moolenaar2e086612020-08-25 22:37:48 +02003035 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003036 || var2.v_type == VAR_CHANNEL
3037 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003038 semsg(_(e_using_invalid_value_as_string_str),
3039 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003040#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003041 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003042 {
3043 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3044 var2.vval.v_float);
3045 s2 = buf2;
3046 }
3047#endif
3048 else
3049 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003050 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003051 {
3052 clear_tv(rettv);
3053 clear_tv(&var2);
3054 return FAIL;
3055 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003056 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003057 clear_tv(rettv);
3058 rettv->v_type = VAR_STRING;
3059 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003061 else if (op == '+' && rettv->v_type == VAR_BLOB
3062 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003063 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003064 else if (op == '+' && rettv->v_type == VAR_LIST
3065 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003066 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003068 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 else
3071 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003072 int error = FALSE;
3073 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003074#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003075 float_T f1 = 0, f2 = 0;
3076
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003077 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003078 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003079 f1 = rettv->vval.v_float;
3080 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003081 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003082 else
3083#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003084 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003085 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003086 if (error)
3087 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003088 // This can only happen for "list + non-list" or
3089 // "blob + non-blob". For "non-list + ..." or
3090 // "something - ...", we returned before evaluating the
3091 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003092 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003093 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003094 return FAIL;
3095 }
3096#ifdef FEAT_FLOAT
3097 if (var2.v_type == VAR_FLOAT)
3098 f1 = n1;
3099#endif
3100 }
3101#ifdef FEAT_FLOAT
3102 if (var2.v_type == VAR_FLOAT)
3103 {
3104 f2 = var2.vval.v_float;
3105 n2 = 0;
3106 }
3107 else
3108#endif
3109 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003110 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003111 if (error)
3112 {
3113 clear_tv(rettv);
3114 clear_tv(&var2);
3115 return FAIL;
3116 }
3117#ifdef FEAT_FLOAT
3118 if (rettv->v_type == VAR_FLOAT)
3119 f2 = n2;
3120#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003121 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003122 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003123
3124#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003125 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003126 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3127 {
3128 if (op == '+')
3129 f1 = f1 + f2;
3130 else
3131 f1 = f1 - f2;
3132 rettv->v_type = VAR_FLOAT;
3133 rettv->vval.v_float = f1;
3134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003136#endif
3137 {
3138 if (op == '+')
3139 n1 = n1 + n2;
3140 else
3141 n1 = n1 - n2;
3142 rettv->v_type = VAR_NUMBER;
3143 rettv->vval.v_number = n1;
3144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003146 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 }
3148 }
3149 return OK;
3150}
3151
3152/*
3153 * Handle fifth level expression:
3154 * * number multiplication
3155 * / number division
3156 * % number modulo
3157 *
3158 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003159 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 *
3161 * Return OK or FAIL.
3162 */
3163 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003164eval6(
3165 char_u **arg,
3166 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003167 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003168 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003170#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003171 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173
3174 /*
3175 * Get the first variable.
3176 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003177 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 return FAIL;
3179
3180 /*
3181 * Repeat computing, until no '*', '/' or '%' is following.
3182 */
3183 for (;;)
3184 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003185 int evaluate;
3186 int getnext;
3187 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003188 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003189 int op;
3190 varnumber_T n1, n2;
3191#ifdef FEAT_FLOAT
3192 float_T f1, f2;
3193#endif
3194 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003195
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003196 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003197 p = eval_next_non_blank(*arg, evalarg, &getnext);
3198 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003199 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003201
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003202 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003203 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003204 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003205 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003206 {
3207 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3208 {
mityu4ac198c2021-05-28 17:52:40 +02003209 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003210 clear_tv(rettv);
3211 return FAIL;
3212 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003213 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003216#ifdef FEAT_FLOAT
3217 f1 = 0;
3218 f2 = 0;
3219#endif
3220 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003221 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003223#ifdef FEAT_FLOAT
3224 if (rettv->v_type == VAR_FLOAT)
3225 {
3226 f1 = rettv->vval.v_float;
3227 use_float = TRUE;
3228 n1 = 0;
3229 }
3230 else
3231#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003232 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003233 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003234 if (error)
3235 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 }
3237 else
3238 n1 = 0;
3239
3240 /*
3241 * Get the second variable.
3242 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003243 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3244 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003245 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003246 clear_tv(rettv);
3247 return FAIL;
3248 }
3249 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003250 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 return FAIL;
3252
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003253 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003255#ifdef FEAT_FLOAT
3256 if (var2.v_type == VAR_FLOAT)
3257 {
3258 if (!use_float)
3259 {
3260 f1 = n1;
3261 use_float = TRUE;
3262 }
3263 f2 = var2.vval.v_float;
3264 n2 = 0;
3265 }
3266 else
3267#endif
3268 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003269 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003270 clear_tv(&var2);
3271 if (error)
3272 return FAIL;
3273#ifdef FEAT_FLOAT
3274 if (use_float)
3275 f2 = n2;
3276#endif
3277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278
3279 /*
3280 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003281 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003283#ifdef FEAT_FLOAT
3284 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003286 if (op == '*')
3287 f1 = f1 * f2;
3288 else if (op == '/')
3289 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003290# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003291 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003292 if (f2 == 0.0)
3293 {
3294 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003295 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003296 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003297 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003298 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003299 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003300 }
3301 else
3302 f1 = f1 / f2;
3303# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003304 // We rely on the floating point library to handle divide
3305 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003306 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003307# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003310 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003311 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003312 return FAIL;
3313 }
3314 rettv->v_type = VAR_FLOAT;
3315 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 }
3317 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003320 int failed = FALSE;
3321
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003322 if (op == '*')
3323 n1 = n1 * n2;
3324 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003325 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003327 n1 = num_modulus(n1, n2, &failed);
3328 if (failed)
3329 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003330
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003331 rettv->v_type = VAR_NUMBER;
3332 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 }
3335 }
3336
3337 return OK;
3338}
3339
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003340/*
3341 * Handle a type cast before a base level expression.
3342 * "arg" must point to the first non-white of the expression.
3343 * "arg" is advanced to just after the recognized expression.
3344 * Return OK or FAIL.
3345 */
3346 static int
3347eval7t(
3348 char_u **arg,
3349 typval_T *rettv,
3350 evalarg_T *evalarg,
3351 int want_string) // after "." operator
3352{
3353 type_T *want_type = NULL;
3354 garray_T type_list; // list of pointers to allocated types
3355 int res;
3356 int evaluate = evalarg == NULL ? 0
3357 : (evalarg->eval_flags & EVAL_EVALUATE);
3358
3359 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003360 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3361 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003362 {
3363 ++*arg;
3364 ga_init2(&type_list, sizeof(type_T *), 10);
3365 want_type = parse_type(arg, &type_list, TRUE);
3366 if (want_type == NULL && (evaluate || **arg != '>'))
3367 {
3368 clear_type_list(&type_list);
3369 return FAIL;
3370 }
3371
3372 if (**arg != '>')
3373 {
3374 if (*skipwhite(*arg) == '>')
3375 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3376 else
3377 emsg(_(e_missing_gt));
3378 clear_type_list(&type_list);
3379 return FAIL;
3380 }
3381 ++*arg;
3382 *arg = skipwhite_and_linebreak(*arg, evalarg);
3383 }
3384
3385 res = eval7(arg, rettv, evalarg, want_string);
3386
3387 if (want_type != NULL && evaluate)
3388 {
3389 if (res == OK)
3390 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003391 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3392 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003393
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003394 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003395 {
3396 if (want_type == &t_bool && actual != &t_bool
3397 && (actual->tt_flags & TTFLAG_BOOL_OK))
3398 {
3399 int n = tv2bool(rettv);
3400
3401 // can use "0" and "1" for boolean in some places
3402 clear_tv(rettv);
3403 rettv->v_type = VAR_BOOL;
3404 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3405 }
3406 else
3407 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003408 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003409
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003410 where.wt_variable = TRUE;
3411 res = check_type(want_type, actual, TRUE, where);
3412 }
3413 }
3414 }
3415 clear_type_list(&type_list);
3416 }
3417
3418 return res;
3419}
3420
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003421 int
3422eval_leader(char_u **arg, int vim9)
3423{
3424 char_u *s = *arg;
3425 char_u *p = *arg;
3426
3427 while (*p == '!' || *p == '-' || *p == '+')
3428 {
3429 char_u *n = skipwhite(p + 1);
3430
3431 // ++, --, -+ and +- are not accepted in Vim9 script
3432 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3433 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003434 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003435 return FAIL;
3436 }
3437 p = n;
3438 }
3439 *arg = p;
3440 return OK;
3441}
3442
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443/*
3444 * Handle sixth level expression:
3445 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003446 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003447 * "string" string constant
3448 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 * &option-name option value
3450 * @r register contents
3451 * identifier variable value
3452 * function() function call
3453 * $VAR environment variable
3454 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003455 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003457 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003458 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 *
3460 * Also handle:
3461 * ! in front logical NOT
3462 * - in front unary minus
3463 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003464 * trailing [] subscript in String or List
3465 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003466 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 *
3468 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003469 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 *
3471 * Return OK or FAIL.
3472 */
3473 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003474eval7(
3475 char_u **arg,
3476 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003477 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003480 int evaluate = evalarg != NULL
3481 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 int len;
3483 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003484 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 char_u *start_leader, *end_leader;
3486 int ret = OK;
3487 char_u *alias;
3488
3489 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003490 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003491 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003493 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494
3495 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003496 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 */
3498 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003499 if (eval_leader(arg, in_vim9script()) == FAIL)
3500 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 end_leader = *arg;
3502
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003503 if (**arg == '.' && (!isdigit(*(*arg + 1))
3504#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003505 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003506#endif
3507 ))
3508 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003509 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003510 ++*arg;
3511 return FAIL;
3512 }
3513
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 switch (**arg)
3515 {
3516 /*
3517 * Number constant.
3518 */
3519 case '0':
3520 case '1':
3521 case '2':
3522 case '3':
3523 case '4':
3524 case '5':
3525 case '6':
3526 case '7':
3527 case '8':
3528 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003529 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003530
3531 // Apply prefixed "-" and "+" now. Matters especially when
3532 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003533 if (ret == OK && evaluate && end_leader > start_leader
3534 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003535 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 break;
3537
3538 /*
3539 * String constant: "string".
3540 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003541 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 break;
3543
3544 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003545 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003547 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003548 break;
3549
3550 /*
3551 * List: [expr, expr]
3552 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003553 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 break;
3555
3556 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003557 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003558 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003559 case '#': if (in_vim9script())
3560 {
3561 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3562 }
3563 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003564 {
3565 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003566 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003567 }
3568 else
3569 ret = NOTDONE;
3570 break;
3571
3572 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003573 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003574 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003575 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003576 case '{': if (in_vim9script())
3577 ret = NOTDONE;
3578 else
3579 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003580 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003581 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003582 break;
3583
3584 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003585 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003587 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 break;
3589
3590 /*
3591 * Environment variable: $VAR.
3592 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003593 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 break;
3595
3596 /*
3597 * Register contents: @r.
3598 */
3599 case '@': ++*arg;
3600 if (evaluate)
3601 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003602 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3603 semsg(_(e_syntax_error_at_str), *arg);
3604 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3605 emsg_invreg(**arg);
3606 else
3607 {
3608 rettv->v_type = VAR_STRING;
3609 rettv->vval.v_string = get_reg_contents(**arg,
3610 GREG_EXPR_SRC);
3611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 }
3613 if (**arg != NUL)
3614 ++*arg;
3615 break;
3616
3617 /*
3618 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003619 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003621 case '(': ret = NOTDONE;
3622 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003623 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003624 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003625 if (ret == OK && evaluate)
3626 {
3627 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3628
Bram Moolenaara9931532021-06-12 15:58:16 +02003629 // Compile it here to get the return type. The return
3630 // type is optional, when it's missing use t_unknown.
3631 // This is recognized in compile_return().
3632 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3633 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003634 if (compile_def_function(ufunc,
Bram Moolenaare99d4222021-06-13 14:01:26 +02003635 FALSE, COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003636 {
3637 clear_tv(rettv);
3638 ret = FAIL;
3639 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003640 }
3641 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003642 if (ret == NOTDONE)
3643 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003644 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003645 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003646
Bram Moolenaar9215f012020-06-27 21:18:00 +02003647 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003648 if (**arg == ')')
3649 ++*arg;
3650 else if (ret == OK)
3651 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003652 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003653 clear_tv(rettv);
3654 ret = FAIL;
3655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 }
3657 break;
3658
Bram Moolenaar8c711452005-01-14 21:53:12 +00003659 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 break;
3661 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003662
3663 if (ret == NOTDONE)
3664 {
3665 /*
3666 * Must be a variable or function name.
3667 * Can also be a curly-braces kind of name: {expr}.
3668 */
3669 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003670 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003671 if (alias != NULL)
3672 s = alias;
3673
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003674 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003675 ret = FAIL;
3676 else
3677 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003678 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3679
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003680 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003681 {
3682 emsg(_(e_cannot_use_underscore_here));
3683 ret = FAIL;
3684 }
3685 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003686 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003687 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003688 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003689 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003690 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003691 else if (flags & EVAL_CONSTANT)
3692 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003693 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003694 {
3695 // get the value of "true", "false" or a variable
3696 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3697 {
3698 rettv->v_type = VAR_BOOL;
3699 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003700 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003701 }
3702 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003703 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003704 {
3705 rettv->v_type = VAR_BOOL;
3706 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003707 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003708 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003709 else if (len == 4 && in_vim9script()
3710 && STRNCMP(s, "null", 4) == 0)
3711 {
3712 rettv->v_type = VAR_SPECIAL;
3713 rettv->vval.v_number = VVAL_NULL;
3714 ret = OK;
3715 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003716 else
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003717 {
3718 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003719 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003720 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003721 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003722 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003723 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003724 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003725 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003726 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003727 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003728 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003729 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003730 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003731 }
3732
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003733 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3734 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003735 if (ret == OK)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003736 ret = handle_subscript(arg, name_start, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737
3738 /*
3739 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3740 */
3741 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003742 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003743 return ret;
3744}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003745
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003746/*
3747 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003748 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003749 * Adjusts "end_leaderp" until it is at "start_leader".
3750 */
3751 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003752eval7_leader(
3753 typval_T *rettv,
3754 int numeric_only,
3755 char_u *start_leader,
3756 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003757{
3758 char_u *end_leader = *end_leaderp;
3759 int ret = OK;
3760 int error = FALSE;
3761 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003762 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003763#ifdef FEAT_FLOAT
3764 float_T f = 0.0;
3765
3766 if (rettv->v_type == VAR_FLOAT)
3767 f = rettv->vval.v_float;
3768 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003769#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003770 {
3771 while (VIM_ISWHITE(end_leader[-1]))
3772 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003773 if (in_vim9script() && end_leader[-1] == '!')
3774 val = tv2bool(rettv);
3775 else
3776 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003777 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003778 if (error)
3779 {
3780 clear_tv(rettv);
3781 ret = FAIL;
3782 }
3783 else
3784 {
3785 while (end_leader > start_leader)
3786 {
3787 --end_leader;
3788 if (*end_leader == '!')
3789 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003790 if (numeric_only)
3791 {
3792 ++end_leader;
3793 break;
3794 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003795#ifdef FEAT_FLOAT
3796 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003797 {
3798 if (in_vim9script())
3799 {
3800 rettv->v_type = VAR_BOOL;
3801 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3802 }
3803 else
3804 f = !f;
3805 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003806 else
3807#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003808 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003809 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003810 type = VAR_BOOL;
3811 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003812 }
3813 else if (*end_leader == '-')
3814 {
3815#ifdef FEAT_FLOAT
3816 if (rettv->v_type == VAR_FLOAT)
3817 f = -f;
3818 else
3819#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003820 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003821 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003822 type = VAR_NUMBER;
3823 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003824 }
3825 }
3826#ifdef FEAT_FLOAT
3827 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003829 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003830 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003832 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003833#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003834 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003835 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003836 if (in_vim9script())
3837 rettv->v_type = type;
3838 else
3839 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003840 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003843 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 return ret;
3845}
3846
3847/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003848 * Call the function referred to in "rettv".
3849 */
3850 static int
3851call_func_rettv(
3852 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003853 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003854 typval_T *rettv,
3855 int evaluate,
3856 dict_T *selfdict,
3857 typval_T *basetv)
3858{
3859 partial_T *pt = NULL;
3860 funcexe_T funcexe;
3861 typval_T functv;
3862 char_u *s;
3863 int ret;
3864
3865 // need to copy the funcref so that we can clear rettv
3866 if (evaluate)
3867 {
3868 functv = *rettv;
3869 rettv->v_type = VAR_UNKNOWN;
3870
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003871 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003872 if (functv.v_type == VAR_PARTIAL)
3873 {
3874 pt = functv.vval.v_partial;
3875 s = partial_name(pt);
3876 }
3877 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003878 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003879 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003880 if (s == NULL || *s == NUL)
3881 {
3882 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02003883 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003884 goto theend;
3885 }
3886 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003887 }
3888 else
3889 s = (char_u *)"";
3890
Bram Moolenaara80faa82020-04-12 19:37:17 +02003891 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003892 funcexe.fe_firstline = curwin->w_cursor.lnum;
3893 funcexe.fe_lastline = curwin->w_cursor.lnum;
3894 funcexe.fe_evaluate = evaluate;
3895 funcexe.fe_partial = pt;
3896 funcexe.fe_selfdict = selfdict;
3897 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003898 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003899
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003900theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003901 // Clear the funcref afterwards, so that deleting it while
3902 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003903 if (evaluate)
3904 clear_tv(&functv);
3905
3906 return ret;
3907}
3908
3909/*
3910 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003911 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003912 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3913 */
3914 static int
3915eval_lambda(
3916 char_u **arg,
3917 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003918 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003919 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003920{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003921 int evaluate = evalarg != NULL
3922 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003923 typval_T base = *rettv;
3924 int ret;
3925
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003926 rettv->v_type = VAR_UNKNOWN;
3927
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003928 if (**arg == '{')
3929 {
3930 // ->{lambda}()
3931 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3932 }
3933 else
3934 {
3935 // ->(lambda)()
3936 ++*arg;
3937 ret = eval1(arg, rettv, evalarg);
3938 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar605ec912021-12-18 16:54:31 +00003939 if (**arg == ')')
3940 {
3941 ++*arg;
3942 }
3943 else
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003944 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003945 emsg(_(e_missing_closing_paren));
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003946 ret = FAIL;
3947 }
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003948 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003949 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003950 return FAIL;
3951 else if (**arg != '(')
3952 {
3953 if (verbose)
3954 {
3955 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00003956 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003957 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003958 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003959 }
3960 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003961 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003962 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003963 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003964 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003965
3966 // Clear the funcref afterwards, so that deleting it while
3967 // evaluating the arguments is possible (see test55).
3968 if (evaluate)
3969 clear_tv(&base);
3970
3971 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003972}
3973
3974/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003975 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003976 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003977 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3978 */
3979 static int
3980eval_method(
3981 char_u **arg,
3982 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003983 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003984 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003985{
3986 char_u *name;
3987 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003988 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003989 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003990 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003991 int evaluate = evalarg != NULL
3992 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003993
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003994 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003995
Bram Moolenaarac92e252019-08-03 21:58:38 +02003996 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003997 len = get_name_len(arg, &alias, evaluate, TRUE);
3998 if (alias != NULL)
3999 name = alias;
4000
4001 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004002 {
4003 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004004 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004005 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004006 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004007 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004008 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004009 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004010 if (**arg != '(')
4011 {
4012 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004013 semsg(_(e_missing_parenthesis_str), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004014 ret = FAIL;
4015 }
Bram Moolenaar51841322019-08-08 21:10:01 +02004016 else if (VIM_ISWHITE((*arg)[-1]))
4017 {
4018 if (verbose)
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004019 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar51841322019-08-08 21:10:01 +02004020 ret = FAIL;
4021 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004022 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004023 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004024 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004025 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004026
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004027 // Clear the funcref afterwards, so that deleting it while
4028 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004029 if (evaluate)
4030 clear_tv(&base);
4031
Bram Moolenaarac92e252019-08-03 21:58:38 +02004032 return ret;
4033}
4034
4035/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004036 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4037 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004038 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4039 */
4040 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004041eval_index(
4042 char_u **arg,
4043 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004044 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004045 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004046{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004047 int evaluate = evalarg != NULL
4048 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004049 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004050 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004051 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004052 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004053 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004054 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004055
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004056 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4057 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004058
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004059 init_tv(&var1);
4060 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004061 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004062 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004063 /*
4064 * dict.name
4065 */
4066 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004067 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004068 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004069 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004070 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004071 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004072 }
4073 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004074 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004075 /*
4076 * something[idx]
4077 *
4078 * Get the (first) variable from inside the [].
4079 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004080 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004081 if (**arg == ':')
4082 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004083 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004084 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004085 else if (vim9 && **arg == ':')
4086 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004087 semsg(_(e_white_space_required_before_and_after_str_at_str),
4088 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004089 clear_tv(&var1);
4090 return FAIL;
4091 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004092 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004093 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004094 int error = FALSE;
4095
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004096#ifdef FEAT_FLOAT
4097 // allow for indexing with float
4098 if (vim9 && rettv->v_type == VAR_DICT
4099 && var1.v_type == VAR_FLOAT)
4100 {
4101 var1.vval.v_string = typval_tostring(&var1, TRUE);
4102 var1.v_type = VAR_STRING;
4103 }
4104#endif
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004105 if (vim9 && rettv->v_type == VAR_LIST)
4106 tv_get_number_chk(&var1, &error);
4107 else
4108 error = tv_get_string_chk(&var1) == NULL;
4109 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004110 {
4111 // not a number or string
4112 clear_tv(&var1);
4113 return FAIL;
4114 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004115 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004116
4117 /*
4118 * Get the second variable from inside the [:].
4119 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004120 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004121 if (**arg == ':')
4122 {
4123 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004124 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004125 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004126 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004127 semsg(_(e_white_space_required_before_and_after_str_at_str),
4128 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004129 if (!empty1)
4130 clear_tv(&var1);
4131 return FAIL;
4132 }
4133 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004134 if (**arg == ']')
4135 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004136 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004137 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004138 if (!empty1)
4139 clear_tv(&var1);
4140 return FAIL;
4141 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004142 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004143 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004144 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004145 if (!empty1)
4146 clear_tv(&var1);
4147 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004148 return FAIL;
4149 }
4150 }
4151
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004152 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004153 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004154 if (**arg != ']')
4155 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004156 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004157 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004158 clear_tv(&var1);
4159 if (range)
4160 clear_tv(&var2);
4161 return FAIL;
4162 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004163 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004164 }
4165
4166 if (evaluate)
4167 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004168 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004169 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004170 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004171
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004172 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004173 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004174 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004175 clear_tv(&var2);
4176 return res;
4177 }
4178 return OK;
4179}
4180
4181/*
4182 * Check if "rettv" can have an [index] or [sli:ce]
4183 */
4184 int
4185check_can_index(typval_T *rettv, int evaluate, int verbose)
4186{
4187 switch (rettv->v_type)
4188 {
4189 case VAR_FUNC:
4190 case VAR_PARTIAL:
4191 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004192 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004193 return FAIL;
4194 case VAR_FLOAT:
4195#ifdef FEAT_FLOAT
4196 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004197 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004198 return FAIL;
4199#endif
4200 case VAR_BOOL:
4201 case VAR_SPECIAL:
4202 case VAR_JOB:
4203 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004204 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004205 if (verbose)
4206 emsg(_(e_cannot_index_special_variable));
4207 return FAIL;
4208 case VAR_UNKNOWN:
4209 case VAR_ANY:
4210 case VAR_VOID:
4211 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004212 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004213 emsg(_(e_cannot_index_special_variable));
4214 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004215 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004216 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004217
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004218 case VAR_STRING:
4219 case VAR_LIST:
4220 case VAR_DICT:
4221 case VAR_BLOB:
4222 break;
4223 case VAR_NUMBER:
4224 if (in_vim9script())
4225 emsg(_(e_cannot_index_number));
4226 break;
4227 }
4228 return OK;
4229}
4230
4231/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004232 * slice() function
4233 */
4234 void
4235f_slice(typval_T *argvars, typval_T *rettv)
4236{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004237 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004238 && ((argvars[0].v_type != VAR_STRING
4239 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004240 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004241 && check_for_list_arg(argvars, 0) == FAIL)
4242 || check_for_number_arg(argvars, 1) == FAIL
4243 || check_for_opt_number_arg(argvars, 2) == FAIL))
4244 return;
4245
Bram Moolenaar6601b622021-01-13 21:47:15 +01004246 if (check_can_index(argvars, TRUE, FALSE) == OK)
4247 {
4248 copy_tv(argvars, rettv);
4249 eval_index_inner(rettv, TRUE, argvars + 1,
4250 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4251 TRUE, NULL, 0, FALSE);
4252 }
4253}
4254
4255/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004256 * Apply index or range to "rettv".
4257 * "var1" is the first index, NULL for [:expr].
4258 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004259 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4260 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004261 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4262 */
4263 int
4264eval_index_inner(
4265 typval_T *rettv,
4266 int is_range,
4267 typval_T *var1,
4268 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004269 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004270 char_u *key,
4271 int keylen,
4272 int verbose)
4273{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004274 varnumber_T n1, n2 = 0;
4275 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004276
4277 n1 = 0;
4278 if (var1 != NULL && rettv->v_type != VAR_DICT)
4279 n1 = tv_get_number(var1);
4280
4281 if (is_range)
4282 {
4283 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004284 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004285 if (verbose)
4286 emsg(_(e_cannot_slice_dictionary));
4287 return FAIL;
4288 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004289 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004290 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004291 else
4292 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004293 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004294
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004295 switch (rettv->v_type)
4296 {
4297 case VAR_UNKNOWN:
4298 case VAR_ANY:
4299 case VAR_VOID:
4300 case VAR_FUNC:
4301 case VAR_PARTIAL:
4302 case VAR_FLOAT:
4303 case VAR_BOOL:
4304 case VAR_SPECIAL:
4305 case VAR_JOB:
4306 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004307 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004308 break; // not evaluating, skipping over subscript
4309
4310 case VAR_NUMBER:
4311 case VAR_STRING:
4312 {
4313 char_u *s = tv_get_string(rettv);
4314
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004315 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004316 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004317 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004318 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004319 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004320 else
4321 s = char_from_string(s, n1);
4322 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004323 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004324 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004325 // The resulting variable is a substring. If the indexes
4326 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004327 if (n1 < 0)
4328 {
4329 n1 = len + n1;
4330 if (n1 < 0)
4331 n1 = 0;
4332 }
4333 if (n2 < 0)
4334 n2 = len + n2;
4335 else if (n2 >= len)
4336 n2 = len;
4337 if (n1 >= len || n2 < 0 || n1 > n2)
4338 s = NULL;
4339 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004340 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004341 }
4342 else
4343 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004344 // The resulting variable is a string of a single
4345 // character. If the index is too big or negative the
4346 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004347 if (n1 >= len || n1 < 0)
4348 s = NULL;
4349 else
4350 s = vim_strnsave(s + n1, 1);
4351 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004352 clear_tv(rettv);
4353 rettv->v_type = VAR_STRING;
4354 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004355 }
4356 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004357
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004358 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004359 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4360 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004361 break;
4362
4363 case VAR_LIST:
4364 if (var1 == NULL)
4365 n1 = 0;
4366 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004367 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004368 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004369 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004370 return FAIL;
4371 break;
4372
4373 case VAR_DICT:
4374 {
4375 dictitem_T *item;
4376 typval_T tmp;
4377
4378 if (key == NULL)
4379 {
4380 key = tv_get_string_chk(var1);
4381 if (key == NULL)
4382 return FAIL;
4383 }
4384
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004385 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004386
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004387 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004388 {
4389 if (verbose)
4390 {
4391 if (keylen > 0)
4392 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004393 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004394 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004395 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004396 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004397
4398 copy_tv(&item->di_tv, &tmp);
4399 clear_tv(rettv);
4400 *rettv = tmp;
4401 }
4402 break;
4403 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004404 return OK;
4405}
4406
4407/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004408 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004409 */
4410 char_u *
4411partial_name(partial_T *pt)
4412{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004413 if (pt != NULL)
4414 {
4415 if (pt->pt_name != NULL)
4416 return pt->pt_name;
4417 if (pt->pt_func != NULL)
4418 return pt->pt_func->uf_name;
4419 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004420 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004421}
4422
Bram Moolenaarddecc252016-04-06 22:59:37 +02004423 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004424partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004425{
4426 int i;
4427
4428 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004429 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004430 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004431 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004432 if (pt->pt_name != NULL)
4433 {
4434 func_unref(pt->pt_name);
4435 vim_free(pt->pt_name);
4436 }
4437 else
4438 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004439
Bram Moolenaar54656012021-06-09 20:50:46 +02004440 // "out_up" is no longer used, decrement refcount on partial that owns it.
4441 partial_unref(pt->pt_outer.out_up_partial);
4442
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004443 // Decrease the reference count for the context of a closure. If down
4444 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004445 if (pt->pt_funcstack != NULL)
4446 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004447 --pt->pt_funcstack->fs_refcount;
4448 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004449 }
4450
Bram Moolenaarddecc252016-04-06 22:59:37 +02004451 vim_free(pt);
4452}
4453
4454/*
4455 * Unreference a closure: decrement the reference count and free it when it
4456 * becomes zero.
4457 */
4458 void
4459partial_unref(partial_T *pt)
4460{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004461 if (pt != NULL)
4462 {
4463 if (--pt->pt_refcount <= 0)
4464 partial_free(pt);
4465
4466 // If the reference count goes down to one, the funcstack may be the
4467 // only reference and can be freed if no other partials reference it.
4468 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4469 funcstack_check_refcount(pt->pt_funcstack);
4470 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004471}
4472
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004473/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004474 * Return the next (unique) copy ID.
4475 * Used for serializing nested structures.
4476 */
4477 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004478get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004479{
4480 current_copyID += COPYID_INC;
4481 return current_copyID;
4482}
4483
4484/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004485 * Garbage collection for lists and dictionaries.
4486 *
4487 * We use reference counts to be able to free most items right away when they
4488 * are no longer used. But for composite items it's possible that it becomes
4489 * unused while the reference count is > 0: When there is a recursive
4490 * reference. Example:
4491 * :let l = [1, 2, 3]
4492 * :let d = {9: l}
4493 * :let l[1] = d
4494 *
4495 * Since this is quite unusual we handle this with garbage collection: every
4496 * once in a while find out which lists and dicts are not referenced from any
4497 * variable.
4498 *
4499 * Here is a good reference text about garbage collection (refers to Python
4500 * but it applies to all reference-counting mechanisms):
4501 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004502 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004503
4504/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004505 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004506 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004507 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004508 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004509 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004510garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004511{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004512 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004513 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004514 buf_T *buf;
4515 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004516 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004517 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004518
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004519 if (!testing)
4520 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004521 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004522 want_garbage_collect = FALSE;
4523 may_garbage_collect = FALSE;
4524 garbage_collect_at_exit = FALSE;
4525 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004526
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004527 // The execution stack can grow big, limit the size.
4528 if (exestack.ga_maxlen - exestack.ga_len > 500)
4529 {
4530 size_t new_len;
4531 char_u *pp;
4532 int n;
4533
4534 // Keep 150% of the current size, with a minimum of the growth size.
4535 n = exestack.ga_len / 2;
4536 if (n < exestack.ga_growsize)
4537 n = exestack.ga_growsize;
4538
4539 // Don't make it bigger though.
4540 if (exestack.ga_len + n < exestack.ga_maxlen)
4541 {
4542 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4543 pp = vim_realloc(exestack.ga_data, new_len);
4544 if (pp == NULL)
4545 return FAIL;
4546 exestack.ga_maxlen = exestack.ga_len + n;
4547 exestack.ga_data = pp;
4548 }
4549 }
4550
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004551 // We advance by two because we add one for items referenced through
4552 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004553 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004554
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004555 /*
4556 * 1. Go through all accessible variables and mark all lists and dicts
4557 * with copyID.
4558 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004559
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004560 // Don't free variables in the previous_funccal list unless they are only
4561 // referenced through previous_funccal. This must be first, because if
4562 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004563 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004564
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004565 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004566 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004567
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004568 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004569 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004570 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4571 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004572
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004573 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004574 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004575 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4576 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004577 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004578 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4579 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004580#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004581 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004582 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4583 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004584 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004585 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004586 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4587 NULL, NULL);
4588#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004589
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004590 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004591 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004592 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4593 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004594 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004595 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004596
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004597 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004598 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004599
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004600 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004601 abort = abort || set_ref_in_functions(copyID);
4602
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004603 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004604 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004605
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004606 // funcstacks keep variables for closures
4607 abort = abort || set_ref_in_funcstacks(copyID);
4608
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004609 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004610 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004611
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004612 // callbacks in buffers
4613 abort = abort || set_ref_in_buffers(copyID);
4614
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004615 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
4616 abort = abort || set_ref_in_insexpand_funcs(copyID);
4617
4618 // 'operatorfunc' callback
4619 abort = abort || set_ref_in_opfunc(copyID);
4620
4621 // 'tagfunc' callback
4622 abort = abort || set_ref_in_tagfunc(copyID);
4623
4624 // 'imactivatefunc' and 'imstatusfunc' callbacks
4625 abort = abort || set_ref_in_im_funcs(copyID);
4626
Bram Moolenaar1dced572012-04-05 16:54:08 +02004627#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004628 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004629#endif
4630
Bram Moolenaardb913952012-06-29 12:54:53 +02004631#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004632 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004633#endif
4634
4635#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004636 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004637#endif
4638
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004639#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004640 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004641 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004642#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004643#ifdef FEAT_NETBEANS_INTG
4644 abort = abort || set_ref_in_nb_channel(copyID);
4645#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004646
Bram Moolenaare3188e22016-05-31 21:13:04 +02004647#ifdef FEAT_TIMERS
4648 abort = abort || set_ref_in_timer(copyID);
4649#endif
4650
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004651#ifdef FEAT_QUICKFIX
4652 abort = abort || set_ref_in_quickfix(copyID);
4653#endif
4654
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004655#ifdef FEAT_TERMINAL
4656 abort = abort || set_ref_in_term(copyID);
4657#endif
4658
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004659#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004660 abort = abort || set_ref_in_popups(copyID);
4661#endif
4662
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004663 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004664 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004665 /*
4666 * 2. Free lists and dictionaries that are not referenced.
4667 */
4668 did_free = free_unref_items(copyID);
4669
4670 /*
4671 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004672 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004673 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004674 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004675 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004676 else if (p_verbose > 0)
4677 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004678 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004679 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004680
4681 return did_free;
4682}
4683
4684/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004685 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004686 */
4687 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004688free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004689{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004690 int did_free = FALSE;
4691
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004692 // Let all "free" functions know that we are here. This means no
4693 // dictionaries, lists, channels or jobs are to be freed, because we will
4694 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004695 in_free_unref_items = TRUE;
4696
4697 /*
4698 * PASS 1: free the contents of the items. We don't free the items
4699 * themselves yet, so that it is possible to decrement refcount counters
4700 */
4701
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004702 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004703 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004704
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004705 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004706 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004707
4708#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004709 // Go through the list of jobs and free items without the copyID. This
4710 // must happen before doing channels, because jobs refer to channels, but
4711 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004712 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4713
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004714 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004715 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4716#endif
4717
4718 /*
4719 * PASS 2: free the items themselves.
4720 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004721 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004722 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004723
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004724#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004725 // Go through the list of jobs and free items without the copyID. This
4726 // must happen before doing channels, because jobs refer to channels, but
4727 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004728 free_unused_jobs(copyID, COPYID_MASK);
4729
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004730 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004731 free_unused_channels(copyID, COPYID_MASK);
4732#endif
4733
4734 in_free_unref_items = FALSE;
4735
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004736 return did_free;
4737}
4738
4739/*
4740 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004741 * "list_stack" is used to add lists to be marked. Can be NULL.
4742 *
4743 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004744 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004745 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004746set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004747{
4748 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004749 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004750 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004751 hashtab_T *cur_ht;
4752 ht_stack_T *ht_stack = NULL;
4753 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004754
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004755 cur_ht = ht;
4756 for (;;)
4757 {
4758 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004759 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004760 // Mark each item in the hashtab. If the item contains a hashtab
4761 // it is added to ht_stack, if it contains a list it is added to
4762 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004763 todo = (int)cur_ht->ht_used;
4764 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4765 if (!HASHITEM_EMPTY(hi))
4766 {
4767 --todo;
4768 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4769 &ht_stack, list_stack);
4770 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004771 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004772
4773 if (ht_stack == NULL)
4774 break;
4775
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004776 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004777 cur_ht = ht_stack->ht;
4778 tempitem = ht_stack;
4779 ht_stack = ht_stack->prev;
4780 free(tempitem);
4781 }
4782
4783 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004784}
4785
4786/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004787 * Mark a dict and its items with "copyID".
4788 * Returns TRUE if setting references failed somehow.
4789 */
4790 int
4791set_ref_in_dict(dict_T *d, int copyID)
4792{
4793 if (d != NULL && d->dv_copyID != copyID)
4794 {
4795 d->dv_copyID = copyID;
4796 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4797 }
4798 return FALSE;
4799}
4800
4801/*
4802 * Mark a list and its items with "copyID".
4803 * Returns TRUE if setting references failed somehow.
4804 */
4805 int
4806set_ref_in_list(list_T *ll, int copyID)
4807{
4808 if (ll != NULL && ll->lv_copyID != copyID)
4809 {
4810 ll->lv_copyID = copyID;
4811 return set_ref_in_list_items(ll, copyID, NULL);
4812 }
4813 return FALSE;
4814}
4815
4816/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004817 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004818 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4819 *
4820 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004821 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004822 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004823set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004824{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004825 listitem_T *li;
4826 int abort = FALSE;
4827 list_T *cur_l;
4828 list_stack_T *list_stack = NULL;
4829 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004830
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004831 cur_l = l;
4832 for (;;)
4833 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004834 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004835 // Mark each item in the list. If the item contains a hashtab
4836 // it is added to ht_stack, if it contains a list it is added to
4837 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004838 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4839 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4840 ht_stack, &list_stack);
4841 if (list_stack == NULL)
4842 break;
4843
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004844 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004845 cur_l = list_stack->list;
4846 tempitem = list_stack;
4847 list_stack = list_stack->prev;
4848 free(tempitem);
4849 }
4850
4851 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004852}
4853
4854/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004855 * Mark the partial in callback 'cb' with "copyID".
4856 */
4857 int
4858set_ref_in_callback(callback_T *cb, int copyID)
4859{
4860 typval_T tv;
4861
4862 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
4863 return FALSE;
4864
4865 tv.v_type = VAR_PARTIAL;
4866 tv.vval.v_partial = cb->cb_partial;
4867 return set_ref_in_item(&tv, copyID, NULL, NULL);
4868}
4869
4870/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004871 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004872 * "list_stack" is used to add lists to be marked. Can be NULL.
4873 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4874 *
4875 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004876 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004877 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004878set_ref_in_item(
4879 typval_T *tv,
4880 int copyID,
4881 ht_stack_T **ht_stack,
4882 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004883{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004884 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004885
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004886 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004887 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004888 dict_T *dd = tv->vval.v_dict;
4889
Bram Moolenaara03f2332016-02-06 18:09:59 +01004890 if (dd != NULL && dd->dv_copyID != copyID)
4891 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004892 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004893 dd->dv_copyID = copyID;
4894 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004895 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004896 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4897 }
4898 else
4899 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004900 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4901
Bram Moolenaara03f2332016-02-06 18:09:59 +01004902 if (newitem == NULL)
4903 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004904 else
4905 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004906 newitem->ht = &dd->dv_hashtab;
4907 newitem->prev = *ht_stack;
4908 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004909 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004910 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004911 }
4912 }
4913 else if (tv->v_type == VAR_LIST)
4914 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004915 list_T *ll = tv->vval.v_list;
4916
Bram Moolenaara03f2332016-02-06 18:09:59 +01004917 if (ll != NULL && ll->lv_copyID != copyID)
4918 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004919 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004920 ll->lv_copyID = copyID;
4921 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004922 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004923 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004924 }
4925 else
4926 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004927 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4928
Bram Moolenaara03f2332016-02-06 18:09:59 +01004929 if (newitem == NULL)
4930 abort = TRUE;
4931 else
4932 {
4933 newitem->list = ll;
4934 newitem->prev = *list_stack;
4935 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004936 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004937 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004938 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004939 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004940 else if (tv->v_type == VAR_FUNC)
4941 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004942 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004943 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004944 else if (tv->v_type == VAR_PARTIAL)
4945 {
4946 partial_T *pt = tv->vval.v_partial;
4947 int i;
4948
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004949 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004950 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004951 // Didn't see this partial yet.
4952 pt->pt_copyID = copyID;
4953
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004954 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004955
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004956 if (pt->pt_dict != NULL)
4957 {
4958 typval_T dtv;
4959
4960 dtv.v_type = VAR_DICT;
4961 dtv.vval.v_dict = pt->pt_dict;
4962 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4963 }
4964
4965 for (i = 0; i < pt->pt_argc; ++i)
4966 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4967 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004968 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004969 }
4970 }
4971#ifdef FEAT_JOB_CHANNEL
4972 else if (tv->v_type == VAR_JOB)
4973 {
4974 job_T *job = tv->vval.v_job;
4975 typval_T dtv;
4976
4977 if (job != NULL && job->jv_copyID != copyID)
4978 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004979 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004980 if (job->jv_channel != NULL)
4981 {
4982 dtv.v_type = VAR_CHANNEL;
4983 dtv.vval.v_channel = job->jv_channel;
4984 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4985 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004986 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004987 {
4988 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004989 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004990 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4991 }
4992 }
4993 }
4994 else if (tv->v_type == VAR_CHANNEL)
4995 {
4996 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004997 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004998 typval_T dtv;
4999 jsonq_T *jq;
5000 cbq_T *cq;
5001
5002 if (ch != NULL && ch->ch_copyID != copyID)
5003 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005004 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005005 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005006 {
5007 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5008 jq = jq->jq_next)
5009 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5010 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5011 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005012 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005013 {
5014 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005015 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005016 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5017 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005018 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005019 {
5020 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005021 dtv.vval.v_partial =
5022 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005023 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5024 }
5025 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005026 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005027 {
5028 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005029 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005030 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5031 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005032 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005033 {
5034 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005035 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005036 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5037 }
5038 }
5039 }
5040#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005041 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005042}
5043
Bram Moolenaar8c711452005-01-14 21:53:12 +00005044/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005045 * Return a string with the string representation of a variable.
5046 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005047 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005048 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005049 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005050 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005051 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005052 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5053 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005054 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005055 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005056 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005057echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005058 typval_T *tv,
5059 char_u **tofree,
5060 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005061 int copyID,
5062 int echo_style,
5063 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005064 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005065{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005066 static int recurse = 0;
5067 char_u *r = NULL;
5068
Bram Moolenaar33570922005-01-25 22:26:29 +00005069 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005070 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005071 if (!did_echo_string_emsg)
5072 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005073 // Only give this message once for a recursive call to avoid
5074 // flooding the user with errors. And stop iterating over lists
5075 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005076 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005077 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005078 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005079 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005080 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005081 }
5082 ++recurse;
5083
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005084 switch (tv->v_type)
5085 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005086 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005087 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005088 {
5089 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005090 r = tv->vval.v_string;
5091 if (r == NULL)
5092 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005093 }
5094 else
5095 {
5096 *tofree = string_quote(tv->vval.v_string, FALSE);
5097 r = *tofree;
5098 }
5099 break;
5100
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005101 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005102 if (echo_style)
5103 {
5104 *tofree = NULL;
5105 r = tv->vval.v_string;
5106 }
5107 else
5108 {
5109 *tofree = string_quote(tv->vval.v_string, TRUE);
5110 r = *tofree;
5111 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005112 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005113
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005114 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005115 {
5116 partial_T *pt = tv->vval.v_partial;
5117 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005118 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005119 garray_T ga;
5120 int i;
5121 char_u *tf;
5122
5123 ga_init2(&ga, 1, 100);
5124 ga_concat(&ga, (char_u *)"function(");
5125 if (fname != NULL)
5126 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005127 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005128 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005129 && vim_isupper(fname[1]))
5130 {
5131 ga_concat(&ga, (char_u *)"'g:");
5132 ga_concat(&ga, fname + 1);
5133 }
5134 else
5135 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005136 vim_free(fname);
5137 }
5138 if (pt != NULL && pt->pt_argc > 0)
5139 {
5140 ga_concat(&ga, (char_u *)", [");
5141 for (i = 0; i < pt->pt_argc; ++i)
5142 {
5143 if (i > 0)
5144 ga_concat(&ga, (char_u *)", ");
5145 ga_concat(&ga,
5146 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5147 vim_free(tf);
5148 }
5149 ga_concat(&ga, (char_u *)"]");
5150 }
5151 if (pt != NULL && pt->pt_dict != NULL)
5152 {
5153 typval_T dtv;
5154
5155 ga_concat(&ga, (char_u *)", ");
5156 dtv.v_type = VAR_DICT;
5157 dtv.vval.v_dict = pt->pt_dict;
5158 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5159 vim_free(tf);
5160 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005161 // terminate with ')' and a NUL
5162 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005163
5164 *tofree = ga.ga_data;
5165 r = *tofree;
5166 break;
5167 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005168
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005169 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005170 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005171 break;
5172
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005173 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005174 if (tv->vval.v_list == NULL)
5175 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005176 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005177 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005178 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005179 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005180 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5181 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005182 {
5183 *tofree = NULL;
5184 r = (char_u *)"[...]";
5185 }
5186 else
5187 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005188 int old_copyID = tv->vval.v_list->lv_copyID;
5189
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005190 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005191 *tofree = list2string(tv, copyID, restore_copyID);
5192 if (restore_copyID)
5193 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005194 r = *tofree;
5195 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005196 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005197
Bram Moolenaar8c711452005-01-14 21:53:12 +00005198 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005199 if (tv->vval.v_dict == NULL)
5200 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005201 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005202 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005203 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005204 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005205 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5206 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005207 {
5208 *tofree = NULL;
5209 r = (char_u *)"{...}";
5210 }
5211 else
5212 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005213 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005214
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005215 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005216 *tofree = dict2string(tv, copyID, restore_copyID);
5217 if (restore_copyID)
5218 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005219 r = *tofree;
5220 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005221 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005222
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005223 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005224 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005225 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005226 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005227 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005228 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005229 break;
5230
Bram Moolenaar835dc632016-02-07 14:27:38 +01005231 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005232 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005233#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005234 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005235 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5236 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005237 if (composite_val)
5238 {
5239 *tofree = string_quote(r, FALSE);
5240 r = *tofree;
5241 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005242#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005243 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005244
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005245 case VAR_INSTR:
5246 *tofree = NULL;
5247 r = (char_u *)"instructions";
5248 break;
5249
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005250 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005251#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005252 *tofree = NULL;
5253 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5254 r = numbuf;
5255 break;
5256#endif
5257
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005258 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005259 case VAR_SPECIAL:
5260 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005261 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005262 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005263 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005264
Bram Moolenaar8502c702014-06-17 12:51:16 +02005265 if (--recurse == 0)
5266 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005267 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005268}
5269
5270/*
5271 * Return a string with the string representation of a variable.
5272 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5273 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005274 * Does not put quotes around strings, as ":echo" displays values.
5275 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5276 * May return NULL.
5277 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005278 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005279echo_string(
5280 typval_T *tv,
5281 char_u **tofree,
5282 char_u *numbuf,
5283 int copyID)
5284{
5285 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5286}
5287
5288/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005289 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5290 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005291 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005292 */
5293 int
5294buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5295{
5296 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005297 char_u *t;
5298 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005299
5300 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5301 return -1;
5302
5303 if (lnum > buf->b_ml.ml_line_count)
5304 lnum = buf->b_ml.ml_line_count;
5305
5306 str = ml_get_buf(buf, lnum, FALSE);
5307 if (str == NULL)
5308 return -1;
5309
5310 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005311 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005312
Bram Moolenaar91458462021-01-13 20:08:38 +01005313 // count the number of characters
5314 t = str;
5315 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5316 t += mb_ptr2len(t);
5317
5318 // In insert mode, when the cursor is at the end of a non-empty line,
5319 // byteidx points to the NUL character immediately past the end of the
5320 // string. In this case, add one to the character count.
5321 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5322 count++;
5323
5324 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005325}
5326
5327/*
5328 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005329 * byte index. Works only for loaded buffers. Returns -1 on failure.
5330 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005331 */
5332 int
5333buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5334{
5335 char_u *str;
5336 char_u *t;
5337
5338 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5339 return -1;
5340
5341 if (lnum > buf->b_ml.ml_line_count)
5342 lnum = buf->b_ml.ml_line_count;
5343
5344 str = ml_get_buf(buf, lnum, FALSE);
5345 if (str == NULL)
5346 return -1;
5347
5348 // Convert the character offset to a byte offset
5349 t = str;
5350 while (*t != NUL && --charidx > 0)
5351 t += mb_ptr2len(t);
5352
Bram Moolenaar91458462021-01-13 20:08:38 +01005353 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005354}
5355
5356/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005358 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005360 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005361var2fpos(
5362 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005363 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005364 int *fnum, // set to fnum for '0, 'A, etc.
5365 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005367 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005369 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005371 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005372 if (varp->v_type == VAR_LIST)
5373 {
5374 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005375 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005376 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005377 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005378
5379 l = varp->vval.v_list;
5380 if (l == NULL)
5381 return NULL;
5382
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005383 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005384 pos.lnum = list_find_nr(l, 0L, &error);
5385 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005386 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005387 if (charcol)
5388 len = (long)mb_charlen(ml_get(pos.lnum));
5389 else
5390 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005391
Bram Moolenaarec65d772020-08-20 22:29:12 +02005392 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005393 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005394 li = list_find(l, 1L);
5395 if (li != NULL && li->li_tv.v_type == VAR_STRING
5396 && li->li_tv.vval.v_string != NULL
5397 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005398 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005399 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005400 }
5401 else
5402 {
5403 pos.col = list_find_nr(l, 1L, &error);
5404 if (error)
5405 return NULL;
5406 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005407
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005408 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005409 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005410 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005411 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005412
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005413 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005414 pos.coladd = list_find_nr(l, 2L, &error);
5415 if (error)
5416 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005417
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005418 return &pos;
5419 }
5420
Bram Moolenaarc5809432021-03-27 21:23:30 +01005421 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5422 return NULL;
5423
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005424 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005425 if (name == NULL)
5426 return NULL;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005427 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005428 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005429 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005430 pos = curwin->w_cursor;
5431 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005432 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005433 return &pos;
5434 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005435 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005436 {
5437 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005438 pos = VIsual;
5439 else
5440 pos = curwin->w_cursor;
5441 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005442 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005443 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005444 }
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005445 if (name[0] == '\'' && (!in_vim9script()
5446 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005448 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005449 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5451 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005452 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005453 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 return pp;
5455 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005456
Bram Moolenaara5525202006-03-02 22:52:09 +00005457 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005458
Bram Moolenaar477933c2007-07-17 14:32:23 +00005459 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005460 {
5461 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005462 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005463 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005464 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005465 // In silent Ex mode topline is zero, but that's not a valid line
5466 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005467 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005468 return &pos;
5469 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005470 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005471 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005472 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005473 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005474 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005475 return &pos;
5476 }
5477 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005478 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005480 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 {
5482 pos.lnum = curbuf->b_ml.ml_line_count;
5483 pos.col = 0;
5484 }
5485 else
5486 {
5487 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005488 if (charcol)
5489 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5490 else
5491 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 }
5493 return &pos;
5494 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005495 if (in_vim9script())
5496 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 return NULL;
5498}
5499
5500/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005501 * Convert list in "arg" into a position and optional file number.
5502 * When "fnump" is NULL there is no file number, only 3 items.
5503 * Note that the column is passed on as-is, the caller may want to decrement
5504 * it to use 1 for the first column.
5505 * Return FAIL when conversion is not possible, doesn't check the position for
5506 * validity.
5507 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005508 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005509list2fpos(
5510 typval_T *arg,
5511 pos_T *posp,
5512 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005513 colnr_T *curswantp,
5514 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005515{
5516 list_T *l = arg->vval.v_list;
5517 long i = 0;
5518 long n;
5519
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005520 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5521 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005522 if (arg->v_type != VAR_LIST
5523 || l == NULL
5524 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005525 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005526 return FAIL;
5527
5528 if (fnump != NULL)
5529 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005530 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005531 if (n < 0)
5532 return FAIL;
5533 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005534 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005535 *fnump = n;
5536 }
5537
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005538 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005539 if (n < 0)
5540 return FAIL;
5541 posp->lnum = n;
5542
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005543 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005544 if (n < 0)
5545 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005546 // If character position is specified, then convert to byte position
5547 if (charcol)
5548 {
5549 buf_T *buf;
5550
5551 // Get the text for the specified line in a loaded buffer
5552 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5553 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5554 return FAIL;
5555
Bram Moolenaar91458462021-01-13 20:08:38 +01005556 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005557 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005558 posp->col = n;
5559
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005560 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005561 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005562 posp->coladd = 0;
5563 else
5564 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005565
Bram Moolenaar493c1782014-05-28 14:34:46 +02005566 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005567 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005568
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005569 return OK;
5570}
5571
5572/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 * Get the length of an environment variable name.
5574 * Advance "arg" to the first character after the name.
5575 * Return 0 for error.
5576 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005577 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005578get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579{
5580 char_u *p;
5581 int len;
5582
5583 for (p = *arg; vim_isIDc(*p); ++p)
5584 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005585 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 return 0;
5587
5588 len = (int)(p - *arg);
5589 *arg = p;
5590 return len;
5591}
5592
5593/*
5594 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005595 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 * Return 0 if something is wrong.
5597 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005598 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005599get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600{
5601 char_u *p;
5602 int len;
5603
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005604 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005606 {
5607 if (*p == ':')
5608 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005609 // "s:" is start of "s:var", but "n:" is not and can be used in
5610 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005611 len = (int)(p - *arg);
5612 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5613 || len > 1)
5614 break;
5615 }
5616 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005617 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 return 0;
5619
5620 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005621 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622
5623 return len;
5624}
5625
5626/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005627 * Get the length of the name of a variable or function.
5628 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005630 * Return -1 if curly braces expansion failed.
5631 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 * If the name contains 'magic' {}'s, expand them and return the
5633 * expanded name in an allocated string via 'alias' - caller must free.
5634 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005635 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005636get_name_len(
5637 char_u **arg,
5638 char_u **alias,
5639 int evaluate,
5640 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641{
5642 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 char_u *p;
5644 char_u *expr_start;
5645 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005647 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648
5649 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5650 && (*arg)[2] == (int)KE_SNR)
5651 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005652 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 *arg += 3;
5654 return get_id_len(arg) + 3;
5655 }
5656 len = eval_fname_script(*arg);
5657 if (len > 0)
5658 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005659 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 *arg += len;
5661 }
5662
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005664 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005666 p = find_name_end(*arg, &expr_start, &expr_end,
5667 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 if (expr_start != NULL)
5669 {
5670 char_u *temp_string;
5671
5672 if (!evaluate)
5673 {
5674 len += (int)(p - *arg);
5675 *arg = skipwhite(p);
5676 return len;
5677 }
5678
5679 /*
5680 * Include any <SID> etc in the expanded string:
5681 * Thus the -len here.
5682 */
5683 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5684 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005685 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 *alias = temp_string;
5687 *arg = skipwhite(p);
5688 return (int)STRLEN(temp_string);
5689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690
5691 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005692 // Only give an error when there is something, otherwise it will be
5693 // reported at a higher level.
5694 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005695 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696
5697 return len;
5698}
5699
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005700/*
5701 * Find the end of a variable or function name, taking care of magic braces.
5702 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5703 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005704 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005705 * Return a pointer to just after the name. Equal to "arg" if there is no
5706 * valid name.
5707 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005708 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005709find_name_end(
5710 char_u *arg,
5711 char_u **expr_start,
5712 char_u **expr_end,
5713 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005715 int mb_nest = 0;
5716 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005718 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005719 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005721 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005723 *expr_start = NULL;
5724 *expr_end = NULL;
5725 }
5726
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005727 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005728 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5729 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005730 return arg;
5731
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005732 for (p = arg; *p != NUL
5733 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005734 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005735 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005736 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005737 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005738 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005739 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005740 if (*p == '\'')
5741 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005742 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005743 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005744 ;
5745 if (*p == NUL)
5746 break;
5747 }
5748 else if (*p == '"')
5749 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005750 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005751 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005752 if (*p == '\\' && p[1] != NUL)
5753 ++p;
5754 if (*p == NUL)
5755 break;
5756 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005757 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5758 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005759 // "s:" is start of "s:var", but "n:" is not and can be used in
5760 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005761 len = (int)(p - arg);
5762 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005763 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005764 break;
5765 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005766
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005767 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005769 if (*p == '[')
5770 ++br_nest;
5771 else if (*p == ']')
5772 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005774
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005775 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005777 if (*p == '{')
5778 {
5779 mb_nest++;
5780 if (expr_start != NULL && *expr_start == NULL)
5781 *expr_start = p;
5782 }
5783 else if (*p == '}')
5784 {
5785 mb_nest--;
5786 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5787 *expr_end = p;
5788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 }
5791
5792 return p;
5793}
5794
5795/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005796 * Expands out the 'magic' {}'s in a variable/function name.
5797 * Note that this can call itself recursively, to deal with
5798 * constructs like foo{bar}{baz}{bam}
5799 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5800 * "in_start" ^
5801 * "expr_start" ^
5802 * "expr_end" ^
5803 * "in_end" ^
5804 *
5805 * Returns a new allocated string, which the caller must free.
5806 * Returns NULL for failure.
5807 */
5808 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005809make_expanded_name(
5810 char_u *in_start,
5811 char_u *expr_start,
5812 char_u *expr_end,
5813 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005814{
5815 char_u c1;
5816 char_u *retval = NULL;
5817 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005818
5819 if (expr_end == NULL || in_end == NULL)
5820 return NULL;
5821 *expr_start = NUL;
5822 *expr_end = NUL;
5823 c1 = *in_end;
5824 *in_end = NUL;
5825
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005826 temp_result = eval_to_string(expr_start + 1, FALSE);
5827 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005828 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005829 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5830 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005831 if (retval != NULL)
5832 {
5833 STRCPY(retval, in_start);
5834 STRCAT(retval, temp_result);
5835 STRCAT(retval, expr_end + 1);
5836 }
5837 }
5838 vim_free(temp_result);
5839
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005840 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005841 *expr_start = '{';
5842 *expr_end = '}';
5843
5844 if (retval != NULL)
5845 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005846 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005847 if (expr_start != NULL)
5848 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005849 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005850 temp_result = make_expanded_name(retval, expr_start,
5851 expr_end, temp_result);
5852 vim_free(retval);
5853 retval = temp_result;
5854 }
5855 }
5856
5857 return retval;
5858}
5859
5860/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005862 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005864 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005865eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005867 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005868}
5869
5870/*
5871 * Return TRUE if character "c" can be used as the first character in a
5872 * variable or function name (excluding '{' and '}').
5873 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005874 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005875eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005876{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005877 return ASCII_ISALPHA(c) || c == '_';
5878}
5879
5880/*
5881 * Return TRUE if character "c" can be used as the first character of a
5882 * dictionary key.
5883 */
5884 int
5885eval_isdictc(int c)
5886{
5887 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888}
5889
5890/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005891 * Handle:
5892 * - expr[expr], expr[expr:expr] subscript
5893 * - ".name" lookup
5894 * - function call with Funcref variable: func(expr)
5895 * - method call: var->method()
5896 *
5897 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00005898 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005899 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005900 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005901handle_subscript(
5902 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00005903 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005904 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005905 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005906 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005907{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005908 int evaluate = evalarg != NULL
5909 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005910 int ret = OK;
5911 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005912 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005913 int getnext;
5914 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005915
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005916 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005917 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005918 // When at the end of the line and ".name" or "->{" or "->X" follows in
5919 // the next line then consume the line break.
5920 p = eval_next_non_blank(*arg, evalarg, &getnext);
5921 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005922 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02005923 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
5924 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
5925 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005926 {
5927 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005928 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005929 check_white = FALSE;
5930 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005931
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005932 if (rettv->v_type == VAR_ANY)
5933 {
5934 char_u *exp_name;
5935 int cc;
5936 int idx;
5937 ufunc_T *ufunc;
5938 type_T *type;
5939
Bram Moolenaard5f400c2022-01-06 21:10:28 +00005940 // Found script from "import {name} as name", script item name must
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005941 // follow.
5942 if (**arg != '.')
5943 {
5944 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00005945 semsg(_(e_expected_dot_after_name_str),
5946 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005947 ret = FAIL;
5948 break;
5949 }
5950 ++*arg;
5951 if (IS_WHITE_OR_NUL(**arg))
5952 {
5953 if (verbose)
5954 emsg(_(e_no_white_space_allowed_after_dot));
5955 ret = FAIL;
5956 break;
5957 }
5958
5959 // isolate the name
5960 exp_name = *arg;
5961 while (eval_isnamec(**arg))
5962 ++*arg;
5963 cc = **arg;
5964 **arg = NUL;
5965
5966 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5967 evalarg->eval_cctx, verbose);
5968 **arg = cc;
5969 *arg = skipwhite(*arg);
5970
5971 if (idx < 0 && ufunc == NULL)
5972 {
5973 ret = FAIL;
5974 break;
5975 }
5976 if (idx >= 0)
5977 {
5978 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5979 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5980
5981 copy_tv(sv->sv_tv, rettv);
5982 }
5983 else
5984 {
5985 rettv->v_type = VAR_FUNC;
5986 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5987 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00005988 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005989 }
5990
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005991 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5992 || rettv->v_type == VAR_PARTIAL))
5993 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005994 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005995 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5996 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005997
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005998 // Stop the expression evaluation when immediately aborting on
5999 // error, or when an interrupt occurred or an exception was thrown
6000 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006001 if (aborting())
6002 {
6003 if (ret == OK)
6004 clear_tv(rettv);
6005 ret = FAIL;
6006 }
6007 dict_unref(selfdict);
6008 selfdict = NULL;
6009 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006010 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006011 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006012 if (in_vim9script())
6013 *arg = skipwhite(p + 2);
6014 else
6015 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006016 if (ret == OK)
6017 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006018 if (VIM_ISWHITE(**arg))
6019 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006020 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006021 ret = FAIL;
6022 }
6023 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006024 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006025 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006026 else
6027 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006028 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006029 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006030 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006031 // "." is ".name" lookup when we found a dict or when evaluating and
6032 // scriptversion is at least 2, where string concatenation is "..".
6033 else if (**arg == '['
6034 || (**arg == '.' && (rettv->v_type == VAR_DICT
6035 || (!evaluate
6036 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006037 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006038 {
6039 dict_unref(selfdict);
6040 if (rettv->v_type == VAR_DICT)
6041 {
6042 selfdict = rettv->vval.v_dict;
6043 if (selfdict != NULL)
6044 ++selfdict->dv_refcount;
6045 }
6046 else
6047 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006048 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006049 {
6050 clear_tv(rettv);
6051 ret = FAIL;
6052 }
6053 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006054 else
6055 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006056 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006057
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006058 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6059 // Don't do this when "Func" is already a partial that was bound
6060 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006061 if (selfdict != NULL
6062 && (rettv->v_type == VAR_FUNC
6063 || (rettv->v_type == VAR_PARTIAL
6064 && (rettv->vval.v_partial->pt_auto
6065 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006066 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006067
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006068 dict_unref(selfdict);
6069 return ret;
6070}
6071
6072/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006073 * Make a copy of an item.
6074 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006075 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6076 * reference to an already copied list/dict can be used.
6077 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006078 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006079 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006080item_copy(
6081 typval_T *from,
6082 typval_T *to,
6083 int deep,
6084 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006085{
6086 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006087 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006088
Bram Moolenaar33570922005-01-25 22:26:29 +00006089 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006090 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006091 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006092 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006093 }
6094 ++recurse;
6095
6096 switch (from->v_type)
6097 {
6098 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006099 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006100 case VAR_STRING:
6101 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006102 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006103 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006104 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006105 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006106 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006107 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006108 copy_tv(from, to);
6109 break;
6110 case VAR_LIST:
6111 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006112 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006113 if (from->vval.v_list == NULL)
6114 to->vval.v_list = NULL;
6115 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6116 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006117 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006118 to->vval.v_list = from->vval.v_list->lv_copylist;
6119 ++to->vval.v_list->lv_refcount;
6120 }
6121 else
6122 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6123 if (to->vval.v_list == NULL)
6124 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006125 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006126 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006127 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006128 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006129 case VAR_DICT:
6130 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006131 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006132 if (from->vval.v_dict == NULL)
6133 to->vval.v_dict = NULL;
6134 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6135 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006136 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006137 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6138 ++to->vval.v_dict->dv_refcount;
6139 }
6140 else
6141 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6142 if (to->vval.v_dict == NULL)
6143 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006144 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006145 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006146 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006147 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006148 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006149 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006150 }
6151 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006152 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006153}
6154
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006155 void
6156echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6157{
6158 char_u *tofree;
6159 char_u numbuf[NUMBUFLEN];
6160 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6161
6162 if (*atstart)
6163 {
6164 *atstart = FALSE;
6165 // Call msg_start() after eval1(), evaluating the expression
6166 // may cause a message to appear.
6167 if (with_space)
6168 {
6169 // Mark the saved text as finishing the line, so that what
6170 // follows is displayed on a new line when scrolling back
6171 // at the more prompt.
6172 msg_sb_eol();
6173 msg_start();
6174 }
6175 }
6176 else if (with_space)
6177 msg_puts_attr(" ", echo_attr);
6178
6179 if (p != NULL)
6180 for ( ; *p != NUL && !got_int; ++p)
6181 {
6182 if (*p == '\n' || *p == '\r' || *p == TAB)
6183 {
6184 if (*p != TAB && *needclr)
6185 {
6186 // remove any text still there from the command
6187 msg_clr_eos();
6188 *needclr = FALSE;
6189 }
6190 msg_putchar_attr(*p, echo_attr);
6191 }
6192 else
6193 {
6194 if (has_mbyte)
6195 {
6196 int i = (*mb_ptr2len)(p);
6197
6198 (void)msg_outtrans_len_attr(p, i, echo_attr);
6199 p += i - 1;
6200 }
6201 else
6202 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6203 }
6204 }
6205 vim_free(tofree);
6206}
6207
Bram Moolenaare9a41262005-01-15 22:18:47 +00006208/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 * ":echo expr1 ..." print each argument separated with a space, add a
6210 * newline at the end.
6211 * ":echon expr1 ..." print each argument plain.
6212 */
6213 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006214ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215{
6216 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006217 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006218 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 int needclr = TRUE;
6220 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006221 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006222 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006223 evalarg_T evalarg;
6224
Bram Moolenaare707c882020-07-01 13:07:37 +02006225 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226
6227 if (eap->skip)
6228 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006229 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006231 // If eval1() causes an error message the text from the command may
6232 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006233 need_clr_eos = needclr;
6234
Bram Moolenaar68db9962021-05-09 23:19:22 +02006235 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006236 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 {
6238 /*
6239 * Report the invalid expression unless the expression evaluation
6240 * has been cancelled due to an aborting error, an interrupt, or an
6241 * exception.
6242 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006243 if (!aborting() && did_emsg == did_emsg_before
6244 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006245 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006246 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 break;
6248 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006249 need_clr_eos = FALSE;
6250
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006252 {
6253 if (rettv.v_type == VAR_VOID)
6254 {
6255 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6256 break;
6257 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006258 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006259 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006260
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006261 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 arg = skipwhite(arg);
6263 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006264 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006265 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266
6267 if (eap->skip)
6268 --emsg_skip;
6269 else
6270 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006271 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 if (needclr)
6273 msg_clr_eos();
6274 if (eap->cmdidx == CMD_echo)
6275 msg_end();
6276 }
6277}
6278
6279/*
6280 * ":echohl {name}".
6281 */
6282 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006283ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006285 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286}
6287
6288/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006289 * Returns the :echo attribute
6290 */
6291 int
6292get_echo_attr(void)
6293{
6294 return echo_attr;
6295}
6296
6297/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 * ":execute expr1 ..." execute the result of an expression.
6299 * ":echomsg expr1 ..." Print a message
6300 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006301 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 * Each gets spaces around each argument and a newline at the end for
6303 * echo commands
6304 */
6305 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006306ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307{
6308 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006309 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 int ret = OK;
6311 char_u *p;
6312 garray_T ga;
6313 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006314 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315
6316 ga_init2(&ga, 1, 80);
6317
6318 if (eap->skip)
6319 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006320 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006322 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006323 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325
6326 if (!eap->skip)
6327 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006328 char_u buf[NUMBUFLEN];
6329
6330 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006331 {
6332 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6333 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006334 semsg(_(e_using_invalid_value_as_string_str),
6335 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006336 p = NULL;
6337 }
6338 else
6339 p = tv_get_string_buf(&rettv, buf);
6340 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006341 else
6342 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006343 if (p == NULL)
6344 {
6345 clear_tv(&rettv);
6346 ret = FAIL;
6347 break;
6348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 len = (int)STRLEN(p);
6350 if (ga_grow(&ga, len + 2) == FAIL)
6351 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006352 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 ret = FAIL;
6354 break;
6355 }
6356 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 ga.ga_len += len;
6360 }
6361
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006362 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 arg = skipwhite(arg);
6364 }
6365
6366 if (ret != FAIL && ga.ga_data != NULL)
6367 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006368 // use the first line of continuation lines for messages
6369 SOURCING_LNUM = start_lnum;
6370
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006371 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6372 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006373 // Mark the already saved text as finishing the line, so that what
6374 // follows is displayed on a new line when scrolling back at the
6375 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006376 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006377 }
6378
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006380 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006381 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006382 out_flush();
6383 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006384 else if (eap->cmdidx == CMD_echoconsole)
6385 {
6386 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6387 ui_write((char_u *)"\r\n", 2, TRUE);
6388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 else if (eap->cmdidx == CMD_echoerr)
6390 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006391 int save_did_emsg = did_emsg;
6392
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006393 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006394 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 if (!force_abort)
6396 did_emsg = save_did_emsg;
6397 }
6398 else if (eap->cmdidx == CMD_execute)
6399 do_cmdline((char_u *)ga.ga_data,
6400 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6401 }
6402
6403 ga_clear(&ga);
6404
6405 if (eap->skip)
6406 --emsg_skip;
6407
Bram Moolenaar63b91732021-08-05 20:40:03 +02006408 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409}
6410
6411/*
6412 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6413 * "arg" points to the "&" or '+' when called, to "option" when returning.
6414 * Returns NULL when no option name found. Otherwise pointer to the char
6415 * after the option name.
6416 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006417 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006418find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419{
6420 char_u *p = *arg;
6421
6422 ++p;
6423 if (*p == 'g' && p[1] == ':')
6424 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006425 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 p += 2;
6427 }
6428 else if (*p == 'l' && p[1] == ':')
6429 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006430 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 p += 2;
6432 }
6433 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006434 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435
6436 if (!ASCII_ISALPHA(*p))
6437 return NULL;
6438 *arg = p;
6439
6440 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006441 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 else
6443 while (ASCII_ISALPHA(*p))
6444 ++p;
6445 return p;
6446}
6447
6448/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006449 * Display script name where an item was last set.
6450 * Should only be invoked when 'verbose' is non-zero.
6451 */
6452 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006453last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006454{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006455 char_u *p;
6456
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006457 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006458 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006459 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006460 if (p != NULL)
6461 {
6462 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006463 msg_puts(_("\n\tLast set from "));
6464 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006465 if (script_ctx.sc_lnum > 0)
6466 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006467 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006468 msg_outnum((long)script_ctx.sc_lnum);
6469 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006470 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006471 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006472 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006473 }
6474}
6475
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006476#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477
6478/*
6479 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006480 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 * "flags" can be "g" to do a global substitute.
6482 * Returns an allocated string, NULL for error.
6483 */
6484 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006485do_string_sub(
6486 char_u *str,
6487 char_u *pat,
6488 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006489 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006490 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491{
6492 int sublen;
6493 regmatch_T regmatch;
6494 int i;
6495 int do_all;
6496 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006497 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 garray_T ga;
6499 char_u *ret;
6500 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006501 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006503 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006505 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506
6507 ga_init2(&ga, 1, 200);
6508
6509 do_all = (flags[0] == 'g');
6510
6511 regmatch.rm_ic = p_ic;
6512 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6513 if (regmatch.regprog != NULL)
6514 {
6515 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006516 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6518 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006519 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006520 if (regmatch.startp[0] == regmatch.endp[0])
6521 {
6522 if (zero_width == regmatch.startp[0])
6523 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006524 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006525 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006526 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6527 (size_t)i);
6528 ga.ga_len += i;
6529 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006530 continue;
6531 }
6532 zero_width = regmatch.startp[0];
6533 }
6534
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 /*
6536 * Get some space for a temporary buffer to do the substitution
6537 * into. It will contain:
6538 * - The text up to where the match is.
6539 * - The substituted text.
6540 * - The text after the match.
6541 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006542 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006543 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6545 {
6546 ga_clear(&ga);
6547 break;
6548 }
6549
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006550 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 i = (int)(regmatch.startp[0] - tail);
6552 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006553 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006554 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 + ga.ga_len + i, TRUE, TRUE, FALSE);
6556 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006557 tail = regmatch.endp[0];
6558 if (*tail == NUL)
6559 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 if (!do_all)
6561 break;
6562 }
6563
6564 if (ga.ga_data != NULL)
6565 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6566
Bram Moolenaar473de612013-06-08 18:19:48 +02006567 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 }
6569
6570 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6571 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006572 if (p_cpo == empty_option)
6573 p_cpo = save_cpo;
6574 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006575 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006576 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006577 // If it's still empty it was changed and restored, need to restore in
6578 // the complicated way.
6579 if (*p_cpo == NUL)
6580 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006581 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583
6584 return ret;
6585}