blob: df73ba1aee3c677931a48cf3bfff5427e0ef807c [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar071d4272004-06-13 20:20:40 +000032/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000033 * Info used by a ":for" loop.
34 */
Bram Moolenaar33570922005-01-25 22:26:29 +000035typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000036{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010037 int fi_semicolon; // TRUE if ending in '; var]'
38 int fi_varcount; // nr of variables in the list
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020039 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010040 listwatch_T fi_lw; // keep an eye on the item used.
41 list_T *fi_list; // list being used
42 int fi_bi; // index of blob
43 blob_T *fi_blob; // blob being used
Bram Moolenaar74e54fc2021-03-26 20:41:29 +010044 char_u *fi_string; // copy of string being used
45 int fi_byte_idx; // byte index in fi_string
Bram Moolenaar33570922005-01-25 22:26:29 +000046} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000047
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020048static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
49static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
52static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +020053static int eval7t(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020054static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020055static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000056
Bram Moolenaar48e697e2016-01-23 22:17:30 +010057static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020058static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020059
Bram Moolenaare21c1582019-03-02 11:57:09 +010060/*
61 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010062 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010063 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020064 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010065num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010066{
67 varnumber_T result;
68
Bram Moolenaar99880f92021-01-20 21:23:14 +010069 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010070 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010071 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010072 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010073 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010074 if (failed != NULL)
75 *failed = TRUE;
76 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010077 if (n1 == 0)
78 result = VARNUM_MIN; // similar to NaN
79 else if (n1 < 0)
80 result = -VARNUM_MAX;
81 else
82 result = VARNUM_MAX;
83 }
84 else
85 result = n1 / n2;
86
87 return result;
88}
89
90/*
91 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010092 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010093 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020094 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010095num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010096{
Bram Moolenaar99880f92021-01-20 21:23:14 +010097 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010098 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010099 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +0100100 if (failed != NULL)
101 *failed = TRUE;
102 }
Bram Moolenaare21c1582019-03-02 11:57:09 +0100103 return (n2 == 0) ? 0 : (n1 % n2);
104}
105
Bram Moolenaar33570922005-01-25 22:26:29 +0000106/*
107 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000108 */
109 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100110eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000111{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200112 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200113 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000114
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200115#ifdef EBCDIC
116 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100117 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200118 */
119 sortFunctions();
120#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000121}
122
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000123#if defined(EXITFREE) || defined(PROTO)
124 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100125eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000126{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200127 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100128 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200129 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000130
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200131 // autoloaded script names
132 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000133
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200134 // unreferenced lists and dicts
135 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200136
137 // functions not garbage collected
138 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000139}
140#endif
141
Bram Moolenaare6b53242020-07-01 17:28:33 +0200142 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200143fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
144{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100145 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200146 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200147 if (eap != NULL)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200148 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200149 evalarg->eval_cstack = eap->cstack;
150 if (getline_equal(eap->getline, eap->cookie, getsourceline))
151 {
152 evalarg->eval_getline = eap->getline;
153 evalarg->eval_cookie = eap->cookie;
154 }
Bram Moolenaar37c83712020-06-30 21:18:36 +0200155 }
156}
157
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158/*
159 * Top level evaluation function, returning a boolean.
160 * Sets "error" to TRUE if there was an error.
161 * Return TRUE or FALSE.
162 */
163 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100164eval_to_bool(
165 char_u *arg,
166 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200167 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100168 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169{
Bram Moolenaar33570922005-01-25 22:26:29 +0000170 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200171 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200172 evalarg_T evalarg;
173
Bram Moolenaar37c83712020-06-30 21:18:36 +0200174 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175
176 if (skip)
177 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200178 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 else
181 {
182 *error = FALSE;
183 if (!skip)
184 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200185 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200186 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200187 else
188 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000189 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 }
191 }
192 if (skip)
193 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200194 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200196 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197}
198
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100199/*
200 * Call eval1() and give an error message if not done at a lower level.
201 */
202 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200203eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100204{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100205 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100206 int ret;
207 int did_emsg_before = did_emsg;
208 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200209 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100210
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200211 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
212
213 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100214 if (ret == FAIL)
215 {
216 // Report the invalid expression unless the expression evaluation has
217 // been cancelled due to an aborting error, an interrupt, or an
218 // exception, or we already gave a more specific error.
219 // Also check called_emsg for when using assert_fails().
220 if (!aborting() && did_emsg == did_emsg_before
221 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200222 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100223 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200224 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100225 return ret;
226}
227
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100228/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200229 * Return whether a typval is a valid expression to pass to eval_expr_typval()
230 * or eval_expr_to_bool(). An empty string returns FALSE;
231 */
232 int
233eval_expr_valid_arg(typval_T *tv)
234{
235 return tv->v_type != VAR_UNKNOWN
236 && (tv->v_type != VAR_STRING
237 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
238}
239
240/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100241 * Evaluate an expression, which can be a function, partial or string.
242 * Pass arguments "argv[argc]".
243 * Return the result in "rettv" and OK or FAIL.
244 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200245 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100246eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
247{
248 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100249 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200250 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100251
252 if (expr->v_type == VAR_FUNC)
253 {
254 s = expr->vval.v_string;
255 if (s == NULL || *s == NUL)
256 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200257 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000258 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200259 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100260 return FAIL;
261 }
262 else if (expr->v_type == VAR_PARTIAL)
263 {
264 partial_T *partial = expr->vval.v_partial;
265
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200266 if (partial == NULL)
267 return FAIL;
268
Bram Moolenaar822ba242020-05-24 23:00:18 +0200269 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200270 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200272 if (call_def_function(partial->pt_func, argc, argv,
273 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274 return FAIL;
275 }
276 else
277 {
278 s = partial_name(partial);
279 if (s == NULL || *s == NUL)
280 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200281 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000282 funcexe.fe_evaluate = TRUE;
283 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100284 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
285 return FAIL;
286 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100287 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200288 else if (expr->v_type == VAR_INSTR)
289 {
290 return exe_typval_instr(expr, rettv);
291 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100292 else
293 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100294 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100295 if (s == NULL)
296 return FAIL;
297 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200298 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100299 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200300 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100301 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200302 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200303 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100304 return FAIL;
305 }
306 }
307 return OK;
308}
309
310/*
311 * Like eval_to_bool() but using a typval_T instead of a string.
312 * Works for string, funcref and partial.
313 */
314 int
315eval_expr_to_bool(typval_T *expr, int *error)
316{
317 typval_T rettv;
318 int res;
319
320 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
321 {
322 *error = TRUE;
323 return FALSE;
324 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200325 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100326 clear_tv(&rettv);
327 return res;
328}
329
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330/*
331 * Top level evaluation function, returning a string. If "skip" is TRUE,
332 * only parsing to "nextcmd" is done, without reporting errors. Return
333 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
334 */
335 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100336eval_to_string_skip(
337 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200338 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100339 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340{
Bram Moolenaar33570922005-01-25 22:26:29 +0000341 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200343 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344
Bram Moolenaar37c83712020-06-30 21:18:36 +0200345 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 if (skip)
347 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200348 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 retval = NULL;
350 else
351 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100352 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000353 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 }
355 if (skip)
356 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200357 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358
359 return retval;
360}
361
362/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000363 * Skip over an expression at "*pp".
364 * Return FAIL for an error, OK otherwise.
365 */
366 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200367skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000368{
Bram Moolenaar33570922005-01-25 22:26:29 +0000369 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000370
371 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200372 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000373}
374
375/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200376 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200377 * If in Vim9 script and line breaks are encountered, the lines are
378 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200379 * "arg" is advanced to just after the expression.
380 * "start" is set to the start of the expression, "end" to just after the end.
381 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200382 * Return FAIL for an error, OK otherwise.
383 */
384 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200385skip_expr_concatenate(
386 char_u **arg,
387 char_u **start,
388 char_u **end,
389 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200390{
391 typval_T rettv;
392 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200393 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200394 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200395 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200396 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200397 int evaluate = evalarg == NULL
398 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200399
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200400 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200401 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200402 {
403 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200404 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200405 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200406 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200407 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200408 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200409 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200410
411 // Don't evaluate the expression.
412 if (evalarg != NULL)
413 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200414 *arg = skipwhite(*arg);
415 res = eval1(arg, &rettv, evalarg);
416 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200417 if (evalarg != NULL)
418 evalarg->eval_flags = save_flags;
419
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200420 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200421 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200422 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200423 if (evalarg->eval_ga.ga_len == 1)
424 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200425 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200426 ga_clear(gap);
427 gap->ga_itemsize = 0;
428 }
429 else
430 {
431 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200432 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200433
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200434 // Line breaks encountered, concatenate all the lines.
435 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200436 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200437
438 // free the lines only when using getsourceline()
439 if (evalarg->eval_cookie != NULL)
440 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200441 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200442 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200443 // Do not free the last line, "arg" points into it, free it
444 // later.
445 vim_free(evalarg->eval_tofree);
446 evalarg->eval_tofree =
447 ((char_u **)gap->ga_data)[gap->ga_len - 1];
448 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200449 ga_clear_strings(gap);
450 }
451 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200452 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200453 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200454
455 // free lines that were explicitly marked for freeing
456 ga_clear_strings(freegap);
457 }
458
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200459 gap->ga_itemsize = 0;
460 if (p == NULL)
461 return FAIL;
462 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200463 vim_free(evalarg->eval_tofree_lambda);
464 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200465 // Compute "end" relative to the end.
466 *end = *start + STRLEN(*start) - endoff;
467 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200468 }
469
470 return res;
471}
472
473/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100474 * Convert "tv" to a string.
475 * When "convert" is TRUE convert a List into a sequence of lines and convert
476 * a Float to a String.
477 * Returns an allocated string (NULL when out of memory).
478 */
479 char_u *
480typval2string(typval_T *tv, int convert)
481{
482 garray_T ga;
483 char_u *retval;
484#ifdef FEAT_FLOAT
485 char_u numbuf[NUMBUFLEN];
486#endif
487
488 if (convert && tv->v_type == VAR_LIST)
489 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000490 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100491 if (tv->vval.v_list != NULL)
492 {
493 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
494 if (tv->vval.v_list->lv_len > 0)
495 ga_append(&ga, NL);
496 }
497 ga_append(&ga, NUL);
498 retval = (char_u *)ga.ga_data;
499 }
500#ifdef FEAT_FLOAT
501 else if (convert && tv->v_type == VAR_FLOAT)
502 {
503 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
504 retval = vim_strsave(numbuf);
505 }
506#endif
507 else
508 retval = vim_strsave(tv_get_string(tv));
509 return retval;
510}
511
512/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200513 * Top level evaluation function, returning a string. Does not handle line
514 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000515 * When "convert" is TRUE convert a List into a sequence of lines and convert
516 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 * Return pointer to allocated memory, or NULL for failure.
518 */
519 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100520eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100521 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100522 int convert,
523 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524{
Bram Moolenaar33570922005-01-25 22:26:29 +0000525 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100527 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100529 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
530 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 retval = NULL;
532 else
533 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100534 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000535 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100537 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538
539 return retval;
540}
541
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100542 char_u *
543eval_to_string(
544 char_u *arg,
545 int convert)
546{
547 return eval_to_string_eap(arg, convert, NULL);
548}
549
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000551 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200552 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200553 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 */
555 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100556eval_to_string_safe(
557 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100558 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559{
560 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200561 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200562 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200563 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200565 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200566 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000567 if (use_sandbox)
568 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200569 ++textwinlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200570 may_garbage_collect = FALSE;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200571 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000572 if (use_sandbox)
573 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200574 --textwinlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200575 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200576 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200577 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 return retval;
579}
580
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581/*
582 * Top level evaluation function, returning a number.
583 * Evaluates "expr" silently.
584 * Returns -1 for an error.
585 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200586 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100587eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588{
Bram Moolenaar33570922005-01-25 22:26:29 +0000589 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200590 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000591 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
593 ++emsg_off;
594
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200595 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 retval = -1;
597 else
598 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100599 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000600 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 }
602 --emsg_off;
603
604 return retval;
605}
606
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000607/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000608 * Top level evaluation function.
609 * Returns an allocated typval_T with the result.
610 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000611 */
612 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200613eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000614{
615 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200616 evalarg_T evalarg;
617
618 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000619
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200620 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200621 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100622 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000623
Bram Moolenaar37c83712020-06-30 21:18:36 +0200624 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000625 return tv;
626}
627
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000629 * "*arg" points to what can be a function name in the form of "import.Name" or
630 * "Funcref". Return the name of the function. Set "tofree" to something that
631 * was allocated.
632 * If "verbose" is FALSE no errors are given.
633 * Return NULL for any failure.
634 */
635 static char_u *
636deref_function_name(
637 char_u **arg,
638 char_u **tofree,
639 evalarg_T *evalarg,
640 int verbose)
641{
642 typval_T ref;
643 char_u *name = *arg;
644
645 ref.v_type = VAR_UNKNOWN;
646 if (eval7(arg, &ref, evalarg, FALSE) == FAIL)
647 return NULL;
648 if (*skipwhite(*arg) != NUL)
649 {
650 if (verbose)
651 semsg(_(e_trailing_characters_str), *arg);
652 name = NULL;
653 }
654 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
655 {
656 name = ref.vval.v_string;
657 ref.vval.v_string = NULL;
658 *tofree = name;
659 }
660 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
661 {
662 if (ref.vval.v_partial->pt_argc > 0
663 || ref.vval.v_partial->pt_dict != NULL)
664 {
665 if (verbose)
666 emsg(_(e_cannot_use_partial_here));
667 name = NULL;
668 }
669 else
670 {
671 name = vim_strsave(partial_name(ref.vval.v_partial));
672 *tofree = name;
673 }
674 }
675 else
676 {
677 if (verbose)
678 semsg(_(e_not_callable_type_str), name);
679 name = NULL;
680 }
681 clear_tv(&ref);
682 return name;
683}
684
685/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100686 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200687 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
688 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000689 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200691 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100692call_vim_function(
693 char_u *func,
694 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200695 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200696 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000698 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200699 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000700 char_u *arg;
701 char_u *name;
702 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100704 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200705 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000706 funcexe.fe_firstline = curwin->w_cursor.lnum;
707 funcexe.fe_lastline = curwin->w_cursor.lnum;
708 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000709
710 // The name might be "import.Func" or "Funcref".
711 arg = func;
712 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
713 if (name == NULL)
714 name = func;
715
716 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
717
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000718 if (ret == FAIL)
719 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000720 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000721
722 return ret;
723}
724
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100725/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100726 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000727 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
728 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000729 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000730 */
731 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100732call_func_retstr(
733 char_u *func,
734 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200735 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000736{
737 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000738 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000739
Bram Moolenaarded27a12018-08-01 19:06:03 +0200740 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000741 return NULL;
742
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100743 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000744 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 return retval;
746}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000747
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000748/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100749 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000750 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000751 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000752 */
753 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100754call_func_retlist(
755 char_u *func,
756 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200757 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000758{
759 typval_T rettv;
760
Bram Moolenaarded27a12018-08-01 19:06:03 +0200761 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000762 return NULL;
763
764 if (rettv.v_type != VAR_LIST)
765 {
766 clear_tv(&rettv);
767 return NULL;
768 }
769
770 return rettv.vval.v_list;
771}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773#ifdef FEAT_FOLDING
774/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100775 * Evaluate "arg", which is 'foldexpr'.
776 * Note: caller must set "curwin" to match "arg".
777 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
778 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 */
780 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100781eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782{
Bram Moolenaar33570922005-01-25 22:26:29 +0000783 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200784 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000786 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
787 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788
789 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000790 if (use_sandbox)
791 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200792 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200794 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 retval = 0;
796 else
797 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100798 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000799 if (tv.v_type == VAR_NUMBER)
800 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000801 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 retval = 0;
803 else
804 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100805 // If the result is a string, check if there is a non-digit before
806 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000807 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 if (!VIM_ISDIGIT(*s) && *s != '-')
809 *cp = *s++;
810 retval = atol((char *)s);
811 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000812 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 }
814 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000815 if (use_sandbox)
816 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200817 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200818 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200820 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821}
822#endif
823
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000825 * Get an lval: variable, Dict item or List item that can be assigned a value
826 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
827 * "name.key", "name.key[expr]" etc.
828 * Indexing only works if "name" is an existing List or Dictionary.
829 * "name" points to the start of the name.
830 * If "rettv" is not NULL it points to the value to be assigned.
831 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
832 * wrong; must end in space or cmd separator.
833 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100834 * flags:
835 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100836 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100837 * GLV_NO_AUTOLOAD: do not use script autoloading
838 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000839 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000840 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000841 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000842 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200843 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100844get_lval(
845 char_u *name,
846 typval_T *rettv,
847 lval_T *lp,
848 int unlet,
849 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100850 int flags, // GLV_ values
851 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000852{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000853 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000854 char_u *expr_start, *expr_end;
855 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000856 dictitem_T *v;
857 typval_T var1;
858 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000859 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000860 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000861 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100862 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100863 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100864 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000865
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100866 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200867 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000868
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100869 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000870 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100871 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000872 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200873 lp->ll_name_end = find_name_end(name, NULL, NULL,
874 FNE_INCL_BR | fne_flags);
875 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000876 }
877
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100878 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000879 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100880 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000881 if (expr_start != NULL)
882 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100883 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100884 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000885 && *p != '[' && *p != '.')
886 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000887 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000888 return NULL;
889 }
890
891 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
892 if (lp->ll_exp_name == NULL)
893 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100894 // Report an invalid expression in braces, unless the
895 // expression evaluation has been cancelled due to an
896 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000897 if (!aborting() && !quiet)
898 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000899 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000900 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000901 return NULL;
902 }
903 }
904 lp->ll_name = lp->ll_exp_name;
905 }
906 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100907 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000908 lp->ll_name = name;
909
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200910 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100911 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200912 // "a: type" is declaring variable "a" with a type, not "a:".
913 if (p == name + 2 && p[-1] == ':')
914 {
915 --p;
916 lp->ll_name_end = p;
917 }
918 if (*p == ':')
919 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000920 garray_T tmp_type_list;
921 garray_T *type_list;
922 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +0200923
924 if (tp == p + 1 && !quiet)
925 {
926 semsg(_(e_white_space_required_after_str_str), ":", p);
927 return NULL;
928 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100929
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000930 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
931 type_list = &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list;
932 else
933 {
934 type_list = &tmp_type_list;
935 ga_init2(type_list, sizeof(type_T), 10);
936 }
937
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200938 // parse the type after the name
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000939 lp->ll_type = parse_type(&tp, type_list, !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100940 if (lp->ll_type == NULL && !quiet)
941 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200942 lp->ll_name_end = tp;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000943
944 // drop the type when not in a script
945 if (type_list == &tmp_type_list)
946 {
947 lp->ll_type = NULL;
948 clear_type_list(type_list);
949 }
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200950 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100951 }
952 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000953 if (lp->ll_name == NULL)
954 return p;
955
956 if (*p == '.' && in_vim9script())
957 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000958 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name,
959 TRUE, NULL);
960
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000961 if (import != NULL)
962 {
963 ufunc_T *ufunc;
964 type_T *type;
965
966 lp->ll_sid = import->imp_sid;
967 lp->ll_name = skipwhite(p + 1);
968 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
969 lp->ll_name_end = p;
970
971 // check the item is exported
972 cc = *p;
973 *p = NUL;
974 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
975 NULL, TRUE) == -1)
976 {
977 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +0000978 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000979 }
980 *p = cc;
981 }
982 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100983
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100984 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000985 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000986 return p;
987
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200988 if (in_vim9script() && lval_root != NULL)
989 {
990 // using local variable
991 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +0200992 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200993 }
994 else
995 {
996 cc = *p;
997 *p = NUL;
998 // When we would write to the variable pass &ht and prevent autoload.
999 writing = !(flags & GLV_READ_ONLY);
1000 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001001 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001002 if (v == NULL && !quiet)
1003 semsg(_(e_undefined_variable_str), lp->ll_name);
1004 *p = cc;
1005 if (v == NULL)
1006 return NULL;
1007 lp->ll_tv = &v->di_tv;
1008 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001009
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001010 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
1011 {
1012 if (!quiet)
1013 semsg(_(e_variable_already_declared), lp->ll_name);
1014 return NULL;
1015 }
1016
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001017 /*
1018 * Loop until no more [idx] or .key is following.
1019 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001020 var1.v_type = VAR_UNKNOWN;
1021 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001022 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001023 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001024 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1025 {
1026 if (!quiet)
1027 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1028 return NULL;
1029 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001030 if (lp->ll_tv->v_type != VAR_LIST
1031 && lp->ll_tv->v_type != VAR_DICT
1032 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001033 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001034 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001035 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001036 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001037 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001038
1039 // a NULL list/blob works like an empty list/blob, allocate one now.
1040 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1041 rettv_list_alloc(lp->ll_tv);
1042 else if (lp->ll_tv->v_type == VAR_BLOB
1043 && lp->ll_tv->vval.v_blob == NULL)
1044 rettv_blob_alloc(lp->ll_tv);
1045
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001046 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001047 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001048 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001049 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001050 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001051 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001052
Bram Moolenaar10c65862020-10-08 21:16:42 +02001053 if (in_vim9script() && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001054 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001055 && lp->ll_tv == &v->di_tv
1056 && ht != NULL && ht == get_script_local_ht())
1057 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001058 svar_T *sv = find_typval_in_script(lp->ll_tv, 0);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001059
1060 // Vim9 script local variable: get the type
1061 if (sv != NULL)
1062 lp->ll_valtype = sv->sv_type;
1063 }
1064
Bram Moolenaar8c711452005-01-14 21:53:12 +00001065 len = -1;
1066 if (*p == '.')
1067 {
1068 key = p + 1;
1069 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1070 ;
1071 if (len == 0)
1072 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001073 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001074 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001075 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001076 }
1077 p = key + len;
1078 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001079 else
1080 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001081 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001082 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001083 if (*p == ':')
1084 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001085 else
1086 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001087 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001088 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001089 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001090 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001091 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001092 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001093 clear_tv(&var1);
1094 return NULL;
1095 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001096 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001097 }
1098
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001099 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001100 if (*p == ':')
1101 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001102 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001103 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001104 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001105 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001106 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001107 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001108 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001109 if (rettv != NULL
1110 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001111 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001112 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001113 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001114 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001115 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001116 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001117 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001118 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001119 }
1120 p = skipwhite(p + 1);
1121 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001122 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001123 else
1124 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001125 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001126 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001127 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001128 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001129 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001130 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001131 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001132 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001133 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001134 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001135 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001136 clear_tv(&var2);
1137 return NULL;
1138 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001139 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001140 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001141 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001142 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001143 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001144
Bram Moolenaar8c711452005-01-14 21:53:12 +00001145 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001146 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001147 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001148 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001149 clear_tv(&var1);
1150 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001151 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001152 }
1153
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001154 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001155 ++p;
1156 }
1157
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001158 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001159 {
1160 if (len == -1)
1161 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001162 // "[key]": get key from "var1"
1163 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001164 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001165 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001166 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001167 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001168 }
1169 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001170 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001171
1172 // a NULL dict is equivalent with an empty dict
1173 if (lp->ll_tv->vval.v_dict == NULL)
1174 {
1175 lp->ll_tv->vval.v_dict = dict_alloc();
1176 if (lp->ll_tv->vval.v_dict == NULL)
1177 {
1178 clear_tv(&var1);
1179 return NULL;
1180 }
1181 ++lp->ll_tv->vval.v_dict->dv_refcount;
1182 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001183 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001184
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001185 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001186
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001187 // When assigning to a scope dictionary check that a function and
1188 // variable name is valid (only variable name unless it is l: or
1189 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001190 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001191 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001192 int prevval;
1193 int wrong;
1194
1195 if (len != -1)
1196 {
1197 prevval = key[len];
1198 key[len] = NUL;
1199 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001200 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001201 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001202 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1203 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001204 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001205 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001206 if (len != -1)
1207 key[len] = prevval;
1208 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001209 {
1210 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001211 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001212 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001213 }
1214
Bram Moolenaar10c65862020-10-08 21:16:42 +02001215 if (lp->ll_valtype != NULL)
1216 // use the type of the member
1217 lp->ll_valtype = lp->ll_valtype->tt_member;
1218
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001219 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001220 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001221 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001222 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001223 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001224 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001225 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001226 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001227 return NULL;
1228 }
1229
Bram Moolenaar31b81602019-02-10 22:14:27 +01001230 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001231 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001232 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001233 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001234 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001235 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001236 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001237 }
1238 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001239 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001240 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001241 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001242 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001243 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001244 p = NULL;
1245 break;
1246 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001247 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001248 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001249 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1250 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001251 {
1252 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001253 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001254 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001255
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001256 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001257 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001258 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001259 else if (lp->ll_tv->v_type == VAR_BLOB)
1260 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001261 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1262
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001263 /*
1264 * Get the number and item for the only or first index of the List.
1265 */
1266 if (empty1)
1267 lp->ll_n1 = 0;
1268 else
1269 // is number or string
1270 lp->ll_n1 = (long)tv_get_number(&var1);
1271 clear_tv(&var1);
1272
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001273 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001274 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001275 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001276 return NULL;
1277 }
1278 if (lp->ll_range && !lp->ll_empty2)
1279 {
1280 lp->ll_n2 = (long)tv_get_number(&var2);
1281 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001282 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1283 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001284 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001285 }
1286 lp->ll_blob = lp->ll_tv->vval.v_blob;
1287 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001288 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001289 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001290 else
1291 {
1292 /*
1293 * Get the number and item for the only or first index of the List.
1294 */
1295 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001296 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001297 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001298 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001299 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001300 clear_tv(&var1);
1301
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001302 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001303 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001304 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001305 if (lp->ll_li == NULL)
1306 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001307 clear_tv(&var2);
1308 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001309 }
1310
Bram Moolenaar10c65862020-10-08 21:16:42 +02001311 if (lp->ll_valtype != NULL)
1312 // use the type of the member
1313 lp->ll_valtype = lp->ll_valtype->tt_member;
1314
Bram Moolenaar8c711452005-01-14 21:53:12 +00001315 /*
1316 * May need to find the item or absolute index for the second
1317 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001318 * When no index given: "lp->ll_empty2" is TRUE.
1319 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001320 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001321 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001322 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001323 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001324 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001325 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001326 if (check_range_index_two(lp->ll_list,
1327 &lp->ll_n1, lp->ll_li,
1328 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001329 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001330 }
1331
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001332 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001333 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001334 }
1335
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001336 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001338 return p;
1339}
1340
1341/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001342 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001343 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001344 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001345clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001346{
1347 vim_free(lp->ll_exp_name);
1348 vim_free(lp->ll_newkey);
1349}
1350
1351/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001352 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001353 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001354 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1355 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001356 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001357 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001358set_var_lval(
1359 lval_T *lp,
1360 char_u *endp,
1361 typval_T *rettv,
1362 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001363 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1364 char_u *op,
1365 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001366{
1367 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001368 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001369
1370 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001371 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001372 cc = *endp;
1373 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001374 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1375 return;
1376
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001377 if (lp->ll_blob != NULL)
1378 {
1379 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001380
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001381 if (op != NULL && *op != '=')
1382 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001383 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001384 return;
1385 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001386 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001387 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001388
1389 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1390 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001391 if (lp->ll_empty2)
1392 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1393
Bram Moolenaar68452172021-04-12 21:21:02 +02001394 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1395 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001396 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001397 }
1398 else
1399 {
1400 val = (int)tv_get_number_chk(rettv, &error);
1401 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001402 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001403 }
1404 }
1405 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001406 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001407 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001408
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001409 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1410 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001411 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001412 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001413 *endp = cc;
1414 return;
1415 }
1416
Bram Moolenaarff697e62019-02-12 22:28:33 +01001417 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001418 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001419 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001420 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001421 {
1422 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001423 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1424 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001425 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001426 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001427 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001428 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001429 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001430 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001431 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001432 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001433 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1434 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001435 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001436 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001437 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001438 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001439 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001440 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001441 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001442 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001443 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001444 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001445 else if (lp->ll_range)
1446 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001447 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1448 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001449 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001450 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001451 return;
1452 }
1453
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001454 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1455 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001456 }
1457 else
1458 {
1459 /*
1460 * Assign to a List or Dictionary item.
1461 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001462 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1463 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001464 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001465 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001466 return;
1467 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001468
1469 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001470 && check_typval_arg_type(lp->ll_valtype, rettv,
1471 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001472 return;
1473
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001474 if (lp->ll_newkey != NULL)
1475 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001476 if (op != NULL && *op != '=')
1477 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001478 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001479 return;
1480 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001481 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1482 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001483 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001484
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001485 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001486 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001487 if (di == NULL)
1488 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001489 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1490 {
1491 vim_free(di);
1492 return;
1493 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001494 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001495 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001496 else if (op != NULL && *op != '=')
1497 {
1498 tv_op(lp->ll_tv, rettv, op);
1499 return;
1500 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001501 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001502 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001503
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001504 /*
1505 * Assign the value to the variable or list item.
1506 */
1507 if (copy)
1508 copy_tv(rettv, lp->ll_tv);
1509 else
1510 {
1511 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001512 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001513 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001514 }
1515 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001516}
1517
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001518/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001519 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1520 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001521 * Returns OK or FAIL.
1522 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001523 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001524tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001525{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001526 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001527 char_u numbuf[NUMBUFLEN];
1528 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001529 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001530
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001531 // Can't do anything with a Funcref or Dict on the right.
1532 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001533 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001534 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1535 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001536 {
1537 switch (tv1->v_type)
1538 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001539 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001540 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001542 case VAR_DICT:
1543 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001544 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001545 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001546 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001547 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001548 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001549 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001550 break;
1551
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001552 case VAR_BLOB:
1553 if (*op != '+' || tv2->v_type != VAR_BLOB)
1554 break;
1555 // BLOB += BLOB
1556 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1557 {
1558 blob_T *b1 = tv1->vval.v_blob;
1559 blob_T *b2 = tv2->vval.v_blob;
1560 int i, len = blob_len(b2);
1561 for (i = 0; i < len; i++)
1562 ga_append(&b1->bv_ga, blob_get(b2, i));
1563 }
1564 return OK;
1565
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001566 case VAR_LIST:
1567 if (*op != '+' || tv2->v_type != VAR_LIST)
1568 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001569 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001570 if (tv2->vval.v_list != NULL)
1571 {
1572 if (tv1->vval.v_list == NULL)
1573 {
1574 tv1->vval.v_list = tv2->vval.v_list;
1575 ++tv1->vval.v_list->lv_refcount;
1576 }
1577 else
1578 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1579 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001580 return OK;
1581
1582 case VAR_NUMBER:
1583 case VAR_STRING:
1584 if (tv2->v_type == VAR_LIST)
1585 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001586 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001587 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001588 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001589 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001590#ifdef FEAT_FLOAT
1591 if (tv2->v_type == VAR_FLOAT)
1592 {
1593 float_T f = n;
1594
Bram Moolenaarff697e62019-02-12 22:28:33 +01001595 if (*op == '%')
1596 break;
1597 switch (*op)
1598 {
1599 case '+': f += tv2->vval.v_float; break;
1600 case '-': f -= tv2->vval.v_float; break;
1601 case '*': f *= tv2->vval.v_float; break;
1602 case '/': f /= tv2->vval.v_float; break;
1603 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001604 clear_tv(tv1);
1605 tv1->v_type = VAR_FLOAT;
1606 tv1->vval.v_float = f;
1607 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001608 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001609#endif
1610 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001611 switch (*op)
1612 {
1613 case '+': n += tv_get_number(tv2); break;
1614 case '-': n -= tv_get_number(tv2); break;
1615 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001616 case '/': n = num_divide(n, tv_get_number(tv2),
1617 &failed); break;
1618 case '%': n = num_modulus(n, tv_get_number(tv2),
1619 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001620 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001621 clear_tv(tv1);
1622 tv1->v_type = VAR_NUMBER;
1623 tv1->vval.v_number = n;
1624 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001625 }
1626 else
1627 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001628 if (tv2->v_type == VAR_FLOAT)
1629 break;
1630
Bram Moolenaarff697e62019-02-12 22:28:33 +01001631 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001632 s = tv_get_string(tv1);
1633 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001634 clear_tv(tv1);
1635 tv1->v_type = VAR_STRING;
1636 tv1->vval.v_string = s;
1637 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001638 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001639
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001640 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001641#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001642 {
1643 float_T f;
1644
Bram Moolenaarff697e62019-02-12 22:28:33 +01001645 if (*op == '%' || *op == '.'
1646 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001647 && tv2->v_type != VAR_NUMBER
1648 && tv2->v_type != VAR_STRING))
1649 break;
1650 if (tv2->v_type == VAR_FLOAT)
1651 f = tv2->vval.v_float;
1652 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001653 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001654 switch (*op)
1655 {
1656 case '+': tv1->vval.v_float += f; break;
1657 case '-': tv1->vval.v_float -= f; break;
1658 case '*': tv1->vval.v_float *= f; break;
1659 case '/': tv1->vval.v_float /= f; break;
1660 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001661 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001662#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001663 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001664 }
1665 }
1666
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001667 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001668 return FAIL;
1669}
1670
1671/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001672 * Evaluate the expression used in a ":for var in expr" command.
1673 * "arg" points to "var".
1674 * Set "*errp" to TRUE for an error, FALSE otherwise;
1675 * Return a pointer that holds the info. Null when there is an error.
1676 */
1677 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001678eval_for_line(
1679 char_u *arg,
1680 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001681 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001682 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001683{
Bram Moolenaar33570922005-01-25 22:26:29 +00001684 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001685 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001686 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001687 typval_T tv;
1688 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001689 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001690
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001691 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001692
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001693 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001694 if (fi == NULL)
1695 return NULL;
1696
Bram Moolenaar404557e2021-07-05 21:41:48 +02001697 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1698 &fi->fi_semicolon, FALSE);
1699 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001700 return fi;
1701
Bram Moolenaar404557e2021-07-05 21:41:48 +02001702 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001703 if (expr[0] != 'i' || expr[1] != 'n'
1704 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001705 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001706 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1707 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1708 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001709 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001710 return fi;
1711 }
1712
1713 if (skip)
1714 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001715 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1716 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001717 {
1718 *errp = FALSE;
1719 if (!skip)
1720 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001721 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001722 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001723 l = tv.vval.v_list;
1724 if (l == NULL)
1725 {
1726 // a null list is like an empty list: do nothing
1727 clear_tv(&tv);
1728 }
1729 else
1730 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001731 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001732 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001733
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001734 // No need to increment the refcount, it's already set for
1735 // the list being used in "tv".
1736 fi->fi_list = l;
1737 list_add_watch(l, &fi->fi_lw);
1738 fi->fi_lw.lw_item = l->lv_first;
1739 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001740 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001741 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001742 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001743 fi->fi_bi = 0;
1744 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001745 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001746 typval_T btv;
1747
1748 // Make a copy, so that the iteration still works when the
1749 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001751 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001752 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001753 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001754 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001755 else if (tv.v_type == VAR_STRING)
1756 {
1757 fi->fi_byte_idx = 0;
1758 fi->fi_string = tv.vval.v_string;
1759 tv.vval.v_string = NULL;
1760 if (fi->fi_string == NULL)
1761 fi->fi_string = vim_strsave((char_u *)"");
1762 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001763 else
1764 {
Sean Dewar80d73952021-08-04 19:25:54 +02001765 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001766 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001767 }
1768 }
1769 }
1770 if (skip)
1771 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001772 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001773
1774 return fi;
1775}
1776
1777/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001778 * Used when looping over a :for line, skip the "in expr" part.
1779 */
1780 void
1781skip_for_lines(void *fi_void, evalarg_T *evalarg)
1782{
1783 forinfo_T *fi = (forinfo_T *)fi_void;
1784 int i;
1785
1786 for (i = 0; i < fi->fi_break_count; ++i)
1787 eval_next_line(evalarg);
1788}
1789
1790/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001791 * Use the first item in a ":for" list. Advance to the next.
1792 * Assign the values to the variable (list). "arg" points to the first one.
1793 * Return TRUE when a valid item was found, FALSE when at end of list or
1794 * something wrong.
1795 */
1796 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001797next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001798{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001799 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001800 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001801 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001802 ? (ASSIGN_FINAL
1803 // first round: error if variable exists
1804 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1805 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001806 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001807 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001808 int skip_assign = in_vim9script() && arg[0] == '_'
1809 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001810
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001811 if (fi->fi_blob != NULL)
1812 {
1813 typval_T tv;
1814
1815 if (fi->fi_bi >= blob_len(fi->fi_blob))
1816 return FALSE;
1817 tv.v_type = VAR_NUMBER;
1818 tv.v_lock = VAR_FIXED;
1819 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1820 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001821 if (skip_assign)
1822 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001823 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001824 fi->fi_varcount, flag, NULL) == OK;
1825 }
1826
1827 if (fi->fi_string != NULL)
1828 {
1829 typval_T tv;
1830 int len;
1831
1832 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1833 if (len == 0)
1834 return FALSE;
1835 tv.v_type = VAR_STRING;
1836 tv.v_lock = VAR_FIXED;
1837 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1838 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001839 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001840 if (skip_assign)
1841 result = TRUE;
1842 else
1843 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001844 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001845 vim_free(tv.vval.v_string);
1846 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001847 }
1848
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001849 item = fi->fi_lw.lw_item;
1850 if (item == NULL)
1851 result = FALSE;
1852 else
1853 {
1854 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001855 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001856 if (skip_assign)
1857 result = TRUE;
1858 else
1859 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001860 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001861 }
1862 return result;
1863}
1864
1865/*
1866 * Free the structure used to store info used by ":for".
1867 */
1868 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001869free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001870{
Bram Moolenaar33570922005-01-25 22:26:29 +00001871 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001872
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001873 if (fi == NULL)
1874 return;
1875 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001876 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001877 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001878 list_unref(fi->fi_list);
1879 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001880 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001881 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001882 else
1883 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001884 vim_free(fi);
1885}
1886
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001888set_context_for_expression(
1889 expand_T *xp,
1890 char_u *arg,
1891 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001893 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001895 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001897 if (cmdidx == CMD_let || cmdidx == CMD_var
1898 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001899 {
1900 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001901 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001902 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001903 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001904 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001905 {
1906 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001907 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001908 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001909 break;
1910 }
1911 return;
1912 }
1913 }
1914 else
1915 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1916 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 while ((xp->xp_pattern = vim_strpbrk(arg,
1918 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1919 {
1920 c = *xp->xp_pattern;
1921 if (c == '&')
1922 {
1923 c = xp->xp_pattern[1];
1924 if (c == '&')
1925 {
1926 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001927 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 }
1929 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001930 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001932 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1933 xp->xp_pattern += 2;
1934
1935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 }
1937 else if (c == '$')
1938 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001939 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 xp->xp_context = EXPAND_ENV_VARS;
1941 }
1942 else if (c == '=')
1943 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001944 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 xp->xp_context = EXPAND_EXPRESSION;
1946 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001947 else if (c == '#'
1948 && xp->xp_context == EXPAND_EXPRESSION)
1949 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001950 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001951 break;
1952 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001953 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 && xp->xp_context == EXPAND_FUNCTIONS
1955 && vim_strchr(xp->xp_pattern, '(') == NULL)
1956 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001957 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 break;
1959 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001960 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001962 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 {
1964 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1965 if (c == '\\' && xp->xp_pattern[1] != NUL)
1966 ++xp->xp_pattern;
1967 xp->xp_context = EXPAND_NOTHING;
1968 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001969 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001971 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1973 /* skip */ ;
1974 xp->xp_context = EXPAND_NOTHING;
1975 }
1976 else if (c == '|')
1977 {
1978 if (xp->xp_pattern[1] == '|')
1979 {
1980 ++xp->xp_pattern;
1981 xp->xp_context = EXPAND_EXPRESSION;
1982 }
1983 else
1984 xp->xp_context = EXPAND_COMMANDS;
1985 }
1986 else
1987 xp->xp_context = EXPAND_EXPRESSION;
1988 }
1989 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001990 // Doesn't look like something valid, expand as an expression
1991 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001992 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 arg = xp->xp_pattern;
1994 if (*arg != NUL)
1995 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1996 /* skip */ ;
1997 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001998
1999 // ":exe one two" completes "two"
2000 if ((cmdidx == CMD_execute
2001 || cmdidx == CMD_echo
2002 || cmdidx == CMD_echon
2003 || cmdidx == CMD_echomsg)
2004 && xp->xp_context == EXPAND_EXPRESSION)
2005 {
2006 for (;;)
2007 {
2008 char_u *n = skiptowhite(arg);
2009
2010 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2011 break;
2012 arg = skipwhite(n);
2013 }
2014 }
2015
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 xp->xp_pattern = arg;
2017}
2018
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002020 * Return TRUE if "pat" matches "text".
2021 * Does not use 'cpo' and always uses 'magic'.
2022 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002023 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002024pattern_match(char_u *pat, char_u *text, int ic)
2025{
2026 int matches = FALSE;
2027 char_u *save_cpo;
2028 regmatch_T regmatch;
2029
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002030 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002031 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002032 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002033 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2034 if (regmatch.regprog != NULL)
2035 {
2036 regmatch.rm_ic = ic;
2037 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2038 vim_regfree(regmatch.regprog);
2039 }
2040 p_cpo = save_cpo;
2041 return matches;
2042}
2043
2044/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002045 * Handle a name followed by "(". Both for just "name(arg)" and for
2046 * "expr->name(arg)".
2047 * Returns OK or FAIL.
2048 */
2049 static int
2050eval_func(
2051 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002052 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002053 char_u *name,
2054 int name_len,
2055 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002056 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002057 typval_T *basetv) // "expr" for "expr->name(arg)"
2058{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002059 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002060 char_u *s = name;
2061 int len = name_len;
2062 partial_T *partial;
2063 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002064 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002065 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002066
2067 if (!evaluate)
2068 check_vars(s, len);
2069
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002070 // If "s" is the name of a variable of type VAR_FUNC
2071 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002072 s = deref_func_name(s, &len, &partial,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002073 in_vim9script() ? &type : NULL, !evaluate, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002074
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002075 // Need to make a copy, in case evaluating the arguments makes
2076 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002077 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002078 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002079 ret = FAIL;
2080 else
2081 {
2082 funcexe_T funcexe;
2083
2084 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002085 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002086 funcexe.fe_firstline = curwin->w_cursor.lnum;
2087 funcexe.fe_lastline = curwin->w_cursor.lnum;
2088 funcexe.fe_evaluate = evaluate;
2089 funcexe.fe_partial = partial;
2090 funcexe.fe_basetv = basetv;
2091 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002092 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002093 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002094 }
2095 vim_free(s);
2096
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002097 // If evaluate is FALSE rettv->v_type was not set in
2098 // get_func_tv, but it's needed in handle_subscript() to parse
2099 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002100 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2101 {
2102 rettv->vval.v_string = NULL;
2103 rettv->v_type = VAR_FUNC;
2104 }
2105
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002106 // Stop the expression evaluation when immediately
2107 // aborting on error, or when an interrupt occurred or
2108 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002109 if (evaluate && aborting())
2110 {
2111 if (ret == OK)
2112 clear_tv(rettv);
2113 ret = FAIL;
2114 }
2115 return ret;
2116}
2117
2118/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002119 * Get the next line source line without advancing. But do skip over comment
2120 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002121 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002122 */
2123 static char_u *
2124getline_peek_skip_comments(evalarg_T *evalarg)
2125{
2126 for (;;)
2127 {
2128 char_u *next = getline_peek(evalarg->eval_getline,
2129 evalarg->eval_cookie);
2130 char_u *p;
2131
2132 if (next == NULL)
2133 break;
2134 p = skipwhite(next);
2135 if (*p != NUL && !vim9_comment_start(p))
2136 return next;
2137 (void)eval_next_line(evalarg);
2138 }
2139 return NULL;
2140}
2141
2142/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002143 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2144 * comment) and there is a next line, return the next line (skipping blanks)
2145 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002146 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2147 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002148 * "arg" must point somewhere inside a line, not at the start.
2149 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002150 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002151eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2152{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002153 char_u *p = skipwhite(arg);
2154
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002155 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002156 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002157 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002158 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002159 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002160 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002161 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002162
2163 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002164 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002165 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002166 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002167
Bram Moolenaar9d489562020-07-30 20:08:50 +02002168 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002169 {
2170 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002171 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002172 }
2173 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002174 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002175}
2176
2177/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002178 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002179 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002180 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002181 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002182eval_next_line(evalarg_T *evalarg)
2183{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002184 garray_T *gap = &evalarg->eval_ga;
2185 char_u *line;
2186
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002187 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002188 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2189 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002190 else
2191 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002192 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002193 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2194 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002195 char_u *p = skipwhite(line);
2196
2197 // Going to concatenate the lines after parsing. For an empty or
2198 // comment line use an empty string.
2199 if (*p == NUL || vim9_comment_start(p))
2200 {
2201 vim_free(line);
2202 line = vim_strsave((char_u *)"");
2203 }
2204
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002205 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2206 ++gap->ga_len;
2207 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002208 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002209 {
2210 vim_free(evalarg->eval_tofree);
2211 evalarg->eval_tofree = line;
2212 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002213
2214 // Advanced to the next line, "arg" no longer points into the previous
2215 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002216 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002217 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002218}
2219
Bram Moolenaare6b53242020-07-01 17:28:33 +02002220/*
2221 * Call eval_next_non_blank() and get the next line if needed.
2222 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002223 char_u *
2224skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2225{
2226 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002227 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002228
Bram Moolenaar962d7212020-07-04 14:15:00 +02002229 if (evalarg == NULL)
2230 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002231 eval_next_non_blank(p, evalarg, &getnext);
2232 if (getnext)
2233 return eval_next_line(evalarg);
2234 return p;
2235}
2236
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002237/*
Bram Moolenaar844fb642021-10-23 13:32:30 +01002238 * Initialize "evalarg" for use.
2239 */
2240 void
2241init_evalarg(evalarg_T *evalarg)
2242{
2243 CLEAR_POINTER(evalarg);
2244 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
2245}
2246
2247/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002248 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002249 */
2250 void
2251clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2252{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002253 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002254 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002255 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002256 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002257 if (eap != NULL)
2258 {
2259 // We may need to keep the original command line, e.g. for
2260 // ":let" it has the variable names. But we may also need the
2261 // new one, "nextcmd" points into it. Keep both.
2262 vim_free(eap->cmdline_tofree);
2263 eap->cmdline_tofree = *eap->cmdlinep;
2264 *eap->cmdlinep = evalarg->eval_tofree;
2265 }
2266 else
2267 vim_free(evalarg->eval_tofree);
2268 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002269 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002270
Bram Moolenaar844fb642021-10-23 13:32:30 +01002271 ga_clear_strings(&evalarg->eval_tofree_ga);
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002272 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002273 }
2274}
2275
2276/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002278 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2280 */
2281
2282/*
2283 * Handle zero level expression.
2284 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002286 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002287 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 * Return OK or FAIL.
2289 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002290 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002291eval0(
2292 char_u *arg,
2293 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002294 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002295 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002297 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2298}
2299
2300/*
2301 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2302 * expression and don't check what comes after the expression.
2303 */
2304 int
2305eval0_retarg(
2306 char_u *arg,
2307 typval_T *rettv,
2308 exarg_T *eap,
2309 evalarg_T *evalarg,
2310 char_u **retarg)
2311{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 int ret;
2313 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002314 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002315 int did_emsg_before = did_emsg;
2316 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002317 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002318 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002319 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320
2321 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002322 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002323 expr_end = p;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002324 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002325
Bram Moolenaar02929a32021-12-17 14:46:12 +00002326 // In Vim9 script a command block is not split at NL characters for
2327 // commands using an expression argument. Skip over a '#' comment to check
2328 // for a following NL. Require white space before the '#'.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002329 if (in_vim9script() && p > expr_end && retarg == NULL)
Bram Moolenaar02929a32021-12-17 14:46:12 +00002330 while (*p == '#')
2331 {
2332 char_u *nl = vim_strchr(p, NL);
2333
2334 if (nl == NULL)
2335 break;
2336 p = skipwhite(nl + 1);
2337 if (eap != NULL && *p != NUL)
2338 eap->nextcmd = p;
2339 check_for_end = FALSE;
2340 }
2341
2342 if (ret != FAIL && check_for_end)
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002343 end_error = !ends_excmd2(arg, p);
2344 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 {
2346 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 /*
2349 * Report the invalid expression unless the expression evaluation has
2350 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002351 * exception, or we already gave a more specific error.
2352 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002354 if (!aborting()
2355 && did_emsg == did_emsg_before
2356 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002357 && (flags & EVAL_CONSTANT) == 0
2358 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002359 {
2360 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002361 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002362 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002363 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002364 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002365
2366 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002367 // a next command to avoid more errors, unless "|" is following, which
2368 // could only be a command separator.
2369 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2370 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002371 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002373
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002374 if (retarg != NULL)
2375 *retarg = p;
2376 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002377 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002378
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 return ret;
2380}
2381
2382/*
2383 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002384 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002385 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 *
2387 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002388 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002390 * Note: "rettv.v_lock" is not set.
2391 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 * Return OK or FAIL.
2393 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002394 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002395eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002397 char_u *p;
2398 int getnext;
2399
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002400 CLEAR_POINTER(rettv);
2401
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 /*
2403 * Get the first variable.
2404 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002405 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 return FAIL;
2407
Bram Moolenaar793648f2020-06-26 21:28:25 +02002408 p = eval_next_non_blank(*arg, evalarg, &getnext);
2409 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002411 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002412 int result;
2413 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002414 evalarg_T *evalarg_used = evalarg;
2415 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002416 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002417 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002418 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002419
2420 if (evalarg == NULL)
2421 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002422 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002423 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002424 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002425 orig_flags = evalarg_used->eval_flags;
2426 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002427
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002428 if (getnext)
2429 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002430 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002431 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002432 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002433 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002434 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002435 clear_tv(rettv);
2436 return FAIL;
2437 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002438 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002439 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002440
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002442 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002444 int error = FALSE;
2445
Bram Moolenaar13106602020-10-04 16:06:05 +02002446 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002447 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002448 else if (vim9script)
2449 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002450 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002452 if (error || !op_falsy || !result)
2453 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002454 if (error)
2455 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 }
2457
2458 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002459 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002461 if (op_falsy)
2462 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002463 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002464 {
mityu4ac198c2021-05-28 17:52:40 +02002465 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002466 clear_tv(rettv);
2467 return FAIL;
2468 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002469 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002470 evalarg_used->eval_flags = (op_falsy ? !result : result)
2471 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2472 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002473 {
2474 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002476 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002477 if (!op_falsy || !result)
2478 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002480 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002482 /*
2483 * Check for the ":".
2484 */
2485 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2486 if (*p != ':')
2487 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002488 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002489 if (evaluate && result)
2490 clear_tv(rettv);
2491 evalarg_used->eval_flags = orig_flags;
2492 return FAIL;
2493 }
2494 if (getnext)
2495 *arg = eval_next_line(evalarg_used);
2496 else
2497 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002498 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002499 {
2500 error_white_both(p, 1);
2501 clear_tv(rettv);
2502 evalarg_used->eval_flags = orig_flags;
2503 return FAIL;
2504 }
2505 *arg = p;
2506 }
2507
2508 /*
2509 * Get the third variable. Recursive!
2510 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002511 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002512 {
mityu4ac198c2021-05-28 17:52:40 +02002513 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002514 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002515 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002516 return FAIL;
2517 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002518 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2519 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002520 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002521 if (eval1(arg, &var2, evalarg_used) == FAIL)
2522 {
2523 if (evaluate && result)
2524 clear_tv(rettv);
2525 evalarg_used->eval_flags = orig_flags;
2526 return FAIL;
2527 }
2528 if (evaluate && !result)
2529 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002531
2532 if (evalarg == NULL)
2533 clear_evalarg(&local_evalarg, NULL);
2534 else
2535 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 }
2537
2538 return OK;
2539}
2540
2541/*
2542 * Handle first level expression:
2543 * expr2 || expr2 || expr2 logical OR
2544 *
2545 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002546 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 *
2548 * Return OK or FAIL.
2549 */
2550 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002551eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002553 char_u *p;
2554 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555
2556 /*
2557 * Get the first variable.
2558 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002559 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 return FAIL;
2561
2562 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002563 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002565 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002566 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002568 evalarg_T *evalarg_used = evalarg;
2569 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002570 int evaluate;
2571 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002572 long result = FALSE;
2573 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002574 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002575 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002576
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002577 if (evalarg == NULL)
2578 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002579 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002580 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002581 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002582 orig_flags = evalarg_used->eval_flags;
2583 evaluate = orig_flags & EVAL_EVALUATE;
2584 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002585 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002586 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002587 result = tv_get_bool_chk(rettv, &error);
2588 else if (tv_get_number_chk(rettv, &error) != 0)
2589 result = TRUE;
2590 clear_tv(rettv);
2591 if (error)
2592 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 }
2594
2595 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002596 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002598 while (p[0] == '|' && p[1] == '|')
2599 {
2600 if (getnext)
2601 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002602 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002603 {
2604 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2605 {
2606 error_white_both(p, 2);
2607 clear_tv(rettv);
2608 return FAIL;
2609 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002610 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002611 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002612
2613 /*
2614 * Get the second variable.
2615 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002616 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2617 {
mityu4ac198c2021-05-28 17:52:40 +02002618 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002619 clear_tv(rettv);
2620 return FAIL;
2621 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002622 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2623 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002624 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002625 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002626 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002627
2628 /*
2629 * Compute the result.
2630 */
2631 if (evaluate && !result)
2632 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002633 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002634 result = tv_get_bool_chk(&var2, &error);
2635 else if (tv_get_number_chk(&var2, &error) != 0)
2636 result = TRUE;
2637 clear_tv(&var2);
2638 if (error)
2639 return FAIL;
2640 }
2641 if (evaluate)
2642 {
2643 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002644 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002645 rettv->v_type = VAR_BOOL;
2646 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002647 }
2648 else
2649 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002650 rettv->v_type = VAR_NUMBER;
2651 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002652 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002653 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002654
2655 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002657
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002658 if (evalarg == NULL)
2659 clear_evalarg(&local_evalarg, NULL);
2660 else
2661 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 }
2663
2664 return OK;
2665}
2666
2667/*
2668 * Handle second level expression:
2669 * expr3 && expr3 && expr3 logical AND
2670 *
2671 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002672 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 *
2674 * Return OK or FAIL.
2675 */
2676 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002677eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002679 char_u *p;
2680 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681
2682 /*
2683 * Get the first variable.
2684 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002685 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 return FAIL;
2687
2688 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002689 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002691 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002692 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002694 evalarg_T *evalarg_used = evalarg;
2695 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002696 int orig_flags;
2697 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002698 long result = TRUE;
2699 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002700 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002701 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002702
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002703 if (evalarg == NULL)
2704 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002705 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002706 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002707 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002708 orig_flags = evalarg_used->eval_flags;
2709 evaluate = orig_flags & EVAL_EVALUATE;
2710 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002711 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002712 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002713 result = tv_get_bool_chk(rettv, &error);
2714 else if (tv_get_number_chk(rettv, &error) == 0)
2715 result = FALSE;
2716 clear_tv(rettv);
2717 if (error)
2718 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 }
2720
2721 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002722 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002724 while (p[0] == '&' && p[1] == '&')
2725 {
2726 if (getnext)
2727 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002728 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002729 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002730 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002731 {
2732 error_white_both(p, 2);
2733 clear_tv(rettv);
2734 return FAIL;
2735 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002736 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002737 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002738
2739 /*
2740 * Get the second variable.
2741 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002742 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2743 {
mityu4ac198c2021-05-28 17:52:40 +02002744 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002745 clear_tv(rettv);
2746 return FAIL;
2747 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002748 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2749 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002750 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002751 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002752 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002753 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002754
2755 /*
2756 * Compute the result.
2757 */
2758 if (evaluate && result)
2759 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002760 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002761 result = tv_get_bool_chk(&var2, &error);
2762 else if (tv_get_number_chk(&var2, &error) == 0)
2763 result = FALSE;
2764 clear_tv(&var2);
2765 if (error)
2766 return FAIL;
2767 }
2768 if (evaluate)
2769 {
2770 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002771 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002772 rettv->v_type = VAR_BOOL;
2773 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002774 }
2775 else
2776 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002777 rettv->v_type = VAR_NUMBER;
2778 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002779 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002780 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002781
2782 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002784
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002785 if (evalarg == NULL)
2786 clear_evalarg(&local_evalarg, NULL);
2787 else
2788 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 }
2790
2791 return OK;
2792}
2793
2794/*
2795 * Handle third level expression:
2796 * var1 == var2
2797 * var1 =~ var2
2798 * var1 != var2
2799 * var1 !~ var2
2800 * var1 > var2
2801 * var1 >= var2
2802 * var1 < var2
2803 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002804 * var1 is var2
2805 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 *
2807 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002808 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 *
2810 * Return OK or FAIL.
2811 */
2812 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002813eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002816 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002817 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002819 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820
2821 /*
2822 * Get the first variable.
2823 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002824 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 return FAIL;
2826
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002827 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002828 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829
2830 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002831 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002833 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002835 typval_T var2;
2836 int ic;
2837 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002838 int evaluate = evalarg == NULL
2839 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002840
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002841 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002842 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002843 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002844 p = *arg;
2845 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002846 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2847 {
mityu4ac198c2021-05-28 17:52:40 +02002848 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002849 clear_tv(rettv);
2850 return FAIL;
2851 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002852
Bram Moolenaar696ba232020-07-29 21:20:41 +02002853 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2854 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002855 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002856 clear_tv(rettv);
2857 return FAIL;
2858 }
2859
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002860 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 if (p[len] == '?')
2862 {
2863 ic = TRUE;
2864 ++len;
2865 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002866 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 else if (p[len] == '#')
2868 {
2869 ic = FALSE;
2870 ++len;
2871 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002872 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002874 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875
2876 /*
2877 * Get the second variable.
2878 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002879 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2880 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002881 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002882 clear_tv(rettv);
2883 return FAIL;
2884 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002885 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002886 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002888 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 return FAIL;
2890 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002891 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002892 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002893 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002894
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002895 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002896 {
2897 ret = FAIL;
2898 clear_tv(rettv);
2899 }
2900 else
2901 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002902 clear_tv(&var2);
2903 return ret;
2904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 }
2906
2907 return OK;
2908}
2909
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002910/*
2911 * Make a copy of blob "tv1" and append blob "tv2".
2912 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913 void
2914eval_addblob(typval_T *tv1, typval_T *tv2)
2915{
2916 blob_T *b1 = tv1->vval.v_blob;
2917 blob_T *b2 = tv2->vval.v_blob;
2918 blob_T *b = blob_alloc();
2919 int i;
2920
2921 if (b != NULL)
2922 {
2923 for (i = 0; i < blob_len(b1); i++)
2924 ga_append(&b->bv_ga, blob_get(b1, i));
2925 for (i = 0; i < blob_len(b2); i++)
2926 ga_append(&b->bv_ga, blob_get(b2, i));
2927
2928 clear_tv(tv1);
2929 rettv_blob_set(tv1, b);
2930 }
2931}
2932
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002933/*
2934 * Make a copy of list "tv1" and append list "tv2".
2935 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 int
2937eval_addlist(typval_T *tv1, typval_T *tv2)
2938{
2939 typval_T var3;
2940
2941 // concatenate Lists
2942 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2943 {
2944 clear_tv(tv1);
2945 clear_tv(tv2);
2946 return FAIL;
2947 }
2948 clear_tv(tv1);
2949 *tv1 = var3;
2950 return OK;
2951}
2952
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953/*
2954 * Handle fourth level expression:
2955 * + number addition
2956 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002957 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002958 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 *
2960 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002961 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 *
2963 * Return OK or FAIL.
2964 */
2965 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002966eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 /*
2969 * Get the first variable.
2970 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002971 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 return FAIL;
2973
2974 /*
2975 * Repeat computing, until no '+', '-' or '.' is following.
2976 */
2977 for (;;)
2978 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002979 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002980 int getnext;
2981 char_u *p;
2982 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002983 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002984 int concat;
2985 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002986 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002987
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002988 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002989 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002990 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002991 p = eval_next_non_blank(*arg, evalarg, &getnext);
2992 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02002993 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002994 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2995 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002997 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
2998 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002999
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003000 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003001 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003002 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003003 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003004 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003005 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003006 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003007 {
mityu4ac198c2021-05-28 17:52:40 +02003008 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003009 clear_tv(rettv);
3010 return FAIL;
3011 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003012 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003013 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003014 if ((op != '+' || (rettv->v_type != VAR_LIST
3015 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003016#ifdef FEAT_FLOAT
3017 && (op == '.' || rettv->v_type != VAR_FLOAT)
3018#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003019 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003020 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003021 int error = FALSE;
3022
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003023 // For "list + ...", an illegal use of the first operand as
3024 // a number cannot be determined before evaluating the 2nd
3025 // operand: if this is also a list, all is ok.
3026 // For "something . ...", "something - ..." or "non-list + ...",
3027 // we know that the first operand needs to be a string or number
3028 // without evaluating the 2nd operand. So check before to avoid
3029 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003030 if (op != '.')
3031 tv_get_number_chk(rettv, &error);
3032 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003033 {
3034 clear_tv(rettv);
3035 return FAIL;
3036 }
3037 }
3038
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 /*
3040 * Get the second variable.
3041 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003042 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003043 {
mityu89dcb4d2021-05-28 13:50:17 +02003044 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003045 clear_tv(rettv);
3046 return FAIL;
3047 }
3048 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02003049 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003051 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 return FAIL;
3053 }
3054
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003055 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 {
3057 /*
3058 * Compute the result.
3059 */
3060 if (op == '.')
3061 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003062 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3063 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003064 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003065
Bram Moolenaar2e086612020-08-25 22:37:48 +02003066 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003067 || var2.v_type == VAR_CHANNEL
3068 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003069 semsg(_(e_using_invalid_value_as_string_str),
3070 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003071#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003072 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003073 {
3074 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3075 var2.vval.v_float);
3076 s2 = buf2;
3077 }
3078#endif
3079 else
3080 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003081 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003082 {
3083 clear_tv(rettv);
3084 clear_tv(&var2);
3085 return FAIL;
3086 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003087 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003088 clear_tv(rettv);
3089 rettv->v_type = VAR_STRING;
3090 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003092 else if (op == '+' && rettv->v_type == VAR_BLOB
3093 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003095 else if (op == '+' && rettv->v_type == VAR_LIST
3096 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003097 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003099 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 else
3102 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003103 int error = FALSE;
3104 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003105#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003106 float_T f1 = 0, f2 = 0;
3107
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003108 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003109 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003110 f1 = rettv->vval.v_float;
3111 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003112 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003113 else
3114#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003115 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003116 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003117 if (error)
3118 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003119 // This can only happen for "list + non-list" or
3120 // "blob + non-blob". For "non-list + ..." or
3121 // "something - ...", we returned before evaluating the
3122 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003123 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003124 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003125 return FAIL;
3126 }
3127#ifdef FEAT_FLOAT
3128 if (var2.v_type == VAR_FLOAT)
3129 f1 = n1;
3130#endif
3131 }
3132#ifdef FEAT_FLOAT
3133 if (var2.v_type == VAR_FLOAT)
3134 {
3135 f2 = var2.vval.v_float;
3136 n2 = 0;
3137 }
3138 else
3139#endif
3140 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003141 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003142 if (error)
3143 {
3144 clear_tv(rettv);
3145 clear_tv(&var2);
3146 return FAIL;
3147 }
3148#ifdef FEAT_FLOAT
3149 if (rettv->v_type == VAR_FLOAT)
3150 f2 = n2;
3151#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003152 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003153 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003154
3155#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003156 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003157 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3158 {
3159 if (op == '+')
3160 f1 = f1 + f2;
3161 else
3162 f1 = f1 - f2;
3163 rettv->v_type = VAR_FLOAT;
3164 rettv->vval.v_float = f1;
3165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003167#endif
3168 {
3169 if (op == '+')
3170 n1 = n1 + n2;
3171 else
3172 n1 = n1 - n2;
3173 rettv->v_type = VAR_NUMBER;
3174 rettv->vval.v_number = n1;
3175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003177 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 }
3179 }
3180 return OK;
3181}
3182
3183/*
3184 * Handle fifth level expression:
3185 * * number multiplication
3186 * / number division
3187 * % number modulo
3188 *
3189 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003190 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 *
3192 * Return OK or FAIL.
3193 */
3194 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003195eval6(
3196 char_u **arg,
3197 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003198 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003199 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003201#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003202 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204
3205 /*
3206 * Get the first variable.
3207 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003208 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 return FAIL;
3210
3211 /*
3212 * Repeat computing, until no '*', '/' or '%' is following.
3213 */
3214 for (;;)
3215 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003216 int evaluate;
3217 int getnext;
3218 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003219 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003220 int op;
3221 varnumber_T n1, n2;
3222#ifdef FEAT_FLOAT
3223 float_T f1, f2;
3224#endif
3225 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003226
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003227 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003228 p = eval_next_non_blank(*arg, evalarg, &getnext);
3229 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003230 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003232
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003233 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003234 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003235 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003236 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003237 {
3238 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3239 {
mityu4ac198c2021-05-28 17:52:40 +02003240 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003241 clear_tv(rettv);
3242 return FAIL;
3243 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003244 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003247#ifdef FEAT_FLOAT
3248 f1 = 0;
3249 f2 = 0;
3250#endif
3251 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003252 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003254#ifdef FEAT_FLOAT
3255 if (rettv->v_type == VAR_FLOAT)
3256 {
3257 f1 = rettv->vval.v_float;
3258 use_float = TRUE;
3259 n1 = 0;
3260 }
3261 else
3262#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003263 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003264 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003265 if (error)
3266 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 }
3268 else
3269 n1 = 0;
3270
3271 /*
3272 * Get the second variable.
3273 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003274 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3275 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003276 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003277 clear_tv(rettv);
3278 return FAIL;
3279 }
3280 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003281 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 return FAIL;
3283
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003284 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003286#ifdef FEAT_FLOAT
3287 if (var2.v_type == VAR_FLOAT)
3288 {
3289 if (!use_float)
3290 {
3291 f1 = n1;
3292 use_float = TRUE;
3293 }
3294 f2 = var2.vval.v_float;
3295 n2 = 0;
3296 }
3297 else
3298#endif
3299 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003300 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003301 clear_tv(&var2);
3302 if (error)
3303 return FAIL;
3304#ifdef FEAT_FLOAT
3305 if (use_float)
3306 f2 = n2;
3307#endif
3308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309
3310 /*
3311 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003312 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003314#ifdef FEAT_FLOAT
3315 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003317 if (op == '*')
3318 f1 = f1 * f2;
3319 else if (op == '/')
3320 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003321# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003322 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003323 if (f2 == 0.0)
3324 {
3325 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003326 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003327 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003328 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003329 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003330 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003331 }
3332 else
3333 f1 = f1 / f2;
3334# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003335 // We rely on the floating point library to handle divide
3336 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003337 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003338# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003341 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003342 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003343 return FAIL;
3344 }
3345 rettv->v_type = VAR_FLOAT;
3346 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 }
3348 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003351 int failed = FALSE;
3352
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003353 if (op == '*')
3354 n1 = n1 * n2;
3355 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003356 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003358 n1 = num_modulus(n1, n2, &failed);
3359 if (failed)
3360 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003361
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003362 rettv->v_type = VAR_NUMBER;
3363 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 }
3366 }
3367
3368 return OK;
3369}
3370
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003371/*
3372 * Handle a type cast before a base level expression.
3373 * "arg" must point to the first non-white of the expression.
3374 * "arg" is advanced to just after the recognized expression.
3375 * Return OK or FAIL.
3376 */
3377 static int
3378eval7t(
3379 char_u **arg,
3380 typval_T *rettv,
3381 evalarg_T *evalarg,
3382 int want_string) // after "." operator
3383{
3384 type_T *want_type = NULL;
3385 garray_T type_list; // list of pointers to allocated types
3386 int res;
3387 int evaluate = evalarg == NULL ? 0
3388 : (evalarg->eval_flags & EVAL_EVALUATE);
3389
3390 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003391 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3392 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003393 {
3394 ++*arg;
3395 ga_init2(&type_list, sizeof(type_T *), 10);
3396 want_type = parse_type(arg, &type_list, TRUE);
3397 if (want_type == NULL && (evaluate || **arg != '>'))
3398 {
3399 clear_type_list(&type_list);
3400 return FAIL;
3401 }
3402
3403 if (**arg != '>')
3404 {
3405 if (*skipwhite(*arg) == '>')
3406 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3407 else
3408 emsg(_(e_missing_gt));
3409 clear_type_list(&type_list);
3410 return FAIL;
3411 }
3412 ++*arg;
3413 *arg = skipwhite_and_linebreak(*arg, evalarg);
3414 }
3415
3416 res = eval7(arg, rettv, evalarg, want_string);
3417
3418 if (want_type != NULL && evaluate)
3419 {
3420 if (res == OK)
3421 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003422 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3423 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003424
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003425 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003426 {
3427 if (want_type == &t_bool && actual != &t_bool
3428 && (actual->tt_flags & TTFLAG_BOOL_OK))
3429 {
3430 int n = tv2bool(rettv);
3431
3432 // can use "0" and "1" for boolean in some places
3433 clear_tv(rettv);
3434 rettv->v_type = VAR_BOOL;
3435 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3436 }
3437 else
3438 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003439 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003440
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003441 where.wt_variable = TRUE;
3442 res = check_type(want_type, actual, TRUE, where);
3443 }
3444 }
3445 }
3446 clear_type_list(&type_list);
3447 }
3448
3449 return res;
3450}
3451
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003452 int
3453eval_leader(char_u **arg, int vim9)
3454{
3455 char_u *s = *arg;
3456 char_u *p = *arg;
3457
3458 while (*p == '!' || *p == '-' || *p == '+')
3459 {
3460 char_u *n = skipwhite(p + 1);
3461
3462 // ++, --, -+ and +- are not accepted in Vim9 script
3463 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3464 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003465 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003466 return FAIL;
3467 }
3468 p = n;
3469 }
3470 *arg = p;
3471 return OK;
3472}
3473
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474/*
3475 * Handle sixth level expression:
3476 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003477 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003478 * "string" string constant
3479 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 * &option-name option value
3481 * @r register contents
3482 * identifier variable value
3483 * function() function call
3484 * $VAR environment variable
3485 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003486 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003488 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003489 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 *
3491 * Also handle:
3492 * ! in front logical NOT
3493 * - in front unary minus
3494 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003495 * trailing [] subscript in String or List
3496 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003497 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 *
3499 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003500 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 *
3502 * Return OK or FAIL.
3503 */
3504 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003505eval7(
3506 char_u **arg,
3507 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003508 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003511 int evaluate = evalarg != NULL
3512 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 int len;
3514 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003515 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 char_u *start_leader, *end_leader;
3517 int ret = OK;
3518 char_u *alias;
3519
3520 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003521 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003522 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003524 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525
3526 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003527 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 */
3529 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003530 if (eval_leader(arg, in_vim9script()) == FAIL)
3531 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 end_leader = *arg;
3533
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003534 if (**arg == '.' && (!isdigit(*(*arg + 1))
3535#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003536 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003537#endif
3538 ))
3539 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003540 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003541 ++*arg;
3542 return FAIL;
3543 }
3544
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 switch (**arg)
3546 {
3547 /*
3548 * Number constant.
3549 */
3550 case '0':
3551 case '1':
3552 case '2':
3553 case '3':
3554 case '4':
3555 case '5':
3556 case '6':
3557 case '7':
3558 case '8':
3559 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003560 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003561
3562 // Apply prefixed "-" and "+" now. Matters especially when
3563 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003564 if (ret == OK && evaluate && end_leader > start_leader
3565 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003566 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 break;
3568
3569 /*
3570 * String constant: "string".
3571 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003572 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 break;
3574
3575 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003576 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003578 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003579 break;
3580
3581 /*
3582 * List: [expr, expr]
3583 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003584 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 break;
3586
3587 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003588 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003589 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003590 case '#': if (in_vim9script())
3591 {
3592 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3593 }
3594 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003595 {
3596 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003597 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003598 }
3599 else
3600 ret = NOTDONE;
3601 break;
3602
3603 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003604 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003605 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003606 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003607 case '{': if (in_vim9script())
3608 ret = NOTDONE;
3609 else
3610 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003611 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003612 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003613 break;
3614
3615 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003616 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003618 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 break;
3620
3621 /*
3622 * Environment variable: $VAR.
3623 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003624 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 break;
3626
3627 /*
3628 * Register contents: @r.
3629 */
3630 case '@': ++*arg;
3631 if (evaluate)
3632 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003633 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3634 semsg(_(e_syntax_error_at_str), *arg);
3635 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3636 emsg_invreg(**arg);
3637 else
3638 {
3639 rettv->v_type = VAR_STRING;
3640 rettv->vval.v_string = get_reg_contents(**arg,
3641 GREG_EXPR_SRC);
3642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 }
3644 if (**arg != NUL)
3645 ++*arg;
3646 break;
3647
3648 /*
3649 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003650 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003652 case '(': ret = NOTDONE;
3653 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003654 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003655 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003656 if (ret == OK && evaluate)
3657 {
3658 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3659
Bram Moolenaara9931532021-06-12 15:58:16 +02003660 // Compile it here to get the return type. The return
3661 // type is optional, when it's missing use t_unknown.
3662 // This is recognized in compile_return().
3663 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3664 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003665 if (compile_def_function(ufunc,
Bram Moolenaare99d4222021-06-13 14:01:26 +02003666 FALSE, COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003667 {
3668 clear_tv(rettv);
3669 ret = FAIL;
3670 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003671 }
3672 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003673 if (ret == NOTDONE)
3674 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003675 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003676 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003677
Bram Moolenaar9215f012020-06-27 21:18:00 +02003678 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003679 if (**arg == ')')
3680 ++*arg;
3681 else if (ret == OK)
3682 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003683 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003684 clear_tv(rettv);
3685 ret = FAIL;
3686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 }
3688 break;
3689
Bram Moolenaar8c711452005-01-14 21:53:12 +00003690 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 break;
3692 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003693
3694 if (ret == NOTDONE)
3695 {
3696 /*
3697 * Must be a variable or function name.
3698 * Can also be a curly-braces kind of name: {expr}.
3699 */
3700 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003701 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003702 if (alias != NULL)
3703 s = alias;
3704
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003705 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003706 ret = FAIL;
3707 else
3708 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003709 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3710
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003711 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003712 {
3713 emsg(_(e_cannot_use_underscore_here));
3714 ret = FAIL;
3715 }
3716 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003717 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003718 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003719 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003720 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003721 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003722 else if (flags & EVAL_CONSTANT)
3723 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003724 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003725 {
3726 // get the value of "true", "false" or a variable
3727 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3728 {
3729 rettv->v_type = VAR_BOOL;
3730 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003731 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003732 }
3733 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003734 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003735 {
3736 rettv->v_type = VAR_BOOL;
3737 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003738 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003739 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003740 else if (len == 4 && in_vim9script()
3741 && STRNCMP(s, "null", 4) == 0)
3742 {
3743 rettv->v_type = VAR_SPECIAL;
3744 rettv->vval.v_number = VVAL_NULL;
3745 ret = OK;
3746 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003747 else
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003748 {
3749 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003750 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003751 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003752 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003753 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003754 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003755 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003756 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003757 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003758 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003759 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003760 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003761 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003762 }
3763
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003764 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3765 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003766 if (ret == OK)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003767 ret = handle_subscript(arg, name_start, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768
3769 /*
3770 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3771 */
3772 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003773 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003774 return ret;
3775}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003776
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003777/*
3778 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003779 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003780 * Adjusts "end_leaderp" until it is at "start_leader".
3781 */
3782 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003783eval7_leader(
3784 typval_T *rettv,
3785 int numeric_only,
3786 char_u *start_leader,
3787 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003788{
3789 char_u *end_leader = *end_leaderp;
3790 int ret = OK;
3791 int error = FALSE;
3792 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003793 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003794#ifdef FEAT_FLOAT
3795 float_T f = 0.0;
3796
3797 if (rettv->v_type == VAR_FLOAT)
3798 f = rettv->vval.v_float;
3799 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003800#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003801 {
3802 while (VIM_ISWHITE(end_leader[-1]))
3803 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003804 if (in_vim9script() && end_leader[-1] == '!')
3805 val = tv2bool(rettv);
3806 else
3807 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003808 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003809 if (error)
3810 {
3811 clear_tv(rettv);
3812 ret = FAIL;
3813 }
3814 else
3815 {
3816 while (end_leader > start_leader)
3817 {
3818 --end_leader;
3819 if (*end_leader == '!')
3820 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003821 if (numeric_only)
3822 {
3823 ++end_leader;
3824 break;
3825 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003826#ifdef FEAT_FLOAT
3827 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003828 {
3829 if (in_vim9script())
3830 {
3831 rettv->v_type = VAR_BOOL;
3832 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3833 }
3834 else
3835 f = !f;
3836 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003837 else
3838#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003839 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003840 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003841 type = VAR_BOOL;
3842 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003843 }
3844 else if (*end_leader == '-')
3845 {
3846#ifdef FEAT_FLOAT
3847 if (rettv->v_type == VAR_FLOAT)
3848 f = -f;
3849 else
3850#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003851 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003852 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003853 type = VAR_NUMBER;
3854 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003855 }
3856 }
3857#ifdef FEAT_FLOAT
3858 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003860 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003861 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003863 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003864#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003865 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003866 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003867 if (in_vim9script())
3868 rettv->v_type = type;
3869 else
3870 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003871 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003874 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 return ret;
3876}
3877
3878/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003879 * Call the function referred to in "rettv".
3880 */
3881 static int
3882call_func_rettv(
3883 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003884 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003885 typval_T *rettv,
3886 int evaluate,
3887 dict_T *selfdict,
3888 typval_T *basetv)
3889{
3890 partial_T *pt = NULL;
3891 funcexe_T funcexe;
3892 typval_T functv;
3893 char_u *s;
3894 int ret;
3895
3896 // need to copy the funcref so that we can clear rettv
3897 if (evaluate)
3898 {
3899 functv = *rettv;
3900 rettv->v_type = VAR_UNKNOWN;
3901
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003902 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003903 if (functv.v_type == VAR_PARTIAL)
3904 {
3905 pt = functv.vval.v_partial;
3906 s = partial_name(pt);
3907 }
3908 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003909 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003910 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003911 if (s == NULL || *s == NUL)
3912 {
3913 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02003914 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003915 goto theend;
3916 }
3917 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003918 }
3919 else
3920 s = (char_u *)"";
3921
Bram Moolenaara80faa82020-04-12 19:37:17 +02003922 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003923 funcexe.fe_firstline = curwin->w_cursor.lnum;
3924 funcexe.fe_lastline = curwin->w_cursor.lnum;
3925 funcexe.fe_evaluate = evaluate;
3926 funcexe.fe_partial = pt;
3927 funcexe.fe_selfdict = selfdict;
3928 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003929 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003930
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003931theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003932 // Clear the funcref afterwards, so that deleting it while
3933 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003934 if (evaluate)
3935 clear_tv(&functv);
3936
3937 return ret;
3938}
3939
3940/*
3941 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003942 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003943 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3944 */
3945 static int
3946eval_lambda(
3947 char_u **arg,
3948 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003949 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003950 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003951{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003952 int evaluate = evalarg != NULL
3953 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003954 typval_T base = *rettv;
3955 int ret;
3956
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003957 rettv->v_type = VAR_UNKNOWN;
3958
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003959 if (**arg == '{')
3960 {
3961 // ->{lambda}()
3962 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3963 }
3964 else
3965 {
3966 // ->(lambda)()
3967 ++*arg;
3968 ret = eval1(arg, rettv, evalarg);
3969 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar605ec912021-12-18 16:54:31 +00003970 if (**arg == ')')
3971 {
3972 ++*arg;
3973 }
3974 else
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003975 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003976 emsg(_(e_missing_closing_paren));
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003977 ret = FAIL;
3978 }
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003979 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003980 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003981 return FAIL;
3982 else if (**arg != '(')
3983 {
3984 if (verbose)
3985 {
3986 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00003987 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003988 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003989 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003990 }
3991 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003992 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003993 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003994 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003995 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003996
3997 // Clear the funcref afterwards, so that deleting it while
3998 // evaluating the arguments is possible (see test55).
3999 if (evaluate)
4000 clear_tv(&base);
4001
4002 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004003}
4004
4005/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004006 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004007 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004008 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4009 */
4010 static int
4011eval_method(
4012 char_u **arg,
4013 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004014 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004015 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004016{
4017 char_u *name;
4018 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004019 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004020 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004021 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004022 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004023 int evaluate = evalarg != NULL
4024 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004025
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004026 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004027
Bram Moolenaarac92e252019-08-03 21:58:38 +02004028 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004029 len = get_name_len(arg, &alias, evaluate, TRUE);
4030 if (alias != NULL)
4031 name = alias;
4032
4033 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004034 {
4035 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004036 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004037 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004038 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004039 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004040 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004041 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004042
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004043 // If there is no "(" immediately following, but there is further on,
4044 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4045 // Does not handle anything where "(" is part of the expression.
4046 *arg = skipwhite(*arg);
4047
4048 if (**arg != '(' && alias == NULL
4049 && (paren = vim_strchr(*arg, '(')) != NULL)
4050 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004051 *arg = name;
4052 *paren = NUL;
Bram Moolenaar15d16352022-01-17 20:09:08 +00004053 name = deref_function_name(arg, &tofree, evalarg, verbose);
4054 if (name == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004055 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004056 *arg = name + len;
4057 ret = FAIL;
4058 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004059 else
Bram Moolenaar15d16352022-01-17 20:09:08 +00004060 len = STRLEN(name);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004061 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004062 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004063
4064 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004065 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004066 *arg = skipwhite(*arg);
4067
4068 if (**arg != '(')
4069 {
4070 if (verbose)
4071 semsg(_(e_missing_parenthesis_str), name);
4072 ret = FAIL;
4073 }
4074 else if (VIM_ISWHITE((*arg)[-1]))
4075 {
4076 if (verbose)
4077 emsg(_(e_no_white_space_allowed_before_parenthesis));
4078 ret = FAIL;
4079 }
4080 else
4081 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004082 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004083 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004084 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004085
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004086 // Clear the funcref afterwards, so that deleting it while
4087 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004088 if (evaluate)
4089 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004090 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004091
Bram Moolenaarac92e252019-08-03 21:58:38 +02004092 return ret;
4093}
4094
4095/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004096 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4097 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004098 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4099 */
4100 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004101eval_index(
4102 char_u **arg,
4103 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004104 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004105 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004106{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004107 int evaluate = evalarg != NULL
4108 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004109 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004110 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004111 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004112 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004113 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004114 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004115
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004116 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4117 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004118
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004119 init_tv(&var1);
4120 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004121 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004122 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004123 /*
4124 * dict.name
4125 */
4126 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004127 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004128 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004129 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004130 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004131 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004132 }
4133 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004134 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004135 /*
4136 * something[idx]
4137 *
4138 * Get the (first) variable from inside the [].
4139 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004140 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004141 if (**arg == ':')
4142 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004143 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004144 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004145 else if (vim9 && **arg == ':')
4146 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004147 semsg(_(e_white_space_required_before_and_after_str_at_str),
4148 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004149 clear_tv(&var1);
4150 return FAIL;
4151 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004152 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004153 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004154 int error = FALSE;
4155
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004156#ifdef FEAT_FLOAT
4157 // allow for indexing with float
4158 if (vim9 && rettv->v_type == VAR_DICT
4159 && var1.v_type == VAR_FLOAT)
4160 {
4161 var1.vval.v_string = typval_tostring(&var1, TRUE);
4162 var1.v_type = VAR_STRING;
4163 }
4164#endif
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004165 if (vim9 && rettv->v_type == VAR_LIST)
4166 tv_get_number_chk(&var1, &error);
4167 else
4168 error = tv_get_string_chk(&var1) == NULL;
4169 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004170 {
4171 // not a number or string
4172 clear_tv(&var1);
4173 return FAIL;
4174 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004175 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004176
4177 /*
4178 * Get the second variable from inside the [:].
4179 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004180 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004181 if (**arg == ':')
4182 {
4183 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004184 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004185 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004186 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004187 semsg(_(e_white_space_required_before_and_after_str_at_str),
4188 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004189 if (!empty1)
4190 clear_tv(&var1);
4191 return FAIL;
4192 }
4193 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004194 if (**arg == ']')
4195 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004196 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004197 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004198 if (!empty1)
4199 clear_tv(&var1);
4200 return FAIL;
4201 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004202 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004203 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004204 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004205 if (!empty1)
4206 clear_tv(&var1);
4207 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004208 return FAIL;
4209 }
4210 }
4211
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004212 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004213 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004214 if (**arg != ']')
4215 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004216 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004217 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004218 clear_tv(&var1);
4219 if (range)
4220 clear_tv(&var2);
4221 return FAIL;
4222 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004223 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004224 }
4225
4226 if (evaluate)
4227 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004228 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004229 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004230 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004231
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004232 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004233 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004234 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004235 clear_tv(&var2);
4236 return res;
4237 }
4238 return OK;
4239}
4240
4241/*
4242 * Check if "rettv" can have an [index] or [sli:ce]
4243 */
4244 int
4245check_can_index(typval_T *rettv, int evaluate, int verbose)
4246{
4247 switch (rettv->v_type)
4248 {
4249 case VAR_FUNC:
4250 case VAR_PARTIAL:
4251 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004252 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004253 return FAIL;
4254 case VAR_FLOAT:
4255#ifdef FEAT_FLOAT
4256 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004257 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004258 return FAIL;
4259#endif
4260 case VAR_BOOL:
4261 case VAR_SPECIAL:
4262 case VAR_JOB:
4263 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004264 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004265 if (verbose)
4266 emsg(_(e_cannot_index_special_variable));
4267 return FAIL;
4268 case VAR_UNKNOWN:
4269 case VAR_ANY:
4270 case VAR_VOID:
4271 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004272 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004273 emsg(_(e_cannot_index_special_variable));
4274 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004275 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004276 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004277
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004278 case VAR_STRING:
4279 case VAR_LIST:
4280 case VAR_DICT:
4281 case VAR_BLOB:
4282 break;
4283 case VAR_NUMBER:
4284 if (in_vim9script())
4285 emsg(_(e_cannot_index_number));
4286 break;
4287 }
4288 return OK;
4289}
4290
4291/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004292 * slice() function
4293 */
4294 void
4295f_slice(typval_T *argvars, typval_T *rettv)
4296{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004297 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004298 && ((argvars[0].v_type != VAR_STRING
4299 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004300 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004301 && check_for_list_arg(argvars, 0) == FAIL)
4302 || check_for_number_arg(argvars, 1) == FAIL
4303 || check_for_opt_number_arg(argvars, 2) == FAIL))
4304 return;
4305
Bram Moolenaar6601b622021-01-13 21:47:15 +01004306 if (check_can_index(argvars, TRUE, FALSE) == OK)
4307 {
4308 copy_tv(argvars, rettv);
4309 eval_index_inner(rettv, TRUE, argvars + 1,
4310 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4311 TRUE, NULL, 0, FALSE);
4312 }
4313}
4314
4315/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004316 * Apply index or range to "rettv".
4317 * "var1" is the first index, NULL for [:expr].
4318 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004319 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4320 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004321 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4322 */
4323 int
4324eval_index_inner(
4325 typval_T *rettv,
4326 int is_range,
4327 typval_T *var1,
4328 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004329 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004330 char_u *key,
4331 int keylen,
4332 int verbose)
4333{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004334 varnumber_T n1, n2 = 0;
4335 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004336
4337 n1 = 0;
4338 if (var1 != NULL && rettv->v_type != VAR_DICT)
4339 n1 = tv_get_number(var1);
4340
4341 if (is_range)
4342 {
4343 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004344 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004345 if (verbose)
4346 emsg(_(e_cannot_slice_dictionary));
4347 return FAIL;
4348 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004349 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004350 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004351 else
4352 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004353 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004354
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004355 switch (rettv->v_type)
4356 {
4357 case VAR_UNKNOWN:
4358 case VAR_ANY:
4359 case VAR_VOID:
4360 case VAR_FUNC:
4361 case VAR_PARTIAL:
4362 case VAR_FLOAT:
4363 case VAR_BOOL:
4364 case VAR_SPECIAL:
4365 case VAR_JOB:
4366 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004367 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004368 break; // not evaluating, skipping over subscript
4369
4370 case VAR_NUMBER:
4371 case VAR_STRING:
4372 {
4373 char_u *s = tv_get_string(rettv);
4374
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004375 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004376 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004377 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004378 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004379 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004380 else
4381 s = char_from_string(s, n1);
4382 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004383 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004384 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004385 // The resulting variable is a substring. If the indexes
4386 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004387 if (n1 < 0)
4388 {
4389 n1 = len + n1;
4390 if (n1 < 0)
4391 n1 = 0;
4392 }
4393 if (n2 < 0)
4394 n2 = len + n2;
4395 else if (n2 >= len)
4396 n2 = len;
4397 if (n1 >= len || n2 < 0 || n1 > n2)
4398 s = NULL;
4399 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004400 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004401 }
4402 else
4403 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004404 // The resulting variable is a string of a single
4405 // character. If the index is too big or negative the
4406 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004407 if (n1 >= len || n1 < 0)
4408 s = NULL;
4409 else
4410 s = vim_strnsave(s + n1, 1);
4411 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004412 clear_tv(rettv);
4413 rettv->v_type = VAR_STRING;
4414 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004415 }
4416 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004417
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004418 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004419 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4420 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004421 break;
4422
4423 case VAR_LIST:
4424 if (var1 == NULL)
4425 n1 = 0;
4426 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004427 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004428 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004429 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004430 return FAIL;
4431 break;
4432
4433 case VAR_DICT:
4434 {
4435 dictitem_T *item;
4436 typval_T tmp;
4437
4438 if (key == NULL)
4439 {
4440 key = tv_get_string_chk(var1);
4441 if (key == NULL)
4442 return FAIL;
4443 }
4444
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004445 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004446
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004447 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004448 {
4449 if (verbose)
4450 {
4451 if (keylen > 0)
4452 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004453 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004454 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004455 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004456 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004457
4458 copy_tv(&item->di_tv, &tmp);
4459 clear_tv(rettv);
4460 *rettv = tmp;
4461 }
4462 break;
4463 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004464 return OK;
4465}
4466
4467/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004468 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004469 */
4470 char_u *
4471partial_name(partial_T *pt)
4472{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004473 if (pt != NULL)
4474 {
4475 if (pt->pt_name != NULL)
4476 return pt->pt_name;
4477 if (pt->pt_func != NULL)
4478 return pt->pt_func->uf_name;
4479 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004480 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004481}
4482
Bram Moolenaarddecc252016-04-06 22:59:37 +02004483 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004484partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004485{
4486 int i;
4487
4488 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004489 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004490 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004491 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004492 if (pt->pt_name != NULL)
4493 {
4494 func_unref(pt->pt_name);
4495 vim_free(pt->pt_name);
4496 }
4497 else
4498 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004499
Bram Moolenaar54656012021-06-09 20:50:46 +02004500 // "out_up" is no longer used, decrement refcount on partial that owns it.
4501 partial_unref(pt->pt_outer.out_up_partial);
4502
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004503 // Decrease the reference count for the context of a closure. If down
4504 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004505 if (pt->pt_funcstack != NULL)
4506 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004507 --pt->pt_funcstack->fs_refcount;
4508 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004509 }
4510
Bram Moolenaarddecc252016-04-06 22:59:37 +02004511 vim_free(pt);
4512}
4513
4514/*
4515 * Unreference a closure: decrement the reference count and free it when it
4516 * becomes zero.
4517 */
4518 void
4519partial_unref(partial_T *pt)
4520{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004521 if (pt != NULL)
4522 {
4523 if (--pt->pt_refcount <= 0)
4524 partial_free(pt);
4525
4526 // If the reference count goes down to one, the funcstack may be the
4527 // only reference and can be freed if no other partials reference it.
4528 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4529 funcstack_check_refcount(pt->pt_funcstack);
4530 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004531}
4532
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004533/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004534 * Return the next (unique) copy ID.
4535 * Used for serializing nested structures.
4536 */
4537 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004538get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004539{
4540 current_copyID += COPYID_INC;
4541 return current_copyID;
4542}
4543
4544/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004545 * Garbage collection for lists and dictionaries.
4546 *
4547 * We use reference counts to be able to free most items right away when they
4548 * are no longer used. But for composite items it's possible that it becomes
4549 * unused while the reference count is > 0: When there is a recursive
4550 * reference. Example:
4551 * :let l = [1, 2, 3]
4552 * :let d = {9: l}
4553 * :let l[1] = d
4554 *
4555 * Since this is quite unusual we handle this with garbage collection: every
4556 * once in a while find out which lists and dicts are not referenced from any
4557 * variable.
4558 *
4559 * Here is a good reference text about garbage collection (refers to Python
4560 * but it applies to all reference-counting mechanisms):
4561 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004562 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004563
4564/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004565 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004566 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004567 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004568 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004569 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004570garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004571{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004572 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004573 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004574 buf_T *buf;
4575 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004576 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004577 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004578
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004579 if (!testing)
4580 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004581 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004582 want_garbage_collect = FALSE;
4583 may_garbage_collect = FALSE;
4584 garbage_collect_at_exit = FALSE;
4585 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004586
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004587 // The execution stack can grow big, limit the size.
4588 if (exestack.ga_maxlen - exestack.ga_len > 500)
4589 {
4590 size_t new_len;
4591 char_u *pp;
4592 int n;
4593
4594 // Keep 150% of the current size, with a minimum of the growth size.
4595 n = exestack.ga_len / 2;
4596 if (n < exestack.ga_growsize)
4597 n = exestack.ga_growsize;
4598
4599 // Don't make it bigger though.
4600 if (exestack.ga_len + n < exestack.ga_maxlen)
4601 {
4602 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4603 pp = vim_realloc(exestack.ga_data, new_len);
4604 if (pp == NULL)
4605 return FAIL;
4606 exestack.ga_maxlen = exestack.ga_len + n;
4607 exestack.ga_data = pp;
4608 }
4609 }
4610
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004611 // We advance by two because we add one for items referenced through
4612 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004613 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004614
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004615 /*
4616 * 1. Go through all accessible variables and mark all lists and dicts
4617 * with copyID.
4618 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004619
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004620 // Don't free variables in the previous_funccal list unless they are only
4621 // referenced through previous_funccal. This must be first, because if
4622 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004623 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004624
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004625 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004626 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004627
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004628 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004629 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004630 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4631 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004632
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004633 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004634 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004635 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4636 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004637 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004638 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4639 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004640#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004641 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004642 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4643 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004644 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004645 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004646 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4647 NULL, NULL);
4648#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004649
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004650 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004651 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004652 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4653 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004654 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004655 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004656
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004657 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004658 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004659
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004660 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004661 abort = abort || set_ref_in_functions(copyID);
4662
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004663 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004664 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004665
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004666 // funcstacks keep variables for closures
4667 abort = abort || set_ref_in_funcstacks(copyID);
4668
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004669 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004670 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004671
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004672 // callbacks in buffers
4673 abort = abort || set_ref_in_buffers(copyID);
4674
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004675 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
4676 abort = abort || set_ref_in_insexpand_funcs(copyID);
4677
4678 // 'operatorfunc' callback
4679 abort = abort || set_ref_in_opfunc(copyID);
4680
4681 // 'tagfunc' callback
4682 abort = abort || set_ref_in_tagfunc(copyID);
4683
4684 // 'imactivatefunc' and 'imstatusfunc' callbacks
4685 abort = abort || set_ref_in_im_funcs(copyID);
4686
Bram Moolenaar1dced572012-04-05 16:54:08 +02004687#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004688 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004689#endif
4690
Bram Moolenaardb913952012-06-29 12:54:53 +02004691#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004692 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004693#endif
4694
4695#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004696 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004697#endif
4698
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004699#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004700 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004701 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004702#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004703#ifdef FEAT_NETBEANS_INTG
4704 abort = abort || set_ref_in_nb_channel(copyID);
4705#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004706
Bram Moolenaare3188e22016-05-31 21:13:04 +02004707#ifdef FEAT_TIMERS
4708 abort = abort || set_ref_in_timer(copyID);
4709#endif
4710
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004711#ifdef FEAT_QUICKFIX
4712 abort = abort || set_ref_in_quickfix(copyID);
4713#endif
4714
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004715#ifdef FEAT_TERMINAL
4716 abort = abort || set_ref_in_term(copyID);
4717#endif
4718
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004719#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004720 abort = abort || set_ref_in_popups(copyID);
4721#endif
4722
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004723 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004724 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004725 /*
4726 * 2. Free lists and dictionaries that are not referenced.
4727 */
4728 did_free = free_unref_items(copyID);
4729
4730 /*
4731 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004732 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004733 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004734 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004735 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004736 else if (p_verbose > 0)
4737 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004738 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004739 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004740
4741 return did_free;
4742}
4743
4744/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004745 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004746 */
4747 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004748free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004749{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004750 int did_free = FALSE;
4751
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004752 // Let all "free" functions know that we are here. This means no
4753 // dictionaries, lists, channels or jobs are to be freed, because we will
4754 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004755 in_free_unref_items = TRUE;
4756
4757 /*
4758 * PASS 1: free the contents of the items. We don't free the items
4759 * themselves yet, so that it is possible to decrement refcount counters
4760 */
4761
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004762 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004763 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004764
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004765 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004766 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004767
4768#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004769 // Go through the list of jobs and free items without the copyID. This
4770 // must happen before doing channels, because jobs refer to channels, but
4771 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004772 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4773
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004774 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004775 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4776#endif
4777
4778 /*
4779 * PASS 2: free the items themselves.
4780 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004781 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004782 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004783
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004784#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004785 // Go through the list of jobs and free items without the copyID. This
4786 // must happen before doing channels, because jobs refer to channels, but
4787 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004788 free_unused_jobs(copyID, COPYID_MASK);
4789
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004790 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004791 free_unused_channels(copyID, COPYID_MASK);
4792#endif
4793
4794 in_free_unref_items = FALSE;
4795
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004796 return did_free;
4797}
4798
4799/*
4800 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004801 * "list_stack" is used to add lists to be marked. Can be NULL.
4802 *
4803 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004804 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004805 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004806set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004807{
4808 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004809 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004810 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004811 hashtab_T *cur_ht;
4812 ht_stack_T *ht_stack = NULL;
4813 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004814
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004815 cur_ht = ht;
4816 for (;;)
4817 {
4818 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004819 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004820 // Mark each item in the hashtab. If the item contains a hashtab
4821 // it is added to ht_stack, if it contains a list it is added to
4822 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004823 todo = (int)cur_ht->ht_used;
4824 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4825 if (!HASHITEM_EMPTY(hi))
4826 {
4827 --todo;
4828 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4829 &ht_stack, list_stack);
4830 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004831 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004832
4833 if (ht_stack == NULL)
4834 break;
4835
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004836 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004837 cur_ht = ht_stack->ht;
4838 tempitem = ht_stack;
4839 ht_stack = ht_stack->prev;
4840 free(tempitem);
4841 }
4842
4843 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004844}
4845
Dominique Pelle748b3082022-01-08 12:41:16 +00004846#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
4847 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004848/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004849 * Mark a dict and its items with "copyID".
4850 * Returns TRUE if setting references failed somehow.
4851 */
4852 int
4853set_ref_in_dict(dict_T *d, int copyID)
4854{
4855 if (d != NULL && d->dv_copyID != copyID)
4856 {
4857 d->dv_copyID = copyID;
4858 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4859 }
4860 return FALSE;
4861}
Dominique Pelle748b3082022-01-08 12:41:16 +00004862#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004863
4864/*
4865 * Mark a list and its items with "copyID".
4866 * Returns TRUE if setting references failed somehow.
4867 */
4868 int
4869set_ref_in_list(list_T *ll, int copyID)
4870{
4871 if (ll != NULL && ll->lv_copyID != copyID)
4872 {
4873 ll->lv_copyID = copyID;
4874 return set_ref_in_list_items(ll, copyID, NULL);
4875 }
4876 return FALSE;
4877}
4878
4879/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004880 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004881 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4882 *
4883 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004884 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004885 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004886set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004887{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004888 listitem_T *li;
4889 int abort = FALSE;
4890 list_T *cur_l;
4891 list_stack_T *list_stack = NULL;
4892 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004893
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004894 cur_l = l;
4895 for (;;)
4896 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004897 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004898 // Mark each item in the list. If the item contains a hashtab
4899 // it is added to ht_stack, if it contains a list it is added to
4900 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004901 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4902 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4903 ht_stack, &list_stack);
4904 if (list_stack == NULL)
4905 break;
4906
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004907 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004908 cur_l = list_stack->list;
4909 tempitem = list_stack;
4910 list_stack = list_stack->prev;
4911 free(tempitem);
4912 }
4913
4914 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004915}
4916
4917/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004918 * Mark the partial in callback 'cb' with "copyID".
4919 */
4920 int
4921set_ref_in_callback(callback_T *cb, int copyID)
4922{
4923 typval_T tv;
4924
4925 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
4926 return FALSE;
4927
4928 tv.v_type = VAR_PARTIAL;
4929 tv.vval.v_partial = cb->cb_partial;
4930 return set_ref_in_item(&tv, copyID, NULL, NULL);
4931}
4932
4933/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004934 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004935 * "list_stack" is used to add lists to be marked. Can be NULL.
4936 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4937 *
4938 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004939 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004940 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004941set_ref_in_item(
4942 typval_T *tv,
4943 int copyID,
4944 ht_stack_T **ht_stack,
4945 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004946{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004947 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004948
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004949 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004950 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004951 dict_T *dd = tv->vval.v_dict;
4952
Bram Moolenaara03f2332016-02-06 18:09:59 +01004953 if (dd != NULL && dd->dv_copyID != copyID)
4954 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004955 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004956 dd->dv_copyID = copyID;
4957 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004958 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004959 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4960 }
4961 else
4962 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004963 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4964
Bram Moolenaara03f2332016-02-06 18:09:59 +01004965 if (newitem == NULL)
4966 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004967 else
4968 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004969 newitem->ht = &dd->dv_hashtab;
4970 newitem->prev = *ht_stack;
4971 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004972 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004973 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004974 }
4975 }
4976 else if (tv->v_type == VAR_LIST)
4977 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004978 list_T *ll = tv->vval.v_list;
4979
Bram Moolenaara03f2332016-02-06 18:09:59 +01004980 if (ll != NULL && ll->lv_copyID != copyID)
4981 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004982 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004983 ll->lv_copyID = copyID;
4984 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004985 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004986 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004987 }
4988 else
4989 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004990 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4991
Bram Moolenaara03f2332016-02-06 18:09:59 +01004992 if (newitem == NULL)
4993 abort = TRUE;
4994 else
4995 {
4996 newitem->list = ll;
4997 newitem->prev = *list_stack;
4998 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004999 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005000 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005001 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005002 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005003 else if (tv->v_type == VAR_FUNC)
5004 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005005 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005006 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005007 else if (tv->v_type == VAR_PARTIAL)
5008 {
5009 partial_T *pt = tv->vval.v_partial;
5010 int i;
5011
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005012 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005013 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005014 // Didn't see this partial yet.
5015 pt->pt_copyID = copyID;
5016
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005017 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005018
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005019 if (pt->pt_dict != NULL)
5020 {
5021 typval_T dtv;
5022
5023 dtv.v_type = VAR_DICT;
5024 dtv.vval.v_dict = pt->pt_dict;
5025 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5026 }
5027
5028 for (i = 0; i < pt->pt_argc; ++i)
5029 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5030 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005031 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005032 }
5033 }
5034#ifdef FEAT_JOB_CHANNEL
5035 else if (tv->v_type == VAR_JOB)
5036 {
5037 job_T *job = tv->vval.v_job;
5038 typval_T dtv;
5039
5040 if (job != NULL && job->jv_copyID != copyID)
5041 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005042 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005043 if (job->jv_channel != NULL)
5044 {
5045 dtv.v_type = VAR_CHANNEL;
5046 dtv.vval.v_channel = job->jv_channel;
5047 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5048 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005049 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005050 {
5051 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005052 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005053 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5054 }
5055 }
5056 }
5057 else if (tv->v_type == VAR_CHANNEL)
5058 {
5059 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005060 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005061 typval_T dtv;
5062 jsonq_T *jq;
5063 cbq_T *cq;
5064
5065 if (ch != NULL && ch->ch_copyID != copyID)
5066 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005067 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005068 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005069 {
5070 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5071 jq = jq->jq_next)
5072 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5073 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5074 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005075 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005076 {
5077 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005078 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005079 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5080 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005081 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005082 {
5083 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005084 dtv.vval.v_partial =
5085 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005086 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5087 }
5088 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005089 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005090 {
5091 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005092 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005093 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5094 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005095 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005096 {
5097 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005098 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005099 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5100 }
5101 }
5102 }
5103#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005104 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005105}
5106
Bram Moolenaar8c711452005-01-14 21:53:12 +00005107/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005108 * Return a string with the string representation of a variable.
5109 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005110 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005111 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005112 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005113 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005114 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005115 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5116 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005117 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005118 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005119 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005120echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005121 typval_T *tv,
5122 char_u **tofree,
5123 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005124 int copyID,
5125 int echo_style,
5126 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005127 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005128{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005129 static int recurse = 0;
5130 char_u *r = NULL;
5131
Bram Moolenaar33570922005-01-25 22:26:29 +00005132 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005133 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005134 if (!did_echo_string_emsg)
5135 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005136 // Only give this message once for a recursive call to avoid
5137 // flooding the user with errors. And stop iterating over lists
5138 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005139 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005140 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005141 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005142 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005143 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005144 }
5145 ++recurse;
5146
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005147 switch (tv->v_type)
5148 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005149 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005150 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005151 {
5152 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005153 r = tv->vval.v_string;
5154 if (r == NULL)
5155 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005156 }
5157 else
5158 {
5159 *tofree = string_quote(tv->vval.v_string, FALSE);
5160 r = *tofree;
5161 }
5162 break;
5163
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005164 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005165 if (echo_style)
5166 {
5167 *tofree = NULL;
5168 r = tv->vval.v_string;
5169 }
5170 else
5171 {
5172 *tofree = string_quote(tv->vval.v_string, TRUE);
5173 r = *tofree;
5174 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005175 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005176
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005177 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005178 {
5179 partial_T *pt = tv->vval.v_partial;
5180 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005181 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005182 garray_T ga;
5183 int i;
5184 char_u *tf;
5185
5186 ga_init2(&ga, 1, 100);
5187 ga_concat(&ga, (char_u *)"function(");
5188 if (fname != NULL)
5189 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005190 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005191 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005192 && vim_isupper(fname[1]))
5193 {
5194 ga_concat(&ga, (char_u *)"'g:");
5195 ga_concat(&ga, fname + 1);
5196 }
5197 else
5198 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005199 vim_free(fname);
5200 }
5201 if (pt != NULL && pt->pt_argc > 0)
5202 {
5203 ga_concat(&ga, (char_u *)", [");
5204 for (i = 0; i < pt->pt_argc; ++i)
5205 {
5206 if (i > 0)
5207 ga_concat(&ga, (char_u *)", ");
5208 ga_concat(&ga,
5209 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5210 vim_free(tf);
5211 }
5212 ga_concat(&ga, (char_u *)"]");
5213 }
5214 if (pt != NULL && pt->pt_dict != NULL)
5215 {
5216 typval_T dtv;
5217
5218 ga_concat(&ga, (char_u *)", ");
5219 dtv.v_type = VAR_DICT;
5220 dtv.vval.v_dict = pt->pt_dict;
5221 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5222 vim_free(tf);
5223 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005224 // terminate with ')' and a NUL
5225 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005226
5227 *tofree = ga.ga_data;
5228 r = *tofree;
5229 break;
5230 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005231
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005232 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005233 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005234 break;
5235
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005236 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005237 if (tv->vval.v_list == NULL)
5238 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005239 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005240 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005241 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005242 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005243 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5244 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005245 {
5246 *tofree = NULL;
5247 r = (char_u *)"[...]";
5248 }
5249 else
5250 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005251 int old_copyID = tv->vval.v_list->lv_copyID;
5252
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005253 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005254 *tofree = list2string(tv, copyID, restore_copyID);
5255 if (restore_copyID)
5256 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005257 r = *tofree;
5258 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005259 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005260
Bram Moolenaar8c711452005-01-14 21:53:12 +00005261 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005262 if (tv->vval.v_dict == NULL)
5263 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005264 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005265 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005266 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005267 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005268 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5269 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005270 {
5271 *tofree = NULL;
5272 r = (char_u *)"{...}";
5273 }
5274 else
5275 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005276 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005277
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005278 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005279 *tofree = dict2string(tv, copyID, restore_copyID);
5280 if (restore_copyID)
5281 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005282 r = *tofree;
5283 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005284 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005285
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005286 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005287 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005288 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005289 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005290 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005291 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005292 break;
5293
Bram Moolenaar835dc632016-02-07 14:27:38 +01005294 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005295 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005296#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005297 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005298 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5299 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005300 if (composite_val)
5301 {
5302 *tofree = string_quote(r, FALSE);
5303 r = *tofree;
5304 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005305#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005306 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005307
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005308 case VAR_INSTR:
5309 *tofree = NULL;
5310 r = (char_u *)"instructions";
5311 break;
5312
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005313 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005314#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005315 *tofree = NULL;
5316 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5317 r = numbuf;
5318 break;
5319#endif
5320
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005321 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005322 case VAR_SPECIAL:
5323 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005324 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005325 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005326 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005327
Bram Moolenaar8502c702014-06-17 12:51:16 +02005328 if (--recurse == 0)
5329 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005330 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005331}
5332
5333/*
5334 * Return a string with the string representation of a variable.
5335 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5336 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005337 * Does not put quotes around strings, as ":echo" displays values.
5338 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5339 * May return NULL.
5340 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005341 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005342echo_string(
5343 typval_T *tv,
5344 char_u **tofree,
5345 char_u *numbuf,
5346 int copyID)
5347{
5348 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5349}
5350
5351/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005352 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5353 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005354 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005355 */
5356 int
5357buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5358{
5359 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005360 char_u *t;
5361 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005362
5363 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5364 return -1;
5365
5366 if (lnum > buf->b_ml.ml_line_count)
5367 lnum = buf->b_ml.ml_line_count;
5368
5369 str = ml_get_buf(buf, lnum, FALSE);
5370 if (str == NULL)
5371 return -1;
5372
5373 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005374 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005375
Bram Moolenaar91458462021-01-13 20:08:38 +01005376 // count the number of characters
5377 t = str;
5378 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5379 t += mb_ptr2len(t);
5380
5381 // In insert mode, when the cursor is at the end of a non-empty line,
5382 // byteidx points to the NUL character immediately past the end of the
5383 // string. In this case, add one to the character count.
5384 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5385 count++;
5386
5387 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005388}
5389
5390/*
5391 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005392 * byte index. Works only for loaded buffers. Returns -1 on failure.
5393 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005394 */
5395 int
5396buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5397{
5398 char_u *str;
5399 char_u *t;
5400
5401 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5402 return -1;
5403
5404 if (lnum > buf->b_ml.ml_line_count)
5405 lnum = buf->b_ml.ml_line_count;
5406
5407 str = ml_get_buf(buf, lnum, FALSE);
5408 if (str == NULL)
5409 return -1;
5410
5411 // Convert the character offset to a byte offset
5412 t = str;
5413 while (*t != NUL && --charidx > 0)
5414 t += mb_ptr2len(t);
5415
Bram Moolenaar91458462021-01-13 20:08:38 +01005416 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005417}
5418
5419/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005421 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005423 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005424var2fpos(
5425 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005426 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005427 int *fnum, // set to fnum for '0, 'A, etc.
5428 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005430 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005432 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005434 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005435 if (varp->v_type == VAR_LIST)
5436 {
5437 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005438 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005439 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005440 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005441
5442 l = varp->vval.v_list;
5443 if (l == NULL)
5444 return NULL;
5445
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005446 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005447 pos.lnum = list_find_nr(l, 0L, &error);
5448 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005449 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005450 if (charcol)
5451 len = (long)mb_charlen(ml_get(pos.lnum));
5452 else
5453 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005454
Bram Moolenaarec65d772020-08-20 22:29:12 +02005455 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005456 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005457 li = list_find(l, 1L);
5458 if (li != NULL && li->li_tv.v_type == VAR_STRING
5459 && li->li_tv.vval.v_string != NULL
5460 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005461 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005462 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005463 }
5464 else
5465 {
5466 pos.col = list_find_nr(l, 1L, &error);
5467 if (error)
5468 return NULL;
5469 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005470
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005471 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005472 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005473 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005474 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005475
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005476 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005477 pos.coladd = list_find_nr(l, 2L, &error);
5478 if (error)
5479 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005480
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005481 return &pos;
5482 }
5483
Bram Moolenaarc5809432021-03-27 21:23:30 +01005484 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5485 return NULL;
5486
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005487 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005488 if (name == NULL)
5489 return NULL;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005490 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005491 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005492 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005493 pos = curwin->w_cursor;
5494 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005495 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005496 return &pos;
5497 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005498 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005499 {
5500 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005501 pos = VIsual;
5502 else
5503 pos = curwin->w_cursor;
5504 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005505 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005506 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005507 }
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005508 if (name[0] == '\'' && (!in_vim9script()
5509 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005511 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005512 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5514 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005515 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005516 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 return pp;
5518 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005519
Bram Moolenaara5525202006-03-02 22:52:09 +00005520 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005521
Bram Moolenaar477933c2007-07-17 14:32:23 +00005522 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005523 {
5524 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005525 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005526 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005527 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005528 // In silent Ex mode topline is zero, but that's not a valid line
5529 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005530 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005531 return &pos;
5532 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005533 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005534 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005535 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005536 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005537 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005538 return &pos;
5539 }
5540 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005541 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005543 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 {
5545 pos.lnum = curbuf->b_ml.ml_line_count;
5546 pos.col = 0;
5547 }
5548 else
5549 {
5550 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005551 if (charcol)
5552 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5553 else
5554 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 }
5556 return &pos;
5557 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005558 if (in_vim9script())
5559 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 return NULL;
5561}
5562
5563/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005564 * Convert list in "arg" into a position and optional file number.
5565 * When "fnump" is NULL there is no file number, only 3 items.
5566 * Note that the column is passed on as-is, the caller may want to decrement
5567 * it to use 1 for the first column.
5568 * Return FAIL when conversion is not possible, doesn't check the position for
5569 * validity.
5570 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005571 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005572list2fpos(
5573 typval_T *arg,
5574 pos_T *posp,
5575 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005576 colnr_T *curswantp,
5577 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005578{
5579 list_T *l = arg->vval.v_list;
5580 long i = 0;
5581 long n;
5582
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005583 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5584 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005585 if (arg->v_type != VAR_LIST
5586 || l == NULL
5587 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005588 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005589 return FAIL;
5590
5591 if (fnump != NULL)
5592 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005593 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005594 if (n < 0)
5595 return FAIL;
5596 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005597 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005598 *fnump = n;
5599 }
5600
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005601 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005602 if (n < 0)
5603 return FAIL;
5604 posp->lnum = n;
5605
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005606 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005607 if (n < 0)
5608 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005609 // If character position is specified, then convert to byte position
5610 if (charcol)
5611 {
5612 buf_T *buf;
5613
5614 // Get the text for the specified line in a loaded buffer
5615 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5616 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5617 return FAIL;
5618
Bram Moolenaar91458462021-01-13 20:08:38 +01005619 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005620 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005621 posp->col = n;
5622
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005623 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005624 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005625 posp->coladd = 0;
5626 else
5627 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005628
Bram Moolenaar493c1782014-05-28 14:34:46 +02005629 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005630 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005631
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005632 return OK;
5633}
5634
5635/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 * Get the length of an environment variable name.
5637 * Advance "arg" to the first character after the name.
5638 * Return 0 for error.
5639 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005640 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005641get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642{
5643 char_u *p;
5644 int len;
5645
5646 for (p = *arg; vim_isIDc(*p); ++p)
5647 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005648 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 return 0;
5650
5651 len = (int)(p - *arg);
5652 *arg = p;
5653 return len;
5654}
5655
5656/*
5657 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005658 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 * Return 0 if something is wrong.
5660 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005661 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005662get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663{
5664 char_u *p;
5665 int len;
5666
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005667 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005669 {
5670 if (*p == ':')
5671 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005672 // "s:" is start of "s:var", but "n:" is not and can be used in
5673 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005674 len = (int)(p - *arg);
5675 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5676 || len > 1)
5677 break;
5678 }
5679 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005680 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 return 0;
5682
5683 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005684 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685
5686 return len;
5687}
5688
5689/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005690 * Get the length of the name of a variable or function.
5691 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005693 * Return -1 if curly braces expansion failed.
5694 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 * If the name contains 'magic' {}'s, expand them and return the
5696 * expanded name in an allocated string via 'alias' - caller must free.
5697 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005698 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005699get_name_len(
5700 char_u **arg,
5701 char_u **alias,
5702 int evaluate,
5703 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704{
5705 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 char_u *p;
5707 char_u *expr_start;
5708 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005710 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711
5712 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5713 && (*arg)[2] == (int)KE_SNR)
5714 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005715 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 *arg += 3;
5717 return get_id_len(arg) + 3;
5718 }
5719 len = eval_fname_script(*arg);
5720 if (len > 0)
5721 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005722 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 *arg += len;
5724 }
5725
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005727 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005729 p = find_name_end(*arg, &expr_start, &expr_end,
5730 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 if (expr_start != NULL)
5732 {
5733 char_u *temp_string;
5734
5735 if (!evaluate)
5736 {
5737 len += (int)(p - *arg);
5738 *arg = skipwhite(p);
5739 return len;
5740 }
5741
5742 /*
5743 * Include any <SID> etc in the expanded string:
5744 * Thus the -len here.
5745 */
5746 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5747 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005748 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 *alias = temp_string;
5750 *arg = skipwhite(p);
5751 return (int)STRLEN(temp_string);
5752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753
5754 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005755 // Only give an error when there is something, otherwise it will be
5756 // reported at a higher level.
5757 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005758 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759
5760 return len;
5761}
5762
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005763/*
5764 * Find the end of a variable or function name, taking care of magic braces.
5765 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5766 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005767 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005768 * Return a pointer to just after the name. Equal to "arg" if there is no
5769 * valid name.
5770 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005771 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005772find_name_end(
5773 char_u *arg,
5774 char_u **expr_start,
5775 char_u **expr_end,
5776 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005778 int mb_nest = 0;
5779 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005781 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005782 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005784 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005786 *expr_start = NULL;
5787 *expr_end = NULL;
5788 }
5789
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005790 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005791 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5792 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005793 return arg;
5794
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005795 for (p = arg; *p != NUL
5796 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005797 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005798 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005799 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005800 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005801 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005802 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005803 if (*p == '\'')
5804 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005805 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005806 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005807 ;
5808 if (*p == NUL)
5809 break;
5810 }
5811 else if (*p == '"')
5812 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005813 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005814 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005815 if (*p == '\\' && p[1] != NUL)
5816 ++p;
5817 if (*p == NUL)
5818 break;
5819 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005820 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5821 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005822 // "s:" is start of "s:var", but "n:" is not and can be used in
5823 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005824 len = (int)(p - arg);
5825 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005826 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005827 break;
5828 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005829
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005830 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005832 if (*p == '[')
5833 ++br_nest;
5834 else if (*p == ']')
5835 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005837
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005838 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005840 if (*p == '{')
5841 {
5842 mb_nest++;
5843 if (expr_start != NULL && *expr_start == NULL)
5844 *expr_start = p;
5845 }
5846 else if (*p == '}')
5847 {
5848 mb_nest--;
5849 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5850 *expr_end = p;
5851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 }
5854
5855 return p;
5856}
5857
5858/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005859 * Expands out the 'magic' {}'s in a variable/function name.
5860 * Note that this can call itself recursively, to deal with
5861 * constructs like foo{bar}{baz}{bam}
5862 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5863 * "in_start" ^
5864 * "expr_start" ^
5865 * "expr_end" ^
5866 * "in_end" ^
5867 *
5868 * Returns a new allocated string, which the caller must free.
5869 * Returns NULL for failure.
5870 */
5871 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005872make_expanded_name(
5873 char_u *in_start,
5874 char_u *expr_start,
5875 char_u *expr_end,
5876 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005877{
5878 char_u c1;
5879 char_u *retval = NULL;
5880 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005881
5882 if (expr_end == NULL || in_end == NULL)
5883 return NULL;
5884 *expr_start = NUL;
5885 *expr_end = NUL;
5886 c1 = *in_end;
5887 *in_end = NUL;
5888
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005889 temp_result = eval_to_string(expr_start + 1, FALSE);
5890 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005891 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005892 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5893 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005894 if (retval != NULL)
5895 {
5896 STRCPY(retval, in_start);
5897 STRCAT(retval, temp_result);
5898 STRCAT(retval, expr_end + 1);
5899 }
5900 }
5901 vim_free(temp_result);
5902
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005903 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005904 *expr_start = '{';
5905 *expr_end = '}';
5906
5907 if (retval != NULL)
5908 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005909 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005910 if (expr_start != NULL)
5911 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005912 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005913 temp_result = make_expanded_name(retval, expr_start,
5914 expr_end, temp_result);
5915 vim_free(retval);
5916 retval = temp_result;
5917 }
5918 }
5919
5920 return retval;
5921}
5922
5923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005925 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005927 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005928eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005930 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005931}
5932
5933/*
5934 * Return TRUE if character "c" can be used as the first character in a
5935 * variable or function name (excluding '{' and '}').
5936 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005937 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005938eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005939{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005940 return ASCII_ISALPHA(c) || c == '_';
5941}
5942
5943/*
5944 * Return TRUE if character "c" can be used as the first character of a
5945 * dictionary key.
5946 */
5947 int
5948eval_isdictc(int c)
5949{
5950 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951}
5952
5953/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005954 * Handle:
5955 * - expr[expr], expr[expr:expr] subscript
5956 * - ".name" lookup
5957 * - function call with Funcref variable: func(expr)
5958 * - method call: var->method()
5959 *
5960 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00005961 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005962 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005963 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005964handle_subscript(
5965 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00005966 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005967 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005968 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005969 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005970{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005971 int evaluate = evalarg != NULL
5972 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005973 int ret = OK;
5974 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005975 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005976 int getnext;
5977 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005978
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005979 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005980 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005981 // When at the end of the line and ".name" or "->{" or "->X" follows in
5982 // the next line then consume the line break.
5983 p = eval_next_non_blank(*arg, evalarg, &getnext);
5984 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005985 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02005986 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
5987 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
5988 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005989 {
5990 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005991 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005992 check_white = FALSE;
5993 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005994
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005995 if (rettv->v_type == VAR_ANY)
5996 {
5997 char_u *exp_name;
5998 int cc;
5999 int idx;
6000 ufunc_T *ufunc;
6001 type_T *type;
6002
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006003 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006004 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006005 if (**arg != '.')
6006 {
6007 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006008 semsg(_(e_expected_dot_after_name_str),
6009 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006010 ret = FAIL;
6011 break;
6012 }
6013 ++*arg;
6014 if (IS_WHITE_OR_NUL(**arg))
6015 {
6016 if (verbose)
6017 emsg(_(e_no_white_space_allowed_after_dot));
6018 ret = FAIL;
6019 break;
6020 }
6021
6022 // isolate the name
6023 exp_name = *arg;
6024 while (eval_isnamec(**arg))
6025 ++*arg;
6026 cc = **arg;
6027 **arg = NUL;
6028
6029 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
6030 evalarg->eval_cctx, verbose);
6031 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006032
6033 if (idx < 0 && ufunc == NULL)
6034 {
6035 ret = FAIL;
6036 break;
6037 }
6038 if (idx >= 0)
6039 {
6040 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6041 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6042
6043 copy_tv(sv->sv_tv, rettv);
6044 }
6045 else
6046 {
6047 rettv->v_type = VAR_FUNC;
6048 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6049 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006050 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006051 }
6052
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006053 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6054 || rettv->v_type == VAR_PARTIAL))
6055 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006056 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006057 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6058 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006059
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006060 // Stop the expression evaluation when immediately aborting on
6061 // error, or when an interrupt occurred or an exception was thrown
6062 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006063 if (aborting())
6064 {
6065 if (ret == OK)
6066 clear_tv(rettv);
6067 ret = FAIL;
6068 }
6069 dict_unref(selfdict);
6070 selfdict = NULL;
6071 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006072 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006073 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006074 if (in_vim9script())
6075 *arg = skipwhite(p + 2);
6076 else
6077 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006078 if (ret == OK)
6079 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006080 if (VIM_ISWHITE(**arg))
6081 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006082 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006083 ret = FAIL;
6084 }
6085 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006086 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006087 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006088 else
6089 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006090 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006091 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006092 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006093 // "." is ".name" lookup when we found a dict or when evaluating and
6094 // scriptversion is at least 2, where string concatenation is "..".
6095 else if (**arg == '['
6096 || (**arg == '.' && (rettv->v_type == VAR_DICT
6097 || (!evaluate
6098 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006099 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006100 {
6101 dict_unref(selfdict);
6102 if (rettv->v_type == VAR_DICT)
6103 {
6104 selfdict = rettv->vval.v_dict;
6105 if (selfdict != NULL)
6106 ++selfdict->dv_refcount;
6107 }
6108 else
6109 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006110 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006111 {
6112 clear_tv(rettv);
6113 ret = FAIL;
6114 }
6115 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006116 else
6117 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006118 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006119
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006120 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6121 // Don't do this when "Func" is already a partial that was bound
6122 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006123 if (selfdict != NULL
6124 && (rettv->v_type == VAR_FUNC
6125 || (rettv->v_type == VAR_PARTIAL
6126 && (rettv->vval.v_partial->pt_auto
6127 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006128 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006129
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006130 dict_unref(selfdict);
6131 return ret;
6132}
6133
6134/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006135 * Make a copy of an item.
6136 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006137 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6138 * reference to an already copied list/dict can be used.
6139 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006140 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006141 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006142item_copy(
6143 typval_T *from,
6144 typval_T *to,
6145 int deep,
6146 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006147{
6148 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006149 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006150
Bram Moolenaar33570922005-01-25 22:26:29 +00006151 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006152 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006153 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006154 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006155 }
6156 ++recurse;
6157
6158 switch (from->v_type)
6159 {
6160 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006161 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006162 case VAR_STRING:
6163 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006164 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006165 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006166 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006167 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006168 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006169 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006170 copy_tv(from, to);
6171 break;
6172 case VAR_LIST:
6173 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006174 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006175 if (from->vval.v_list == NULL)
6176 to->vval.v_list = NULL;
6177 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6178 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006179 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006180 to->vval.v_list = from->vval.v_list->lv_copylist;
6181 ++to->vval.v_list->lv_refcount;
6182 }
6183 else
6184 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6185 if (to->vval.v_list == NULL)
6186 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006187 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006188 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006189 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006190 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006191 case VAR_DICT:
6192 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006193 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006194 if (from->vval.v_dict == NULL)
6195 to->vval.v_dict = NULL;
6196 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6197 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006198 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006199 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6200 ++to->vval.v_dict->dv_refcount;
6201 }
6202 else
6203 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6204 if (to->vval.v_dict == NULL)
6205 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006206 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006207 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006208 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006209 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006210 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006211 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006212 }
6213 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006214 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006215}
6216
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006217 void
6218echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6219{
6220 char_u *tofree;
6221 char_u numbuf[NUMBUFLEN];
6222 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6223
6224 if (*atstart)
6225 {
6226 *atstart = FALSE;
6227 // Call msg_start() after eval1(), evaluating the expression
6228 // may cause a message to appear.
6229 if (with_space)
6230 {
6231 // Mark the saved text as finishing the line, so that what
6232 // follows is displayed on a new line when scrolling back
6233 // at the more prompt.
6234 msg_sb_eol();
6235 msg_start();
6236 }
6237 }
6238 else if (with_space)
6239 msg_puts_attr(" ", echo_attr);
6240
6241 if (p != NULL)
6242 for ( ; *p != NUL && !got_int; ++p)
6243 {
6244 if (*p == '\n' || *p == '\r' || *p == TAB)
6245 {
6246 if (*p != TAB && *needclr)
6247 {
6248 // remove any text still there from the command
6249 msg_clr_eos();
6250 *needclr = FALSE;
6251 }
6252 msg_putchar_attr(*p, echo_attr);
6253 }
6254 else
6255 {
6256 if (has_mbyte)
6257 {
6258 int i = (*mb_ptr2len)(p);
6259
6260 (void)msg_outtrans_len_attr(p, i, echo_attr);
6261 p += i - 1;
6262 }
6263 else
6264 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6265 }
6266 }
6267 vim_free(tofree);
6268}
6269
Bram Moolenaare9a41262005-01-15 22:18:47 +00006270/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 * ":echo expr1 ..." print each argument separated with a space, add a
6272 * newline at the end.
6273 * ":echon expr1 ..." print each argument plain.
6274 */
6275 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006276ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277{
6278 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006279 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006280 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 int needclr = TRUE;
6282 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006283 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006284 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006285 evalarg_T evalarg;
6286
Bram Moolenaare707c882020-07-01 13:07:37 +02006287 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288
6289 if (eap->skip)
6290 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006291 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006293 // If eval1() causes an error message the text from the command may
6294 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006295 need_clr_eos = needclr;
6296
Bram Moolenaar68db9962021-05-09 23:19:22 +02006297 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006298 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 {
6300 /*
6301 * Report the invalid expression unless the expression evaluation
6302 * has been cancelled due to an aborting error, an interrupt, or an
6303 * exception.
6304 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006305 if (!aborting() && did_emsg == did_emsg_before
6306 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006307 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006308 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 break;
6310 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006311 need_clr_eos = FALSE;
6312
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006314 {
6315 if (rettv.v_type == VAR_VOID)
6316 {
6317 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6318 break;
6319 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006320 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006321 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006322
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006323 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 arg = skipwhite(arg);
6325 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006326 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006327 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328
6329 if (eap->skip)
6330 --emsg_skip;
6331 else
6332 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006333 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 if (needclr)
6335 msg_clr_eos();
6336 if (eap->cmdidx == CMD_echo)
6337 msg_end();
6338 }
6339}
6340
6341/*
6342 * ":echohl {name}".
6343 */
6344 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006345ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006347 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348}
6349
6350/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006351 * Returns the :echo attribute
6352 */
6353 int
6354get_echo_attr(void)
6355{
6356 return echo_attr;
6357}
6358
6359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 * ":execute expr1 ..." execute the result of an expression.
6361 * ":echomsg expr1 ..." Print a message
6362 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006363 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 * Each gets spaces around each argument and a newline at the end for
6365 * echo commands
6366 */
6367 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006368ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369{
6370 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006371 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 int ret = OK;
6373 char_u *p;
6374 garray_T ga;
6375 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006376 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377
6378 ga_init2(&ga, 1, 80);
6379
6380 if (eap->skip)
6381 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006382 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006384 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006385 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387
6388 if (!eap->skip)
6389 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006390 char_u buf[NUMBUFLEN];
6391
6392 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006393 {
6394 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6395 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006396 semsg(_(e_using_invalid_value_as_string_str),
6397 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006398 p = NULL;
6399 }
6400 else
6401 p = tv_get_string_buf(&rettv, buf);
6402 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006403 else
6404 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006405 if (p == NULL)
6406 {
6407 clear_tv(&rettv);
6408 ret = FAIL;
6409 break;
6410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 len = (int)STRLEN(p);
6412 if (ga_grow(&ga, len + 2) == FAIL)
6413 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006414 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 ret = FAIL;
6416 break;
6417 }
6418 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 ga.ga_len += len;
6422 }
6423
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006424 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 arg = skipwhite(arg);
6426 }
6427
6428 if (ret != FAIL && ga.ga_data != NULL)
6429 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006430 // use the first line of continuation lines for messages
6431 SOURCING_LNUM = start_lnum;
6432
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006433 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6434 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006435 // Mark the already saved text as finishing the line, so that what
6436 // follows is displayed on a new line when scrolling back at the
6437 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006438 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006439 }
6440
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006442 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006443 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006444 out_flush();
6445 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006446 else if (eap->cmdidx == CMD_echoconsole)
6447 {
6448 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6449 ui_write((char_u *)"\r\n", 2, TRUE);
6450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 else if (eap->cmdidx == CMD_echoerr)
6452 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006453 int save_did_emsg = did_emsg;
6454
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006455 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006456 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 if (!force_abort)
6458 did_emsg = save_did_emsg;
6459 }
6460 else if (eap->cmdidx == CMD_execute)
6461 do_cmdline((char_u *)ga.ga_data,
6462 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6463 }
6464
6465 ga_clear(&ga);
6466
6467 if (eap->skip)
6468 --emsg_skip;
6469
Bram Moolenaar63b91732021-08-05 20:40:03 +02006470 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471}
6472
6473/*
6474 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6475 * "arg" points to the "&" or '+' when called, to "option" when returning.
6476 * Returns NULL when no option name found. Otherwise pointer to the char
6477 * after the option name.
6478 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006479 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006480find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481{
6482 char_u *p = *arg;
6483
6484 ++p;
6485 if (*p == 'g' && p[1] == ':')
6486 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006487 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 p += 2;
6489 }
6490 else if (*p == 'l' && p[1] == ':')
6491 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006492 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 p += 2;
6494 }
6495 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006496 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497
6498 if (!ASCII_ISALPHA(*p))
6499 return NULL;
6500 *arg = p;
6501
6502 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006503 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 else
6505 while (ASCII_ISALPHA(*p))
6506 ++p;
6507 return p;
6508}
6509
6510/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006511 * Display script name where an item was last set.
6512 * Should only be invoked when 'verbose' is non-zero.
6513 */
6514 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006515last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006516{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006517 char_u *p;
6518
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006519 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006520 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006521 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006522 if (p != NULL)
6523 {
6524 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006525 msg_puts(_("\n\tLast set from "));
6526 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006527 if (script_ctx.sc_lnum > 0)
6528 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006529 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006530 msg_outnum((long)script_ctx.sc_lnum);
6531 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006532 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006533 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006534 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006535 }
6536}
6537
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006538#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539
6540/*
6541 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006542 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 * "flags" can be "g" to do a global substitute.
6544 * Returns an allocated string, NULL for error.
6545 */
6546 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006547do_string_sub(
6548 char_u *str,
6549 char_u *pat,
6550 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006551 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006552 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553{
6554 int sublen;
6555 regmatch_T regmatch;
6556 int i;
6557 int do_all;
6558 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006559 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 garray_T ga;
6561 char_u *ret;
6562 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006563 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006565 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006567 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568
6569 ga_init2(&ga, 1, 200);
6570
6571 do_all = (flags[0] == 'g');
6572
6573 regmatch.rm_ic = p_ic;
6574 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6575 if (regmatch.regprog != NULL)
6576 {
6577 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006578 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6580 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006581 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006582 if (regmatch.startp[0] == regmatch.endp[0])
6583 {
6584 if (zero_width == regmatch.startp[0])
6585 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006586 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006587 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006588 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6589 (size_t)i);
6590 ga.ga_len += i;
6591 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006592 continue;
6593 }
6594 zero_width = regmatch.startp[0];
6595 }
6596
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 /*
6598 * Get some space for a temporary buffer to do the substitution
6599 * into. It will contain:
6600 * - The text up to where the match is.
6601 * - The substituted text.
6602 * - The text after the match.
6603 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006604 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006605 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6607 {
6608 ga_clear(&ga);
6609 break;
6610 }
6611
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006612 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 i = (int)(regmatch.startp[0] - tail);
6614 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006615 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006616 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 + ga.ga_len + i, TRUE, TRUE, FALSE);
6618 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006619 tail = regmatch.endp[0];
6620 if (*tail == NUL)
6621 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 if (!do_all)
6623 break;
6624 }
6625
6626 if (ga.ga_data != NULL)
6627 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6628
Bram Moolenaar473de612013-06-08 18:19:48 +02006629 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 }
6631
6632 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6633 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006634 if (p_cpo == empty_option)
6635 p_cpo = save_cpo;
6636 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006637 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006638 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006639 // If it's still empty it was changed and restored, need to restore in
6640 // the complicated way.
6641 if (*p_cpo == NUL)
6642 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006643 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645
6646 return ret;
6647}