blob: 2942d0fe1d9bc81876dca236c661f93cf6ba4413 [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 Moolenaara7043832005-01-21 11:56:39 +0000114}
115
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000116#if defined(EXITFREE) || defined(PROTO)
117 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100118eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000119{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200120 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100121 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200122 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000123
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200124 // autoloaded script names
125 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000126
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200127 // unreferenced lists and dicts
128 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200129
130 // functions not garbage collected
131 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000132}
133#endif
134
Bram Moolenaare6b53242020-07-01 17:28:33 +0200135 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200136fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
137{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100138 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200139 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200140 if (eap != NULL)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200141 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200142 evalarg->eval_cstack = eap->cstack;
143 if (getline_equal(eap->getline, eap->cookie, getsourceline))
144 {
145 evalarg->eval_getline = eap->getline;
146 evalarg->eval_cookie = eap->cookie;
147 }
Bram Moolenaar37c83712020-06-30 21:18:36 +0200148 }
149}
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
152 * Top level evaluation function, returning a boolean.
153 * Sets "error" to TRUE if there was an error.
154 * Return TRUE or FALSE.
155 */
156 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100157eval_to_bool(
158 char_u *arg,
159 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200160 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100161 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162{
Bram Moolenaar33570922005-01-25 22:26:29 +0000163 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200164 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200165 evalarg_T evalarg;
166
Bram Moolenaar37c83712020-06-30 21:18:36 +0200167 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168
169 if (skip)
170 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200171 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 else
174 {
175 *error = FALSE;
176 if (!skip)
177 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200178 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200179 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200180 else
181 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000182 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 }
184 }
185 if (skip)
186 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200187 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200189 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190}
191
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100192/*
193 * Call eval1() and give an error message if not done at a lower level.
194 */
195 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200196eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100197{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100198 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100199 int ret;
200 int did_emsg_before = did_emsg;
201 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200202 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100203
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200204 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
205
206 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100207 if (ret == FAIL)
208 {
209 // Report the invalid expression unless the expression evaluation has
210 // been cancelled due to an aborting error, an interrupt, or an
211 // exception, or we already gave a more specific error.
212 // Also check called_emsg for when using assert_fails().
213 if (!aborting() && did_emsg == did_emsg_before
214 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200215 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100216 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200217 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100218 return ret;
219}
220
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100221/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200222 * Return whether a typval is a valid expression to pass to eval_expr_typval()
223 * or eval_expr_to_bool(). An empty string returns FALSE;
224 */
225 int
226eval_expr_valid_arg(typval_T *tv)
227{
228 return tv->v_type != VAR_UNKNOWN
229 && (tv->v_type != VAR_STRING
230 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
231}
232
233/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100234 * Evaluate an expression, which can be a function, partial or string.
235 * Pass arguments "argv[argc]".
236 * Return the result in "rettv" and OK or FAIL.
237 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200238 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100239eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
240{
241 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100242 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200243 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100244
245 if (expr->v_type == VAR_FUNC)
246 {
247 s = expr->vval.v_string;
248 if (s == NULL || *s == NUL)
249 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200250 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000251 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200252 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100253 return FAIL;
254 }
255 else if (expr->v_type == VAR_PARTIAL)
256 {
257 partial_T *partial = expr->vval.v_partial;
258
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200259 if (partial == NULL)
260 return FAIL;
261
Bram Moolenaar822ba242020-05-24 23:00:18 +0200262 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200263 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100264 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200265 if (call_def_function(partial->pt_func, argc, argv,
266 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100267 return FAIL;
268 }
269 else
270 {
271 s = partial_name(partial);
272 if (s == NULL || *s == NUL)
273 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200274 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000275 funcexe.fe_evaluate = TRUE;
276 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
278 return FAIL;
279 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100280 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200281 else if (expr->v_type == VAR_INSTR)
282 {
283 return exe_typval_instr(expr, rettv);
284 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100285 else
286 {
Bram Moolenaarfc4c4482022-01-26 21:17:04 +0000287 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
Bram Moolenaar48570482017-10-30 21:48:41 +0100288 if (s == NULL)
289 return FAIL;
290 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200291 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100292 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200293 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100294 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200295 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200296 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100297 return FAIL;
298 }
299 }
300 return OK;
301}
302
303/*
304 * Like eval_to_bool() but using a typval_T instead of a string.
305 * Works for string, funcref and partial.
306 */
307 int
308eval_expr_to_bool(typval_T *expr, int *error)
309{
310 typval_T rettv;
311 int res;
312
313 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
314 {
315 *error = TRUE;
316 return FALSE;
317 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200318 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100319 clear_tv(&rettv);
320 return res;
321}
322
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323/*
324 * Top level evaluation function, returning a string. If "skip" is TRUE,
325 * only parsing to "nextcmd" is done, without reporting errors. Return
326 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
327 */
328 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100329eval_to_string_skip(
330 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200331 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100332 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333{
Bram Moolenaar33570922005-01-25 22:26:29 +0000334 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200336 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337
Bram Moolenaar37c83712020-06-30 21:18:36 +0200338 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 if (skip)
340 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200341 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 retval = NULL;
343 else
344 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100345 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000346 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 }
348 if (skip)
349 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200350 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351
352 return retval;
353}
354
355/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000356 * Skip over an expression at "*pp".
357 * Return FAIL for an error, OK otherwise.
358 */
359 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200360skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000361{
Bram Moolenaar33570922005-01-25 22:26:29 +0000362 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000363
364 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200365 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000366}
367
368/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200369 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200370 * If in Vim9 script and line breaks are encountered, the lines are
371 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200372 * "arg" is advanced to just after the expression.
373 * "start" is set to the start of the expression, "end" to just after the end.
374 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200375 * Return FAIL for an error, OK otherwise.
376 */
377 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200378skip_expr_concatenate(
379 char_u **arg,
380 char_u **start,
381 char_u **end,
382 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200383{
384 typval_T rettv;
385 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200386 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200387 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200388 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200389 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200390 int evaluate = evalarg == NULL
391 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200392
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200393 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200394 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200395 {
396 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200397 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200398 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200399 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200400 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200401 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200402 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200403
404 // Don't evaluate the expression.
405 if (evalarg != NULL)
406 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200407 *arg = skipwhite(*arg);
408 res = eval1(arg, &rettv, evalarg);
409 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200410 if (evalarg != NULL)
411 evalarg->eval_flags = save_flags;
412
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200413 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200414 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200415 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200416 if (evalarg->eval_ga.ga_len == 1)
417 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200418 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200419 ga_clear(gap);
420 gap->ga_itemsize = 0;
421 }
422 else
423 {
424 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200425 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200426
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200427 // Line breaks encountered, concatenate all the lines.
428 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200429 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200430
431 // free the lines only when using getsourceline()
432 if (evalarg->eval_cookie != NULL)
433 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200434 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200435 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200436 // Do not free the last line, "arg" points into it, free it
437 // later.
438 vim_free(evalarg->eval_tofree);
439 evalarg->eval_tofree =
440 ((char_u **)gap->ga_data)[gap->ga_len - 1];
441 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200442 ga_clear_strings(gap);
443 }
444 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200445 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200446 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200447
448 // free lines that were explicitly marked for freeing
449 ga_clear_strings(freegap);
450 }
451
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200452 gap->ga_itemsize = 0;
453 if (p == NULL)
454 return FAIL;
455 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200456 vim_free(evalarg->eval_tofree_lambda);
457 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200458 // Compute "end" relative to the end.
459 *end = *start + STRLEN(*start) - endoff;
460 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200461 }
462
463 return res;
464}
465
466/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100467 * Convert "tv" to a string.
468 * When "convert" is TRUE convert a List into a sequence of lines and convert
469 * a Float to a String.
470 * Returns an allocated string (NULL when out of memory).
471 */
472 char_u *
473typval2string(typval_T *tv, int convert)
474{
475 garray_T ga;
476 char_u *retval;
477#ifdef FEAT_FLOAT
478 char_u numbuf[NUMBUFLEN];
479#endif
480
481 if (convert && tv->v_type == VAR_LIST)
482 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000483 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100484 if (tv->vval.v_list != NULL)
485 {
486 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
487 if (tv->vval.v_list->lv_len > 0)
488 ga_append(&ga, NL);
489 }
490 ga_append(&ga, NUL);
491 retval = (char_u *)ga.ga_data;
492 }
493#ifdef FEAT_FLOAT
494 else if (convert && tv->v_type == VAR_FLOAT)
495 {
496 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
497 retval = vim_strsave(numbuf);
498 }
499#endif
500 else
501 retval = vim_strsave(tv_get_string(tv));
502 return retval;
503}
504
505/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200506 * Top level evaluation function, returning a string. Does not handle line
507 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000508 * When "convert" is TRUE convert a List into a sequence of lines and convert
509 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 * Return pointer to allocated memory, or NULL for failure.
511 */
512 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100513eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100514 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100515 int convert,
516 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517{
Bram Moolenaar33570922005-01-25 22:26:29 +0000518 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100520 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100522 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
523 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 retval = NULL;
525 else
526 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100527 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000528 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100530 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531
532 return retval;
533}
534
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100535 char_u *
536eval_to_string(
537 char_u *arg,
538 int convert)
539{
540 return eval_to_string_eap(arg, convert, NULL);
541}
542
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000544 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200545 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200546 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 */
548 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100549eval_to_string_safe(
550 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000551 int use_sandbox,
552 int keep_script_version)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553{
554 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200555 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200556 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200557 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558
Bram Moolenaar9530b582022-01-22 13:39:08 +0000559 if (!keep_script_version)
560 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200561 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000562 if (use_sandbox)
563 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200564 ++textwinlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200565 may_garbage_collect = FALSE;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200566 retval = eval_to_string(arg, FALSE);
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 = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200571 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200572 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 return retval;
574}
575
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576/*
577 * Top level evaluation function, returning a number.
578 * Evaluates "expr" silently.
579 * Returns -1 for an error.
580 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200581 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100582eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583{
Bram Moolenaar33570922005-01-25 22:26:29 +0000584 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200585 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000586 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587
588 ++emsg_off;
589
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200590 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 retval = -1;
592 else
593 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100594 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000595 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 }
597 --emsg_off;
598
599 return retval;
600}
601
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000602/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000603 * Top level evaluation function.
604 * Returns an allocated typval_T with the result.
605 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000606 */
607 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200608eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000609{
610 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200611 evalarg_T evalarg;
612
613 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000614
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200615 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200616 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100617 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000618
Bram Moolenaar37c83712020-06-30 21:18:36 +0200619 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000620 return tv;
621}
622
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000624 * "*arg" points to what can be a function name in the form of "import.Name" or
625 * "Funcref". Return the name of the function. Set "tofree" to something that
626 * was allocated.
627 * If "verbose" is FALSE no errors are given.
628 * Return NULL for any failure.
629 */
630 static char_u *
631deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000632 char_u **arg,
633 char_u **tofree,
634 evalarg_T *evalarg,
635 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000636{
637 typval_T ref;
638 char_u *name = *arg;
639
640 ref.v_type = VAR_UNKNOWN;
641 if (eval7(arg, &ref, evalarg, FALSE) == FAIL)
642 return NULL;
643 if (*skipwhite(*arg) != NUL)
644 {
645 if (verbose)
646 semsg(_(e_trailing_characters_str), *arg);
647 name = NULL;
648 }
649 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
650 {
651 name = ref.vval.v_string;
652 ref.vval.v_string = NULL;
653 *tofree = name;
654 }
655 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
656 {
657 if (ref.vval.v_partial->pt_argc > 0
658 || ref.vval.v_partial->pt_dict != NULL)
659 {
660 if (verbose)
661 emsg(_(e_cannot_use_partial_here));
662 name = NULL;
663 }
664 else
665 {
666 name = vim_strsave(partial_name(ref.vval.v_partial));
667 *tofree = name;
668 }
669 }
670 else
671 {
672 if (verbose)
673 semsg(_(e_not_callable_type_str), name);
674 name = NULL;
675 }
676 clear_tv(&ref);
677 return name;
678}
679
680/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100681 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200682 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
683 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000684 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200686 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100687call_vim_function(
688 char_u *func,
689 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200690 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200691 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000693 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200694 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000695 char_u *arg;
696 char_u *name;
697 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100699 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200700 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000701 funcexe.fe_firstline = curwin->w_cursor.lnum;
702 funcexe.fe_lastline = curwin->w_cursor.lnum;
703 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000704
705 // The name might be "import.Func" or "Funcref".
706 arg = func;
Bram Moolenaarb3d9cee2022-01-17 21:13:28 +0000707 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000708 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarb3d9cee2022-01-17 21:13:28 +0000709 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000710 if (name == NULL)
711 name = func;
712
713 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
714
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000715 if (ret == FAIL)
716 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000717 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000718
719 return ret;
720}
721
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100722/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100723 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000724 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
725 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000726 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000727 */
728 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100729call_func_retstr(
730 char_u *func,
731 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200732 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000733{
734 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000735 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000736
Bram Moolenaarded27a12018-08-01 19:06:03 +0200737 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000738 return NULL;
739
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100740 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000741 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 return retval;
743}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000744
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000745/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100746 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000747 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000748 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000749 */
750 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100751call_func_retlist(
752 char_u *func,
753 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200754 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000755{
756 typval_T rettv;
757
Bram Moolenaarded27a12018-08-01 19:06:03 +0200758 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000759 return NULL;
760
761 if (rettv.v_type != VAR_LIST)
762 {
763 clear_tv(&rettv);
764 return NULL;
765 }
766
767 return rettv.vval.v_list;
768}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769
Bram Moolenaare70dd112022-01-21 16:31:11 +0000770#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100772 * Evaluate "arg", which is 'foldexpr'.
773 * Note: caller must set "curwin" to match "arg".
774 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
775 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 */
777 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000778eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000780 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000781 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200782 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000784 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000785 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
786 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787
Bram Moolenaare70dd112022-01-21 16:31:11 +0000788 arg = wp->w_p_fde;
789 current_sctx = wp->w_p_script_ctx[WV_FDE];
790
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000792 if (use_sandbox)
793 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200794 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200796 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 retval = 0;
798 else
799 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100800 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000801 if (tv.v_type == VAR_NUMBER)
802 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000803 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 retval = 0;
805 else
806 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100807 // If the result is a string, check if there is a non-digit before
808 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000809 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 if (!VIM_ISDIGIT(*s) && *s != '-')
811 *cp = *s++;
812 retval = atol((char *)s);
813 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000814 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 }
816 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000817 if (use_sandbox)
818 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200819 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200820 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000821 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200823 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824}
825#endif
826
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000828 * Get an lval: variable, Dict item or List item that can be assigned a value
829 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
830 * "name.key", "name.key[expr]" etc.
831 * Indexing only works if "name" is an existing List or Dictionary.
832 * "name" points to the start of the name.
833 * If "rettv" is not NULL it points to the value to be assigned.
834 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
835 * wrong; must end in space or cmd separator.
836 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100837 * flags:
838 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100839 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100840 * GLV_NO_AUTOLOAD: do not use script autoloading
841 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000842 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000843 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000844 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000845 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200846 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100847get_lval(
848 char_u *name,
849 typval_T *rettv,
850 lval_T *lp,
851 int unlet,
852 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100853 int flags, // GLV_ values
854 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000855{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000856 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000857 char_u *expr_start, *expr_end;
858 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000859 dictitem_T *v;
860 typval_T var1;
861 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000862 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000863 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000864 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100865 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100866 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100867 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000868
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100869 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200870 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000871
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100872 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000873 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100874 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000875 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200876 lp->ll_name_end = find_name_end(name, NULL, NULL,
877 FNE_INCL_BR | fne_flags);
878 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000879 }
880
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100881 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000882 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100883 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000884 if (expr_start != NULL)
885 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100886 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100887 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000888 && *p != '[' && *p != '.')
889 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000890 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000891 return NULL;
892 }
893
894 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
895 if (lp->ll_exp_name == NULL)
896 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100897 // Report an invalid expression in braces, unless the
898 // expression evaluation has been cancelled due to an
899 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000900 if (!aborting() && !quiet)
901 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000902 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000903 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000904 return NULL;
905 }
906 }
907 lp->ll_name = lp->ll_exp_name;
908 }
909 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100910 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000911 lp->ll_name = name;
912
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200913 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100914 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200915 // "a: type" is declaring variable "a" with a type, not "a:".
916 if (p == name + 2 && p[-1] == ':')
917 {
918 --p;
919 lp->ll_name_end = p;
920 }
921 if (*p == ':')
922 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000923 garray_T tmp_type_list;
924 garray_T *type_list;
925 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +0200926
927 if (tp == p + 1 && !quiet)
928 {
929 semsg(_(e_white_space_required_after_str_str), ":", p);
930 return NULL;
931 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100932
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000933 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
934 type_list = &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list;
935 else
936 {
937 type_list = &tmp_type_list;
938 ga_init2(type_list, sizeof(type_T), 10);
939 }
940
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200941 // parse the type after the name
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000942 lp->ll_type = parse_type(&tp, type_list, !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100943 if (lp->ll_type == NULL && !quiet)
944 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200945 lp->ll_name_end = tp;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000946
947 // drop the type when not in a script
948 if (type_list == &tmp_type_list)
949 {
950 lp->ll_type = NULL;
951 clear_type_list(type_list);
952 }
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200953 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100954 }
955 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000956 if (lp->ll_name == NULL)
957 return p;
958
Bram Moolenaar62aec932022-01-29 21:45:34 +0000959 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000960 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000961 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name,
962 TRUE, NULL);
963
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000964 if (import != NULL)
965 {
966 ufunc_T *ufunc;
967 type_T *type;
968
969 lp->ll_sid = import->imp_sid;
970 lp->ll_name = skipwhite(p + 1);
971 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
972 lp->ll_name_end = p;
973
974 // check the item is exported
975 cc = *p;
976 *p = NUL;
977 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
978 NULL, TRUE) == -1)
979 {
980 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +0000981 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000982 }
983 *p = cc;
984 }
985 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100986
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100987 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000988 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000989 return p;
990
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200991 if (in_vim9script() && lval_root != NULL)
992 {
993 // using local variable
994 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +0200995 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200996 }
997 else
998 {
999 cc = *p;
1000 *p = NUL;
1001 // When we would write to the variable pass &ht and prevent autoload.
1002 writing = !(flags & GLV_READ_ONLY);
1003 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001004 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001005 if (v == NULL && !quiet)
1006 semsg(_(e_undefined_variable_str), lp->ll_name);
1007 *p = cc;
1008 if (v == NULL)
1009 return NULL;
1010 lp->ll_tv = &v->di_tv;
1011 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001012
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001013 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
1014 {
1015 if (!quiet)
1016 semsg(_(e_variable_already_declared), lp->ll_name);
1017 return NULL;
1018 }
1019
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001020 /*
1021 * Loop until no more [idx] or .key is following.
1022 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001023 var1.v_type = VAR_UNKNOWN;
1024 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001025 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001026 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001027 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1028 {
1029 if (!quiet)
1030 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1031 return NULL;
1032 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001033 if (lp->ll_tv->v_type != VAR_LIST
1034 && lp->ll_tv->v_type != VAR_DICT
1035 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001036 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001037 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001038 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001039 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001040 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001041
1042 // a NULL list/blob works like an empty list/blob, allocate one now.
1043 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1044 rettv_list_alloc(lp->ll_tv);
1045 else if (lp->ll_tv->v_type == VAR_BLOB
1046 && lp->ll_tv->vval.v_blob == NULL)
1047 rettv_blob_alloc(lp->ll_tv);
1048
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001049 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001050 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001051 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001052 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001053 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001054 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001055
Bram Moolenaar10c65862020-10-08 21:16:42 +02001056 if (in_vim9script() && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001057 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001058 && lp->ll_tv == &v->di_tv
1059 && ht != NULL && ht == get_script_local_ht())
1060 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001061 svar_T *sv = find_typval_in_script(lp->ll_tv, 0);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001062
1063 // Vim9 script local variable: get the type
1064 if (sv != NULL)
1065 lp->ll_valtype = sv->sv_type;
1066 }
1067
Bram Moolenaar8c711452005-01-14 21:53:12 +00001068 len = -1;
1069 if (*p == '.')
1070 {
1071 key = p + 1;
1072 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1073 ;
1074 if (len == 0)
1075 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001076 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001077 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001078 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001079 }
1080 p = key + len;
1081 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001082 else
1083 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001084 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001085 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001086 if (*p == ':')
1087 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001088 else
1089 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001090 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001091 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001092 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001093 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001094 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001095 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001096 clear_tv(&var1);
1097 return NULL;
1098 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001099 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001100 }
1101
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001102 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001103 if (*p == ':')
1104 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001105 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001106 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001107 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001108 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001109 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001110 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001111 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001112 if (rettv != NULL
1113 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001114 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001115 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001116 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001117 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001118 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001119 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001120 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001121 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001122 }
1123 p = skipwhite(p + 1);
1124 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001125 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001126 else
1127 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001128 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001129 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001130 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001131 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001132 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001133 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001134 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001135 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001136 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001137 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001138 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001139 clear_tv(&var2);
1140 return NULL;
1141 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001142 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001143 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001144 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001145 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001146 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001147
Bram Moolenaar8c711452005-01-14 21:53:12 +00001148 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001149 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001150 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001151 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001152 clear_tv(&var1);
1153 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001154 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001155 }
1156
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001157 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001158 ++p;
1159 }
1160
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001161 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001162 {
1163 if (len == -1)
1164 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001165 // "[key]": get key from "var1"
1166 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001167 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001168 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001169 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001170 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001171 }
1172 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001173 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001174
1175 // a NULL dict is equivalent with an empty dict
1176 if (lp->ll_tv->vval.v_dict == NULL)
1177 {
1178 lp->ll_tv->vval.v_dict = dict_alloc();
1179 if (lp->ll_tv->vval.v_dict == NULL)
1180 {
1181 clear_tv(&var1);
1182 return NULL;
1183 }
1184 ++lp->ll_tv->vval.v_dict->dv_refcount;
1185 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001186 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001187
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001188 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001189
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001190 // When assigning to a scope dictionary check that a function and
1191 // variable name is valid (only variable name unless it is l: or
1192 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001193 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001194 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001195 int prevval;
1196 int wrong;
1197
1198 if (len != -1)
1199 {
1200 prevval = key[len];
1201 key[len] = NUL;
1202 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001203 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001204 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001205 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1206 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001207 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001208 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001209 if (len != -1)
1210 key[len] = prevval;
1211 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001212 {
1213 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001214 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001215 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001216 }
1217
Bram Moolenaar10c65862020-10-08 21:16:42 +02001218 if (lp->ll_valtype != NULL)
1219 // use the type of the member
1220 lp->ll_valtype = lp->ll_valtype->tt_member;
1221
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001222 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001223 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001224 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001225 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001226 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001227 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001228 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001229 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001230 return NULL;
1231 }
1232
Bram Moolenaar31b81602019-02-10 22:14:27 +01001233 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001234 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001235 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001236 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001237 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001238 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001239 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001240 }
1241 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001242 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001243 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001244 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001245 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001246 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001247 p = NULL;
1248 break;
1249 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001250 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001251 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001252 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1253 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001254 {
1255 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001256 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001257 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001258
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001259 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001260 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001261 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001262 else if (lp->ll_tv->v_type == VAR_BLOB)
1263 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001264 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1265
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001266 /*
1267 * Get the number and item for the only or first index of the List.
1268 */
1269 if (empty1)
1270 lp->ll_n1 = 0;
1271 else
1272 // is number or string
1273 lp->ll_n1 = (long)tv_get_number(&var1);
1274 clear_tv(&var1);
1275
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001276 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001277 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001278 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001279 return NULL;
1280 }
1281 if (lp->ll_range && !lp->ll_empty2)
1282 {
1283 lp->ll_n2 = (long)tv_get_number(&var2);
1284 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001285 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1286 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001287 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001288 }
1289 lp->ll_blob = lp->ll_tv->vval.v_blob;
1290 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001291 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001292 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001293 else
1294 {
1295 /*
1296 * Get the number and item for the only or first index of the List.
1297 */
1298 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001299 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001300 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001301 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001302 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001303 clear_tv(&var1);
1304
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001305 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001306 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001307 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001308 if (lp->ll_li == NULL)
1309 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001310 clear_tv(&var2);
1311 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001312 }
1313
Bram Moolenaar10c65862020-10-08 21:16:42 +02001314 if (lp->ll_valtype != NULL)
1315 // use the type of the member
1316 lp->ll_valtype = lp->ll_valtype->tt_member;
1317
Bram Moolenaar8c711452005-01-14 21:53:12 +00001318 /*
1319 * May need to find the item or absolute index for the second
1320 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001321 * When no index given: "lp->ll_empty2" is TRUE.
1322 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001323 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001324 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001325 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001326 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001327 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001328 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001329 if (check_range_index_two(lp->ll_list,
1330 &lp->ll_n1, lp->ll_li,
1331 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001332 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001333 }
1334
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001335 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001336 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001337 }
1338
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001339 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001341 return p;
1342}
1343
1344/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001345 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001346 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001347 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001348clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001349{
1350 vim_free(lp->ll_exp_name);
1351 vim_free(lp->ll_newkey);
1352}
1353
1354/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001355 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001356 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001357 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1358 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001359 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001360 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001361set_var_lval(
1362 lval_T *lp,
1363 char_u *endp,
1364 typval_T *rettv,
1365 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001366 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1367 char_u *op,
1368 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001369{
1370 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001371 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001372
1373 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001374 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001375 cc = *endp;
1376 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001377 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1378 return;
1379
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001380 if (lp->ll_blob != NULL)
1381 {
1382 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001383
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001384 if (op != NULL && *op != '=')
1385 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001386 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001387 return;
1388 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001389 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001390 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001391
1392 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1393 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001394 if (lp->ll_empty2)
1395 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1396
Bram Moolenaar68452172021-04-12 21:21:02 +02001397 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1398 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001399 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001400 }
1401 else
1402 {
1403 val = (int)tv_get_number_chk(rettv, &error);
1404 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001405 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001406 }
1407 }
1408 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001409 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001410 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001411
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001412 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1413 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001414 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001415 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001416 *endp = cc;
1417 return;
1418 }
1419
Bram Moolenaarff697e62019-02-12 22:28:33 +01001420 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001421 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001422 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001423 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001424 {
1425 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001426 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1427 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001428 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001429 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001430 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001431 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001432 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001433 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001434 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001435 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001436 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1437 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001438 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001439 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001440 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001441 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001442 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001443 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001444 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001445 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001446 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001447 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001448 else if (lp->ll_range)
1449 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001450 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1451 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001452 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001453 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001454 return;
1455 }
1456
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001457 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1458 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001459 }
1460 else
1461 {
1462 /*
1463 * Assign to a List or Dictionary item.
1464 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001465 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1466 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001467 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001468 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001469 return;
1470 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001471
1472 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001473 && check_typval_arg_type(lp->ll_valtype, rettv,
1474 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001475 return;
1476
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001477 if (lp->ll_newkey != NULL)
1478 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001479 if (op != NULL && *op != '=')
1480 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001481 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001482 return;
1483 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001484 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1485 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001486 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001487
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001488 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001489 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001490 if (di == NULL)
1491 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001492 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1493 {
1494 vim_free(di);
1495 return;
1496 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001497 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001498 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001499 else if (op != NULL && *op != '=')
1500 {
1501 tv_op(lp->ll_tv, rettv, op);
1502 return;
1503 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001504 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001505 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001506
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001507 /*
1508 * Assign the value to the variable or list item.
1509 */
1510 if (copy)
1511 copy_tv(rettv, lp->ll_tv);
1512 else
1513 {
1514 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001515 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001516 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001517 }
1518 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001519}
1520
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001521/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001522 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1523 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001524 * Returns OK or FAIL.
1525 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001526 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001527tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001528{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001529 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001530 char_u numbuf[NUMBUFLEN];
1531 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001532 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001533
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001534 // Can't do anything with a Funcref or Dict on the right.
1535 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001536 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001537 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1538 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001539 {
1540 switch (tv1->v_type)
1541 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001542 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001543 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001545 case VAR_DICT:
1546 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001547 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001548 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001549 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001550 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001551 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001552 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001553 break;
1554
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001555 case VAR_BLOB:
1556 if (*op != '+' || tv2->v_type != VAR_BLOB)
1557 break;
1558 // BLOB += BLOB
1559 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1560 {
1561 blob_T *b1 = tv1->vval.v_blob;
1562 blob_T *b2 = tv2->vval.v_blob;
1563 int i, len = blob_len(b2);
1564 for (i = 0; i < len; i++)
1565 ga_append(&b1->bv_ga, blob_get(b2, i));
1566 }
1567 return OK;
1568
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001569 case VAR_LIST:
1570 if (*op != '+' || tv2->v_type != VAR_LIST)
1571 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001572 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001573 if (tv2->vval.v_list != NULL)
1574 {
1575 if (tv1->vval.v_list == NULL)
1576 {
1577 tv1->vval.v_list = tv2->vval.v_list;
1578 ++tv1->vval.v_list->lv_refcount;
1579 }
1580 else
1581 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1582 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001583 return OK;
1584
1585 case VAR_NUMBER:
1586 case VAR_STRING:
1587 if (tv2->v_type == VAR_LIST)
1588 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001589 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001590 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001591 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001592 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001593#ifdef FEAT_FLOAT
1594 if (tv2->v_type == VAR_FLOAT)
1595 {
1596 float_T f = n;
1597
Bram Moolenaarff697e62019-02-12 22:28:33 +01001598 if (*op == '%')
1599 break;
1600 switch (*op)
1601 {
1602 case '+': f += tv2->vval.v_float; break;
1603 case '-': f -= tv2->vval.v_float; break;
1604 case '*': f *= tv2->vval.v_float; break;
1605 case '/': f /= tv2->vval.v_float; break;
1606 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001607 clear_tv(tv1);
1608 tv1->v_type = VAR_FLOAT;
1609 tv1->vval.v_float = f;
1610 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001611 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001612#endif
1613 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001614 switch (*op)
1615 {
1616 case '+': n += tv_get_number(tv2); break;
1617 case '-': n -= tv_get_number(tv2); break;
1618 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001619 case '/': n = num_divide(n, tv_get_number(tv2),
1620 &failed); break;
1621 case '%': n = num_modulus(n, tv_get_number(tv2),
1622 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001623 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001624 clear_tv(tv1);
1625 tv1->v_type = VAR_NUMBER;
1626 tv1->vval.v_number = n;
1627 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001628 }
1629 else
1630 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001631 if (tv2->v_type == VAR_FLOAT)
1632 break;
1633
Bram Moolenaarff697e62019-02-12 22:28:33 +01001634 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001635 s = tv_get_string(tv1);
1636 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001637 clear_tv(tv1);
1638 tv1->v_type = VAR_STRING;
1639 tv1->vval.v_string = s;
1640 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001641 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001642
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001643 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001644#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001645 {
1646 float_T f;
1647
Bram Moolenaarff697e62019-02-12 22:28:33 +01001648 if (*op == '%' || *op == '.'
1649 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001650 && tv2->v_type != VAR_NUMBER
1651 && tv2->v_type != VAR_STRING))
1652 break;
1653 if (tv2->v_type == VAR_FLOAT)
1654 f = tv2->vval.v_float;
1655 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001656 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001657 switch (*op)
1658 {
1659 case '+': tv1->vval.v_float += f; break;
1660 case '-': tv1->vval.v_float -= f; break;
1661 case '*': tv1->vval.v_float *= f; break;
1662 case '/': tv1->vval.v_float /= f; break;
1663 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001664 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001665#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001666 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001667 }
1668 }
1669
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001670 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001671 return FAIL;
1672}
1673
1674/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001675 * Evaluate the expression used in a ":for var in expr" command.
1676 * "arg" points to "var".
1677 * Set "*errp" to TRUE for an error, FALSE otherwise;
1678 * Return a pointer that holds the info. Null when there is an error.
1679 */
1680 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001681eval_for_line(
1682 char_u *arg,
1683 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001684 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001685 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001686{
Bram Moolenaar33570922005-01-25 22:26:29 +00001687 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001688 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001689 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001690 typval_T tv;
1691 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001692 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001693
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001694 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001695
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001696 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001697 if (fi == NULL)
1698 return NULL;
1699
Bram Moolenaar404557e2021-07-05 21:41:48 +02001700 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1701 &fi->fi_semicolon, FALSE);
1702 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001703 return fi;
1704
Bram Moolenaar404557e2021-07-05 21:41:48 +02001705 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001706 if (expr[0] != 'i' || expr[1] != 'n'
1707 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001708 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001709 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1710 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1711 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001712 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001713 return fi;
1714 }
1715
1716 if (skip)
1717 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001718 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1719 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001720 {
1721 *errp = FALSE;
1722 if (!skip)
1723 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001724 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001725 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001726 l = tv.vval.v_list;
1727 if (l == NULL)
1728 {
1729 // a null list is like an empty list: do nothing
1730 clear_tv(&tv);
1731 }
1732 else
1733 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001735 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001736
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001737 // No need to increment the refcount, it's already set for
1738 // the list being used in "tv".
1739 fi->fi_list = l;
1740 list_add_watch(l, &fi->fi_lw);
1741 fi->fi_lw.lw_item = l->lv_first;
1742 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001743 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001744 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001745 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001746 fi->fi_bi = 0;
1747 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001748 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001749 typval_T btv;
1750
1751 // Make a copy, so that the iteration still works when the
1752 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001753 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001754 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001755 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001756 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001757 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001758 else if (tv.v_type == VAR_STRING)
1759 {
1760 fi->fi_byte_idx = 0;
1761 fi->fi_string = tv.vval.v_string;
1762 tv.vval.v_string = NULL;
1763 if (fi->fi_string == NULL)
1764 fi->fi_string = vim_strsave((char_u *)"");
1765 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001766 else
1767 {
Sean Dewar80d73952021-08-04 19:25:54 +02001768 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001769 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001770 }
1771 }
1772 }
1773 if (skip)
1774 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001775 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001776
1777 return fi;
1778}
1779
1780/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001781 * Used when looping over a :for line, skip the "in expr" part.
1782 */
1783 void
1784skip_for_lines(void *fi_void, evalarg_T *evalarg)
1785{
1786 forinfo_T *fi = (forinfo_T *)fi_void;
1787 int i;
1788
1789 for (i = 0; i < fi->fi_break_count; ++i)
1790 eval_next_line(evalarg);
1791}
1792
1793/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001794 * Use the first item in a ":for" list. Advance to the next.
1795 * Assign the values to the variable (list). "arg" points to the first one.
1796 * Return TRUE when a valid item was found, FALSE when at end of list or
1797 * something wrong.
1798 */
1799 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001800next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001801{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001802 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001803 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001804 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001805 ? (ASSIGN_FINAL
1806 // first round: error if variable exists
1807 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1808 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001809 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001810 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001811 int skip_assign = in_vim9script() && arg[0] == '_'
1812 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001813
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001814 if (fi->fi_blob != NULL)
1815 {
1816 typval_T tv;
1817
1818 if (fi->fi_bi >= blob_len(fi->fi_blob))
1819 return FALSE;
1820 tv.v_type = VAR_NUMBER;
1821 tv.v_lock = VAR_FIXED;
1822 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1823 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001824 if (skip_assign)
1825 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001826 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001827 fi->fi_varcount, flag, NULL) == OK;
1828 }
1829
1830 if (fi->fi_string != NULL)
1831 {
1832 typval_T tv;
1833 int len;
1834
1835 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1836 if (len == 0)
1837 return FALSE;
1838 tv.v_type = VAR_STRING;
1839 tv.v_lock = VAR_FIXED;
1840 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1841 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001842 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001843 if (skip_assign)
1844 result = TRUE;
1845 else
1846 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001847 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001848 vim_free(tv.vval.v_string);
1849 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001850 }
1851
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001852 item = fi->fi_lw.lw_item;
1853 if (item == NULL)
1854 result = FALSE;
1855 else
1856 {
1857 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001858 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001859 if (skip_assign)
1860 result = TRUE;
1861 else
1862 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001863 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001864 }
1865 return result;
1866}
1867
1868/*
1869 * Free the structure used to store info used by ":for".
1870 */
1871 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001872free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001873{
Bram Moolenaar33570922005-01-25 22:26:29 +00001874 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001875
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001876 if (fi == NULL)
1877 return;
1878 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001879 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001880 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001881 list_unref(fi->fi_list);
1882 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001883 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001884 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001885 else
1886 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001887 vim_free(fi);
1888}
1889
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001891set_context_for_expression(
1892 expand_T *xp,
1893 char_u *arg,
1894 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001896 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001898 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001900 if (cmdidx == CMD_let || cmdidx == CMD_var
1901 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001902 {
1903 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001904 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001905 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001906 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001907 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001908 {
1909 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001910 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001911 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 break;
1913 }
1914 return;
1915 }
1916 }
1917 else
1918 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1919 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 while ((xp->xp_pattern = vim_strpbrk(arg,
1921 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1922 {
1923 c = *xp->xp_pattern;
1924 if (c == '&')
1925 {
1926 c = xp->xp_pattern[1];
1927 if (c == '&')
1928 {
1929 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001930 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 }
1932 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001933 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001935 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1936 xp->xp_pattern += 2;
1937
1938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 }
1940 else if (c == '$')
1941 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001942 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 xp->xp_context = EXPAND_ENV_VARS;
1944 }
1945 else if (c == '=')
1946 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001947 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 xp->xp_context = EXPAND_EXPRESSION;
1949 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001950 else if (c == '#'
1951 && xp->xp_context == EXPAND_EXPRESSION)
1952 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001953 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001954 break;
1955 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001956 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 && xp->xp_context == EXPAND_FUNCTIONS
1958 && vim_strchr(xp->xp_pattern, '(') == NULL)
1959 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001960 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 break;
1962 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001963 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001965 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 {
1967 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1968 if (c == '\\' && xp->xp_pattern[1] != NUL)
1969 ++xp->xp_pattern;
1970 xp->xp_context = EXPAND_NOTHING;
1971 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001972 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001974 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1976 /* skip */ ;
1977 xp->xp_context = EXPAND_NOTHING;
1978 }
1979 else if (c == '|')
1980 {
1981 if (xp->xp_pattern[1] == '|')
1982 {
1983 ++xp->xp_pattern;
1984 xp->xp_context = EXPAND_EXPRESSION;
1985 }
1986 else
1987 xp->xp_context = EXPAND_COMMANDS;
1988 }
1989 else
1990 xp->xp_context = EXPAND_EXPRESSION;
1991 }
1992 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001993 // Doesn't look like something valid, expand as an expression
1994 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001995 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 arg = xp->xp_pattern;
1997 if (*arg != NUL)
1998 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1999 /* skip */ ;
2000 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002001
2002 // ":exe one two" completes "two"
2003 if ((cmdidx == CMD_execute
2004 || cmdidx == CMD_echo
2005 || cmdidx == CMD_echon
2006 || cmdidx == CMD_echomsg)
2007 && xp->xp_context == EXPAND_EXPRESSION)
2008 {
2009 for (;;)
2010 {
2011 char_u *n = skiptowhite(arg);
2012
2013 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2014 break;
2015 arg = skipwhite(n);
2016 }
2017 }
2018
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 xp->xp_pattern = arg;
2020}
2021
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002023 * Return TRUE if "pat" matches "text".
2024 * Does not use 'cpo' and always uses 'magic'.
2025 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002026 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002027pattern_match(char_u *pat, char_u *text, int ic)
2028{
2029 int matches = FALSE;
2030 char_u *save_cpo;
2031 regmatch_T regmatch;
2032
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002033 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002034 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002035 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002036 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2037 if (regmatch.regprog != NULL)
2038 {
2039 regmatch.rm_ic = ic;
2040 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2041 vim_regfree(regmatch.regprog);
2042 }
2043 p_cpo = save_cpo;
2044 return matches;
2045}
2046
2047/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002048 * Handle a name followed by "(". Both for just "name(arg)" and for
2049 * "expr->name(arg)".
2050 * Returns OK or FAIL.
2051 */
2052 static int
2053eval_func(
2054 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002055 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002056 char_u *name,
2057 int name_len,
2058 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002059 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002060 typval_T *basetv) // "expr" for "expr->name(arg)"
2061{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002062 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002063 char_u *s = name;
2064 int len = name_len;
2065 partial_T *partial;
2066 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002067 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002068 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002069
2070 if (!evaluate)
2071 check_vars(s, len);
2072
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002073 // If "s" is the name of a variable of type VAR_FUNC
2074 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002075 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002076 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002077
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002078 // Need to make a copy, in case evaluating the arguments makes
2079 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002080 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002081 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002082 ret = FAIL;
2083 else
2084 {
2085 funcexe_T funcexe;
2086
2087 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002088 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002089 funcexe.fe_firstline = curwin->w_cursor.lnum;
2090 funcexe.fe_lastline = curwin->w_cursor.lnum;
2091 funcexe.fe_evaluate = evaluate;
2092 funcexe.fe_partial = partial;
2093 funcexe.fe_basetv = basetv;
2094 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002095 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002096 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002097 }
2098 vim_free(s);
2099
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002100 // If evaluate is FALSE rettv->v_type was not set in
2101 // get_func_tv, but it's needed in handle_subscript() to parse
2102 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002103 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2104 {
2105 rettv->vval.v_string = NULL;
2106 rettv->v_type = VAR_FUNC;
2107 }
2108
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002109 // Stop the expression evaluation when immediately
2110 // aborting on error, or when an interrupt occurred or
2111 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002112 if (evaluate && aborting())
2113 {
2114 if (ret == OK)
2115 clear_tv(rettv);
2116 ret = FAIL;
2117 }
2118 return ret;
2119}
2120
2121/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002122 * Get the next line source line without advancing. But do skip over comment
2123 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002124 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002125 */
2126 static char_u *
2127getline_peek_skip_comments(evalarg_T *evalarg)
2128{
2129 for (;;)
2130 {
2131 char_u *next = getline_peek(evalarg->eval_getline,
2132 evalarg->eval_cookie);
2133 char_u *p;
2134
2135 if (next == NULL)
2136 break;
2137 p = skipwhite(next);
2138 if (*p != NUL && !vim9_comment_start(p))
2139 return next;
2140 (void)eval_next_line(evalarg);
2141 }
2142 return NULL;
2143}
2144
2145/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002146 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2147 * comment) and there is a next line, return the next line (skipping blanks)
2148 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002149 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2150 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002151 * "arg" must point somewhere inside a line, not at the start.
2152 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002153 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002154eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2155{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002156 char_u *p = skipwhite(arg);
2157
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002158 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002159 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002160 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002161 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002162 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002163 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002164 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002165
2166 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002167 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002168 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002169 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002170
Bram Moolenaar9d489562020-07-30 20:08:50 +02002171 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002172 {
2173 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002174 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002175 }
2176 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002177 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002178}
2179
2180/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002181 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002182 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002183 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002184 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002185eval_next_line(evalarg_T *evalarg)
2186{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002187 garray_T *gap = &evalarg->eval_ga;
2188 char_u *line;
2189
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002190 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002191 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2192 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002193 else
2194 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002195 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002196 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2197 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002198 char_u *p = skipwhite(line);
2199
2200 // Going to concatenate the lines after parsing. For an empty or
2201 // comment line use an empty string.
2202 if (*p == NUL || vim9_comment_start(p))
2203 {
2204 vim_free(line);
2205 line = vim_strsave((char_u *)"");
2206 }
2207
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002208 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2209 ++gap->ga_len;
2210 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002211 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002212 {
2213 vim_free(evalarg->eval_tofree);
2214 evalarg->eval_tofree = line;
2215 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002216
2217 // Advanced to the next line, "arg" no longer points into the previous
2218 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002219 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002220 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002221}
2222
Bram Moolenaare6b53242020-07-01 17:28:33 +02002223/*
2224 * Call eval_next_non_blank() and get the next line if needed.
2225 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002226 char_u *
2227skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2228{
2229 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002230 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002231
Bram Moolenaar962d7212020-07-04 14:15:00 +02002232 if (evalarg == NULL)
2233 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002234 eval_next_non_blank(p, evalarg, &getnext);
2235 if (getnext)
2236 return eval_next_line(evalarg);
2237 return p;
2238}
2239
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002240/*
Bram Moolenaar844fb642021-10-23 13:32:30 +01002241 * Initialize "evalarg" for use.
2242 */
2243 void
2244init_evalarg(evalarg_T *evalarg)
2245{
2246 CLEAR_POINTER(evalarg);
2247 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
2248}
2249
2250/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002251 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002252 */
2253 void
2254clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2255{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002256 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002257 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002258 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002259 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002260 if (eap != NULL)
2261 {
2262 // We may need to keep the original command line, e.g. for
2263 // ":let" it has the variable names. But we may also need the
2264 // new one, "nextcmd" points into it. Keep both.
2265 vim_free(eap->cmdline_tofree);
2266 eap->cmdline_tofree = *eap->cmdlinep;
2267 *eap->cmdlinep = evalarg->eval_tofree;
2268 }
2269 else
2270 vim_free(evalarg->eval_tofree);
2271 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002272 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002273
Bram Moolenaar844fb642021-10-23 13:32:30 +01002274 ga_clear_strings(&evalarg->eval_tofree_ga);
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002275 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002276 }
2277}
2278
2279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2283 */
2284
2285/*
2286 * Handle zero level expression.
2287 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002289 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002290 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 * Return OK or FAIL.
2292 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002293 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002294eval0(
2295 char_u *arg,
2296 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002297 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002298 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002300 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2301}
2302
2303/*
2304 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2305 * expression and don't check what comes after the expression.
2306 */
2307 int
2308eval0_retarg(
2309 char_u *arg,
2310 typval_T *rettv,
2311 exarg_T *eap,
2312 evalarg_T *evalarg,
2313 char_u **retarg)
2314{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 int ret;
2316 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002317 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002318 int did_emsg_before = did_emsg;
2319 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002320 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002321 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002322 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323
2324 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002325 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002326 expr_end = p;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002327 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002328
Bram Moolenaar02929a32021-12-17 14:46:12 +00002329 // In Vim9 script a command block is not split at NL characters for
2330 // commands using an expression argument. Skip over a '#' comment to check
2331 // for a following NL. Require white space before the '#'.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002332 if (in_vim9script() && p > expr_end && retarg == NULL)
Bram Moolenaar02929a32021-12-17 14:46:12 +00002333 while (*p == '#')
2334 {
2335 char_u *nl = vim_strchr(p, NL);
2336
2337 if (nl == NULL)
2338 break;
2339 p = skipwhite(nl + 1);
2340 if (eap != NULL && *p != NUL)
2341 eap->nextcmd = p;
2342 check_for_end = FALSE;
2343 }
2344
2345 if (ret != FAIL && check_for_end)
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002346 end_error = !ends_excmd2(arg, p);
2347 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 {
2349 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002350 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 /*
2352 * Report the invalid expression unless the expression evaluation has
2353 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002354 * exception, or we already gave a more specific error.
2355 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002357 if (!aborting()
2358 && did_emsg == did_emsg_before
2359 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002360 && (flags & EVAL_CONSTANT) == 0
2361 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002362 {
2363 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002364 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002365 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002366 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002367 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002368
2369 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002370 // a next command to avoid more errors, unless "|" is following, which
2371 // could only be a command separator.
2372 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2373 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002374 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002376
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002377 if (retarg != NULL)
2378 *retarg = p;
2379 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002380 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002381
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 return ret;
2383}
2384
2385/*
2386 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002387 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002388 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 *
2390 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002391 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002393 * Note: "rettv.v_lock" is not set.
2394 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 * Return OK or FAIL.
2396 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002397 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002398eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002400 char_u *p;
2401 int getnext;
2402
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002403 CLEAR_POINTER(rettv);
2404
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 /*
2406 * Get the first variable.
2407 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002408 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 return FAIL;
2410
Bram Moolenaar793648f2020-06-26 21:28:25 +02002411 p = eval_next_non_blank(*arg, evalarg, &getnext);
2412 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002414 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002415 int result;
2416 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002417 evalarg_T *evalarg_used = evalarg;
2418 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002419 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002420 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002421 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002422
2423 if (evalarg == NULL)
2424 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002425 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002426 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002427 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002428 orig_flags = evalarg_used->eval_flags;
2429 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002430
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002431 if (getnext)
2432 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002433 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002434 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002435 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002436 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002437 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002438 clear_tv(rettv);
2439 return FAIL;
2440 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002441 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002442 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002443
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002445 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002447 int error = FALSE;
2448
Bram Moolenaar13106602020-10-04 16:06:05 +02002449 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002450 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002451 else if (vim9script)
2452 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002453 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002455 if (error || !op_falsy || !result)
2456 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002457 if (error)
2458 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 }
2460
2461 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002462 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002464 if (op_falsy)
2465 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002466 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002467 {
mityu4ac198c2021-05-28 17:52:40 +02002468 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002469 clear_tv(rettv);
2470 return FAIL;
2471 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002472 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002473 evalarg_used->eval_flags = (op_falsy ? !result : result)
2474 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2475 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002476 {
2477 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002479 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002480 if (!op_falsy || !result)
2481 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002483 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002485 /*
2486 * Check for the ":".
2487 */
2488 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2489 if (*p != ':')
2490 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002491 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002492 if (evaluate && result)
2493 clear_tv(rettv);
2494 evalarg_used->eval_flags = orig_flags;
2495 return FAIL;
2496 }
2497 if (getnext)
2498 *arg = eval_next_line(evalarg_used);
2499 else
2500 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002501 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002502 {
2503 error_white_both(p, 1);
2504 clear_tv(rettv);
2505 evalarg_used->eval_flags = orig_flags;
2506 return FAIL;
2507 }
2508 *arg = p;
2509 }
2510
2511 /*
2512 * Get the third variable. Recursive!
2513 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002514 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002515 {
mityu4ac198c2021-05-28 17:52:40 +02002516 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002517 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002518 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002519 return FAIL;
2520 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002521 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2522 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002523 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002524 if (eval1(arg, &var2, evalarg_used) == FAIL)
2525 {
2526 if (evaluate && result)
2527 clear_tv(rettv);
2528 evalarg_used->eval_flags = orig_flags;
2529 return FAIL;
2530 }
2531 if (evaluate && !result)
2532 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002534
2535 if (evalarg == NULL)
2536 clear_evalarg(&local_evalarg, NULL);
2537 else
2538 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 }
2540
2541 return OK;
2542}
2543
2544/*
2545 * Handle first level expression:
2546 * expr2 || expr2 || expr2 logical OR
2547 *
2548 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002549 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 *
2551 * Return OK or FAIL.
2552 */
2553 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002554eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002556 char_u *p;
2557 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558
2559 /*
2560 * Get the first variable.
2561 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002562 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 return FAIL;
2564
2565 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002566 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002568 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002569 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002571 evalarg_T *evalarg_used = evalarg;
2572 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002573 int evaluate;
2574 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002575 long result = FALSE;
2576 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002577 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002578 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002579
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002580 if (evalarg == NULL)
2581 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002582 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002583 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002584 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002585 orig_flags = evalarg_used->eval_flags;
2586 evaluate = orig_flags & EVAL_EVALUATE;
2587 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002588 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002589 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002590 result = tv_get_bool_chk(rettv, &error);
2591 else if (tv_get_number_chk(rettv, &error) != 0)
2592 result = TRUE;
2593 clear_tv(rettv);
2594 if (error)
2595 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 }
2597
2598 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002599 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002601 while (p[0] == '|' && p[1] == '|')
2602 {
2603 if (getnext)
2604 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002605 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002606 {
2607 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2608 {
2609 error_white_both(p, 2);
2610 clear_tv(rettv);
2611 return FAIL;
2612 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002613 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002614 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002615
2616 /*
2617 * Get the second variable.
2618 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002619 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2620 {
mityu4ac198c2021-05-28 17:52:40 +02002621 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002622 clear_tv(rettv);
2623 return FAIL;
2624 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002625 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2626 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002627 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002628 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002629 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002630
2631 /*
2632 * Compute the result.
2633 */
2634 if (evaluate && !result)
2635 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002636 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002637 result = tv_get_bool_chk(&var2, &error);
2638 else if (tv_get_number_chk(&var2, &error) != 0)
2639 result = TRUE;
2640 clear_tv(&var2);
2641 if (error)
2642 return FAIL;
2643 }
2644 if (evaluate)
2645 {
2646 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002647 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002648 rettv->v_type = VAR_BOOL;
2649 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002650 }
2651 else
2652 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002653 rettv->v_type = VAR_NUMBER;
2654 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002655 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002656 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002657
2658 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002660
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002661 if (evalarg == NULL)
2662 clear_evalarg(&local_evalarg, NULL);
2663 else
2664 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 }
2666
2667 return OK;
2668}
2669
2670/*
2671 * Handle second level expression:
2672 * expr3 && expr3 && expr3 logical AND
2673 *
2674 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002675 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 *
2677 * Return OK or FAIL.
2678 */
2679 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002680eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002682 char_u *p;
2683 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684
2685 /*
2686 * Get the first variable.
2687 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002688 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 return FAIL;
2690
2691 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002692 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002694 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002695 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002697 evalarg_T *evalarg_used = evalarg;
2698 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002699 int orig_flags;
2700 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002701 long result = TRUE;
2702 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002703 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002704 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002705
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002706 if (evalarg == NULL)
2707 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002708 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002709 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002710 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002711 orig_flags = evalarg_used->eval_flags;
2712 evaluate = orig_flags & EVAL_EVALUATE;
2713 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002714 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002715 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002716 result = tv_get_bool_chk(rettv, &error);
2717 else if (tv_get_number_chk(rettv, &error) == 0)
2718 result = FALSE;
2719 clear_tv(rettv);
2720 if (error)
2721 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 }
2723
2724 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002725 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002727 while (p[0] == '&' && p[1] == '&')
2728 {
2729 if (getnext)
2730 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002731 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002732 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002733 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002734 {
2735 error_white_both(p, 2);
2736 clear_tv(rettv);
2737 return FAIL;
2738 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002739 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002740 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002741
2742 /*
2743 * Get the second variable.
2744 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002745 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2746 {
mityu4ac198c2021-05-28 17:52:40 +02002747 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002748 clear_tv(rettv);
2749 return FAIL;
2750 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002751 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2752 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002753 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002754 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002755 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002756 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002757
2758 /*
2759 * Compute the result.
2760 */
2761 if (evaluate && result)
2762 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002763 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002764 result = tv_get_bool_chk(&var2, &error);
2765 else if (tv_get_number_chk(&var2, &error) == 0)
2766 result = FALSE;
2767 clear_tv(&var2);
2768 if (error)
2769 return FAIL;
2770 }
2771 if (evaluate)
2772 {
2773 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002774 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002775 rettv->v_type = VAR_BOOL;
2776 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002777 }
2778 else
2779 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002780 rettv->v_type = VAR_NUMBER;
2781 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002782 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002783 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002784
2785 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002787
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002788 if (evalarg == NULL)
2789 clear_evalarg(&local_evalarg, NULL);
2790 else
2791 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 }
2793
2794 return OK;
2795}
2796
2797/*
2798 * Handle third level expression:
2799 * var1 == var2
2800 * var1 =~ var2
2801 * var1 != var2
2802 * var1 !~ var2
2803 * var1 > var2
2804 * var1 >= var2
2805 * var1 < var2
2806 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002807 * var1 is var2
2808 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 *
2810 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002811 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 *
2813 * Return OK or FAIL.
2814 */
2815 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002816eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002819 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002820 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002822 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823
2824 /*
2825 * Get the first variable.
2826 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002827 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 return FAIL;
2829
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002830 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002831 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832
2833 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002834 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002836 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002838 typval_T var2;
2839 int ic;
2840 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002841 int evaluate = evalarg == NULL
2842 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002843
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002844 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002845 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002846 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002847 p = *arg;
2848 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002849 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2850 {
mityu4ac198c2021-05-28 17:52:40 +02002851 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002852 clear_tv(rettv);
2853 return FAIL;
2854 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002855
Bram Moolenaar696ba232020-07-29 21:20:41 +02002856 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2857 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002858 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002859 clear_tv(rettv);
2860 return FAIL;
2861 }
2862
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002863 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 if (p[len] == '?')
2865 {
2866 ic = TRUE;
2867 ++len;
2868 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002869 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 else if (p[len] == '#')
2871 {
2872 ic = FALSE;
2873 ++len;
2874 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002875 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002877 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878
2879 /*
2880 * Get the second variable.
2881 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002882 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2883 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002884 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002885 clear_tv(rettv);
2886 return FAIL;
2887 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002888 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002889 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002891 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 return FAIL;
2893 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002894 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002895 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002896 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002897
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002898 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002899 {
2900 ret = FAIL;
2901 clear_tv(rettv);
2902 }
2903 else
2904 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002905 clear_tv(&var2);
2906 return ret;
2907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 }
2909
2910 return OK;
2911}
2912
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002913/*
2914 * Make a copy of blob "tv1" and append blob "tv2".
2915 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916 void
2917eval_addblob(typval_T *tv1, typval_T *tv2)
2918{
2919 blob_T *b1 = tv1->vval.v_blob;
2920 blob_T *b2 = tv2->vval.v_blob;
2921 blob_T *b = blob_alloc();
2922 int i;
2923
2924 if (b != NULL)
2925 {
2926 for (i = 0; i < blob_len(b1); i++)
2927 ga_append(&b->bv_ga, blob_get(b1, i));
2928 for (i = 0; i < blob_len(b2); i++)
2929 ga_append(&b->bv_ga, blob_get(b2, i));
2930
2931 clear_tv(tv1);
2932 rettv_blob_set(tv1, b);
2933 }
2934}
2935
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002936/*
2937 * Make a copy of list "tv1" and append list "tv2".
2938 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002939 int
2940eval_addlist(typval_T *tv1, typval_T *tv2)
2941{
2942 typval_T var3;
2943
2944 // concatenate Lists
2945 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2946 {
2947 clear_tv(tv1);
2948 clear_tv(tv2);
2949 return FAIL;
2950 }
2951 clear_tv(tv1);
2952 *tv1 = var3;
2953 return OK;
2954}
2955
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956/*
2957 * Handle fourth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00002958 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002960 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002961 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 *
2963 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002964 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 *
2966 * Return OK or FAIL.
2967 */
2968 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002969eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 /*
2972 * Get the first variable.
2973 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002974 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 return FAIL;
2976
2977 /*
2978 * Repeat computing, until no '+', '-' or '.' is following.
2979 */
2980 for (;;)
2981 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002982 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002983 int getnext;
2984 char_u *p;
2985 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002986 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002987 int concat;
2988 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002989 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002990
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002991 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002992 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002993 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002994 p = eval_next_non_blank(*arg, evalarg, &getnext);
2995 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02002996 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002997 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2998 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003000 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3001 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003002
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003003 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003004 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003005 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003006 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003007 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003008 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003009 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003010 {
mityu4ac198c2021-05-28 17:52:40 +02003011 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003012 clear_tv(rettv);
3013 return FAIL;
3014 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003015 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003016 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003017 if ((op != '+' || (rettv->v_type != VAR_LIST
3018 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003019#ifdef FEAT_FLOAT
3020 && (op == '.' || rettv->v_type != VAR_FLOAT)
3021#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003022 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003023 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003024 int error = FALSE;
3025
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003026 // For "list + ...", an illegal use of the first operand as
3027 // a number cannot be determined before evaluating the 2nd
3028 // operand: if this is also a list, all is ok.
3029 // For "something . ...", "something - ..." or "non-list + ...",
3030 // we know that the first operand needs to be a string or number
3031 // without evaluating the 2nd operand. So check before to avoid
3032 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003033 if (op != '.')
3034 tv_get_number_chk(rettv, &error);
3035 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003036 {
3037 clear_tv(rettv);
3038 return FAIL;
3039 }
3040 }
3041
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 /*
3043 * Get the second variable.
3044 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003045 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003046 {
mityu89dcb4d2021-05-28 13:50:17 +02003047 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003048 clear_tv(rettv);
3049 return FAIL;
3050 }
3051 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02003052 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003054 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 return FAIL;
3056 }
3057
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003058 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 {
3060 /*
3061 * Compute the result.
3062 */
3063 if (op == '.')
3064 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003065 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3066 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003067 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003068
Bram Moolenaar2e086612020-08-25 22:37:48 +02003069 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003070 || var2.v_type == VAR_CHANNEL
3071 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003072 semsg(_(e_using_invalid_value_as_string_str),
3073 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003074#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003075 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003076 {
3077 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3078 var2.vval.v_float);
3079 s2 = buf2;
3080 }
3081#endif
3082 else
3083 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003084 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003085 {
3086 clear_tv(rettv);
3087 clear_tv(&var2);
3088 return FAIL;
3089 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003090 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003091 clear_tv(rettv);
3092 rettv->v_type = VAR_STRING;
3093 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003095 else if (op == '+' && rettv->v_type == VAR_BLOB
3096 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003098 else if (op == '+' && rettv->v_type == VAR_LIST
3099 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003100 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003102 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 else
3105 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003106 int error = FALSE;
3107 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003108#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003109 float_T f1 = 0, f2 = 0;
3110
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003111 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003112 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003113 f1 = rettv->vval.v_float;
3114 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003115 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003116 else
3117#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003118 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003119 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003120 if (error)
3121 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003122 // This can only happen for "list + non-list" or
3123 // "blob + non-blob". For "non-list + ..." or
3124 // "something - ...", we returned before evaluating the
3125 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003126 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003127 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003128 return FAIL;
3129 }
3130#ifdef FEAT_FLOAT
3131 if (var2.v_type == VAR_FLOAT)
3132 f1 = n1;
3133#endif
3134 }
3135#ifdef FEAT_FLOAT
3136 if (var2.v_type == VAR_FLOAT)
3137 {
3138 f2 = var2.vval.v_float;
3139 n2 = 0;
3140 }
3141 else
3142#endif
3143 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003144 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003145 if (error)
3146 {
3147 clear_tv(rettv);
3148 clear_tv(&var2);
3149 return FAIL;
3150 }
3151#ifdef FEAT_FLOAT
3152 if (rettv->v_type == VAR_FLOAT)
3153 f2 = n2;
3154#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003155 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003156 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003157
3158#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003159 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003160 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3161 {
3162 if (op == '+')
3163 f1 = f1 + f2;
3164 else
3165 f1 = f1 - f2;
3166 rettv->v_type = VAR_FLOAT;
3167 rettv->vval.v_float = f1;
3168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003170#endif
3171 {
3172 if (op == '+')
3173 n1 = n1 + n2;
3174 else
3175 n1 = n1 - n2;
3176 rettv->v_type = VAR_NUMBER;
3177 rettv->vval.v_number = n1;
3178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003180 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 }
3182 }
3183 return OK;
3184}
3185
3186/*
3187 * Handle fifth level expression:
3188 * * number multiplication
3189 * / number division
3190 * % number modulo
3191 *
3192 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003193 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 *
3195 * Return OK or FAIL.
3196 */
3197 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003198eval6(
3199 char_u **arg,
3200 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003201 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003202 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003204#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003205 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207
3208 /*
3209 * Get the first variable.
3210 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003211 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 return FAIL;
3213
3214 /*
3215 * Repeat computing, until no '*', '/' or '%' is following.
3216 */
3217 for (;;)
3218 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003219 int evaluate;
3220 int getnext;
3221 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003222 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003223 int op;
3224 varnumber_T n1, n2;
3225#ifdef FEAT_FLOAT
3226 float_T f1, f2;
3227#endif
3228 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003229
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003230 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003231 p = eval_next_non_blank(*arg, evalarg, &getnext);
3232 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003233 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003235
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003236 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003237 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003238 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003239 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003240 {
3241 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3242 {
mityu4ac198c2021-05-28 17:52:40 +02003243 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003244 clear_tv(rettv);
3245 return FAIL;
3246 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003247 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003250#ifdef FEAT_FLOAT
3251 f1 = 0;
3252 f2 = 0;
3253#endif
3254 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003255 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003257#ifdef FEAT_FLOAT
3258 if (rettv->v_type == VAR_FLOAT)
3259 {
3260 f1 = rettv->vval.v_float;
3261 use_float = TRUE;
3262 n1 = 0;
3263 }
3264 else
3265#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003266 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003267 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003268 if (error)
3269 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 }
3271 else
3272 n1 = 0;
3273
3274 /*
3275 * Get the second variable.
3276 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003277 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3278 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003279 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003280 clear_tv(rettv);
3281 return FAIL;
3282 }
3283 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003284 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 return FAIL;
3286
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003287 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003289#ifdef FEAT_FLOAT
3290 if (var2.v_type == VAR_FLOAT)
3291 {
3292 if (!use_float)
3293 {
3294 f1 = n1;
3295 use_float = TRUE;
3296 }
3297 f2 = var2.vval.v_float;
3298 n2 = 0;
3299 }
3300 else
3301#endif
3302 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003303 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003304 clear_tv(&var2);
3305 if (error)
3306 return FAIL;
3307#ifdef FEAT_FLOAT
3308 if (use_float)
3309 f2 = n2;
3310#endif
3311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312
3313 /*
3314 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003315 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003317#ifdef FEAT_FLOAT
3318 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003320 if (op == '*')
3321 f1 = f1 * f2;
3322 else if (op == '/')
3323 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003324# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003325 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003326 if (f2 == 0.0)
3327 {
3328 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003329 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003330 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003331 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003332 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003333 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003334 }
3335 else
3336 f1 = f1 / f2;
3337# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003338 // We rely on the floating point library to handle divide
3339 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003340 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003341# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003344 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003345 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003346 return FAIL;
3347 }
3348 rettv->v_type = VAR_FLOAT;
3349 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 }
3351 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003354 int failed = FALSE;
3355
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003356 if (op == '*')
3357 n1 = n1 * n2;
3358 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003359 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003361 n1 = num_modulus(n1, n2, &failed);
3362 if (failed)
3363 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003364
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003365 rettv->v_type = VAR_NUMBER;
3366 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 }
3369 }
3370
3371 return OK;
3372}
3373
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003374/*
3375 * Handle a type cast before a base level expression.
3376 * "arg" must point to the first non-white of the expression.
3377 * "arg" is advanced to just after the recognized expression.
3378 * Return OK or FAIL.
3379 */
3380 static int
3381eval7t(
3382 char_u **arg,
3383 typval_T *rettv,
3384 evalarg_T *evalarg,
3385 int want_string) // after "." operator
3386{
3387 type_T *want_type = NULL;
3388 garray_T type_list; // list of pointers to allocated types
3389 int res;
3390 int evaluate = evalarg == NULL ? 0
3391 : (evalarg->eval_flags & EVAL_EVALUATE);
3392
3393 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003394 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3395 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003396 {
3397 ++*arg;
3398 ga_init2(&type_list, sizeof(type_T *), 10);
3399 want_type = parse_type(arg, &type_list, TRUE);
3400 if (want_type == NULL && (evaluate || **arg != '>'))
3401 {
3402 clear_type_list(&type_list);
3403 return FAIL;
3404 }
3405
3406 if (**arg != '>')
3407 {
3408 if (*skipwhite(*arg) == '>')
3409 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3410 else
3411 emsg(_(e_missing_gt));
3412 clear_type_list(&type_list);
3413 return FAIL;
3414 }
3415 ++*arg;
3416 *arg = skipwhite_and_linebreak(*arg, evalarg);
3417 }
3418
3419 res = eval7(arg, rettv, evalarg, want_string);
3420
3421 if (want_type != NULL && evaluate)
3422 {
3423 if (res == OK)
3424 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003425 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3426 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003427
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003428 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003429 {
3430 if (want_type == &t_bool && actual != &t_bool
3431 && (actual->tt_flags & TTFLAG_BOOL_OK))
3432 {
3433 int n = tv2bool(rettv);
3434
3435 // can use "0" and "1" for boolean in some places
3436 clear_tv(rettv);
3437 rettv->v_type = VAR_BOOL;
3438 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3439 }
3440 else
3441 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003442 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003443
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003444 where.wt_variable = TRUE;
3445 res = check_type(want_type, actual, TRUE, where);
3446 }
3447 }
3448 }
3449 clear_type_list(&type_list);
3450 }
3451
3452 return res;
3453}
3454
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003455 int
3456eval_leader(char_u **arg, int vim9)
3457{
3458 char_u *s = *arg;
3459 char_u *p = *arg;
3460
3461 while (*p == '!' || *p == '-' || *p == '+')
3462 {
3463 char_u *n = skipwhite(p + 1);
3464
3465 // ++, --, -+ and +- are not accepted in Vim9 script
3466 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3467 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003468 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003469 return FAIL;
3470 }
3471 p = n;
3472 }
3473 *arg = p;
3474 return OK;
3475}
3476
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477/*
3478 * Handle sixth level expression:
3479 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003480 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003481 * "string" string constant
3482 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 * &option-name option value
3484 * @r register contents
3485 * identifier variable value
3486 * function() function call
3487 * $VAR environment variable
3488 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003489 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003490 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003491 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003492 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 *
3494 * Also handle:
3495 * ! in front logical NOT
3496 * - in front unary minus
3497 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003498 * trailing [] subscript in String or List
3499 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003500 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 *
3502 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003503 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 *
3505 * Return OK or FAIL.
3506 */
3507 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003508eval7(
3509 char_u **arg,
3510 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003511 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003512 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003514 int evaluate = evalarg != NULL
3515 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 int len;
3517 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003518 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 char_u *start_leader, *end_leader;
3520 int ret = OK;
3521 char_u *alias;
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003522 static int recurse = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523
3524 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003525 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003526 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003528 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529
3530 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003531 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 */
3533 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003534 if (eval_leader(arg, in_vim9script()) == FAIL)
3535 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 end_leader = *arg;
3537
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003538 if (**arg == '.' && (!isdigit(*(*arg + 1))
3539#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003540 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003541#endif
3542 ))
3543 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003544 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003545 ++*arg;
3546 return FAIL;
3547 }
3548
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003549 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003550 // and crash. With MSVC the stack is smaller.
3551 if (recurse ==
3552#ifdef _MSC_VER
3553 300
3554#else
3555 1000
3556#endif
3557 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003558 {
3559 semsg(_(e_expression_too_recursive_str), *arg);
3560 return FAIL;
3561 }
3562 ++recurse;
3563
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 switch (**arg)
3565 {
3566 /*
3567 * Number constant.
3568 */
3569 case '0':
3570 case '1':
3571 case '2':
3572 case '3':
3573 case '4':
3574 case '5':
3575 case '6':
3576 case '7':
3577 case '8':
3578 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003579 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003580
3581 // Apply prefixed "-" and "+" now. Matters especially when
3582 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003583 if (ret == OK && evaluate && end_leader > start_leader
3584 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003585 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 break;
3587
3588 /*
3589 * String constant: "string".
3590 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003591 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 break;
3593
3594 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003595 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003597 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003598 break;
3599
3600 /*
3601 * List: [expr, expr]
3602 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003603 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 break;
3605
3606 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003607 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003608 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003609 case '#': if (in_vim9script())
3610 {
3611 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3612 }
3613 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003614 {
3615 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003616 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003617 }
3618 else
3619 ret = NOTDONE;
3620 break;
3621
3622 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003623 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003624 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003625 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003626 case '{': if (in_vim9script())
3627 ret = NOTDONE;
3628 else
3629 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003630 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003631 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003632 break;
3633
3634 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003635 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003637 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 break;
3639
3640 /*
3641 * Environment variable: $VAR.
3642 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003643 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 break;
3645
3646 /*
3647 * Register contents: @r.
3648 */
3649 case '@': ++*arg;
3650 if (evaluate)
3651 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003652 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3653 semsg(_(e_syntax_error_at_str), *arg);
3654 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3655 emsg_invreg(**arg);
3656 else
3657 {
3658 rettv->v_type = VAR_STRING;
3659 rettv->vval.v_string = get_reg_contents(**arg,
3660 GREG_EXPR_SRC);
3661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 }
3663 if (**arg != NUL)
3664 ++*arg;
3665 break;
3666
3667 /*
3668 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003669 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003671 case '(': ret = NOTDONE;
3672 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003673 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003674 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003675 if (ret == OK && evaluate)
3676 {
3677 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3678
Bram Moolenaara9931532021-06-12 15:58:16 +02003679 // Compile it here to get the return type. The return
3680 // type is optional, when it's missing use t_unknown.
3681 // This is recognized in compile_return().
3682 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3683 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003684 if (compile_def_function(ufunc,
Bram Moolenaare99d4222021-06-13 14:01:26 +02003685 FALSE, COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003686 {
3687 clear_tv(rettv);
3688 ret = FAIL;
3689 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003690 }
3691 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003692 if (ret == NOTDONE)
3693 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003694 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003695 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003696
Bram Moolenaar9215f012020-06-27 21:18:00 +02003697 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003698 if (**arg == ')')
3699 ++*arg;
3700 else if (ret == OK)
3701 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003702 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003703 clear_tv(rettv);
3704 ret = FAIL;
3705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 }
3707 break;
3708
Bram Moolenaar8c711452005-01-14 21:53:12 +00003709 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 break;
3711 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003712
3713 if (ret == NOTDONE)
3714 {
3715 /*
3716 * Must be a variable or function name.
3717 * Can also be a curly-braces kind of name: {expr}.
3718 */
3719 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003720 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003721 if (alias != NULL)
3722 s = alias;
3723
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003724 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003725 ret = FAIL;
3726 else
3727 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003728 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3729
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003730 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003731 {
3732 emsg(_(e_cannot_use_underscore_here));
3733 ret = FAIL;
3734 }
3735 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003736 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003737 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003738 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003739 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003740 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003741 else if (flags & EVAL_CONSTANT)
3742 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003743 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003744 {
3745 // get the value of "true", "false" or a variable
3746 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3747 {
3748 rettv->v_type = VAR_BOOL;
3749 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003750 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003751 }
3752 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003753 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003754 {
3755 rettv->v_type = VAR_BOOL;
3756 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003757 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003758 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003759 else if (len == 4 && in_vim9script()
3760 && STRNCMP(s, "null", 4) == 0)
3761 {
3762 rettv->v_type = VAR_SPECIAL;
3763 rettv->vval.v_number = VVAL_NULL;
3764 ret = OK;
3765 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003766 else
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003767 {
3768 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003769 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003770 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003771 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003772 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003773 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003774 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003775 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003776 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003777 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003778 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003779 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003780 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003781 }
3782
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003783 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3784 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003785 if (ret == OK)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003786 ret = handle_subscript(arg, name_start, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787
3788 /*
3789 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3790 */
3791 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003792 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003793
3794 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003795 return ret;
3796}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003797
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003798/*
3799 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003800 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003801 * Adjusts "end_leaderp" until it is at "start_leader".
3802 */
3803 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003804eval7_leader(
3805 typval_T *rettv,
3806 int numeric_only,
3807 char_u *start_leader,
3808 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003809{
3810 char_u *end_leader = *end_leaderp;
3811 int ret = OK;
3812 int error = FALSE;
3813 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003814 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003815#ifdef FEAT_FLOAT
3816 float_T f = 0.0;
3817
3818 if (rettv->v_type == VAR_FLOAT)
3819 f = rettv->vval.v_float;
3820 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003821#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003822 {
3823 while (VIM_ISWHITE(end_leader[-1]))
3824 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003825 if (in_vim9script() && end_leader[-1] == '!')
3826 val = tv2bool(rettv);
3827 else
3828 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003829 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003830 if (error)
3831 {
3832 clear_tv(rettv);
3833 ret = FAIL;
3834 }
3835 else
3836 {
3837 while (end_leader > start_leader)
3838 {
3839 --end_leader;
3840 if (*end_leader == '!')
3841 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003842 if (numeric_only)
3843 {
3844 ++end_leader;
3845 break;
3846 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003847#ifdef FEAT_FLOAT
3848 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003849 {
3850 if (in_vim9script())
3851 {
3852 rettv->v_type = VAR_BOOL;
3853 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3854 }
3855 else
3856 f = !f;
3857 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003858 else
3859#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003860 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003861 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003862 type = VAR_BOOL;
3863 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003864 }
3865 else if (*end_leader == '-')
3866 {
3867#ifdef FEAT_FLOAT
3868 if (rettv->v_type == VAR_FLOAT)
3869 f = -f;
3870 else
3871#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003872 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003873 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003874 type = VAR_NUMBER;
3875 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003876 }
3877 }
3878#ifdef FEAT_FLOAT
3879 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003881 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003882 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003884 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003885#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003886 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003887 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003888 if (in_vim9script())
3889 rettv->v_type = type;
3890 else
3891 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003892 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003895 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 return ret;
3897}
3898
3899/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003900 * Call the function referred to in "rettv".
3901 */
3902 static int
3903call_func_rettv(
3904 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003905 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003906 typval_T *rettv,
3907 int evaluate,
3908 dict_T *selfdict,
3909 typval_T *basetv)
3910{
3911 partial_T *pt = NULL;
3912 funcexe_T funcexe;
3913 typval_T functv;
3914 char_u *s;
3915 int ret;
3916
3917 // need to copy the funcref so that we can clear rettv
3918 if (evaluate)
3919 {
3920 functv = *rettv;
3921 rettv->v_type = VAR_UNKNOWN;
3922
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003923 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003924 if (functv.v_type == VAR_PARTIAL)
3925 {
3926 pt = functv.vval.v_partial;
3927 s = partial_name(pt);
3928 }
3929 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003930 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003931 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003932 if (s == NULL || *s == NUL)
3933 {
3934 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02003935 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003936 goto theend;
3937 }
3938 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003939 }
3940 else
3941 s = (char_u *)"";
3942
Bram Moolenaara80faa82020-04-12 19:37:17 +02003943 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003944 funcexe.fe_firstline = curwin->w_cursor.lnum;
3945 funcexe.fe_lastline = curwin->w_cursor.lnum;
3946 funcexe.fe_evaluate = evaluate;
3947 funcexe.fe_partial = pt;
3948 funcexe.fe_selfdict = selfdict;
3949 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003950 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003951
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003952theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003953 // Clear the funcref afterwards, so that deleting it while
3954 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003955 if (evaluate)
3956 clear_tv(&functv);
3957
3958 return ret;
3959}
3960
3961/*
3962 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003963 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003964 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3965 */
3966 static int
3967eval_lambda(
3968 char_u **arg,
3969 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003970 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003971 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003972{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003973 int evaluate = evalarg != NULL
3974 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003975 typval_T base = *rettv;
3976 int ret;
3977
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003978 rettv->v_type = VAR_UNKNOWN;
3979
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003980 if (**arg == '{')
3981 {
3982 // ->{lambda}()
3983 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3984 }
3985 else
3986 {
3987 // ->(lambda)()
3988 ++*arg;
3989 ret = eval1(arg, rettv, evalarg);
3990 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar605ec912021-12-18 16:54:31 +00003991 if (**arg == ')')
3992 {
3993 ++*arg;
3994 }
3995 else
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003996 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003997 emsg(_(e_missing_closing_paren));
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003998 ret = FAIL;
3999 }
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004000 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004001 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004002 return FAIL;
4003 else if (**arg != '(')
4004 {
4005 if (verbose)
4006 {
4007 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004008 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004009 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004010 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004011 }
4012 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004013 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004014 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004015 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004016 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004017
4018 // Clear the funcref afterwards, so that deleting it while
4019 // evaluating the arguments is possible (see test55).
4020 if (evaluate)
4021 clear_tv(&base);
4022
4023 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004024}
4025
4026/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004027 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004028 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004029 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4030 */
4031 static int
4032eval_method(
4033 char_u **arg,
4034 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004035 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004036 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004037{
4038 char_u *name;
4039 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004040 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004041 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004042 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004043 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004044 int evaluate = evalarg != NULL
4045 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004046
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004047 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004048
Bram Moolenaarac92e252019-08-03 21:58:38 +02004049 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004050 len = get_name_len(arg, &alias, evaluate, TRUE);
4051 if (alias != NULL)
4052 name = alias;
4053
4054 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004055 {
4056 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004057 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004058 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004059 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004060 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004061 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004062 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004063
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004064 // If there is no "(" immediately following, but there is further on,
4065 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4066 // Does not handle anything where "(" is part of the expression.
4067 *arg = skipwhite(*arg);
4068
4069 if (**arg != '(' && alias == NULL
4070 && (paren = vim_strchr(*arg, '(')) != NULL)
4071 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004072 char_u *deref;
4073
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004074 *arg = name;
4075 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004076 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4077 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004078 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004079 *arg = name + len;
4080 ret = FAIL;
4081 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004082 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004083 {
4084 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004085 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004086 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004087 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004088 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004089
4090 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004091 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004092 *arg = skipwhite(*arg);
4093
4094 if (**arg != '(')
4095 {
4096 if (verbose)
4097 semsg(_(e_missing_parenthesis_str), name);
4098 ret = FAIL;
4099 }
4100 else if (VIM_ISWHITE((*arg)[-1]))
4101 {
4102 if (verbose)
4103 emsg(_(e_no_white_space_allowed_before_parenthesis));
4104 ret = FAIL;
4105 }
4106 else
4107 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004108 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004109 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004110 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004111
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004112 // Clear the funcref afterwards, so that deleting it while
4113 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004114 if (evaluate)
4115 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004116 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004117
Bram Moolenaarac92e252019-08-03 21:58:38 +02004118 return ret;
4119}
4120
4121/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004122 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4123 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004124 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4125 */
4126 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004127eval_index(
4128 char_u **arg,
4129 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004130 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004131 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004132{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004133 int evaluate = evalarg != NULL
4134 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004135 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004136 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004137 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004138 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004139 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004140 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004141
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004142 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4143 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004144
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004145 init_tv(&var1);
4146 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004147 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004148 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004149 /*
4150 * dict.name
4151 */
4152 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004153 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004154 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004155 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004156 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004157 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004158 }
4159 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004160 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004161 /*
4162 * something[idx]
4163 *
4164 * Get the (first) variable from inside the [].
4165 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004166 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004167 if (**arg == ':')
4168 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004169 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004170 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004171 else if (vim9 && **arg == ':')
4172 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004173 semsg(_(e_white_space_required_before_and_after_str_at_str),
4174 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004175 clear_tv(&var1);
4176 return FAIL;
4177 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004178 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004179 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004180 int error = FALSE;
4181
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004182#ifdef FEAT_FLOAT
4183 // allow for indexing with float
4184 if (vim9 && rettv->v_type == VAR_DICT
4185 && var1.v_type == VAR_FLOAT)
4186 {
4187 var1.vval.v_string = typval_tostring(&var1, TRUE);
4188 var1.v_type = VAR_STRING;
4189 }
4190#endif
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004191 if (vim9 && rettv->v_type == VAR_LIST)
4192 tv_get_number_chk(&var1, &error);
4193 else
4194 error = tv_get_string_chk(&var1) == NULL;
4195 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004196 {
4197 // not a number or string
4198 clear_tv(&var1);
4199 return FAIL;
4200 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004201 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004202
4203 /*
4204 * Get the second variable from inside the [:].
4205 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004206 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004207 if (**arg == ':')
4208 {
4209 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004210 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004211 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004212 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004213 semsg(_(e_white_space_required_before_and_after_str_at_str),
4214 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004215 if (!empty1)
4216 clear_tv(&var1);
4217 return FAIL;
4218 }
4219 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004220 if (**arg == ']')
4221 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004222 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004223 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004224 if (!empty1)
4225 clear_tv(&var1);
4226 return FAIL;
4227 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004228 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004229 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004230 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004231 if (!empty1)
4232 clear_tv(&var1);
4233 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004234 return FAIL;
4235 }
4236 }
4237
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004238 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004239 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004240 if (**arg != ']')
4241 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004242 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004243 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004244 clear_tv(&var1);
4245 if (range)
4246 clear_tv(&var2);
4247 return FAIL;
4248 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004249 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004250 }
4251
4252 if (evaluate)
4253 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004254 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004255 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004256 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004257
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004258 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004259 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004260 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004261 clear_tv(&var2);
4262 return res;
4263 }
4264 return OK;
4265}
4266
4267/*
4268 * Check if "rettv" can have an [index] or [sli:ce]
4269 */
4270 int
4271check_can_index(typval_T *rettv, int evaluate, int verbose)
4272{
4273 switch (rettv->v_type)
4274 {
4275 case VAR_FUNC:
4276 case VAR_PARTIAL:
4277 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004278 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004279 return FAIL;
4280 case VAR_FLOAT:
4281#ifdef FEAT_FLOAT
4282 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004283 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004284 return FAIL;
4285#endif
4286 case VAR_BOOL:
4287 case VAR_SPECIAL:
4288 case VAR_JOB:
4289 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004290 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004291 if (verbose)
4292 emsg(_(e_cannot_index_special_variable));
4293 return FAIL;
4294 case VAR_UNKNOWN:
4295 case VAR_ANY:
4296 case VAR_VOID:
4297 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004298 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004299 emsg(_(e_cannot_index_special_variable));
4300 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004301 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004302 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004303
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004304 case VAR_STRING:
4305 case VAR_LIST:
4306 case VAR_DICT:
4307 case VAR_BLOB:
4308 break;
4309 case VAR_NUMBER:
4310 if (in_vim9script())
4311 emsg(_(e_cannot_index_number));
4312 break;
4313 }
4314 return OK;
4315}
4316
4317/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004318 * slice() function
4319 */
4320 void
4321f_slice(typval_T *argvars, typval_T *rettv)
4322{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004323 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004324 && ((argvars[0].v_type != VAR_STRING
4325 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004326 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004327 && check_for_list_arg(argvars, 0) == FAIL)
4328 || check_for_number_arg(argvars, 1) == FAIL
4329 || check_for_opt_number_arg(argvars, 2) == FAIL))
4330 return;
4331
Bram Moolenaar6601b622021-01-13 21:47:15 +01004332 if (check_can_index(argvars, TRUE, FALSE) == OK)
4333 {
4334 copy_tv(argvars, rettv);
4335 eval_index_inner(rettv, TRUE, argvars + 1,
4336 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4337 TRUE, NULL, 0, FALSE);
4338 }
4339}
4340
4341/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004342 * Apply index or range to "rettv".
4343 * "var1" is the first index, NULL for [:expr].
4344 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004345 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4346 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004347 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4348 */
4349 int
4350eval_index_inner(
4351 typval_T *rettv,
4352 int is_range,
4353 typval_T *var1,
4354 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004355 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004356 char_u *key,
4357 int keylen,
4358 int verbose)
4359{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004360 varnumber_T n1, n2 = 0;
4361 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004362
4363 n1 = 0;
4364 if (var1 != NULL && rettv->v_type != VAR_DICT)
4365 n1 = tv_get_number(var1);
4366
4367 if (is_range)
4368 {
4369 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004370 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004371 if (verbose)
4372 emsg(_(e_cannot_slice_dictionary));
4373 return FAIL;
4374 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004375 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004376 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004377 else
4378 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004379 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004380
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004381 switch (rettv->v_type)
4382 {
4383 case VAR_UNKNOWN:
4384 case VAR_ANY:
4385 case VAR_VOID:
4386 case VAR_FUNC:
4387 case VAR_PARTIAL:
4388 case VAR_FLOAT:
4389 case VAR_BOOL:
4390 case VAR_SPECIAL:
4391 case VAR_JOB:
4392 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004393 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004394 break; // not evaluating, skipping over subscript
4395
4396 case VAR_NUMBER:
4397 case VAR_STRING:
4398 {
4399 char_u *s = tv_get_string(rettv);
4400
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004401 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004402 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004403 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004404 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004405 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004406 else
4407 s = char_from_string(s, n1);
4408 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004409 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004410 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004411 // The resulting variable is a substring. If the indexes
4412 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004413 if (n1 < 0)
4414 {
4415 n1 = len + n1;
4416 if (n1 < 0)
4417 n1 = 0;
4418 }
4419 if (n2 < 0)
4420 n2 = len + n2;
4421 else if (n2 >= len)
4422 n2 = len;
4423 if (n1 >= len || n2 < 0 || n1 > n2)
4424 s = NULL;
4425 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004426 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004427 }
4428 else
4429 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004430 // The resulting variable is a string of a single
4431 // character. If the index is too big or negative the
4432 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004433 if (n1 >= len || n1 < 0)
4434 s = NULL;
4435 else
4436 s = vim_strnsave(s + n1, 1);
4437 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004438 clear_tv(rettv);
4439 rettv->v_type = VAR_STRING;
4440 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004441 }
4442 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004443
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004444 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004445 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4446 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004447 break;
4448
4449 case VAR_LIST:
4450 if (var1 == NULL)
4451 n1 = 0;
4452 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004453 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004454 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004455 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004456 return FAIL;
4457 break;
4458
4459 case VAR_DICT:
4460 {
4461 dictitem_T *item;
4462 typval_T tmp;
4463
4464 if (key == NULL)
4465 {
4466 key = tv_get_string_chk(var1);
4467 if (key == NULL)
4468 return FAIL;
4469 }
4470
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004471 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004472
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004473 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004474 {
4475 if (verbose)
4476 {
4477 if (keylen > 0)
4478 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004479 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004480 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004481 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004482 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004483
4484 copy_tv(&item->di_tv, &tmp);
4485 clear_tv(rettv);
4486 *rettv = tmp;
4487 }
4488 break;
4489 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004490 return OK;
4491}
4492
4493/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004494 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004495 */
4496 char_u *
4497partial_name(partial_T *pt)
4498{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004499 if (pt != NULL)
4500 {
4501 if (pt->pt_name != NULL)
4502 return pt->pt_name;
4503 if (pt->pt_func != NULL)
4504 return pt->pt_func->uf_name;
4505 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004506 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004507}
4508
Bram Moolenaarddecc252016-04-06 22:59:37 +02004509 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004510partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004511{
4512 int i;
4513
4514 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004515 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004516 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004517 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004518 if (pt->pt_name != NULL)
4519 {
4520 func_unref(pt->pt_name);
4521 vim_free(pt->pt_name);
4522 }
4523 else
4524 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004525
Bram Moolenaar54656012021-06-09 20:50:46 +02004526 // "out_up" is no longer used, decrement refcount on partial that owns it.
4527 partial_unref(pt->pt_outer.out_up_partial);
4528
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004529 // Using pt_outer from another partial.
4530 partial_unref(pt->pt_outer_partial);
4531
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004532 // Decrease the reference count for the context of a closure. If down
4533 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004534 if (pt->pt_funcstack != NULL)
4535 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004536 --pt->pt_funcstack->fs_refcount;
4537 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004538 }
4539
Bram Moolenaarddecc252016-04-06 22:59:37 +02004540 vim_free(pt);
4541}
4542
4543/*
4544 * Unreference a closure: decrement the reference count and free it when it
4545 * becomes zero.
4546 */
4547 void
4548partial_unref(partial_T *pt)
4549{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004550 if (pt != NULL)
4551 {
4552 if (--pt->pt_refcount <= 0)
4553 partial_free(pt);
4554
4555 // If the reference count goes down to one, the funcstack may be the
4556 // only reference and can be freed if no other partials reference it.
4557 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4558 funcstack_check_refcount(pt->pt_funcstack);
4559 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004560}
4561
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004562/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004563 * Return the next (unique) copy ID.
4564 * Used for serializing nested structures.
4565 */
4566 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004567get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004568{
4569 current_copyID += COPYID_INC;
4570 return current_copyID;
4571}
4572
4573/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004574 * Garbage collection for lists and dictionaries.
4575 *
4576 * We use reference counts to be able to free most items right away when they
4577 * are no longer used. But for composite items it's possible that it becomes
4578 * unused while the reference count is > 0: When there is a recursive
4579 * reference. Example:
4580 * :let l = [1, 2, 3]
4581 * :let d = {9: l}
4582 * :let l[1] = d
4583 *
4584 * Since this is quite unusual we handle this with garbage collection: every
4585 * once in a while find out which lists and dicts are not referenced from any
4586 * variable.
4587 *
4588 * Here is a good reference text about garbage collection (refers to Python
4589 * but it applies to all reference-counting mechanisms):
4590 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004591 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004592
4593/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004594 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004595 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004596 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004597 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004598 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004599garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004600{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004601 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004602 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004603 buf_T *buf;
4604 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004605 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004606 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004607
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004608 if (!testing)
4609 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004610 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004611 want_garbage_collect = FALSE;
4612 may_garbage_collect = FALSE;
4613 garbage_collect_at_exit = FALSE;
4614 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004615
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004616 // The execution stack can grow big, limit the size.
4617 if (exestack.ga_maxlen - exestack.ga_len > 500)
4618 {
4619 size_t new_len;
4620 char_u *pp;
4621 int n;
4622
4623 // Keep 150% of the current size, with a minimum of the growth size.
4624 n = exestack.ga_len / 2;
4625 if (n < exestack.ga_growsize)
4626 n = exestack.ga_growsize;
4627
4628 // Don't make it bigger though.
4629 if (exestack.ga_len + n < exestack.ga_maxlen)
4630 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004631 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004632 pp = vim_realloc(exestack.ga_data, new_len);
4633 if (pp == NULL)
4634 return FAIL;
4635 exestack.ga_maxlen = exestack.ga_len + n;
4636 exestack.ga_data = pp;
4637 }
4638 }
4639
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004640 // We advance by two because we add one for items referenced through
4641 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004642 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004643
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004644 /*
4645 * 1. Go through all accessible variables and mark all lists and dicts
4646 * with copyID.
4647 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004648
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004649 // Don't free variables in the previous_funccal list unless they are only
4650 // referenced through previous_funccal. This must be first, because if
4651 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004652 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004653
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004654 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004655 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004656
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004657 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004658 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004659 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4660 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004661
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004662 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004663 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004664 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4665 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004666 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004667 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4668 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004669#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004670 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004671 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4672 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004673 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004674 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004675 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4676 NULL, NULL);
4677#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004678
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004679 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004680 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004681 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4682 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004683 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004684 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004685
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004686 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004687 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004688
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004689 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004690 abort = abort || set_ref_in_functions(copyID);
4691
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004692 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004693 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004694
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004695 // funcstacks keep variables for closures
4696 abort = abort || set_ref_in_funcstacks(copyID);
4697
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004698 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004699 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004700
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004701 // callbacks in buffers
4702 abort = abort || set_ref_in_buffers(copyID);
4703
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004704 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
4705 abort = abort || set_ref_in_insexpand_funcs(copyID);
4706
4707 // 'operatorfunc' callback
4708 abort = abort || set_ref_in_opfunc(copyID);
4709
4710 // 'tagfunc' callback
4711 abort = abort || set_ref_in_tagfunc(copyID);
4712
4713 // 'imactivatefunc' and 'imstatusfunc' callbacks
4714 abort = abort || set_ref_in_im_funcs(copyID);
4715
Bram Moolenaar1dced572012-04-05 16:54:08 +02004716#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004717 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004718#endif
4719
Bram Moolenaardb913952012-06-29 12:54:53 +02004720#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004721 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004722#endif
4723
4724#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004725 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004726#endif
4727
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004728#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004729 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004730 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004731#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004732#ifdef FEAT_NETBEANS_INTG
4733 abort = abort || set_ref_in_nb_channel(copyID);
4734#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004735
Bram Moolenaare3188e22016-05-31 21:13:04 +02004736#ifdef FEAT_TIMERS
4737 abort = abort || set_ref_in_timer(copyID);
4738#endif
4739
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004740#ifdef FEAT_QUICKFIX
4741 abort = abort || set_ref_in_quickfix(copyID);
4742#endif
4743
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004744#ifdef FEAT_TERMINAL
4745 abort = abort || set_ref_in_term(copyID);
4746#endif
4747
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004748#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004749 abort = abort || set_ref_in_popups(copyID);
4750#endif
4751
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004752 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004753 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004754 /*
4755 * 2. Free lists and dictionaries that are not referenced.
4756 */
4757 did_free = free_unref_items(copyID);
4758
4759 /*
4760 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004761 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004762 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004763 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004764 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004765 else if (p_verbose > 0)
4766 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004767 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004768 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004769
4770 return did_free;
4771}
4772
4773/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004774 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004775 */
4776 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004777free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004778{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004779 int did_free = FALSE;
4780
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004781 // Let all "free" functions know that we are here. This means no
4782 // dictionaries, lists, channels or jobs are to be freed, because we will
4783 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004784 in_free_unref_items = TRUE;
4785
4786 /*
4787 * PASS 1: free the contents of the items. We don't free the items
4788 * themselves yet, so that it is possible to decrement refcount counters
4789 */
4790
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004791 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004792 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004793
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004794 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004795 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004796
4797#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004798 // Go through the list of jobs and free items without the copyID. This
4799 // must happen before doing channels, because jobs refer to channels, but
4800 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004801 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4802
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004803 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004804 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4805#endif
4806
4807 /*
4808 * PASS 2: free the items themselves.
4809 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004810 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004811 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004812
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004813#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004814 // Go through the list of jobs and free items without the copyID. This
4815 // must happen before doing channels, because jobs refer to channels, but
4816 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004817 free_unused_jobs(copyID, COPYID_MASK);
4818
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004819 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004820 free_unused_channels(copyID, COPYID_MASK);
4821#endif
4822
4823 in_free_unref_items = FALSE;
4824
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004825 return did_free;
4826}
4827
4828/*
4829 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004830 * "list_stack" is used to add lists to be marked. Can be NULL.
4831 *
4832 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004833 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004834 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004835set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004836{
4837 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004838 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004839 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004840 hashtab_T *cur_ht;
4841 ht_stack_T *ht_stack = NULL;
4842 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004843
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004844 cur_ht = ht;
4845 for (;;)
4846 {
4847 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004848 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004849 // Mark each item in the hashtab. If the item contains a hashtab
4850 // it is added to ht_stack, if it contains a list it is added to
4851 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004852 todo = (int)cur_ht->ht_used;
4853 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4854 if (!HASHITEM_EMPTY(hi))
4855 {
4856 --todo;
4857 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4858 &ht_stack, list_stack);
4859 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004860 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004861
4862 if (ht_stack == NULL)
4863 break;
4864
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004865 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004866 cur_ht = ht_stack->ht;
4867 tempitem = ht_stack;
4868 ht_stack = ht_stack->prev;
4869 free(tempitem);
4870 }
4871
4872 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004873}
4874
Dominique Pelle748b3082022-01-08 12:41:16 +00004875#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
4876 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004877/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004878 * Mark a dict and its items with "copyID".
4879 * Returns TRUE if setting references failed somehow.
4880 */
4881 int
4882set_ref_in_dict(dict_T *d, int copyID)
4883{
4884 if (d != NULL && d->dv_copyID != copyID)
4885 {
4886 d->dv_copyID = copyID;
4887 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4888 }
4889 return FALSE;
4890}
Dominique Pelle748b3082022-01-08 12:41:16 +00004891#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004892
4893/*
4894 * Mark a list and its items with "copyID".
4895 * Returns TRUE if setting references failed somehow.
4896 */
4897 int
4898set_ref_in_list(list_T *ll, int copyID)
4899{
4900 if (ll != NULL && ll->lv_copyID != copyID)
4901 {
4902 ll->lv_copyID = copyID;
4903 return set_ref_in_list_items(ll, copyID, NULL);
4904 }
4905 return FALSE;
4906}
4907
4908/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004909 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004910 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4911 *
4912 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004913 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004914 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004915set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004916{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004917 listitem_T *li;
4918 int abort = FALSE;
4919 list_T *cur_l;
4920 list_stack_T *list_stack = NULL;
4921 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004922
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004923 cur_l = l;
4924 for (;;)
4925 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004926 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004927 // Mark each item in the list. If the item contains a hashtab
4928 // it is added to ht_stack, if it contains a list it is added to
4929 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004930 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4931 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4932 ht_stack, &list_stack);
4933 if (list_stack == NULL)
4934 break;
4935
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004936 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004937 cur_l = list_stack->list;
4938 tempitem = list_stack;
4939 list_stack = list_stack->prev;
4940 free(tempitem);
4941 }
4942
4943 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004944}
4945
4946/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004947 * Mark the partial in callback 'cb' with "copyID".
4948 */
4949 int
4950set_ref_in_callback(callback_T *cb, int copyID)
4951{
4952 typval_T tv;
4953
4954 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
4955 return FALSE;
4956
4957 tv.v_type = VAR_PARTIAL;
4958 tv.vval.v_partial = cb->cb_partial;
4959 return set_ref_in_item(&tv, copyID, NULL, NULL);
4960}
4961
4962/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004963 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004964 * "list_stack" is used to add lists to be marked. Can be NULL.
4965 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4966 *
4967 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004968 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004969 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004970set_ref_in_item(
4971 typval_T *tv,
4972 int copyID,
4973 ht_stack_T **ht_stack,
4974 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004975{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004976 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004977
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004978 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004979 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004980 dict_T *dd = tv->vval.v_dict;
4981
Bram Moolenaara03f2332016-02-06 18:09:59 +01004982 if (dd != NULL && dd->dv_copyID != copyID)
4983 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004984 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004985 dd->dv_copyID = copyID;
4986 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004987 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004988 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4989 }
4990 else
4991 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004992 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4993
Bram Moolenaara03f2332016-02-06 18:09:59 +01004994 if (newitem == NULL)
4995 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004996 else
4997 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004998 newitem->ht = &dd->dv_hashtab;
4999 newitem->prev = *ht_stack;
5000 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005001 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005002 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005003 }
5004 }
5005 else if (tv->v_type == VAR_LIST)
5006 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005007 list_T *ll = tv->vval.v_list;
5008
Bram Moolenaara03f2332016-02-06 18:09:59 +01005009 if (ll != NULL && ll->lv_copyID != copyID)
5010 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005011 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005012 ll->lv_copyID = copyID;
5013 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005014 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005015 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005016 }
5017 else
5018 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005019 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5020
Bram Moolenaara03f2332016-02-06 18:09:59 +01005021 if (newitem == NULL)
5022 abort = TRUE;
5023 else
5024 {
5025 newitem->list = ll;
5026 newitem->prev = *list_stack;
5027 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005028 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005029 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005030 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005031 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005032 else if (tv->v_type == VAR_FUNC)
5033 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005034 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005035 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005036 else if (tv->v_type == VAR_PARTIAL)
5037 {
5038 partial_T *pt = tv->vval.v_partial;
5039 int i;
5040
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005041 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005042 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005043 // Didn't see this partial yet.
5044 pt->pt_copyID = copyID;
5045
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005046 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005047
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005048 if (pt->pt_dict != NULL)
5049 {
5050 typval_T dtv;
5051
5052 dtv.v_type = VAR_DICT;
5053 dtv.vval.v_dict = pt->pt_dict;
5054 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5055 }
5056
5057 for (i = 0; i < pt->pt_argc; ++i)
5058 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5059 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005060 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005061 }
5062 }
5063#ifdef FEAT_JOB_CHANNEL
5064 else if (tv->v_type == VAR_JOB)
5065 {
5066 job_T *job = tv->vval.v_job;
5067 typval_T dtv;
5068
5069 if (job != NULL && job->jv_copyID != copyID)
5070 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005071 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005072 if (job->jv_channel != NULL)
5073 {
5074 dtv.v_type = VAR_CHANNEL;
5075 dtv.vval.v_channel = job->jv_channel;
5076 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5077 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005078 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005079 {
5080 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005081 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005082 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5083 }
5084 }
5085 }
5086 else if (tv->v_type == VAR_CHANNEL)
5087 {
5088 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005089 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005090 typval_T dtv;
5091 jsonq_T *jq;
5092 cbq_T *cq;
5093
5094 if (ch != NULL && ch->ch_copyID != copyID)
5095 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005096 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005097 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005098 {
5099 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5100 jq = jq->jq_next)
5101 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5102 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5103 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005104 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005105 {
5106 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005107 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005108 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5109 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005110 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005111 {
5112 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005113 dtv.vval.v_partial =
5114 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005115 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5116 }
5117 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005118 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005119 {
5120 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005121 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005122 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5123 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005124 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005125 {
5126 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005127 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005128 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5129 }
5130 }
5131 }
5132#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005133 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005134}
5135
Bram Moolenaar8c711452005-01-14 21:53:12 +00005136/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005137 * Return a string with the string representation of a variable.
5138 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005139 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005140 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005141 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005142 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005143 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005144 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5145 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005146 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005147 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005148 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005149echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005150 typval_T *tv,
5151 char_u **tofree,
5152 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005153 int copyID,
5154 int echo_style,
5155 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005156 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005157{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005158 static int recurse = 0;
5159 char_u *r = NULL;
5160
Bram Moolenaar33570922005-01-25 22:26:29 +00005161 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005162 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005163 if (!did_echo_string_emsg)
5164 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005165 // Only give this message once for a recursive call to avoid
5166 // flooding the user with errors. And stop iterating over lists
5167 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005168 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005169 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005170 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005171 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005172 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005173 }
5174 ++recurse;
5175
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005176 switch (tv->v_type)
5177 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005178 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005179 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005180 {
5181 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005182 r = tv->vval.v_string;
5183 if (r == NULL)
5184 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005185 }
5186 else
5187 {
5188 *tofree = string_quote(tv->vval.v_string, FALSE);
5189 r = *tofree;
5190 }
5191 break;
5192
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005193 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005194 if (echo_style)
5195 {
5196 *tofree = NULL;
5197 r = tv->vval.v_string;
5198 }
5199 else
5200 {
5201 *tofree = string_quote(tv->vval.v_string, TRUE);
5202 r = *tofree;
5203 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005204 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005205
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005206 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005207 {
5208 partial_T *pt = tv->vval.v_partial;
5209 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005210 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005211 garray_T ga;
5212 int i;
5213 char_u *tf;
5214
5215 ga_init2(&ga, 1, 100);
5216 ga_concat(&ga, (char_u *)"function(");
5217 if (fname != NULL)
5218 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005219 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005220 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005221 && vim_isupper(fname[1]))
5222 {
5223 ga_concat(&ga, (char_u *)"'g:");
5224 ga_concat(&ga, fname + 1);
5225 }
5226 else
5227 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005228 vim_free(fname);
5229 }
5230 if (pt != NULL && pt->pt_argc > 0)
5231 {
5232 ga_concat(&ga, (char_u *)", [");
5233 for (i = 0; i < pt->pt_argc; ++i)
5234 {
5235 if (i > 0)
5236 ga_concat(&ga, (char_u *)", ");
5237 ga_concat(&ga,
5238 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5239 vim_free(tf);
5240 }
5241 ga_concat(&ga, (char_u *)"]");
5242 }
5243 if (pt != NULL && pt->pt_dict != NULL)
5244 {
5245 typval_T dtv;
5246
5247 ga_concat(&ga, (char_u *)", ");
5248 dtv.v_type = VAR_DICT;
5249 dtv.vval.v_dict = pt->pt_dict;
5250 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5251 vim_free(tf);
5252 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005253 // terminate with ')' and a NUL
5254 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005255
5256 *tofree = ga.ga_data;
5257 r = *tofree;
5258 break;
5259 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005260
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005261 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005262 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005263 break;
5264
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005265 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005266 if (tv->vval.v_list == NULL)
5267 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005268 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005269 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005270 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005271 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005272 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5273 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005274 {
5275 *tofree = NULL;
5276 r = (char_u *)"[...]";
5277 }
5278 else
5279 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005280 int old_copyID = tv->vval.v_list->lv_copyID;
5281
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005282 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005283 *tofree = list2string(tv, copyID, restore_copyID);
5284 if (restore_copyID)
5285 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005286 r = *tofree;
5287 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005288 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005289
Bram Moolenaar8c711452005-01-14 21:53:12 +00005290 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005291 if (tv->vval.v_dict == NULL)
5292 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005293 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005294 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005295 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005296 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005297 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5298 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005299 {
5300 *tofree = NULL;
5301 r = (char_u *)"{...}";
5302 }
5303 else
5304 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005305 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005306
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005307 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005308 *tofree = dict2string(tv, copyID, restore_copyID);
5309 if (restore_copyID)
5310 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005311 r = *tofree;
5312 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005313 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005314
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005315 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005316 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005317 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005318 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005319 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005320 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005321 break;
5322
Bram Moolenaar835dc632016-02-07 14:27:38 +01005323 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005324 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005325#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005326 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005327 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5328 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005329 if (composite_val)
5330 {
5331 *tofree = string_quote(r, FALSE);
5332 r = *tofree;
5333 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005334#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005335 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005336
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005337 case VAR_INSTR:
5338 *tofree = NULL;
5339 r = (char_u *)"instructions";
5340 break;
5341
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005342 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005343#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005344 *tofree = NULL;
5345 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5346 r = numbuf;
5347 break;
5348#endif
5349
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005350 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005351 case VAR_SPECIAL:
5352 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005353 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005354 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005355 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005356
Bram Moolenaar8502c702014-06-17 12:51:16 +02005357 if (--recurse == 0)
5358 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005359 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005360}
5361
5362/*
5363 * Return a string with the string representation of a variable.
5364 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5365 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005366 * Does not put quotes around strings, as ":echo" displays values.
5367 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5368 * May return NULL.
5369 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005370 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005371echo_string(
5372 typval_T *tv,
5373 char_u **tofree,
5374 char_u *numbuf,
5375 int copyID)
5376{
5377 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5378}
5379
5380/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005381 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5382 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005383 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005384 */
5385 int
5386buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5387{
5388 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005389 char_u *t;
5390 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005391
5392 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5393 return -1;
5394
5395 if (lnum > buf->b_ml.ml_line_count)
5396 lnum = buf->b_ml.ml_line_count;
5397
5398 str = ml_get_buf(buf, lnum, FALSE);
5399 if (str == NULL)
5400 return -1;
5401
5402 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005403 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005404
Bram Moolenaar91458462021-01-13 20:08:38 +01005405 // count the number of characters
5406 t = str;
5407 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5408 t += mb_ptr2len(t);
5409
5410 // In insert mode, when the cursor is at the end of a non-empty line,
5411 // byteidx points to the NUL character immediately past the end of the
5412 // string. In this case, add one to the character count.
5413 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5414 count++;
5415
5416 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005417}
5418
5419/*
5420 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005421 * byte index. Works only for loaded buffers. Returns -1 on failure.
5422 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005423 */
5424 int
5425buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5426{
5427 char_u *str;
5428 char_u *t;
5429
5430 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5431 return -1;
5432
5433 if (lnum > buf->b_ml.ml_line_count)
5434 lnum = buf->b_ml.ml_line_count;
5435
5436 str = ml_get_buf(buf, lnum, FALSE);
5437 if (str == NULL)
5438 return -1;
5439
5440 // Convert the character offset to a byte offset
5441 t = str;
5442 while (*t != NUL && --charidx > 0)
5443 t += mb_ptr2len(t);
5444
Bram Moolenaar91458462021-01-13 20:08:38 +01005445 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005446}
5447
5448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005450 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005452 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005453var2fpos(
5454 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005455 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005456 int *fnum, // set to fnum for '0, 'A, etc.
5457 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005459 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005461 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005463 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005464 if (varp->v_type == VAR_LIST)
5465 {
5466 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005467 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005468 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005469 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005470
5471 l = varp->vval.v_list;
5472 if (l == NULL)
5473 return NULL;
5474
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005475 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005476 pos.lnum = list_find_nr(l, 0L, &error);
5477 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005478 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005479 if (charcol)
5480 len = (long)mb_charlen(ml_get(pos.lnum));
5481 else
5482 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005483
Bram Moolenaarec65d772020-08-20 22:29:12 +02005484 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005485 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005486 li = list_find(l, 1L);
5487 if (li != NULL && li->li_tv.v_type == VAR_STRING
5488 && li->li_tv.vval.v_string != NULL
5489 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005490 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005491 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005492 }
5493 else
5494 {
5495 pos.col = list_find_nr(l, 1L, &error);
5496 if (error)
5497 return NULL;
5498 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005499
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005500 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005501 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005502 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005503 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005504
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005505 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005506 pos.coladd = list_find_nr(l, 2L, &error);
5507 if (error)
5508 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005509
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005510 return &pos;
5511 }
5512
Bram Moolenaarc5809432021-03-27 21:23:30 +01005513 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5514 return NULL;
5515
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005516 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005517 if (name == NULL)
5518 return NULL;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005519 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005520 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005521 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005522 pos = curwin->w_cursor;
5523 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005524 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005525 return &pos;
5526 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005527 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005528 {
5529 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005530 pos = VIsual;
5531 else
5532 pos = curwin->w_cursor;
5533 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005534 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005535 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005536 }
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005537 if (name[0] == '\'' && (!in_vim9script()
5538 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005540 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005541 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5543 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005544 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005545 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 return pp;
5547 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005548
Bram Moolenaara5525202006-03-02 22:52:09 +00005549 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005550
Bram Moolenaar477933c2007-07-17 14:32:23 +00005551 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005552 {
5553 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005554 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005555 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005556 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005557 // In silent Ex mode topline is zero, but that's not a valid line
5558 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005559 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005560 return &pos;
5561 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005562 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005563 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005564 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005565 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005566 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005567 return &pos;
5568 }
5569 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005570 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005572 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 {
5574 pos.lnum = curbuf->b_ml.ml_line_count;
5575 pos.col = 0;
5576 }
5577 else
5578 {
5579 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005580 if (charcol)
5581 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5582 else
5583 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 }
5585 return &pos;
5586 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005587 if (in_vim9script())
5588 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 return NULL;
5590}
5591
5592/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005593 * Convert list in "arg" into a position and optional file number.
5594 * When "fnump" is NULL there is no file number, only 3 items.
5595 * Note that the column is passed on as-is, the caller may want to decrement
5596 * it to use 1 for the first column.
5597 * Return FAIL when conversion is not possible, doesn't check the position for
5598 * validity.
5599 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005600 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005601list2fpos(
5602 typval_T *arg,
5603 pos_T *posp,
5604 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005605 colnr_T *curswantp,
5606 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005607{
5608 list_T *l = arg->vval.v_list;
5609 long i = 0;
5610 long n;
5611
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005612 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5613 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005614 if (arg->v_type != VAR_LIST
5615 || l == NULL
5616 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005617 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005618 return FAIL;
5619
5620 if (fnump != NULL)
5621 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005622 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005623 if (n < 0)
5624 return FAIL;
5625 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005626 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005627 *fnump = n;
5628 }
5629
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005630 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005631 if (n < 0)
5632 return FAIL;
5633 posp->lnum = n;
5634
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005635 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005636 if (n < 0)
5637 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005638 // If character position is specified, then convert to byte position
5639 if (charcol)
5640 {
5641 buf_T *buf;
5642
5643 // Get the text for the specified line in a loaded buffer
5644 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5645 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5646 return FAIL;
5647
Bram Moolenaar91458462021-01-13 20:08:38 +01005648 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005649 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005650 posp->col = n;
5651
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005652 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005653 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005654 posp->coladd = 0;
5655 else
5656 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005657
Bram Moolenaar493c1782014-05-28 14:34:46 +02005658 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005659 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005660
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005661 return OK;
5662}
5663
5664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 * Get the length of an environment variable name.
5666 * Advance "arg" to the first character after the name.
5667 * Return 0 for error.
5668 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005669 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005670get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671{
5672 char_u *p;
5673 int len;
5674
5675 for (p = *arg; vim_isIDc(*p); ++p)
5676 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005677 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 return 0;
5679
5680 len = (int)(p - *arg);
5681 *arg = p;
5682 return len;
5683}
5684
5685/*
5686 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005687 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 * Return 0 if something is wrong.
5689 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005690 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005691get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692{
5693 char_u *p;
5694 int len;
5695
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005696 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005698 {
5699 if (*p == ':')
5700 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005701 // "s:" is start of "s:var", but "n:" is not and can be used in
5702 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005703 len = (int)(p - *arg);
5704 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5705 || len > 1)
5706 break;
5707 }
5708 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005709 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 return 0;
5711
5712 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005713 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714
5715 return len;
5716}
5717
5718/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005719 * Get the length of the name of a variable or function.
5720 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005722 * Return -1 if curly braces expansion failed.
5723 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 * If the name contains 'magic' {}'s, expand them and return the
5725 * expanded name in an allocated string via 'alias' - caller must free.
5726 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005727 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005728get_name_len(
5729 char_u **arg,
5730 char_u **alias,
5731 int evaluate,
5732 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733{
5734 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 char_u *p;
5736 char_u *expr_start;
5737 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005739 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740
5741 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5742 && (*arg)[2] == (int)KE_SNR)
5743 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005744 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 *arg += 3;
5746 return get_id_len(arg) + 3;
5747 }
5748 len = eval_fname_script(*arg);
5749 if (len > 0)
5750 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005751 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 *arg += len;
5753 }
5754
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005756 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005758 p = find_name_end(*arg, &expr_start, &expr_end,
5759 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 if (expr_start != NULL)
5761 {
5762 char_u *temp_string;
5763
5764 if (!evaluate)
5765 {
5766 len += (int)(p - *arg);
5767 *arg = skipwhite(p);
5768 return len;
5769 }
5770
5771 /*
5772 * Include any <SID> etc in the expanded string:
5773 * Thus the -len here.
5774 */
5775 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5776 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005777 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 *alias = temp_string;
5779 *arg = skipwhite(p);
5780 return (int)STRLEN(temp_string);
5781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782
5783 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005784 // Only give an error when there is something, otherwise it will be
5785 // reported at a higher level.
5786 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005787 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788
5789 return len;
5790}
5791
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005792/*
5793 * Find the end of a variable or function name, taking care of magic braces.
5794 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5795 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005796 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005797 * Return a pointer to just after the name. Equal to "arg" if there is no
5798 * valid name.
5799 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005800 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005801find_name_end(
5802 char_u *arg,
5803 char_u **expr_start,
5804 char_u **expr_end,
5805 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005807 int mb_nest = 0;
5808 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005810 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005811 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005813 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005815 *expr_start = NULL;
5816 *expr_end = NULL;
5817 }
5818
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005819 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005820 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5821 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005822 return arg;
5823
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005824 for (p = arg; *p != NUL
5825 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005826 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005827 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005828 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005829 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005830 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005831 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005832 if (*p == '\'')
5833 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005834 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005835 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005836 ;
5837 if (*p == NUL)
5838 break;
5839 }
5840 else if (*p == '"')
5841 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005842 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005843 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005844 if (*p == '\\' && p[1] != NUL)
5845 ++p;
5846 if (*p == NUL)
5847 break;
5848 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005849 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5850 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005851 // "s:" is start of "s:var", but "n:" is not and can be used in
5852 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005853 len = (int)(p - arg);
5854 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005855 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005856 break;
5857 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005858
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005859 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005861 if (*p == '[')
5862 ++br_nest;
5863 else if (*p == ']')
5864 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005866
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005867 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005869 if (*p == '{')
5870 {
5871 mb_nest++;
5872 if (expr_start != NULL && *expr_start == NULL)
5873 *expr_start = p;
5874 }
5875 else if (*p == '}')
5876 {
5877 mb_nest--;
5878 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5879 *expr_end = p;
5880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 }
5883
5884 return p;
5885}
5886
5887/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005888 * Expands out the 'magic' {}'s in a variable/function name.
5889 * Note that this can call itself recursively, to deal with
5890 * constructs like foo{bar}{baz}{bam}
5891 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5892 * "in_start" ^
5893 * "expr_start" ^
5894 * "expr_end" ^
5895 * "in_end" ^
5896 *
5897 * Returns a new allocated string, which the caller must free.
5898 * Returns NULL for failure.
5899 */
5900 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005901make_expanded_name(
5902 char_u *in_start,
5903 char_u *expr_start,
5904 char_u *expr_end,
5905 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005906{
5907 char_u c1;
5908 char_u *retval = NULL;
5909 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005910
5911 if (expr_end == NULL || in_end == NULL)
5912 return NULL;
5913 *expr_start = NUL;
5914 *expr_end = NUL;
5915 c1 = *in_end;
5916 *in_end = NUL;
5917
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005918 temp_result = eval_to_string(expr_start + 1, FALSE);
5919 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005920 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005921 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5922 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005923 if (retval != NULL)
5924 {
5925 STRCPY(retval, in_start);
5926 STRCAT(retval, temp_result);
5927 STRCAT(retval, expr_end + 1);
5928 }
5929 }
5930 vim_free(temp_result);
5931
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005932 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005933 *expr_start = '{';
5934 *expr_end = '}';
5935
5936 if (retval != NULL)
5937 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005938 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005939 if (expr_start != NULL)
5940 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005941 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005942 temp_result = make_expanded_name(retval, expr_start,
5943 expr_end, temp_result);
5944 vim_free(retval);
5945 retval = temp_result;
5946 }
5947 }
5948
5949 return retval;
5950}
5951
5952/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005954 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005956 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005957eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005959 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005960}
5961
5962/*
5963 * Return TRUE if character "c" can be used as the first character in a
5964 * variable or function name (excluding '{' and '}').
5965 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005966 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005967eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005968{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005969 return ASCII_ISALPHA(c) || c == '_';
5970}
5971
5972/*
5973 * Return TRUE if character "c" can be used as the first character of a
5974 * dictionary key.
5975 */
5976 int
5977eval_isdictc(int c)
5978{
5979 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980}
5981
5982/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005983 * Handle:
5984 * - expr[expr], expr[expr:expr] subscript
5985 * - ".name" lookup
5986 * - function call with Funcref variable: func(expr)
5987 * - method call: var->method()
5988 *
5989 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00005990 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005991 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005992 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005993handle_subscript(
5994 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00005995 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005996 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005997 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005998 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005999{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006000 int evaluate = evalarg != NULL
6001 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006002 int ret = OK;
6003 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006004 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006005 int getnext;
6006 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006007
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006008 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006009 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006010 // When at the end of the line and ".name" or "->{" or "->X" follows in
6011 // the next line then consume the line break.
6012 p = eval_next_non_blank(*arg, evalarg, &getnext);
6013 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006014 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006015 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6016 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6017 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006018 {
6019 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006020 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006021 check_white = FALSE;
6022 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006023
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006024 if (rettv->v_type == VAR_ANY)
6025 {
6026 char_u *exp_name;
6027 int cc;
6028 int idx;
6029 ufunc_T *ufunc;
6030 type_T *type;
6031
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006032 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006033 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006034 if (**arg != '.')
6035 {
6036 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006037 semsg(_(e_expected_dot_after_name_str),
6038 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006039 ret = FAIL;
6040 break;
6041 }
6042 ++*arg;
6043 if (IS_WHITE_OR_NUL(**arg))
6044 {
6045 if (verbose)
6046 emsg(_(e_no_white_space_allowed_after_dot));
6047 ret = FAIL;
6048 break;
6049 }
6050
6051 // isolate the name
6052 exp_name = *arg;
6053 while (eval_isnamec(**arg))
6054 ++*arg;
6055 cc = **arg;
6056 **arg = NUL;
6057
6058 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
6059 evalarg->eval_cctx, verbose);
6060 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006061
6062 if (idx < 0 && ufunc == NULL)
6063 {
6064 ret = FAIL;
6065 break;
6066 }
6067 if (idx >= 0)
6068 {
6069 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6070 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6071
6072 copy_tv(sv->sv_tv, rettv);
6073 }
6074 else
6075 {
6076 rettv->v_type = VAR_FUNC;
6077 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6078 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006079 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006080 }
6081
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006082 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6083 || rettv->v_type == VAR_PARTIAL))
6084 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006085 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006086 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6087 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006088
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006089 // Stop the expression evaluation when immediately aborting on
6090 // error, or when an interrupt occurred or an exception was thrown
6091 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006092 if (aborting())
6093 {
6094 if (ret == OK)
6095 clear_tv(rettv);
6096 ret = FAIL;
6097 }
6098 dict_unref(selfdict);
6099 selfdict = NULL;
6100 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006101 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006102 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006103 if (in_vim9script())
6104 *arg = skipwhite(p + 2);
6105 else
6106 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006107 if (ret == OK)
6108 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006109 if (VIM_ISWHITE(**arg))
6110 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006111 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006112 ret = FAIL;
6113 }
6114 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006115 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006116 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006117 else
6118 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006119 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006120 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006121 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006122 // "." is ".name" lookup when we found a dict or when evaluating and
6123 // scriptversion is at least 2, where string concatenation is "..".
6124 else if (**arg == '['
6125 || (**arg == '.' && (rettv->v_type == VAR_DICT
6126 || (!evaluate
6127 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006128 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006129 {
6130 dict_unref(selfdict);
6131 if (rettv->v_type == VAR_DICT)
6132 {
6133 selfdict = rettv->vval.v_dict;
6134 if (selfdict != NULL)
6135 ++selfdict->dv_refcount;
6136 }
6137 else
6138 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006139 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006140 {
6141 clear_tv(rettv);
6142 ret = FAIL;
6143 }
6144 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006145 else
6146 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006147 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006148
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006149 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6150 // Don't do this when "Func" is already a partial that was bound
6151 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006152 if (selfdict != NULL
6153 && (rettv->v_type == VAR_FUNC
6154 || (rettv->v_type == VAR_PARTIAL
6155 && (rettv->vval.v_partial->pt_auto
6156 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006157 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006158
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006159 dict_unref(selfdict);
6160 return ret;
6161}
6162
6163/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006164 * Make a copy of an item.
6165 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006166 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006167 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6168 * reference to an already copied list/dict can be used.
6169 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006170 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006171 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006172item_copy(
6173 typval_T *from,
6174 typval_T *to,
6175 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006176 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006177 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006178{
6179 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006180 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006181
Bram Moolenaar33570922005-01-25 22:26:29 +00006182 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006183 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006184 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006185 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006186 }
6187 ++recurse;
6188
6189 switch (from->v_type)
6190 {
6191 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006192 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006193 case VAR_STRING:
6194 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006195 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006196 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006197 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006198 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006199 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006200 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006201 copy_tv(from, to);
6202 break;
6203 case VAR_LIST:
6204 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006205 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006206 if (from->vval.v_list == NULL)
6207 to->vval.v_list = NULL;
6208 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6209 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006210 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006211 to->vval.v_list = from->vval.v_list->lv_copylist;
6212 ++to->vval.v_list->lv_refcount;
6213 }
6214 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006215 to->vval.v_list = list_copy(from->vval.v_list,
6216 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006217 if (to->vval.v_list == NULL)
6218 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006219 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006220 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006221 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006222 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006223 case VAR_DICT:
6224 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006225 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006226 if (from->vval.v_dict == NULL)
6227 to->vval.v_dict = NULL;
6228 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6229 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006230 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006231 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6232 ++to->vval.v_dict->dv_refcount;
6233 }
6234 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006235 to->vval.v_dict = dict_copy(from->vval.v_dict,
6236 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006237 if (to->vval.v_dict == NULL)
6238 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006239 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006240 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006241 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006242 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006243 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006244 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006245 }
6246 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006247 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006248}
6249
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006250 void
6251echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6252{
6253 char_u *tofree;
6254 char_u numbuf[NUMBUFLEN];
6255 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6256
6257 if (*atstart)
6258 {
6259 *atstart = FALSE;
6260 // Call msg_start() after eval1(), evaluating the expression
6261 // may cause a message to appear.
6262 if (with_space)
6263 {
6264 // Mark the saved text as finishing the line, so that what
6265 // follows is displayed on a new line when scrolling back
6266 // at the more prompt.
6267 msg_sb_eol();
6268 msg_start();
6269 }
6270 }
6271 else if (with_space)
6272 msg_puts_attr(" ", echo_attr);
6273
6274 if (p != NULL)
6275 for ( ; *p != NUL && !got_int; ++p)
6276 {
6277 if (*p == '\n' || *p == '\r' || *p == TAB)
6278 {
6279 if (*p != TAB && *needclr)
6280 {
6281 // remove any text still there from the command
6282 msg_clr_eos();
6283 *needclr = FALSE;
6284 }
6285 msg_putchar_attr(*p, echo_attr);
6286 }
6287 else
6288 {
6289 if (has_mbyte)
6290 {
6291 int i = (*mb_ptr2len)(p);
6292
6293 (void)msg_outtrans_len_attr(p, i, echo_attr);
6294 p += i - 1;
6295 }
6296 else
6297 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6298 }
6299 }
6300 vim_free(tofree);
6301}
6302
Bram Moolenaare9a41262005-01-15 22:18:47 +00006303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 * ":echo expr1 ..." print each argument separated with a space, add a
6305 * newline at the end.
6306 * ":echon expr1 ..." print each argument plain.
6307 */
6308 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006309ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310{
6311 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006312 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006313 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 int needclr = TRUE;
6315 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006316 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006317 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006318 evalarg_T evalarg;
6319
Bram Moolenaare707c882020-07-01 13:07:37 +02006320 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321
6322 if (eap->skip)
6323 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006324 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006326 // If eval1() causes an error message the text from the command may
6327 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006328 need_clr_eos = needclr;
6329
Bram Moolenaar68db9962021-05-09 23:19:22 +02006330 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006331 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 {
6333 /*
6334 * Report the invalid expression unless the expression evaluation
6335 * has been cancelled due to an aborting error, an interrupt, or an
6336 * exception.
6337 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006338 if (!aborting() && did_emsg == did_emsg_before
6339 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006340 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006341 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 break;
6343 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006344 need_clr_eos = FALSE;
6345
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006347 {
6348 if (rettv.v_type == VAR_VOID)
6349 {
6350 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6351 break;
6352 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006353 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006354 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006355
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006356 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 arg = skipwhite(arg);
6358 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006359 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006360 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361
6362 if (eap->skip)
6363 --emsg_skip;
6364 else
6365 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006366 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 if (needclr)
6368 msg_clr_eos();
6369 if (eap->cmdidx == CMD_echo)
6370 msg_end();
6371 }
6372}
6373
6374/*
6375 * ":echohl {name}".
6376 */
6377 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006378ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006380 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381}
6382
6383/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006384 * Returns the :echo attribute
6385 */
6386 int
6387get_echo_attr(void)
6388{
6389 return echo_attr;
6390}
6391
6392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 * ":execute expr1 ..." execute the result of an expression.
6394 * ":echomsg expr1 ..." Print a message
6395 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006396 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 * Each gets spaces around each argument and a newline at the end for
6398 * echo commands
6399 */
6400 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006401ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402{
6403 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006404 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 int ret = OK;
6406 char_u *p;
6407 garray_T ga;
6408 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006409 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410
6411 ga_init2(&ga, 1, 80);
6412
6413 if (eap->skip)
6414 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006415 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006417 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006418 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420
6421 if (!eap->skip)
6422 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006423 char_u buf[NUMBUFLEN];
6424
6425 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006426 {
6427 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6428 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006429 semsg(_(e_using_invalid_value_as_string_str),
6430 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006431 p = NULL;
6432 }
6433 else
6434 p = tv_get_string_buf(&rettv, buf);
6435 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006436 else
6437 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006438 if (p == NULL)
6439 {
6440 clear_tv(&rettv);
6441 ret = FAIL;
6442 break;
6443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 len = (int)STRLEN(p);
6445 if (ga_grow(&ga, len + 2) == FAIL)
6446 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006447 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 ret = FAIL;
6449 break;
6450 }
6451 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 ga.ga_len += len;
6455 }
6456
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006457 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 arg = skipwhite(arg);
6459 }
6460
6461 if (ret != FAIL && ga.ga_data != NULL)
6462 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006463 // use the first line of continuation lines for messages
6464 SOURCING_LNUM = start_lnum;
6465
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006466 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6467 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006468 // Mark the already saved text as finishing the line, so that what
6469 // follows is displayed on a new line when scrolling back at the
6470 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006471 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006472 }
6473
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006475 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006476 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006477 out_flush();
6478 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006479 else if (eap->cmdidx == CMD_echoconsole)
6480 {
6481 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6482 ui_write((char_u *)"\r\n", 2, TRUE);
6483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 else if (eap->cmdidx == CMD_echoerr)
6485 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006486 int save_did_emsg = did_emsg;
6487
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006488 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006489 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 if (!force_abort)
6491 did_emsg = save_did_emsg;
6492 }
6493 else if (eap->cmdidx == CMD_execute)
6494 do_cmdline((char_u *)ga.ga_data,
6495 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6496 }
6497
6498 ga_clear(&ga);
6499
6500 if (eap->skip)
6501 --emsg_skip;
6502
Bram Moolenaar63b91732021-08-05 20:40:03 +02006503 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504}
6505
6506/*
6507 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6508 * "arg" points to the "&" or '+' when called, to "option" when returning.
6509 * Returns NULL when no option name found. Otherwise pointer to the char
6510 * after the option name.
6511 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006512 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006513find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514{
6515 char_u *p = *arg;
6516
6517 ++p;
6518 if (*p == 'g' && p[1] == ':')
6519 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006520 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 p += 2;
6522 }
6523 else if (*p == 'l' && p[1] == ':')
6524 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006525 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 p += 2;
6527 }
6528 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006529 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530
6531 if (!ASCII_ISALPHA(*p))
6532 return NULL;
6533 *arg = p;
6534
6535 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006536 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 else
6538 while (ASCII_ISALPHA(*p))
6539 ++p;
6540 return p;
6541}
6542
6543/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006544 * Display script name where an item was last set.
6545 * Should only be invoked when 'verbose' is non-zero.
6546 */
6547 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006548last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006549{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006550 char_u *p;
6551
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006552 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006553 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006554 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006555 if (p != NULL)
6556 {
6557 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006558 msg_puts(_("\n\tLast set from "));
6559 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006560 if (script_ctx.sc_lnum > 0)
6561 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006562 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006563 msg_outnum((long)script_ctx.sc_lnum);
6564 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006565 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006566 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006567 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006568 }
6569}
6570
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006571#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572
6573/*
6574 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006575 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 * "flags" can be "g" to do a global substitute.
6577 * Returns an allocated string, NULL for error.
6578 */
6579 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006580do_string_sub(
6581 char_u *str,
6582 char_u *pat,
6583 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006584 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006585 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586{
6587 int sublen;
6588 regmatch_T regmatch;
6589 int i;
6590 int do_all;
6591 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006592 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 garray_T ga;
6594 char_u *ret;
6595 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006596 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006598 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006600 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601
6602 ga_init2(&ga, 1, 200);
6603
6604 do_all = (flags[0] == 'g');
6605
6606 regmatch.rm_ic = p_ic;
6607 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6608 if (regmatch.regprog != NULL)
6609 {
6610 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006611 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6613 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006614 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006615 if (regmatch.startp[0] == regmatch.endp[0])
6616 {
6617 if (zero_width == regmatch.startp[0])
6618 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006619 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006620 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006621 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6622 (size_t)i);
6623 ga.ga_len += i;
6624 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006625 continue;
6626 }
6627 zero_width = regmatch.startp[0];
6628 }
6629
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 /*
6631 * Get some space for a temporary buffer to do the substitution
6632 * into. It will contain:
6633 * - The text up to where the match is.
6634 * - The substituted text.
6635 * - The text after the match.
6636 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006637 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006638 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6640 {
6641 ga_clear(&ga);
6642 break;
6643 }
6644
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006645 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 i = (int)(regmatch.startp[0] - tail);
6647 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006648 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006649 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 + ga.ga_len + i, TRUE, TRUE, FALSE);
6651 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006652 tail = regmatch.endp[0];
6653 if (*tail == NUL)
6654 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 if (!do_all)
6656 break;
6657 }
6658
6659 if (ga.ga_data != NULL)
6660 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6661
Bram Moolenaar473de612013-06-08 18:19:48 +02006662 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 }
6664
6665 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6666 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006667 if (p_cpo == empty_option)
6668 p_cpo = save_cpo;
6669 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006670 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006671 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006672 // If it's still empty it was changed and restored, need to restore in
6673 // the complicated way.
6674 if (*p_cpo == NUL)
6675 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006676 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678
6679 return ret;
6680}